-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathregexp_reader_test.go
More file actions
326 lines (310 loc) · 8.25 KB
/
Copy pathregexp_reader_test.go
File metadata and controls
326 lines (310 loc) · 8.25 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package quamina
import (
"cmp"
"slices"
"strings"
"testing"
"unicode"
)
// NormalChar = ( %x00-27 / "," / "-" / %x2F-3E ; '/'-'>'
// / %x40-5A ; '@'-'Z'
// / %x5E-7A ; '^'-'z'
// / %x7E-D7FF ; skip surrogate code points
// / %xE000-10FFFF )
func TestIsNormalChar(t *testing.T) {
normals := []rune{
0, 1, 0x26, 0x27,
0x40, 0x41, 0x59, 0x5a, 0x5c,
0x5e, 0x5f, 0x79, 0x7a,
0x7f, 0xd7fe, 0xd7ff,
0xe000, 0xe001, 0x10fffe, 0x10ffff,
}
for _, normal := range normals {
if !isNormalChar(normal) {
t.Errorf("%x abnormal", normal)
}
}
abormals := []rune{
0x28, 0x2e, 0x3f,
0x3f, 0x5b,
0x5d, 0x7b,
0x7d, 0x7e, 0xd800,
0xdfff,
}
for _, abnormal := range abormals {
if isNormalChar(abnormal) {
t.Errorf("%x normal", abnormal)
}
}
}
func TestSingleCharEscape(t *testing.T) {
// SingleCharEsc = "\" ( %x28-2B ; '('-'+'
// / "-" / "." / "?" / %x5B-5E ; '['-'^'
// / %s"n" / %s"r" / %s"t" / %x7B-7D ; '{'-'}'
//)
sces := []rune{
0x28, 0x29, 0x2a, 0x2b,
'-', '.', '?', 0x5B, 0x5C, 0x5D, 0x5E,
'n', 'r', 't', 0x7B, 0x7C, 0x7D,
'~',
}
for _, sce := range sces {
_, ok := checkSingleCharEscape(sce)
if !ok {
t.Errorf("%x not sce", sce)
}
}
notSces := []rune{
0x27, 0x2C, 0x5A, 0x5F, 'j', 0x7A, 0x7F,
}
for _, notSce := range notSces {
_, ok := checkSingleCharEscape(notSce)
if ok {
t.Errorf("%x is sce", notSce)
}
}
}
func TestReadCCE1(t *testing.T) {
goods := []string{
"a", `~n-~r`, "ab", "a-b",
}
bads := []string{
"a-~P{Lu}", "~P{Lu}-x",
}
for _, good := range goods {
_, err := readRegexp("[" + good + "]")
if err != nil {
t.Errorf("Missed good /[%s]/: %s", good, err.Error())
}
}
for _, bad := range bads {
_, err := readRegexp("[" + bad + "]")
if err == nil {
t.Errorf("Missed bad %s", bad)
}
}
}
func TestRegexpCharacterClassMatchesSupplementaryCharacter(t *testing.T) {
q, err := New()
if err != nil {
t.Fatalf("New: %v", err)
}
pattern := `{"value": [{"regexp": "[\uD835\uDFCE\uD835\uDFCF]"}]}`
if err := q.AddPattern("supplementary", pattern); err != nil {
t.Fatalf("AddPattern: %v", err)
}
matches, err := q.MatchesForEvent([]byte(`{"value": "\uD835\uDFCE"}`))
if err != nil {
t.Fatalf("MatchesForEvent: %v", err)
}
if len(matches) != 1 || matches[0] != "supplementary" {
t.Fatalf("MatchesForEvent = %v, want [supplementary]", matches)
}
}
func TestIsCCcharBoundaries(t *testing.T) {
tests := []struct {
name string
r rune
want bool
}{
{name: "before surrogates", r: 0xd7ff, want: true},
{name: "first surrogate", r: 0xd800, want: false},
{name: "last surrogate", r: 0xdfff, want: false},
{name: "after surrogates", r: 0xe000, want: true},
{name: "previous typo boundary", r: 0x10fff, want: true},
{name: "after previous typo boundary", r: 0x11000, want: true},
{name: "maximum Unicode code point", r: unicode.MaxRune, want: true},
{name: "above maximum Unicode code point", r: unicode.MaxRune + 1, want: false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := isCCchar(test.r); got != test.want {
t.Errorf("isCCchar(U+%04X) = %t, want %t", test.r, got, test.want)
}
})
}
}
func TestInvertRuneRange(t *testing.T) {
ranges := []RuneRange{
{{Lo: 'b', Hi: 'b'}},
{{Lo: 'l', Hi: 'n'}},
{{Lo: 'b', Hi: 'n'}, {Lo: 'p', Hi: 'q'}},
{{Lo: 0, Hi: 'x'}, {Lo: 'z', Hi: runeMax}},
{{Lo: 'd', Hi: 'd'}, {Lo: 'b', Hi: 'b'}, {Lo: 'c', Hi: 'c'}},
}
wanted := []RuneRange{
{{Lo: 0, Hi: 'a'}, {Lo: 'c', Hi: runeMax}},
{{Lo: 0, Hi: 'k'}, {Lo: 'o', Hi: runeMax}},
{{Lo: 0, Hi: 'a'}, {Lo: 'o', Hi: 'o'}, {Lo: 'r', Hi: runeMax}},
{{Lo: 'y', Hi: 'y'}},
{{Lo: 0, Hi: 'a'}, {Lo: 'e', Hi: runeMax}},
}
for i := range ranges {
inverted := InvertRuneRange(ranges[i])
if !equalRR(t, inverted, wanted[i]) {
t.Errorf("BOTCH on %d", i)
}
}
}
func equalRR(t *testing.T, rr1, rr2 RuneRange) bool {
t.Helper()
slices.SortFunc(rr1, func(a, b RunePair) int { return cmp.Compare(a.Lo, b.Lo) })
slices.SortFunc(rr2, func(a, b RunePair) int { return cmp.Compare(a.Lo, b.Lo) })
for i := range rr1 {
if rr1[i].Lo != rr2[i].Lo || rr1[i].Hi != rr2[i].Hi {
return false
}
}
return true
}
func TestRuneRangesFromCCE1(t *testing.T) {
cce1s := []string{
"[ax]", "[a]", "[abc]",
"[c-g]", "[ah-mq]",
"[~n-~r]",
"[-bdg-h]",
}
wanted := []RuneRange{
{{'a', 'a'}, {'x', 'x'}}, {{'a', 'a'}}, {{'a', 'c'}},
{{'c', 'g'}}, {{'a', 'a'}, {'h', 'm'}, {'q', 'q'}},
{{10, 13}},
{{'-', '-'}, {'b', 'b'}, {'d', 'd'}, {'g', 'h'}},
}
for i, cce1 := range cce1s {
parse := newRxParseState([]byte(cce1[1:]))
rr, err := readCCE1s(parse)
if err != nil {
t.Error("RC: " + err.Error())
}
if !runeRangeEqual(t, wanted[i], rr) {
t.Errorf("Failed on %s", cce1)
}
}
}
func TestSimplifyRR(t *testing.T) {
in := []RuneRange{
{{'a', 'b'}, {'e', 'j'}, {'l', 'n'}},
{{'a', 'e'}, {'b', 'e'}, {'d', 'm'}},
{{'a', 'c'}, {'d', 'r'}, {'s', 'x'}},
}
wanteds := []RuneRange{
{{'a', 'b'}, {'e', 'j'}, {'l', 'n'}},
{{'a', 'm'}},
{{'a', 'x'}},
}
for i, rrin := range in {
wanted := wanteds[i]
out := simplifyRuneRange(rrin)
if !runeRangeEqual(t, out, wanted) {
t.Errorf("botch at %d", i)
}
}
}
func runeRangeEqual(t *testing.T, wanted RuneRange, got RuneRange) bool {
t.Helper()
if len(wanted) != len(got) {
return false
}
slices.SortFunc(wanted, func(a, b RunePair) int { return cmp.Compare(a.Lo, b.Lo) })
slices.SortFunc(got, func(a, b RunePair) int { return cmp.Compare(a.Lo, b.Lo) })
for i, w := range wanted {
g := got[i]
if w.Lo != g.Lo || w.Hi != g.Hi {
return false
}
}
return true
}
func TestBasicRegexpFeatureRead(t *testing.T) {
type fw struct {
rx string
wanted []regexpFeature
}
var tfw = []fw{
{rx: "a.b", wanted: []regexpFeature{rxfDot}},
{rx: "ab*", wanted: []regexpFeature{rxfStar}},
{rx: "a+b", wanted: []regexpFeature{rxfPlus}},
{rx: "(ab)+", wanted: []regexpFeature{rxfParenGroup, rxfPlus}},
{rx: "zz?zz", wanted: []regexpFeature{rxfQM}},
{rx: "zzzz{3}", wanted: []regexpFeature{rxfRange}},
{rx: "zzzz{0,3}", wanted: []regexpFeature{rxfRange}},
{rx: "zzzz{3,}", wanted: []regexpFeature{rxfRange}},
{rx: "a~p{Lt}", wanted: []regexpFeature{rxfProperty}},
{rx: "a~P{Me}", wanted: []regexpFeature{rxfNegatedProperty}},
{rx: "a[fox37é]z", wanted: []regexpFeature{rxfClass}},
{rx: "a[-fox37é-]z", wanted: []regexpFeature{rxfClass}},
{rx: "a[fox33-87é]z", wanted: []regexpFeature{rxfClass}},
{rx: "a[^fox37é]z", wanted: []regexpFeature{rxfClass, rxfNegatedClass}},
{rx: "(abc)|(def)", wanted: []regexpFeature{rxfOrBar, rxfParenGroup}},
}
var parse *regexpParse
var err error
for _, w := range tfw {
parse, err = readRegexp(w.rx)
if err != nil {
t.Errorf("botch on %s: %s", w.rx, err.Error())
}
if len(w.wanted) != len(parse.features.found) {
t.Errorf("for %s got %d wanted %d", w.rx, len(parse.features.found), len(w.wanted))
} else {
for _, f := range w.wanted {
_, ok := parse.features.found[f]
if !ok {
t.Errorf("for %s missed feature %s", w.rx, f)
}
}
}
}
}
func TestRegexpErrors(t *testing.T) {
bads := []string{
"~P{L",
"~P{L*}",
string([]byte{'~', 0xfe, 0xff}),
string([]byte{'[', 'a', 'b', 0xfe, 0xff, ']'}),
string([]byte{'[', 'a', '-', 0xff, ']'}),
string([]byte{'[', 'a', '-', '~', 0xff, ']'}),
string([]byte{'a', 0xff}),
string([]byte{'a', '{', 0xff, '}'}),
string([]byte{'a', '{', '2', 0xff, '}'}),
"a{9999999999998,9999999999999}",
"a{2x-3}",
"a{2,",
string([]byte{'a', '{', '2', 0xff}),
"a{2,r}",
string([]byte{'a', '{', '2', ',', 0xff}),
"a{2,4",
string([]byte{'a', '{', '2', ',', '4', 0xff}),
"a{2,4x",
"a{2,9999999999999}",
"abc)",
}
for _, bad := range bads {
_, err := readRegexp(bad)
if err == nil {
t.Error("Took " + bad)
}
}
}
func TestAddRegexpTransition(t *testing.T) {
goods := []string{
"a.", "a?", "a+",
}
template := `{"a":[{"regexp": "FOO"}]}`
cm := newCoreMatcher()
for _, good := range goods {
pat := strings.Replace(template, "FOO", good, 10)
err := cm.addPattern("foo", pat, BuiltForComfort)
if err != nil {
t.Errorf("thinks it found unimplemented feature in /%s/", good)
}
}
}
func TestRegexpReader(t *testing.T) {
pat := `{"a":[{"regexp": "a.b"}]}`
cm := newCoreMatcher()
err := cm.addPattern("x", pat, BuiltForComfort)
if err != nil {
t.Error("ap: " + err.Error())
}
}