-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_wildcard_test.go
78 lines (68 loc) · 2.53 KB
/
pattern_wildcard_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
package hypermatch
import (
"gotest.tools/v3/assert"
"testing"
)
type testTable struct {
input string
shouldMatch []string
shouldNotMatch []string
}
func TestCompilePatternWildcard(t *testing.T) {
test := []testTable{
{input: "ha*o", shouldMatch: []string{"hao", "halo", "hallo", "haweltlo"}, shouldNotMatch: []string{"", "welt", "hoa", "haa"}},
{input: "wel*", shouldMatch: []string{"wel", "welt", "weltttttttt"}, shouldNotMatch: []string{"", "hallo", "walt", "wet"}},
{input: "*elt", shouldMatch: []string{"welt", "elt", "weltttttttelt"}, shouldNotMatch: []string{"", "wel", "walt", "wet"}},
{input: "*", shouldMatch: []string{"anything", ""}, shouldNotMatch: []string{}},
{input: "*-mon-*", shouldMatch: []string{"s1-mon-test", "s1-mon-mon-mon-test"}, shouldNotMatch: []string{"se1-monn-test"}},
}
for _, tt := range test {
start := newNfaStep()
fm := compilePatternWildcard(start, charReplace(str2value(tt.input, nil, nil), charWildcard, byteWildcard), nil)
for _, m := range tt.shouldMatch {
target := transitionNfa(start, str2value(m, nil, nil), nil)
assert.Check(t, len(target) > 0, "expected match '%s' with pattern '%s'", m, tt.input)
assert.Check(t, fm == target[0], "expected match '%s' with pattern '%s'", m, tt.input)
}
for _, n := range tt.shouldNotMatch {
target := transitionNfa(start, str2value(n, nil, nil), nil)
assert.Check(t, len(target) == 0, "expected not to match '%s' with pattern '%s'", n, tt.input)
}
}
}
func TestValidatePatternWildcard_EmptyValue(t *testing.T) {
pattern := &Pattern{
Type: PatternWildcard,
}
err := validatePatternWildcard(pattern)
assert.ErrorContains(t, err, "[wildcard] must contain a value")
}
func TestValidatePatternWildcard_SubPatterns(t *testing.T) {
pattern := &Pattern{
Type: PatternWildcard,
Sub: []Pattern{
{Type: PatternEquals, Value: "test"},
},
}
err := validatePatternWildcard(pattern)
assert.ErrorContains(t, err, "[wildcard] must contain a value")
}
func TestValidatePatternWildcard_SubPatterns2(t *testing.T) {
pattern := &Pattern{
Type: PatternWildcard,
Value: "invalid",
Sub: []Pattern{
{Type: PatternEquals, Value: "test"},
},
}
err := validatePatternWildcard(pattern)
assert.ErrorContains(t, err, "[wildcard] must not contain sub-patterns")
}
func TestValidatePatternWildcard_TwoConsecutiveWildcards(t *testing.T) {
pattern := &Pattern{
Type: PatternWildcard,
Value: "**test",
}
err := validatePatternWildcard(pattern)
assert.ErrorContains(t, err, "[wildcard] must not contain two consecutive wildcards")
}