-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathregex_stdlib_compat_test.go
More file actions
350 lines (299 loc) · 8.96 KB
/
regex_stdlib_compat_test.go
File metadata and controls
350 lines (299 loc) · 8.96 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package coregex
import (
"bytes"
"regexp"
"strings"
"testing"
)
// TestPackageLevelMatch tests the package-level Match function.
func TestPackageLevelMatch(t *testing.T) {
tests := []struct {
pattern string
input []byte
want bool
}{
{`\d+`, []byte("hello 123"), true},
{`\d+`, []byte("hello"), false},
{`^hello`, []byte("hello world"), true},
{`^hello`, []byte("say hello"), false},
}
for _, tt := range tests {
got, err := Match(tt.pattern, tt.input)
if err != nil {
t.Errorf("Match(%q, %q) error: %v", tt.pattern, tt.input, err)
continue
}
if got != tt.want {
t.Errorf("Match(%q, %q) = %v, want %v", tt.pattern, tt.input, got, tt.want)
}
// Compare with stdlib
stdGot, _ := regexp.Match(tt.pattern, tt.input)
if got != stdGot {
t.Errorf("Match(%q, %q) = %v, stdlib = %v", tt.pattern, tt.input, got, stdGot)
}
}
}
// TestPackageLevelMatchString tests the package-level MatchString function.
func TestPackageLevelMatchString(t *testing.T) {
tests := []struct {
pattern string
input string
want bool
}{
{`\d+`, "hello 123", true},
{`\d+`, "hello", false},
{`^hello`, "hello world", true},
{`^hello`, "say hello", false},
}
for _, tt := range tests {
got, err := MatchString(tt.pattern, tt.input)
if err != nil {
t.Errorf("MatchString(%q, %q) error: %v", tt.pattern, tt.input, err)
continue
}
if got != tt.want {
t.Errorf("MatchString(%q, %q) = %v, want %v", tt.pattern, tt.input, got, tt.want)
}
// Compare with stdlib
stdGot, _ := regexp.MatchString(tt.pattern, tt.input)
if got != stdGot {
t.Errorf("MatchString(%q, %q) = %v, stdlib = %v", tt.pattern, tt.input, got, stdGot)
}
}
}
// TestCompilePOSIX tests POSIX compilation with leftmost-longest semantics.
func TestCompilePOSIX(t *testing.T) {
// Test that CompilePOSIX sets longest mode
re, err := CompilePOSIX(`(a|ab)`)
if err != nil {
t.Fatalf("CompilePOSIX error: %v", err)
}
// With POSIX semantics, should match "ab" (longest) not "a" (leftmost-first)
stdRe := regexp.MustCompilePOSIX(`(a|ab)`)
stdMatch := stdRe.FindString("ab")
ourMatch := re.FindString("ab")
if ourMatch != stdMatch {
t.Errorf("CompilePOSIX match = %q, stdlib = %q", ourMatch, stdMatch)
}
}
// TestMustCompilePOSIXPanic tests that MustCompilePOSIX panics on invalid pattern.
func TestMustCompilePOSIXPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("MustCompilePOSIX did not panic on invalid pattern")
}
}()
MustCompilePOSIX(`[invalid`)
}
// TestSubexpIndex tests the SubexpIndex method.
func TestSubexpIndex(t *testing.T) {
tests := []struct {
pattern string
name string
want int
}{
{`(?P<year>\d+)-(?P<month>\d+)`, "year", 1},
{`(?P<year>\d+)-(?P<month>\d+)`, "month", 2},
{`(?P<year>\d+)-(?P<month>\d+)`, "day", -1},
{`(?P<first>\w+)(?P<second>\w+)`, "first", 1},
{`(?P<first>\w+)(?P<second>\w+)`, "second", 2},
{`(\d+)`, "unnamed", -1},
{`(?P<foo>a)`, "", -1}, // empty name returns -1
}
for _, tt := range tests {
re := MustCompile(tt.pattern)
got := re.SubexpIndex(tt.name)
if got != tt.want {
t.Errorf("SubexpIndex(%q, %q) = %d, want %d", tt.pattern, tt.name, got, tt.want)
}
// Compare with stdlib
stdRe := regexp.MustCompile(tt.pattern)
stdGot := stdRe.SubexpIndex(tt.name)
if got != stdGot {
t.Errorf("SubexpIndex(%q, %q) = %d, stdlib = %d", tt.pattern, tt.name, got, stdGot)
}
}
}
// TestLiteralPrefix tests the LiteralPrefix method.
func TestLiteralPrefix(t *testing.T) {
tests := []struct {
pattern string
wantPrefix string
wantComplete bool
}{
{`hello`, "hello", true},
{`hello.*`, "hello", false},
{`Hello, \w+`, "Hello, ", false},
{`abc`, "abc", true},
{`\d+`, "", false},
{`.*`, "", false},
{`^hello`, "", false}, // anchors are not literal
}
for _, tt := range tests {
re := MustCompile(tt.pattern)
gotPrefix, gotComplete := re.LiteralPrefix()
// Compare with stdlib
stdRe := regexp.MustCompile(tt.pattern)
stdPrefix, stdComplete := stdRe.LiteralPrefix()
if gotPrefix != stdPrefix {
t.Errorf("LiteralPrefix(%q) prefix = %q, stdlib = %q", tt.pattern, gotPrefix, stdPrefix)
}
if gotComplete != stdComplete {
t.Errorf("LiteralPrefix(%q) complete = %v, stdlib = %v", tt.pattern, gotComplete, stdComplete)
}
}
}
// TestCopy tests the Copy method.
func TestCopy(t *testing.T) {
re := MustCompile(`\d+`)
copyRe := re.Copy()
if copyRe == nil {
t.Fatal("Copy returned nil")
}
// Both should match the same
input := "test 123"
if re.FindString(input) != copyRe.FindString(input) {
t.Error("Copy produces different results")
}
// Calling Longest on one shouldn't affect the other
copyRe.Longest()
// They may now produce different results for some patterns (tested separately)
}
// TestCopyWithLongest tests that Copy preserves Longest setting.
func TestCopyWithLongest(t *testing.T) {
re := MustCompile(`(a|ab)`)
re.Longest()
copyRe := re.Copy()
input := "ab"
if re.FindString(input) != copyRe.FindString(input) {
t.Error("Copy did not preserve Longest setting")
}
}
// TestMarshalText tests the MarshalText method.
func TestMarshalText(t *testing.T) {
patterns := []string{`\d+`, `hello.*world`, `[a-z]+`, `(?P<name>\w+)`}
for _, pattern := range patterns {
re := MustCompile(pattern)
data, err := re.MarshalText()
if err != nil {
t.Errorf("MarshalText(%q) error: %v", pattern, err)
continue
}
if string(data) != pattern {
t.Errorf("MarshalText(%q) = %q", pattern, string(data))
}
}
}
// TestUnmarshalText tests the UnmarshalText method.
func TestUnmarshalText(t *testing.T) {
patterns := []string{`\d+`, `hello.*world`, `[a-z]+`}
for _, pattern := range patterns {
var re Regex
err := re.UnmarshalText([]byte(pattern))
if err != nil {
t.Errorf("UnmarshalText(%q) error: %v", pattern, err)
continue
}
// Should work like a compiled regex
if re.String() != pattern {
t.Errorf("UnmarshalText(%q) String() = %q", pattern, re.String())
}
// Should be able to match
if pattern == `\d+` && !re.MatchString("test 123") {
t.Errorf("UnmarshalText(%q) failed to match", pattern)
}
}
}
// TestUnmarshalTextInvalid tests UnmarshalText with invalid pattern.
func TestUnmarshalTextInvalid(t *testing.T) {
var re Regex
err := re.UnmarshalText([]byte(`[invalid`))
if err == nil {
t.Error("UnmarshalText with invalid pattern should return error")
}
}
// TestMatchReader tests the MatchReader method.
func TestMatchReader(t *testing.T) {
re := MustCompile(`\d+`)
// Test with matching input
reader := strings.NewReader("hello 123 world")
if !re.MatchReader(reader) {
t.Error("MatchReader failed to match digits")
}
// Test with non-matching input
reader = strings.NewReader("hello world")
if re.MatchReader(reader) {
t.Error("MatchReader incorrectly matched")
}
}
// TestFindReaderIndex tests the FindReaderIndex method.
func TestFindReaderIndex(t *testing.T) {
re := MustCompile(`\d+`)
reader := strings.NewReader("hello 123 world")
idx := re.FindReaderIndex(reader)
if idx == nil {
t.Fatal("FindReaderIndex returned nil")
}
// "123" starts at position 6
if idx[0] != 6 || idx[1] != 9 {
t.Errorf("FindReaderIndex = %v, want [6 9]", idx)
}
}
// TestFindReaderSubmatchIndex tests the FindReaderSubmatchIndex method.
func TestFindReaderSubmatchIndex(t *testing.T) {
re := MustCompile(`(\w+)@(\w+)`)
reader := strings.NewReader("user@domain")
idx := re.FindReaderSubmatchIndex(reader)
if idx == nil {
t.Fatal("FindReaderSubmatchIndex returned nil")
}
// Full match [0:11], group1 [0:4], group2 [5:11]
if len(idx) < 6 {
t.Fatalf("FindReaderSubmatchIndex = %v, expected at least 6 elements", idx)
}
}
// TestPackageLevelMatchReader tests the package-level MatchReader function.
func TestPackageLevelMatchReader(t *testing.T) {
reader := strings.NewReader("hello 123")
matched, err := MatchReader(`\d+`, reader)
if err != nil {
t.Fatalf("MatchReader error: %v", err)
}
if !matched {
t.Error("MatchReader should have matched")
}
// Test with invalid pattern
reader = strings.NewReader("test")
_, err = MatchReader(`[invalid`, reader)
if err == nil {
t.Error("MatchReader with invalid pattern should return error")
}
}
// TestExpandStdlibCompat tests Expand compatibility with stdlib.
func TestExpandStdlibCompat(t *testing.T) {
tests := []struct {
pattern string
input string
template string
}{
{`(\w+)@(\w+)`, "user@domain", "$1 at $2"},
{`(\d+)-(\d+)`, "123-456", "$2-$1"},
{`(a)(b)(c)`, "abc", "$3$2$1"},
{`\d+`, "123", "number: $0"},
}
for _, tt := range tests {
re := MustCompile(tt.pattern)
stdRe := regexp.MustCompile(tt.pattern)
match := re.FindStringSubmatchIndex(tt.input)
stdMatch := stdRe.FindStringSubmatchIndex(tt.input)
if match == nil || stdMatch == nil {
continue
}
got := re.ExpandString(nil, tt.template, tt.input, match)
want := stdRe.ExpandString(nil, tt.template, tt.input, stdMatch)
if !bytes.Equal(got, want) {
t.Errorf("ExpandString(%q, %q, %q) = %q, stdlib = %q",
tt.pattern, tt.template, tt.input, got, want)
}
}
}