-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
readme_test.go
192 lines (155 loc) · 3.75 KB
/
readme_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
181
182
183
184
185
186
187
188
189
190
191
192
package govaluate
/*
Contains test cases for all the expression examples given in the README.
While all of the functionality for these cases should be covered in other tests,
this is really just a sanity check.
*/
import (
"testing"
)
func TestBasicEvaluation(test *testing.T) {
expression, err := NewEvaluableExpression("10 > 0")
if err != nil {
test.Log(err)
test.Fail()
}
result, err := expression.Evaluate(nil)
if err != nil {
test.Log(err)
test.Fail()
}
if result != true {
test.Logf("Expected 'true', got '%v'\n", result)
test.Fail()
}
}
func TestParameterEvaluation(test *testing.T) {
expression, err := NewEvaluableExpression("foo > 0")
if err != nil {
test.Log(err)
test.Fail()
}
parameters := make(map[string]interface{}, 8)
parameters["foo"] = -1
result, err := expression.Evaluate(parameters)
if err != nil {
test.Log(err)
test.Fail()
}
if result != false {
test.Logf("Expected 'false', got '%v'\n", result)
test.Fail()
}
}
func TestModifierEvaluation(test *testing.T) {
expression, err := NewEvaluableExpression("(requests_made * requests_succeeded / 100) >= 90")
if err != nil {
test.Log(err)
test.Fail()
}
parameters := make(map[string]interface{}, 8)
parameters["requests_made"] = 100
parameters["requests_succeeded"] = 80
result, err := expression.Evaluate(parameters)
if err != nil {
test.Log(err)
test.Fail()
}
if result != false {
test.Logf("Expected 'false', got '%v'\n", result)
test.Fail()
}
}
func TestStringEvaluation(test *testing.T) {
expression, err := NewEvaluableExpression("http_response_body == 'service is ok'")
if err != nil {
test.Log(err)
test.Fail()
}
parameters := make(map[string]interface{}, 8)
parameters["http_response_body"] = "service is ok"
result, err := expression.Evaluate(parameters)
if err != nil {
test.Log(err)
test.Fail()
}
if result != true {
test.Logf("Expected 'false', got '%v'\n", result)
test.Fail()
}
}
func TestFloatEvaluation(test *testing.T) {
expression, err := NewEvaluableExpression("(mem_used / total_mem) * 100")
if err != nil {
test.Log(err)
test.Fail()
}
parameters := make(map[string]interface{}, 8)
parameters["total_mem"] = 1024
parameters["mem_used"] = 512
result, err := expression.Evaluate(parameters)
if err != nil {
test.Log(err)
test.Fail()
}
if result != 50.0 {
test.Logf("Expected '50.0', got '%v'\n", result)
test.Fail()
}
}
func TestDateComparison(test *testing.T) {
expression, err := NewEvaluableExpression("'2014-01-02' > '2014-01-01 23:59:59'")
if err != nil {
test.Log(err)
test.Fail()
}
result, err := expression.Evaluate(nil)
if err != nil {
test.Log(err)
test.Fail()
}
if result != true {
test.Logf("Expected 'true', got '%v'\n", result)
test.Fail()
}
}
func TestMultipleEvaluation(test *testing.T) {
expression, _ := NewEvaluableExpression("response_time <= 100")
parameters := make(map[string]interface{}, 8)
for i := 0; i < 64; i++ {
parameters["response_time"] = i
result, err := expression.Evaluate(parameters)
if err != nil {
test.Log(err)
test.Fail()
}
if result != true {
test.Logf("Expected 'true', got '%v'\n", result)
test.Fail()
break
}
}
}
func TestStrlenFunction(test *testing.T) {
functions := map[string]ExpressionFunction{
"strlen": func(args ...interface{}) (interface{}, error) {
length := len(args[0].(string))
return (float64)(length), nil
},
}
expString := "strlen('someReallyLongInputString') <= 16"
expression, err := NewEvaluableExpressionWithFunctions(expString, functions)
if err != nil {
test.Log(err)
test.Fail()
}
result, err := expression.Evaluate(nil)
if err != nil {
test.Log(err)
test.Fail()
}
if result != false {
test.Logf("Expected 'false', got '%v'\n", result)
test.Fail()
}
}