-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_allof_test.go
104 lines (95 loc) · 2.76 KB
/
pattern_allof_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
package hypermatch
import (
"gotest.tools/v3/assert"
"testing"
)
type compoundMultiTestTable struct {
pattern Pattern
shouldMatch [][]string
shouldNotMatch [][]string
}
func TestCompilePatternAllOf(t *testing.T) {
test := []compoundMultiTestTable{
{pattern: Pattern{Type: PatternAnyOf, Sub: []Pattern{
{Type: PatternEquals, Value: "hallo"},
{Type: PatternEquals, Value: "welt"},
}}, shouldMatch: [][]string{{"welt", "hallo"}}, shouldNotMatch: [][]string{{"welt"}, {"hallo"}, {"welt", "eins"}}},
{pattern: Pattern{Type: PatternAnyOf, Sub: []Pattern{
{Type: PatternEquals, Value: "hallo"},
{Type: PatternWildcard, Value: "wel*"},
}}, shouldMatch: [][]string{{"welt", "hallo", "weltttt"}}, shouldNotMatch: [][]string{{"welt"}, {"hallo"}, {"welt", "eins"}}},
}
for i, tt := range test {
sourceFm := newFieldMatcher()
fm := compilePatternAllOf(RuleIdentifier(i), "test", &tt.pattern, sourceFm, nil)
for _, m := range tt.shouldMatch {
start := sourceFm
for range m {
match := matchAny(start, m)
assert.Check(t, match != nil, "expected match '%s' with pattern '%v'", m, tt.pattern)
start = match
if start == fm {
break
}
}
assert.Check(t, fm == start, "expected match '%s' with pattern '%v'", m, tt.pattern)
}
for _, n := range tt.shouldNotMatch {
start := sourceFm
found := true
for range n {
match := matchAny(start, n)
if match == nil {
found = false
break
}
start = match
}
assert.Check(t, !found || fm != start, "expected not to match '%s' with pattern '%v'", n, tt.pattern)
}
}
}
func matchAny(start *fieldMatcher, values []string) *fieldMatcher {
for _, v := range values {
fm := transitionNfa(start.GetTransition("test").Nfa, str2value(v, nil, nil), nil)
if len(fm) > 0 {
return fm[0]
}
}
return nil
}
func TestValidatePatternAllOf_WithValue(t *testing.T) {
err := validatePatternAllOf("test", &Pattern{
Type: PatternAllOf,
Value: "invalid",
Sub: []Pattern{
{Type: PatternEquals, Value: "value1"},
{Type: PatternEquals, Value: "value2"},
},
})
assert.ErrorContains(t, err, "'[allOf] must not contain a value")
}
func TestValidatePatternAllOf_NoSubPatterns(t *testing.T) {
err := validatePatternAllOf("test", &Pattern{
Type: PatternAllOf,
Sub: []Pattern{},
})
assert.ErrorContains(t, err, "[allOf] must contain sub-patterns")
}
func TestValidatePatternAllOf_NestedPatterns(t *testing.T) {
err := validatePatternAllOf("test", &Pattern{
Type: PatternAllOf,
Sub: []Pattern{
{Type: PatternEquals, Value: "value1"},
{
Type: PatternAllOf,
Sub: []Pattern{
{Type: PatternEquals, Value: "value2"},
{Type: PatternEquals, Value: "value3"},
},
},
{Type: PatternEquals, Value: "value4"},
},
})
assert.NilError(t, err)
}