forked from Knetic/govaluate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluationFailure_test.go
543 lines (441 loc) · 12.7 KB
/
evaluationFailure_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package govaluate
/*
Tests to make sure evaluation fails in the expected ways.
*/
import (
"errors"
"fmt"
"strings"
"testing"
)
type DebugStruct struct {
x int
}
/*
Represents a test for parsing failures
*/
type EvaluationFailureTest struct {
Name string
Input string
Functions map[string]ExpressionFunction
Parameters map[string]interface{}
Expected string
}
const (
INVALID_MODIFIER_TYPES string = "cannot be used with the modifier"
INVALID_COMPARATOR_TYPES = "cannot be used with the comparator"
INVALID_LOGICALOP_TYPES = "cannot be used with the logical operator"
INVALID_TERNARY_TYPES = "cannot be used with the ternary operator"
ABSENT_PARAMETER = "No parameter"
INVALID_REGEX = "Unable to compile regexp pattern"
INVALID_PARAMETER_CALL = "No method or field"
TOO_FEW_ARGS = "Too few arguments to parameter call"
TOO_MANY_ARGS = "Too many arguments to parameter call"
MISMATCHED_PARAMETERS = "Argument type conversion failed"
)
// preset parameter map of types that can be used in an evaluation failure test to check typing.
var EVALUATION_FAILURE_PARAMETERS = map[string]interface{}{
"number": 1,
"string": "foo",
"bool": true,
}
func TestComplexParameter(test *testing.T) {
var expression *EvaluableExpression
var err error
var v interface{}
parameters := map[string]interface{}{
"complex64": complex64(0),
"complex128": complex128(0),
}
expression, _ = NewEvaluableExpression("complex64")
v, err = expression.Evaluate(parameters)
if err != nil {
test.Errorf("Expected no error, but have %s", err)
}
if v.(complex64) != complex64(0) {
test.Errorf("Expected %v == %v", v, complex64(0))
}
expression, _ = NewEvaluableExpression("complex128")
v, err = expression.Evaluate(parameters)
if err != nil {
test.Errorf("Expected no error, but have %s", err)
}
if v.(complex128) != complex128(0) {
test.Errorf("Expected %v == %v", v, complex128(0))
}
}
func TestStructParameter(t *testing.T) {
expected := DebugStruct{}
expression, _ := NewEvaluableExpression("foo")
parameters := map[string]interface{}{"foo": expected}
v, err := expression.Evaluate(parameters)
if err != nil {
t.Errorf("Expected no error, but have %s", err)
} else if v.(DebugStruct) != expected {
t.Errorf("Values mismatch: %v != %v", expected, v)
}
}
func TestNilParameterUsage(test *testing.T) {
expression, err := NewEvaluableExpression("2 > 1")
_, err = expression.Evaluate(nil)
if err != nil {
test.Errorf("Expected no error from nil parameter evaluation, got %v\n", err)
return
}
}
func TestModifierTyping(test *testing.T) {
evaluationTests := []EvaluationFailureTest{
EvaluationFailureTest{
Name: "PLUS literal number to literal bool",
Input: "1 + true",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "PLUS number to bool",
Input: "number + bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "MINUS number to bool",
Input: "number - bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "MINUS number to bool",
Input: "number - bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "MULTIPLY number to bool",
Input: "number * bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "DIVIDE number to bool",
Input: "number / bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "EXPONENT number to bool",
Input: "number ** bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "MODULUS number to bool",
Input: "number % bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "XOR number to bool",
Input: "number % bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "BITWISE_OR number to bool",
Input: "number | bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "BITWISE_AND number to bool",
Input: "number & bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "BITWISE_XOR number to bool",
Input: "number ^ bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "BITWISE_LSHIFT number to bool",
Input: "number << bool",
Expected: INVALID_MODIFIER_TYPES,
},
EvaluationFailureTest{
Name: "BITWISE_RSHIFT number to bool",
Input: "number >> bool",
Expected: INVALID_MODIFIER_TYPES,
},
}
runEvaluationFailureTests(evaluationTests, test)
}
func TestLogicalOperatorTyping(test *testing.T) {
evaluationTests := []EvaluationFailureTest{
EvaluationFailureTest{
Name: "AND number to number",
Input: "number && number",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{
Name: "OR number to number",
Input: "number || number",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{
Name: "AND string to string",
Input: "string && string",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{
Name: "OR string to string",
Input: "string || string",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{
Name: "AND number to string",
Input: "number && string",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{
Name: "OR number to string",
Input: "number || string",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{
Name: "AND bool to string",
Input: "bool && string",
Expected: INVALID_LOGICALOP_TYPES,
},
EvaluationFailureTest{
Name: "OR string to bool",
Input: "string || bool",
Expected: INVALID_LOGICALOP_TYPES,
},
}
runEvaluationFailureTests(evaluationTests, test)
}
/*
While there is type-safe transitions checked at parse-time, tested in the "parsing_test" and "parsingFailure_test" files,
we also need to make sure that we receive type mismatch errors during evaluation.
*/
func TestComparatorTyping(test *testing.T) {
evaluationTests := []EvaluationFailureTest{
EvaluationFailureTest{
Name: "GT literal bool to literal bool",
Input: "true > true",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "GT bool to bool",
Input: "bool > bool",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "GTE bool to bool",
Input: "bool >= bool",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "LT bool to bool",
Input: "bool < bool",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "LTE bool to bool",
Input: "bool <= bool",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "GT number to string",
Input: "number > string",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "GTE number to string",
Input: "number >= string",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "LT number to string",
Input: "number < string",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "REQ number to string",
Input: "number =~ string",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "REQ number to bool",
Input: "number =~ bool",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "REQ bool to number",
Input: "bool =~ number",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "REQ bool to string",
Input: "bool =~ string",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "NREQ number to string",
Input: "number !~ string",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "NREQ number to bool",
Input: "number !~ bool",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "NREQ bool to number",
Input: "bool !~ number",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "NREQ bool to string",
Input: "bool !~ string",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "IN non-array numeric",
Input: "1 in 2",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "IN non-array string",
Input: "1 in 'foo'",
Expected: INVALID_COMPARATOR_TYPES,
},
EvaluationFailureTest{
Name: "IN non-array boolean",
Input: "1 in true",
Expected: INVALID_COMPARATOR_TYPES,
},
}
runEvaluationFailureTests(evaluationTests, test)
}
func TestTernaryTyping(test *testing.T) {
evaluationTests := []EvaluationFailureTest{
EvaluationFailureTest{
Name: "Ternary with number",
Input: "10 ? true",
Expected: INVALID_TERNARY_TYPES,
},
EvaluationFailureTest{
Name: "Ternary with string",
Input: "'foo' ? true",
Expected: INVALID_TERNARY_TYPES,
},
}
runEvaluationFailureTests(evaluationTests, test)
}
func TestRegexParameterCompilation(test *testing.T) {
evaluationTests := []EvaluationFailureTest{
EvaluationFailureTest{
Name: "Regex equality runtime parsing",
Input: "'foo' =~ foo",
Parameters: map[string]interface{}{
"foo": "[foo",
},
Expected: INVALID_REGEX,
},
EvaluationFailureTest{
Name: "Regex inequality runtime parsing",
Input: "'foo' =~ foo",
Parameters: map[string]interface{}{
"foo": "[foo",
},
Expected: INVALID_REGEX,
},
}
runEvaluationFailureTests(evaluationTests, test)
}
func TestFunctionExecution(test *testing.T) {
evaluationTests := []EvaluationFailureTest{
EvaluationFailureTest{
Name: "Function error bubbling",
Input: "error()",
Functions: map[string]ExpressionFunction{
"error": func(arguments ...interface{}) (interface{}, error) {
return nil, errors.New("Huge problems")
},
},
Expected: "Huge problems",
},
}
runEvaluationFailureTests(evaluationTests, test)
}
func TestInvalidParameterCalls(test *testing.T) {
evaluationTests := []EvaluationFailureTest{
EvaluationFailureTest{
Name: "Missing parameter field reference",
Input: "foo.NotExists",
Parameters: fooFailureParameters,
Expected: INVALID_PARAMETER_CALL,
},
EvaluationFailureTest{
Name: "Parameter method call on missing function",
Input: "foo.NotExist()",
Parameters: fooFailureParameters,
Expected: INVALID_PARAMETER_CALL,
},
EvaluationFailureTest{
Name: "Nested missing parameter field reference",
Input: "foo.Nested.NotExists",
Parameters: fooFailureParameters,
Expected: INVALID_PARAMETER_CALL,
},
EvaluationFailureTest{
Name: "Parameter method call returns error",
Input: "foo.AlwaysFail()",
Parameters: fooFailureParameters,
Expected: "function should always fail",
},
EvaluationFailureTest{
Name: "Too few arguments to parameter call",
Input: "foo.FuncArgStr()",
Parameters: fooFailureParameters,
Expected: TOO_FEW_ARGS,
},
EvaluationFailureTest{
Name: "Too many arguments to parameter call",
Input: "foo.FuncArgStr('foo', 'bar', 15)",
Parameters: fooFailureParameters,
Expected: TOO_MANY_ARGS,
},
EvaluationFailureTest{
Name: "Mismatched parameters",
Input: "foo.FuncArgStr(5)",
Parameters: fooFailureParameters,
Expected: MISMATCHED_PARAMETERS,
},
}
runEvaluationFailureTests(evaluationTests, test)
}
func runEvaluationFailureTests(evaluationTests []EvaluationFailureTest, test *testing.T) {
var expression *EvaluableExpression
var err error
fmt.Printf("Running %d negative parsing test cases...\n", len(evaluationTests))
for _, testCase := range evaluationTests {
if len(testCase.Functions) > 0 {
expression, err = NewEvaluableExpressionWithFunctions(testCase.Input, testCase.Functions)
} else {
expression, err = NewEvaluableExpression(testCase.Input)
}
if err != nil {
test.Logf("Test '%s' failed", testCase.Name)
test.Logf("Expected evaluation error, but got parsing error: '%s'", err)
test.Fail()
continue
}
if testCase.Parameters == nil {
testCase.Parameters = EVALUATION_FAILURE_PARAMETERS
}
_, err = expression.Evaluate(testCase.Parameters)
if err == nil {
test.Logf("Test '%s' failed", testCase.Name)
test.Logf("Expected error, received none.")
test.Fail()
continue
}
if !strings.Contains(err.Error(), testCase.Expected) {
test.Logf("Test '%s' failed", testCase.Name)
test.Logf("Got error: '%s', expected '%s'", err.Error(), testCase.Expected)
test.Fail()
continue
}
}
}