forked from gorules/zen-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression_test.go
76 lines (67 loc) · 1.72 KB
/
expression_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
package zen
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestEvaluateExpression(t *testing.T) {
type TestCase[T any] struct {
expression string
output T
context any
}
// Example usage with int
intTestCases := []TestCase[int]{
{expression: "1 + 1", output: 2},
{expression: "2 + 2", output: 4},
{expression: "10 + a", output: 14, context: map[string]int{"a": 4}},
}
// Example usage with string
stringTestCases := []TestCase[string]{
{expression: `"hello" + " " + "world"`, output: "hello world"},
{expression: `"foo" + "bar"`, output: "foobar"},
}
for _, intTestCase := range intTestCases {
res, err := EvaluateExpression[int](intTestCase.expression, intTestCase.context)
assert.NoError(t, err)
assert.Equal(t, intTestCase.output, res)
}
for _, stringTestCase := range stringTestCases {
res, err := EvaluateExpression[string](stringTestCase.expression, stringTestCase.context)
assert.NoError(t, err)
assert.Equal(t, stringTestCase.output, res)
}
}
func TestEvaluateUnaryExpression(t *testing.T) {
type TestCase struct {
expression string
output bool
context any
}
testCases := []TestCase{
{
expression: "> 10",
output: false,
context: map[string]any{"$": 5},
},
{
expression: "> 10",
output: true,
context: map[string]any{"$": 15},
},
{
expression: "'US', 'GB'",
output: true,
context: map[string]any{"$": "US"},
},
{
expression: "'US', 'GB'",
output: false,
context: map[string]any{"$": "AA"},
},
}
for _, testCase := range testCases {
isTrue, err := EvaluateUnaryExpression(testCase.expression, testCase.context)
assert.NoError(t, err)
assert.Equal(t, testCase.output, isTrue)
}
}