-
Notifications
You must be signed in to change notification settings - Fork 0
/
datacomparison_test.go
101 lines (83 loc) · 2.33 KB
/
datacomparison_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package bexpression
import (
"context"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/raito-io/bexpression/base"
"github.com/raito-io/bexpression/datacomparison"
"github.com/raito-io/bexpression/utils"
)
var _ (base.Visitor) = (*testVisitor)(nil)
type Stringify interface {
String() string
}
type testVisitor struct {
strBuilder strings.Builder
t string
}
func (t *testVisitor) EnterExpressionElement(_ context.Context, element base.VisitableElement) error {
switch element.(type) {
case *base.BinaryExpression[*datacomparison.DataComparison]:
t.strBuilder.WriteString("(")
}
return nil
}
func (t *testVisitor) LeaveExpressionElement(_ context.Context, element base.VisitableElement) {
switch element.(type) {
case *base.BinaryExpression[*datacomparison.DataComparison]:
t.strBuilder.WriteString(")")
}
}
func (t *testVisitor) Literal(_ context.Context, l interface{}) error {
if s, ok := l.(Stringify); ok {
t.strBuilder.WriteString(" " + s.String() + " ")
} else if reference, ok := l.(*datacomparison.Reference); ok {
t.strBuilder.WriteString(fmt.Sprintf("%s.%s", reference.EntityType.String(), reference.EntityID))
} else {
t.strBuilder.WriteString(fmt.Sprintf("%v ", l))
}
return nil
}
func TestDataComparisonExpression_Visitor(t *testing.T) {
tests := []struct {
name string
expr *DataComparisonExpression
want string
}{
{
name: "simple comparison",
expr: &DataComparisonExpression{
Comparison: &datacomparison.DataComparison{
Operator: datacomparison.ComparisonOperatorGreaterThan,
LeftOperand: datacomparison.Operand{
Literal: &datacomparison.Literal{
Int: utils.Ptr(10),
},
},
RightOperand: datacomparison.Operand{
Reference: &datacomparison.Reference{
EntityType: datacomparison.EntityTypeDataObject,
EntityID: "EntityID",
},
},
},
},
want: "(10 GreaterThan DataObject.EntityID)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
visitor := testVisitor{
strBuilder: strings.Builder{},
t: "root",
}
err := tt.expr.Accept(context.Background(), &visitor)
require.NoError(t, err)
if visitor.strBuilder.String() != tt.want {
t.Errorf("DataComparisonExpression.Visitor() = %v, want %v", visitor.strBuilder.String(), tt.want)
}
})
}
}