-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine_test.go
83 lines (68 loc) · 1.97 KB
/
combine_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
package parse_test
import (
"github.com/orkes-io/go-parse"
"github.com/orkes-io/go-parse/bools"
"github.com/orkes-io/go-parse/comp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestBoolComp(t *testing.T) {
b, err := bools.NewParser()
require.NoError(t, err)
c, err := comp.NewParser()
require.NoError(t, err)
tests := []struct {
input string
output parse.AST
}{
{
"x > 3 AND y == 5 OR z != 3",
and(gt(un("x"), un("3")), or(eq(un("y"), un("5")), neq(un("z"), un("3")))),
},
{
"${foo.bar.var1} > ${foo.baz.var2} OR ${bar.foo.var2} == 3",
or(gt(un("${foo.bar.var1}"), un("${foo.baz.var2}")), eq(un("${bar.foo.var2}"), un("3"))),
},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
ast, err := b.ParseStr(tt.input)
require.NoError(t, err)
err = ast.Parse(c)
assert.NoError(t, err)
assert.EqualValues(t, tt.output, ast)
})
}
}
func eq(a, b parse.AST) parse.AST {
return &comp.EqualExpr{LHS: a, RHS: b, Op: comp.OpEqual}
}
func neq(a, b parse.AST) parse.AST {
return &comp.EqualExpr{LHS: a, RHS: b, Op: comp.OpNotEqual}
}
func gt(a, b parse.AST) parse.AST {
return &comp.OrdinalExpr{LHS: a, RHS: b, Op: comp.OpGreater}
}
func lt(a, b parse.AST) parse.AST {
return &comp.OrdinalExpr{LHS: a, RHS: b, Op: comp.OpLess}
}
func gte(a, b parse.AST) parse.AST {
return &comp.OrdinalExpr{LHS: a, RHS: b, Op: comp.OpGreaterOrEqual}
}
func lte(a, b parse.AST) parse.AST {
return &comp.OrdinalExpr{LHS: a, RHS: b, Op: comp.OpLessOrEqual}
}
func or(lhs parse.AST, rhs parse.AST) parse.AST {
return &bools.BinExpr{LHS: lhs, RHS: rhs, Op: bools.OpOr}
}
func and(lhs parse.AST, rhs parse.AST) parse.AST {
return &bools.BinExpr{LHS: lhs, RHS: rhs, Op: bools.OpAnd}
}
func not(inside parse.AST) parse.AST {
return &bools.UnaryExpr{Expr: inside, Op: bools.OpNot}
}
// un stands for unparsed and returns a Unparsed
func un(tokens ...string) parse.AST {
return parse.Unparsed{Contents: tokens}
}