Repository navigation
Expand file tree
/
Copy pathregexp_codegen_test.go
More file actions
323 lines (299 loc) · 8.24 KB
/
Copy pathregexp_codegen_test.go
File metadata and controls
323 lines (299 loc) · 8.24 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
package regexp2
import (
"slices"
"testing"
)
type emptyEngine struct {
findFirstChar func(r *Runner) bool
execute func(r *Runner) error
}
func (t *emptyEngine) FindFirstChar(r *Runner) bool {
return t.findFirstChar(r)
}
func (t *emptyEngine) Execute(r *Runner) error {
return t.execute(r)
}
func TestRegisterEngine_CacheHit(t *testing.T) {
didFindFirst := false
didExecute := false
engine := emptyEngine{
findFirstChar: func(r *Runner) bool {
didFindFirst = true
return true
},
execute: func(r *Runner) error {
didExecute = true
return nil
},
}
RegisterEngine("This is a regexp", RuntimeEngineData{
CapSize: 1,
FindFirstChar: engine.FindFirstChar,
Execute: engine.Execute,
}, RE2)
re := MustCompile("This is a regexp", RE2)
val, err := re.MatchString("This is a regexp")
if !didFindFirst {
t.Fatal("Expected find first char to be called, but it wasn't")
}
if !didExecute {
t.Fatal("Expected execute to be called, but it wasn't")
}
if err != nil {
t.Fatal("Unexpected error", err)
}
if val {
t.Fatal("Expected no match")
}
}
func TestRegisterEngine_QuickExecute(t *testing.T) {
const pattern = "quick-execute"
fullCalls := 0
quickCalls := 0
RegisterEngine(pattern, RuntimeEngineData{
CapSize: 1,
FindFirstChar: func(r *Runner) bool {
return r.Runtextpos == 0
},
Execute: func(r *Runner) error {
fullCalls++
r.Capture(0, 0, 1)
return nil
},
ExecuteQuick: func(r *Runner) error {
quickCalls++
r.Capture(0, 0, 1)
return nil
},
})
re := MustCompile(pattern)
if matched, err := re.MatchString("x"); err != nil || !matched {
t.Fatalf("MatchString = %v, %v; want true, nil", matched, err)
}
if fullCalls != 0 || quickCalls != 1 {
t.Fatalf("after MatchString: full calls = %d, quick calls = %d; want 0, 1", fullCalls, quickCalls)
}
if match, err := re.FindStringMatch("x"); err != nil || match == nil {
t.Fatalf("FindStringMatch = %v, %v; want match, nil", match, err)
}
if fullCalls != 1 || quickCalls != 1 {
t.Fatalf("after FindStringMatch: full calls = %d, quick calls = %d; want 1, 1", fullCalls, quickCalls)
}
}
func TestRegisterEngine_FindNextMatch(t *testing.T) {
const pattern = "find-next-match"
RegisterEngine(pattern, RuntimeEngineData{
CapSize: 1,
FindFirstChar: func(r *Runner) bool {
return r.Runtextpos < len(r.Runtext)
},
Execute: func(r *Runner) error {
r.Capture(0, r.Runtextpos, r.Runtextpos+1)
r.Runtextpos++
return nil
},
})
re := MustCompile(pattern)
m, err := re.FindStringMatch("ab")
if err != nil || m == nil {
t.Fatalf("FindStringMatch = %v, %v; want match, nil", m, err)
}
if m, err = re.FindNextMatch(m); err != nil || m == nil || m.String() != "b" {
t.Fatalf("FindNextMatch = %v, %v; want b, nil", m, err)
}
}
func TestRegisterEngine_StringOperationsRetainCaptures(t *testing.T) {
const pattern = "string-operations-retain-captures"
RegisterEngine(pattern, RuntimeEngineData{
CapSize: 2,
FindFirstChar: func(r *Runner) bool {
return r.Runtextpos < r.Runtextend
},
Execute: func(r *Runner) error {
start := r.Runtextpos
r.Runtextpos++
r.Capture(1, start, r.Runtextpos)
r.Capture(0, start, r.Runtextpos)
return nil
},
ExecuteQuick: func(r *Runner) error {
t.Fatal("capture-producing APIs must use the full engine")
return nil
},
})
re := MustCompile(pattern)
m, err := re.FindStringMatch("éa")
if err != nil || m == nil || m.GroupByNumber(1).String() != "é" {
t.Fatalf("FindStringMatch = %v, %v; want captured é", m, err)
}
if next, err := re.FindNextMatch(m); err != nil || next == nil || next.GroupByNumber(1).String() != "a" {
t.Fatalf("FindNextMatch = %v, %v; want captured a", next, err)
}
if got, err := re.Replace("éa", "<$1>", -1, -1); err != nil || got != "<é><a>" {
t.Fatalf("Replace = %q, %v; want <é><a>", got, err)
}
if got, err := re.Split("éa", -1); err != nil || !slices.Equal(got, []string{"", "é", "", "a", ""}) {
t.Fatalf("Split = %q, %v; want captured delimiters", got, err)
}
}
func TestRegisterEngine_CacheKeyIncludesBacktrackingStackLimit(t *testing.T) {
const pattern = "stack-limit-cache-key"
register := func(limit, marker int) {
RegisterEngine(pattern, RuntimeEngineData{
CapSize: 1,
FindFirstChar: func(r *Runner) bool {
return r.Runtextpos == 0
},
Execute: func(r *Runner) error {
r.Runtextpos = marker
r.Capture(0, 0, marker)
return nil
},
}, OptionMaxBacktrackingStackSize(limit))
}
register(10, 1)
register(20, 2)
for _, tt := range []struct {
limit, wantLength int
}{
{limit: 10, wantLength: 1},
{limit: 20, wantLength: 2},
} {
re := MustCompile(pattern, OptionMaxBacktrackingStackSize(tt.limit))
match, err := re.FindStringMatch("xx")
if err != nil || match == nil {
t.Fatalf("limit %d: FindStringMatch = %v, %v; want match, nil", tt.limit, match, err)
}
if match.RuneLength != tt.wantLength {
t.Fatalf("limit %d: match length = %d, want %d", tt.limit, match.RuneLength, tt.wantLength)
}
}
}
func TestRegisterEngine_CacheMiss(t *testing.T) {
didFindFirst := false
didExecute := false
engine := emptyEngine{
findFirstChar: func(r *Runner) bool {
didFindFirst = true
return true
},
execute: func(r *Runner) error {
didExecute = true
return nil
},
}
RegisterEngine("This is a regexp", RuntimeEngineData{
CapSize: 1,
FindFirstChar: engine.FindFirstChar,
Execute: engine.Execute,
}, IgnoreCase)
// only difference in our cache is our options
re := MustCompile("This is a regexp")
val, err := re.MatchString("This is a regexp")
if didFindFirst {
t.Fatal("Expected find first to not be called, but it was")
}
if didExecute {
t.Fatal("Expected execute to not be called, but it was")
}
if err != nil {
t.Fatal("Unexpected error", err)
}
if !val {
t.Fatal("Expected match")
}
}
func TestRegisterEngine_CacheMissMaintainCaptureOrder(t *testing.T) {
didFindFirst := false
didExecute := false
engine := emptyEngine{
findFirstChar: func(r *Runner) bool {
didFindFirst = true
return true
},
execute: func(r *Runner) error {
didExecute = true
return nil
},
}
RegisterEngine("(?<first>This) (is)", RuntimeEngineData{
CapSize: 3,
FindFirstChar: engine.FindFirstChar,
Execute: engine.Execute,
})
re := MustCompile("(?<first>This) (is)", OptionMaintainCaptureOrder())
val, err := re.MatchString("This is")
if didFindFirst {
t.Fatal("Expected find first to not be called, but it was")
}
if didExecute {
t.Fatal("Expected execute to not be called, but it was")
}
if err != nil {
t.Fatal("Unexpected error", err)
}
if !val {
t.Fatal("Expected match")
}
}
func TestRegisterEngine_CacheHitMaintainCaptureOrder(t *testing.T) {
didFindFirst := false
didExecute := false
engine := emptyEngine{
findFirstChar: func(r *Runner) bool {
didFindFirst = true
return true
},
execute: func(r *Runner) error {
didExecute = true
return nil
},
}
RegisterEngine("(?<first>This) (is)", RuntimeEngineData{
CapSize: 3,
FindFirstChar: engine.FindFirstChar,
Execute: engine.Execute,
}, OptionMaintainCaptureOrder())
re := MustCompile("(?<first>This) (is)", OptionMaintainCaptureOrder())
val, err := re.MatchString("This is")
if !didFindFirst {
t.Fatal("Expected find first char to be called, but it wasn't")
}
if !didExecute {
t.Fatal("Expected execute to be called, but it wasn't")
}
if err != nil {
t.Fatal("Unexpected error", err)
}
if val {
t.Fatal("Expected no match")
}
}
func TestStringPrefixFilterSkipsDecodeOnMiss(t *testing.T) {
const pattern = `string-prefix-filter-miss-only`
RegisterEngine(pattern, RuntimeEngineData{
CapSize: 1,
StringPrefixFilter: func(input string, startAt int) (candidateByteIndex int, ok bool) {
if input != "missing" || startAt != 0 {
t.Fatalf("filter got input=%q startAt=%d", input, startAt)
}
return 0, false
},
FindFirstChar: func(r *Runner) bool {
t.Fatal("FindFirstChar should not run after a string prefix filter miss")
return false
},
Execute: func(r *Runner) error {
t.Fatal("Execute should not run after a string prefix filter miss")
return nil
},
})
re := MustCompile(pattern)
matched, err := re.MatchString("missing")
if err != nil {
t.Fatalf("MatchString failed: %v", err)
}
if matched {
t.Fatal("unexpected match")
}
}