-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluable_expression_test.go
180 lines (163 loc) · 4.24 KB
/
evaluable_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package valuate
import (
"reflect"
"sync"
"testing"
"time"
"github.com/bruceding/go-antlr-valuate/parser"
)
func TestExpression(t *testing.T) {
expr := "1+2"
evaluableExpression, err := NewEvaluableExpression(expr)
if err != nil {
t.Fatal(err)
}
if evaluableExpression == nil {
t.Fatal("evaluableExpression is nil")
}
}
func TestEvaluableExpressionResult(t *testing.T) {
testCases := []struct {
input string
expectType string
expectValue any
params map[string]any
}{
{
input: "foo > 0",
expectType: "bool",
expectValue: false,
params: map[string]any{"foo": -1},
},
{
input: "(requests_made * requests_succeeded / 100) >= 90",
expectType: "bool",
expectValue: false,
params: map[string]any{"requests_made": 100, "requests_succeeded": 80},
},
{
input: "http_response_body == 'service is ok'",
expectType: "bool",
expectValue: true,
params: map[string]any{"http_response_body": "service is ok"},
},
{
input: "${http-response_body} == 'service is ok'",
expectType: "bool",
expectValue: true,
params: map[string]any{"http-response_body": "service is ok"},
},
{
input: "1 + 3 * 5",
expectType: "float64",
expectValue: float64(16),
params: map[string]any{},
},
{
input: "max(3, 5)",
expectType: "float64",
expectValue: float64(5),
params: map[string]any{},
},
{
input: "(mem_used / total_mem) * 100",
expectType: "float64",
expectValue: float64(50),
params: map[string]any{"mem_used": 512, "total_mem": 1024},
},
}
for _, tcase := range testCases {
evaluableExpression, err := NewEvaluableExpression(tcase.input)
if err != nil {
t.Fatal(evaluableExpression)
}
evaluableExpression.Evaluate(tcase.params)
result, err := evaluableExpression.Evaluate(tcase.params)
if err != nil {
t.Fatal(err)
}
switch val := result.(type) {
case []any:
if tcase.expectType != "[]any" {
t.Fatal("expect type is []any")
}
if reflect.DeepEqual(val, tcase.expectValue) != true {
t.Fatal("expect value is not equal")
}
case float64:
if tcase.expectType != "float64" {
t.Fatal("expect type is float64")
}
if val != tcase.expectValue {
t.Fatal("expect value is not equal")
}
case string:
if tcase.expectType != "string" {
t.Fatal("expect type is string")
}
if val != tcase.expectValue {
t.Fatal("expect value is not equal")
}
case bool:
if tcase.expectType != "bool" {
t.Fatal("expect type is bool")
}
if val != tcase.expectValue {
t.Fatalf("expect value is not equal,input:%s, expect:%v, actual:%v", tcase.input, tcase.expectValue, val)
}
case time.Time:
if tcase.expectType != "Time" {
t.Fatal("expect type is Time")
}
if val != tcase.expectValue {
t.Fatal("expect value is not equal")
}
case error:
t.Fatal(val)
default:
t.Fatal("expect result error", result)
}
}
}
func TestEvaluableExpressionFunctions(t *testing.T) {
functions := map[string]parser.ExpressionFunction{
"strlen": func(args ...interface{}) (interface{}, error) {
length := len(args[0].(string))
return (float64)(length), nil
},
}
expString := "strlen('someReallyLongInputString') <= 16"
expression, _ := NewEvaluableExpressionWithFunctions(expString, functions)
result, _ := expression.Evaluate(nil)
if result.(bool) != false {
t.Fatal("expect result wrong, expected false")
}
expString = "len('someReallyLongInputString') <= 16"
expression, _ = NewEvaluableExpressionWithFunctions(expString, map[string]parser.ExpressionFunction{})
result, _ = expression.Evaluate(nil)
if result.(bool) != false {
t.Fatal("expect result wrong, expected false")
}
}
func TestConcurrentExpression(t *testing.T) {
source := "(mem_used / total_mem) * 100"
evaluableExpression, err := NewEvaluableExpression(source)
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
result, err := evaluableExpression.Evaluate(map[string]any{"mem_used": 512, "total_mem": 1024})
if err != nil {
t.Fatal(err)
}
if result.(float64) != float64(50) {
t.Fatal("expect result wrong, expected 50")
}
}()
}
wg.Wait()
}