-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexpression_test.go
112 lines (98 loc) · 2.51 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
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
102
103
104
105
106
107
108
109
110
111
112
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)
}
}
func TestRenderTemplate(t *testing.T) {
type TestCase[T any] struct {
template string
output any
context any
}
intTestCases := []TestCase[int]{
{
template: "{{ a + b }}",
output: 15,
context: map[string]any{"a": 5, "b": 10},
},
}
stringTestCases := []TestCase[string]{
{
template: "Hello: {{ a + b }}",
output: "Hello: 15",
context: map[string]any{"a": 5, "b": 10},
},
}
for _, testCase := range intTestCases {
isTrue, err := RenderTemplate[int](testCase.template, testCase.context)
assert.NoError(t, err)
assert.Equal(t, testCase.output, isTrue)
}
for _, testCase := range stringTestCases {
isTrue, err := RenderTemplate[string](testCase.template, testCase.context)
assert.NoError(t, err)
assert.Equal(t, testCase.output, isTrue)
}
}