-
Notifications
You must be signed in to change notification settings - Fork 3
/
match_test.go
277 lines (261 loc) · 5.6 KB
/
match_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package yarex_test
//go:generate cmd/yarexgen/yarexgen match_test.go
import (
"regexp"
"testing"
"github.com/Maki-Daisuke/go-yarex"
)
func testMatchStrings(t *testing.T, restr string, tests []string) {
ast, err := yarex.Parse(restr)
if err != nil {
t.Fatalf("want nil, but got %s", err)
}
stdRe := regexp.MustCompile(restr)
ast = yarex.OptimizeAst(ast)
opRe := yarex.MustCompileOp(restr)
compRe := yarex.MustCompile(restr)
if !yarex.IsCompiledMatcher(compRe) {
t.Errorf("%v should be Compiled matcher, but isn't", compRe)
}
for _, str := range tests {
match := stdRe.MatchString(str)
if yarex.AstMatch(ast, str) != match {
if match {
t.Errorf("(Interp) %v should match against %q, but didn't", ast, str)
} else {
t.Errorf("(Interp) %v shouldn't match against %q, but did", ast, str)
}
}
if opRe.MatchString(str) != match {
if match {
t.Errorf("(OpTree) %v should match against %q, but didn't", opRe, str)
} else {
t.Errorf("(OpTree) %v shouldn't match against %q, but did", opRe, str)
}
}
if compRe.MatchString(str) != match {
if match {
t.Errorf("(Compiled) %v should match against %q, but didn't", compRe, str)
} else {
t.Errorf("(Compiled) %v shouldn't match against %q, but did", compRe, str)
}
}
}
}
func TestMatchFooBar(t *testing.T) {
re := "foo bar" //yarexgen
testMatchStrings(t, re, []string{
"foo bar",
"foo bar",
"hogefoo barfuga",
"foo barf",
"Afoo bar",
"foo ba",
})
}
func TestMatchFooOrBar(t *testing.T) {
re := "foo|bar" //yarexgen
testMatchStrings(t, re, []string{
"foo bar",
"hogefoo barfuga",
"foo baz",
"bar f",
"foba",
"",
})
}
func TestMatchBacktracking(t *testing.T) {
re := "(?:foo|fo)oh" //yarexgen
testMatchStrings(t, re, []string{
"fooh",
"foooh",
"foh",
"fooooooooooh",
"fooooooooofoooh",
"",
})
}
func TestMatchZeroOrMore(t *testing.T) {
re := "fo*oh" //yarexgen
testMatchStrings(t, re, []string{
"fooh",
"foh",
"fh",
"fooooooooooh",
"fooooooooofoooh",
"",
"fo",
"oh",
})
}
func TestMatchOneOrMore(t *testing.T) {
re := "fo+oh" //yarexgen
testMatchStrings(t, re, []string{
"fooh",
"foh",
"fh",
"fooooooooooh",
"fooooooooofoooh",
"",
"fo",
"oh",
})
}
func TestMatchQuantifier(t *testing.T) {
re := "fo{2,5}oh" //yarexgen
testMatchStrings(t, re, []string{
"fooh",
"foh",
"fh",
"fooooooooooh",
"fooooooooofoooh",
"",
"fo",
"oh",
})
}
func TestMatchOpt(t *testing.T) {
re := "fo?oh" //yarexgen
testMatchStrings(t, re, []string{
"fooh",
"foh",
"fh",
"fooooooooooh",
"fooooooooofooh",
"",
"fo",
"oh",
})
re = "fo*oh?" //yarexgen
testMatchStrings(t, re, []string{
"ABfooh",
"foo",
"fh",
"foooohoooooo",
"foooooooooooCD",
"",
"fo",
"oh",
})
}
func TestMatchWildcard(t *testing.T) {
re := "." //yarexgen
testMatchStrings(t, re, []string{
"aiueo",
"\n",
"",
" ",
"\b",
})
re = ".+x" //yarexgen
testMatchStrings(t, re, []string{
"",
"x",
"xx",
"aaaaax",
"\nx",
"xx\nx",
"xxxxxa",
})
}
func TestMatchBegin(t *testing.T) {
re := "^foo bar" //yarexgen
testMatchStrings(t, re, []string{
"foo bar",
"foo bar",
"hogefoo barfuga",
"foo barf",
"Afoo bar",
"foo ba",
"\nfoo bar",
})
re = "(^|A)*foo bar" //yarexgen
testMatchStrings(t, re, []string{
"foo bar",
"foo bar",
"hogefoo barfuga",
"foo barf",
"Afoo bar",
"AAfoo bar",
"AAAAfoo bar",
"AABAAfoo bar",
})
}
func TestMatchBackRef(t *testing.T) {
// Here, we cannot use testMatchStrings, because Go's regexp does not
// support back-reference.
tests := []struct {
str string
result bool
}{
{"hogehogefuga", true},
{"AAAhogehogefugaBBB", true},
{"hogefuga", false},
{"hoge", false},
{"fuga", false},
}
pattern := `(hoge)\1fuga` //yarexgen
ast, err := yarex.Parse(pattern)
if err != nil {
t.Fatalf("want nil, but got %s", err)
}
ast = yarex.OptimizeAst(ast)
opRe := yarex.MustCompileOp(pattern)
compRe := yarex.MustCompileOp(pattern)
for _, test := range tests {
if yarex.AstMatch(ast, test.str) != test.result {
if test.result {
t.Errorf("(Interp) %v should match against %q, but didn't", ast, test.str)
} else {
t.Errorf("(Interp) %v shouldn't match against %q, but did", ast, test.str)
}
}
if opRe.MatchString(test.str) != test.result {
if test.result {
t.Errorf("(OpTree) %v should match against %q, but didn't", opRe, test.str)
} else {
t.Errorf("(OpTree) %v shouldn't match against %q, but did", opRe, test.str)
}
}
if compRe.MatchString(test.str) != test.result {
if test.result {
t.Errorf("(Compiled) %v should match against %q, but didn't", compRe, test.str)
} else {
t.Errorf("(Compiled) %v shouldn't match against %q, but did", compRe, test.str)
}
}
}
}
func TestMatchClass(t *testing.T) {
re := "[0aB]" //yarexgen
testMatchStrings(t, re, []string{
"foo", // false
"foo bar", // true
"FOO BAR", // true
"AAAAAA", // false
"012345", // true
"\000hoge", // false
"\000hage", // true
})
re = "[A-Z0-9][a-z]" //yarexgen
testMatchStrings(t, re, []string{
"absksdjhasd",
"alsdAAA",
"asl;k3as7djj",
"Aiiiiiiii9",
"foo BAR",
"FOO bar",
"FOObar",
"fooBARbaz",
})
}
func TestSipAddress(t *testing.T) {
re := `^["]{0,1}([^"]*)["]{0,1}[ ]*<(sip|tel|sips):(([^@]*)@){0,1}([^>^:]*|\[[a-fA-F0-9:]*\]):{0,1}([0-9]*){0,1}>(;.*){0,1}$` //yarexgen
testMatchStrings(t, re, []string{
"\"display_name\"<sip:[email protected]:5060>;user=phone;hogehoge",
"<sip:[email protected]>",
"\"display_name\"<sip:[email protected]>",
"<sip:whois.this>;user=phone",
"\"0333334444\"<sip:[2001:30:fe::4:123]>;user=phone",
})
}