forked from hashicorp/go-bexpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_test.go
655 lines (626 loc) · 22.9 KB
/
evaluate_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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package bexpr
import (
"errors"
"fmt"
"reflect"
"testing"
"github.com/mitchellh/pointerstructure"
"github.com/stretchr/testify/require"
)
type expressionCheck struct {
expression string
result bool
err string
benchQuick bool
hook ValueTransformationHookFn
}
type expressionTest struct {
value interface{}
expressions []expressionCheck
}
var evaluateTests map[string]expressionTest = map[string]expressionTest{
"Flat Struct": {
testFlatStruct{
Int: -1,
Int8: -2,
Int16: -3,
Int32: -4,
Int64: -5,
Uint: 6,
Uint8: 7,
Uint16: 8,
Uint32: 9,
Uint64: 10,
Float32: 1.1,
Float64: 1.2,
Bool: true,
String: "exported",
ColonString: "expo:rted",
Slash: "hello",
unexported: "unexported",
Hidden: true,
},
[]expressionCheck{
{expression: "Int == -1", result: true, benchQuick: true},
{expression: "Int == `foo`", result: true, hook: func(reflect.Value) reflect.Value { return reflect.ValueOf("foo") }},
{expression: "Int == -99", result: false, benchQuick: true},
{expression: "Int != -1", result: false},
{expression: "Int != -99", result: true},
{expression: "Int8 == -2", result: true},
{expression: "Int8 == -99", result: false},
{expression: "Int8 != -2", result: false},
{expression: "Int8 != -99", result: true},
{expression: "Int16 == -3", result: true},
{expression: "Int16 == -99", result: false},
{expression: "Int16 != -3", result: false},
{expression: "Int16 != -99", result: true},
{expression: "Int32 == -4", result: true},
{expression: "Int32 == -99", result: false},
{expression: "Int32 != -4", result: false},
{expression: "Int32 != -99", result: true},
{expression: "Int64 == -5", result: true},
{expression: "Int64 == -99", result: false},
{expression: "Int64 != -5", result: false},
{expression: "Int64 != -99", result: true},
{expression: "Uint == 6", result: true},
{expression: "Uint == 99", result: false},
{expression: "Uint != 6", result: false},
{expression: "Uint != 99", result: true},
{expression: "Uint8 == 7", result: true},
{expression: "Uint8 == 99", result: false},
{expression: "Uint8 != 7", result: false},
{expression: "Uint8 != 99", result: true},
{expression: "Uint16 == 8", result: true},
{expression: "Uint16 == 99", result: false},
{expression: "Uint16 != 8", result: false},
{expression: "Uint16 != 99", result: true},
{expression: "Uint32 == 9", result: true},
{expression: "Uint32 == 99", result: false},
{expression: "Uint32 != 9", result: false},
{expression: "Uint32 != 99", result: true},
{expression: "Uint64 == 10", result: true},
{expression: "Uint64 == 99", result: false},
{expression: "Uint64 != 10", result: false, benchQuick: true},
{expression: "Uint64 != 99", result: true, benchQuick: true},
{expression: "Float32 == 1.1", result: true},
{expression: "Float32 == 9.9", result: false},
{expression: "Float32 != 1.1", result: false},
{expression: "Float32 != 9.9", result: true},
{expression: "Float64 == 1.2", result: true},
{expression: "Float64 == 9.9", result: false},
{expression: "Float64 != 1.2", result: false, benchQuick: true},
{expression: "Float64 != 9.9", result: true, benchQuick: true},
{expression: "Bool == true", result: true},
{expression: "Bool == false", result: false},
{expression: "Bool != true", result: false},
{expression: "Bool != false", result: true},
{expression: "String == `exported`", result: true, benchQuick: true},
{expression: "String == `not-it`", result: false, benchQuick: true},
{expression: "String != `exported`", result: false},
{expression: "String != `not-it`", result: true},
{expression: "String != `not-it`", result: false, hook: func(value reflect.Value) reflect.Value { return reflect.ValueOf("not-it") }},
{expression: "port in String", result: true, benchQuick: true},
{expression: "part in String", result: false},
{expression: "port not in String", result: false},
{expression: "part not in String", result: true},
{expression: "ColonString == `expo:rted`", result: true},
{expression: "ColonString != `expor:ted`", result: true},
{expression: "slash/value == `hello`", result: true},
{expression: "unexported == `unexported`", result: false, err: `error finding value in datum: /unexported at part 0: couldn't find key: struct field with name "unexported"`},
{expression: "Hidden == false", result: false, err: "error finding value in datum: /Hidden at part 0: struct field \"Hidden\" is ignored and cannot be used"},
{expression: "String matches `^ex.*`", result: true, benchQuick: true},
{expression: "String not matches `^anchored.*`", result: true, benchQuick: true},
{expression: "String matches `^anchored.*`", result: false},
{expression: "String not matches `^ex.*`", result: false},
},
},
"Flat Struct Alt Types": {
testFlatStructAlt{
Int: -1,
Int8: -2,
Int16: -3,
Int32: -4,
Int64: -5,
Uint: 6,
Uint8: 7,
Uint16: 8,
Uint32: 9,
Uint64: 10,
Float32: 1.1,
Float64: 1.2,
Bool: true,
String: "exported",
Hidden: true,
},
[]expressionCheck{
{expression: "Int == -1", result: true, benchQuick: true},
{expression: "Int == -99", result: false, benchQuick: true},
{expression: "Int != -1", result: false},
{expression: "Int != -99", result: true},
{expression: "Int8 == -2", result: true},
{expression: "Int8 == -99", result: false},
{expression: "Int8 != -2", result: false},
{expression: "Int8 != -99", result: true},
{expression: "Int16 == -3", result: true},
{expression: "Int16 == -99", result: false},
{expression: "Int16 != -3", result: false},
{expression: "Int16 != -99", result: true},
{expression: "Int32 == -4", result: true},
{expression: "Int32 == -99", result: false},
{expression: "Int32 != -4", result: false},
{expression: "Int32 != -99", result: true},
{expression: "Int64 == -5", result: true},
{expression: "Int64 == -99", result: false},
{expression: "Int64 != -5", result: false},
{expression: "Int64 != -99", result: true},
{expression: "Uint == 6", result: true},
{expression: "Uint == 99", result: false},
{expression: "Uint != 6", result: false},
{expression: "Uint != 99", result: true},
{expression: "Uint8 == 7", result: true},
{expression: "Uint8 == 99", result: false},
{expression: "Uint8 != 7", result: false},
{expression: "Uint8 != 99", result: true},
{expression: "Uint16 == 8", result: true},
{expression: "Uint16 == 99", result: false},
{expression: "Uint16 != 8", result: false},
{expression: "Uint16 != 99", result: true},
{expression: "Uint32 == 9", result: true},
{expression: "Uint32 == 99", result: false},
{expression: "Uint32 != 9", result: false},
{expression: "Uint32 != 99", result: true},
{expression: "Uint64 == 10", result: true},
{expression: "Uint64 == 99", result: false},
{expression: "Uint64 != 10", result: false, benchQuick: true},
{expression: "Uint64 != 99", result: true, benchQuick: true},
{expression: "Float32 == 1.1", result: true},
{expression: "Float32 == 9.9", result: false},
{expression: "Float32 != 1.1", result: false},
{expression: "Float32 != 9.9", result: true},
{expression: "Float64 == 1.2", result: true},
{expression: "Float64 == 9.9", result: false},
{expression: "Float64 != 1.2", result: false, benchQuick: true},
{expression: "Float64 != 9.9", result: true, benchQuick: true},
{expression: "Bool == true", result: true},
{expression: "Bool == false", result: false},
{expression: "Bool != true", result: false},
{expression: "Bool != false", result: true},
{expression: "String == `exported`", result: true, benchQuick: true},
{expression: "String == `not-it`", result: false, benchQuick: true},
{expression: "String != `exported`", result: false},
{expression: "String != `not-it`", result: true},
{expression: "unexported == `unexported`", result: false, err: `error finding value in datum: /unexported at part 0: couldn't find key: struct field with name "unexported"`},
{expression: "Hidden == false", result: false, err: "error finding value in datum: /Hidden at part 0: struct field \"Hidden\" is ignored and cannot be used"},
},
},
"map[string]map[string]bool": {
map[string]map[string]bool{
"foo": {
"bar": true,
"baz": false,
},
"abc": nil,
"co:lon": {
"bar": true,
},
},
[]expressionCheck{
{expression: "foo == true", result: true, hook: func(v reflect.Value) reflect.Value {
if r := v.MapIndex(reflect.ValueOf("bar")); !r.IsZero() {
return r
}
return v
}},
{expression: "bar in foo", result: true},
{expression: `bar in "/co:lon"`, result: true},
{expression: "arg in foo", result: false},
{expression: "arg not in foo", result: true},
{expression: "baz not in foo", result: false},
{expression: "foo is empty", result: false},
{expression: "foo is not empty", result: true},
{expression: "abc is empty", result: true},
{expression: "abc is not empty", result: false},
{expression: "foo in abc", result: false, benchQuick: true},
{expression: "foo not in abc", result: true},
{expression: "foo.bar == true", result: true},
{expression: "foo.bar == false", result: false},
{expression: "foo.baz == false", result: true},
{expression: "foo.baz == true", result: false, benchQuick: true},
{expression: "foo.bar != true", result: false},
{expression: "foo.bar != false", result: true},
{expression: "foo.baz != false", result: false},
{expression: "foo.baz != true", result: true},
{expression: "foo.bar.baz == 3", result: false, err: `error finding value in datum: /foo/bar/baz: at part 2, invalid value kind: bool`},
},
},
"Nested Structs and Maps": {
testNestedTypes{
Nested: testNestedLevel1{
Map: map[string]string{
"foo": "bar",
"bar": "baz",
"abc": "123",
"colon": "co:lon",
"co:lon": "co:lon",
"email": "foo@example.com",
},
MapOfStructs: map[string]testNestedLevel2_1{
"one": {
Foo: 42,
Baz: "exported",
},
"two": {
Foo: 77,
Baz: "consul",
},
},
SliceOfInts: []int{1, 3, 5, 7, 9},
SliceOfStructs: []testNestedLevel2_2{
{
X: 1,
Y: 2,
z: 10,
},
{
X: 3,
Y: 5,
z: 10,
},
},
SliceOfInfs: []interface{}{"foobar", 1, true},
},
TopInt: 5,
},
[]expressionCheck{
{expression: "Nested.Map == bar", result: true, benchQuick: true, hook: func(v reflect.Value) reflect.Value {
if r, ok := v.Interface().(map[string]string); ok {
return reflect.ValueOf(r["foo"])
}
return v
}},
{expression: "Nested.Map.foo == bar", result: true, benchQuick: true},
{expression: "Nested.Map.foo contains ba", result: true, benchQuick: true},
{expression: "Nested.Map.foo == baz", result: false},
{expression: "Nested.Map is not empty", result: true},
{expression: "Nested.Map is not empty", result: true},
{expression: "Nested.Map contains foo and Nested.Map contains bar", result: true, benchQuick: true},
{expression: `Nested.Map.colon == "co:lon"`, result: true},
{expression: `"/Nested/Map/co:lon" == "co:lon"`, result: true},
{expression: "Nested.Map contains nope", result: false},
{expression: "Nested.Map contains bar", result: true},
{expression: "Nested.Map.bar == `bazel`", result: false, benchQuick: true},
{expression: "TopInt != 0", result: true},
{expression: "Nested.Map contains nope or (Nested.Map contains bar and Nested.Map.bar == `bazel`) or TopInt != 0", result: true, benchQuick: true},
{expression: "Nested.MapOfStructs.one.Foo == 42", result: true},
{expression: "7 in Nested.SliceOfInts", result: true},
{expression: `"/Nested/SliceOfInts" == "7"`, result: false, err: `unable to find suitable primitive comparison function for matching`},
{expression: "Nested.MapOfStructs is empty or (Nested.SliceOfInts contains 7 and 9 in Nested.SliceOfInts)", result: true, benchQuick: true},
{expression: "Nested.SliceOfStructs.0.X == 1", result: true},
{expression: "Nested.SliceOfStructs.0.Y == 4", result: false},
{expression: "Map in Nested", result: false, err: "Cannot perform in/contains operations on type struct for selector: \"Nested\""},
{expression: `"foobar" in "/Nested/SliceOfInfs"`, result: true},
{expression: `"1" in "/Nested/SliceOfInfs"`, result: true},
{expression: `"2" in "/Nested/SliceOfInfs"`, result: false},
{expression: `"true" in "/Nested/SliceOfInfs"`, result: true},
{expression: `"/Nested/Map/email" matches "(foz|foo)@example.com"`, result: true},
// Missing key in map tests
{expression: "Nested.Map.notfound == 4", result: false},
{expression: "Nested.Map.notfound != 4", result: true},
{expression: "4 in Nested.Map.notfound", result: false},
{expression: "4 not in Nested.Map.notfound", result: true},
{expression: "Nested.Map.notfound is empty", result: true},
{expression: "Nested.Map.notfound is not empty", result: false},
{expression: `Nested.Map.notfound matches ".*"`, result: false},
{expression: `Nested.Map.notfound not matches ".*"`, result: true},
// Missing field in struct tests
{expression: "Nested.Notfound == 4", result: false, err: `error finding value in datum: /Nested/Notfound at part 1: couldn't find key: struct field with name "Notfound"`},
{expression: "Nested.Notfound != 4", result: false, err: `error finding value in datum: /Nested/Notfound at part 1: couldn't find key: struct field with name "Notfound"`},
{expression: "4 in Nested.Notfound", result: false, err: `error finding value in datum: /Nested/Notfound at part 1: couldn't find key: struct field with name "Notfound"`},
{expression: "4 not in Nested.Notfound", result: false, err: `error finding value in datum: /Nested/Notfound at part 1: couldn't find key: struct field with name "Notfound"`},
{expression: "Nested.Notfound is empty", result: false, err: `error finding value in datum: /Nested/Notfound at part 1: couldn't find key: struct field with name "Notfound"`},
{expression: "Nested.Notfound is not empty", result: false, err: `error finding value in datum: /Nested/Notfound at part 1: couldn't find key: struct field with name "Notfound"`},
{expression: `Nested.Notfound matches ".*"`, result: false, err: `error finding value in datum: /Nested/Notfound at part 1: couldn't find key: struct field with name "Notfound"`},
{expression: `Nested.Notfound not matches ".*"`, result: false, err: `error finding value in datum: /Nested/Notfound at part 1: couldn't find key: struct field with name "Notfound"`},
// all
{expression: `all Nested.SliceOfInts as i { i != 42 }`, result: true},
{expression: `all Nested.SliceOfInts as i { i == 1 }`, result: false},
{expression: `all Nested.Map as v { v == "bar" }`, result: false},
{expression: `all Nested.Map as v { v != "hello" }`, result: true},
{expression: `all Nested.Map as k, k { TopInt == 5 }`, err: `"k" cannot be used as a placeholder for both the index and the value`},
{expression: `all Nested.Map as k, _ { k != "foo" }`, result: false},
{expression: `all Nested.Map as k, _ { k != "hello" }`, result: true},
{expression: `all Nested.Map as k, v { k != "foo" or v != "baz" }`, result: true},
{expression: `all TopInt as k, v { k != "foo" or v != "baz" }`, err: "TopInt is not a list or a map"},
// any
{expression: `any Nested.SliceOfInts as i { i == 1 }`, result: true},
{expression: `any Nested.SliceOfInts as i { i == 42 }`, result: false},
{expression: `any Nested.SliceOfStructs as i { "/i/X" == 1 }`, result: true},
{expression: `any Nested.Map as k { k != "bar" }`, result: true},
{expression: `any Nested.Map as k { k == "bar" }`, result: true},
{expression: `any Nested.Map as k { k == "hello" }`, result: false},
{expression: `any Nested.Map as k, v { k == "foo" and v == "bar" }`, result: true},
{expression: `any Nested.Map as k { k.Color == "red" }`, err: "/k references a string so /k/Color is invalid"},
{expression: `any Nested.SliceOfInts as i, _ { i.Color == "red" }`, err: "/i references a int so /i/Color is invalid"},
},
},
}
func TestEvaluate(t *testing.T) {
t.Parallel()
for name, tcase := range evaluateTests {
// capture these values in the closure
name := name
tcase := tcase
t.Run(name, func(t *testing.T) {
t.Parallel()
for i, expTest := range tcase.expressions {
// capture these values in the closure
expTest := expTest
t.Run(fmt.Sprintf("#%d - %s", i, expTest.expression), func(t *testing.T) {
t.Parallel()
expr, err := CreateEvaluator(expTest.expression, WithHookFn(expTest.hook))
require.NoError(t, err)
match, err := expr.Evaluate(tcase.value)
if expTest.err != "" {
require.Error(t, err)
require.EqualError(t, err, expTest.err)
} else {
require.NoError(t, err)
}
require.Equal(t, expTest.result, match)
})
}
})
}
}
func TestWithHookFn(t *testing.T) {
t.Parallel()
type testStruct struct {
I interface{}
S *testStruct
}
cases := []struct {
name string
hook ValueTransformationHookFn
in *testStruct
eval []expressionCheck
}{
{
name: "simple",
hook: func(v reflect.Value) reflect.Value { return v },
in: &testStruct{I: "foo"},
eval: []expressionCheck{
{expression: `"/I"=="foo"`, result: true},
},
},
{
name: "dive to pointer",
hook: func(v reflect.Value) reflect.Value {
if r, ok := v.Interface().(*testStruct); ok {
return reflect.ValueOf(r.I)
}
return v
},
in: &testStruct{S: &testStruct{I: "foo"}, I: &testStruct{I: &testStruct{I: "bar"}}},
eval: []expressionCheck{
{expression: `"/S"=="foo"`, result: true},
{expression: `"/I/I"=="bar"`, result: true},
{
expression: `"/S/I"=="foo"`, result: false,
err: "error finding value in datum: /S/I: at part 1, invalid value kind: string",
},
},
},
{
name: "valueTransformationHook returns nil interface{}",
hook: func(v reflect.Value) reflect.Value { return reflect.ValueOf(nil) },
in: &testStruct{I: "foo"},
eval: []expressionCheck{
{
expression: `"/I"=="foo"`, result: false,
err: "error finding value in datum: /I at part 0: ValueTransformationHook returned the value of a nil interface",
},
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
for _, eval := range tc.eval {
expr, err := CreateEvaluator(eval.expression, WithHookFn(tc.hook))
require.NoError(t, err)
match, err := expr.Evaluate(tc.in)
if eval.err != "" {
require.Error(t, err)
require.Equal(t, eval.err, err.Error())
} else {
require.NoError(t, err)
}
require.Equal(t, eval.result, match)
}
})
}
}
func TestUnknownVal(t *testing.T) {
t.Parallel()
cases := []struct {
name string
expression string
unknownVal interface{}
result bool
err string
}{
{
name: "key exists",
expression: `key == "foo"`,
unknownVal: "bar",
result: true,
},
{
name: "key does not exist",
expression: `unknown == "bar"`,
unknownVal: "bar",
result: true,
},
{
name: "key does not exist (fail)",
expression: `unknown == "qux"`,
unknownVal: "bar",
result: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
expr, err := CreateEvaluator(tc.expression, WithUnknownValue(tc.unknownVal))
require.NoError(t, err)
match, err := expr.Evaluate(map[string]string{
"key": "foo",
})
if tc.err != "" {
require.Error(t, err)
require.EqualError(t, err, tc.err)
} else {
require.NoError(t, err)
}
require.Equal(t, tc.result, match)
})
}
}
func TestUnknownVal_struct(t *testing.T) {
t.Parallel()
cases := []struct {
name string
expression string
unknownVal interface{}
result bool
err string
}{
{
name: "key exists",
expression: `key == "foo"`,
unknownVal: "bar",
result: true,
},
{
name: "key does not exist",
expression: `unknown == "bar"`,
unknownVal: "bar",
result: true,
},
{
name: "key does not exist (fail)",
expression: `unknown == "qux"`,
unknownVal: "bar",
result: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
expr, err := CreateEvaluator(tc.expression, WithUnknownValue(tc.unknownVal))
require.NoError(t, err)
match, err := expr.Evaluate(struct {
Key string `bexpr:"key"`
}{
Key: "foo",
})
if tc.err != "" {
require.Error(t, err)
require.EqualError(t, err, tc.err)
} else {
require.NoError(t, err)
}
require.Equal(t, tc.result, match)
})
}
}
func TestCustomTag(t *testing.T) {
t.Parallel()
type testStruct struct {
BexprName string `bexpr:"bname"`
JsonName string `json:"jname"`
}
ts := testStruct{BexprName: "foo", JsonName: "bar"}
cases := []struct {
name string
expression string
jsonTag bool
bnameFound bool
jnameFound bool
}{
{
name: "bexpr tag, bname",
expression: `"/bname" == "foo"`,
bnameFound: true,
},
{
name: "bexpr tag, jname",
expression: `"/jname" == "bar"`,
},
{
name: "json tag, bname",
expression: `"/bname" == "foo"`,
jsonTag: true,
},
{
name: "json tag, jname",
expression: `"/jname" == "bar"`,
jsonTag: true,
jnameFound: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var opts []Option
if tc.jsonTag {
opts = append(opts, WithTagName("json"))
}
expr, err := CreateEvaluator(tc.expression, opts...)
require.NoError(t, err)
match, err := expr.Evaluate(ts)
if tc.jsonTag {
if tc.jnameFound {
require.NoError(t, err)
require.True(t, match)
} else {
require.True(t, errors.Is(err, pointerstructure.ErrNotFound))
}
} else {
if tc.bnameFound {
require.NoError(t, err)
require.True(t, match)
} else {
require.True(t, errors.Is(err, pointerstructure.ErrNotFound))
}
}
})
}
}
func BenchmarkEvaluate(b *testing.B) {
for name, tcase := range evaluateTests {
// capture these values in the closure
name := name
tcase := tcase
b.Run(name, func(b *testing.B) {
for i, expTest := range tcase.expressions {
// capture these values in the closure
expTest := expTest
b.Run(fmt.Sprintf("#%d", i), func(b *testing.B) {
if !expTest.benchQuick && !FullBenchmarks() {
b.Skip("Skipping benchmark - rerun with -bench-full to enable")
}
expr, err := CreateEvaluator(expTest.expression, WithHookFn(expTest.hook))
require.NoError(b, err)
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err = expr.Evaluate(tcase.value)
if expTest.err != "" {
require.Error(b, err)
} else {
require.NoError(b, err)
}
}
})
}
})
}
}