-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary_arithmetic_test.go
73 lines (65 loc) · 1.29 KB
/
library_arithmetic_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
package cel_test
import (
"testing"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
)
func TestArithmetic(tt *testing.T) {
tests := map[string]struct {
source string
out ref.Val
}{
"6 + 7": {
source: "6 + 7",
out: types.Int(13),
},
"6 - 7": {
source: "6 - 7",
out: types.Int(-1),
},
"10 * 13": {
source: "10 * 13",
out: types.Int(130),
},
"20 / 10": {
source: "20 / 10",
out: types.Int(2),
},
`13 % 10`: {
source: `13 % 10`,
out: types.Int(3),
},
}
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 != v.out {
t.FailNow()
}
})
}
}