-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_anythingbut_test.go
62 lines (50 loc) · 2.17 KB
/
pattern_anythingbut_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
package hypermatch
import (
"gotest.tools/v3/assert"
"testing"
)
type compoundTestTable struct {
pattern Pattern
shouldMatch []string
shouldNotMatch []string
}
func TestCompilePatternAnythingBut(t *testing.T) {
test := []compoundTestTable{
{pattern: Pattern{Type: PatternAnythingBut, Sub: []Pattern{{Type: PatternEquals, Value: "hallo"}}}, shouldMatch: []string{"welt"}, shouldNotMatch: []string{"hallo"}},
{pattern: Pattern{Type: PatternAnythingBut, Sub: []Pattern{{Type: PatternWildcard, Value: "ha*o"}}}, shouldMatch: []string{"welt"}, shouldNotMatch: []string{"hallo"}},
}
for i, tt := range test {
sourceFm := newFieldMatcher()
compilePatternAnythingBut(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)
}
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)
_, ok := sourceFm.AnythingButTransitions["test"][target[0].MatchingAnythingButRuleIdentifiers[0]]
assert.Check(t, ok, "expected not to match '%s' with pattern '%v'", n, tt.pattern)
}
}
}
func TestValidatePatternAnythingBut_EmptyValueAndSubPatterns(t *testing.T) {
path := "testPath"
pattern := &Pattern{Type: PatternAnythingBut}
err := validatePatternAnythingBut(path, pattern)
assert.ErrorContains(t, err, "[anythingBut] must contain a value or sub-patterns")
}
func TestValidatePatternAnythingBut_WithValueAndEmptySubPatterns(t *testing.T) {
path := "testPath"
pattern := &Pattern{Type: PatternAnythingBut, Value: "testValue"}
err := validatePatternAnythingBut(path, pattern)
assert.NilError(t, err)
}
func TestValidatePatternAnythingBut_EmptyValueAndNonEmptySubPatterns(t *testing.T) {
path := "testPath"
pattern := &Pattern{Type: PatternAnythingBut, Sub: []Pattern{
{Type: PatternEquals, Value: "testValue"},
}}
err := validatePatternAnythingBut(path, pattern)
assert.NilError(t, err)
}