-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary_logical_test.go
80 lines (72 loc) · 1.41 KB
/
library_logical_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
package cel_test
import (
"testing"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
)
func TestLogical(tt *testing.T) {
tests := map[string]struct {
source string
out types.Bool
}{
"6 == 6": {
source: "6 == 6",
out: true,
},
"6u != 7u": {
source: "6u != 7u",
out: true,
},
"6.0 == 6.0": {
source: "6.0 == 6.0",
out: true,
},
"6.0 != 6.0": {
source: "6.0 != 6.0",
out: false,
},
`"abc" != "cba"`: {
source: `"abc" != "cba"`,
out: true,
},
`b"\x31" == b"\x32"`: {
source: `b"\x31" == b"\x32"`,
out: false,
},
`5 > 6 ? true : false`: {
source: `5 > 6 ? true : false`,
out: false,
},
}
env, err := cel.NewEnv(
cel.Variable("buff", cel.IntType),
)
if err != nil {
tt.Errorf("environment creation error: %v\n", err)
tt.FailNow()
}
for k, v := range tests {
tt.Run(k, func(t *testing.T) {
// Check iss for error in both Parse and Check.
ast, iss := env.Compile(v.source)
if iss.Err() != nil {
t.Error(iss.Err())
t.FailNow()
}
prg, err := env.Program(ast)
if err != nil {
t.Errorf("Program creation error: %v\n", err)
t.FailNow()
}
out, _, err := prg.Eval(map[string]interface{}{})
if err != nil {
t.Errorf("Evaluation error: %v\n", err)
t.FailNow()
}
t.Logf("%s -> %v", v.source, out)
if out.(types.Bool) != v.out {
t.FailNow()
}
})
}
}