-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_anyof_test.go
70 lines (61 loc) · 2.15 KB
/
pattern_anyof_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
package hypermatch
import (
"gotest.tools/v3/assert"
"testing"
)
func TestCompilePatternAnyOf(t *testing.T) {
test := []compoundTestTable{
{pattern: Pattern{Type: PatternAnyOf, Sub: []Pattern{
{Type: PatternEquals, Value: "hallo"},
{Type: PatternEquals, Value: "welt"},
}}, shouldMatch: []string{"welt", "hallo"}, shouldNotMatch: []string{"was"}},
{pattern: Pattern{Type: PatternAnyOf, Sub: []Pattern{
{Type: PatternEquals, Value: "hallo"},
{Type: PatternWildcard, Value: "wel*"},
}}, shouldMatch: []string{"welt", "hallo", "weltttt"}, shouldNotMatch: []string{"was"}},
}
for i, tt := range test {
sourceFm := newFieldMatcher()
fm := compilePatternAnyOf(RuleIdentifier(i), "test", &tt.pattern, sourceFm, nil)
for _, m := range tt.shouldMatch {
target := transitionNfa(sourceFm.GetTransition("test").Nfa, str2value(m, nil, nil), nil)
assert.Check(t, len(target) > 0, "expected match '%s' with pattern '%v'", m, tt.pattern)
assert.Check(t, fm == target[0], "expected match '%s' with pattern '%v'", m, tt.pattern)
}
for _, n := range tt.shouldNotMatch {
target := transitionNfa(sourceFm.GetTransition("test").Nfa, str2value(n, nil, nil), nil)
assert.Check(t, len(target) == 0, "expected not to match '%s' with pattern '%v'", n, tt.pattern)
}
}
}
func TestValidatePatternAnyOf_ErrorForValue(t *testing.T) {
pattern := Pattern{
Type: PatternAnyOf,
Value: "invalid",
Sub: []Pattern{
{Type: PatternEquals, Value: "hallo"},
{Type: PatternEquals, Value: "welt"},
},
}
err := validatePatternAnyOf("test", &pattern)
assert.ErrorContains(t, err, "[anyOf] must not contain a value")
}
func TestValidatePatternAnyOf_ErrorForNoSubPatterns(t *testing.T) {
pattern := Pattern{
Type: PatternAnyOf,
Sub: []Pattern{},
}
err := validatePatternAnyOf("test", &pattern)
assert.ErrorContains(t, err, "[anyOf] must contain sub-patterns")
}
func TestValidatePatternAnyOf_CorrectlyValidatesSubPatterns(t *testing.T) {
pattern := Pattern{
Type: PatternAnyOf,
Sub: []Pattern{
{Type: PatternEquals, Value: "hallo"},
{Type: PatternEquals, Value: "welt"},
},
}
err := validatePatternAnyOf("test", &pattern)
assert.NilError(t, err)
}