-
Notifications
You must be signed in to change notification settings - Fork 1
/
wildcard_test.go
117 lines (102 loc) · 2.06 KB
/
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
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
package wildcard
import (
"testing"
"github.com/stretchr/testify/assert"
)
type wildPatternTestCase struct {
p string
m bool
}
type matchTestCase struct {
p string
s string
m bool
}
func TestIsWildPattern(t *testing.T) {
testCases1 := []wildPatternTestCase{
{"*", true},
{"**", true},
{"*?", true},
{"?", true},
{".", false},
{"a", false},
{"a?c", true},
}
m1 := NewMatcher()
for _, tc := range testCases1 {
b := m1.isWildPattern(tc.p)
if !assert.Equal(t, b, tc.m) {
println(tc.p, tc.m)
}
}
testCases2 := []wildPatternTestCase{
{"*", true},
{"**", true},
{"*.", true},
{"?", false},
{".", true},
{"a", false},
{"a.c", true},
}
m2 := NewMatcher()
m2.S = '.'
for _, tc := range testCases2 {
b := m2.isWildPattern(tc.p)
if !assert.Equal(t, b, tc.m) {
println(tc.p, tc.m)
}
}
}
func TestMatch(t *testing.T) {
testCases1 := []matchTestCase{
{"", "", true},
{"*", "", true},
{"?", "", false},
{"", "a", false},
{"abc", "abc", true},
{"abc", "ac", false},
{"abc", "abd", false},
{"a?c", "abc", true},
{"a*c", "abc", true},
{"a*c", "abcbc", true},
{"a*c", "abcbd", false},
{"a*b??c", "ajcbjcklbjic", true},
{"a*b??c", "ajcbjcklbjimc", false},
{"a*b*c", "ajkembbcldkcedc", true},
}
m1 := NewMatcher()
for _, tc := range testCases1 {
m, err := m1.Match(tc.p, tc.s)
if !assert.Equal(t, m, tc.m) {
println(tc.p, tc.s, tc.m)
}
assert.Nil(t, err)
}
m2 := NewMatcher()
m2.S = '.'
testCases2 := []matchTestCase{
{"", "", true},
{"*", "", true},
{".", "", false},
{"", "a", false},
{"abc", "abc", true},
{"abc", "ac", false},
{"abc", "abd", false},
{"a.c", "abc", true},
{"a?c", "abc", false},
{"a*c", "abc", true},
{"a*c", "abcbc", true},
{"a*c", "abcbd", false},
{"a*b..c", "ajcbjcklbjic", true},
{"a*b.?c", "ajcbjcklbjic", false},
{"a*b..c", "ajcbjcklbjimc", false},
{"a*b*c", "ajkembbcldkcedc", true},
}
for _, tc := range testCases2 {
m, err := m2.Match(tc.p, tc.s)
if !assert.Equal(t, m, tc.m) {
println(tc.p, tc.s, tc.m)
}
assert.Nil(t, err)
}
}