-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrassemble_test.go
446 lines (444 loc) · 12 KB
/
rassemble_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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package rassemble
import "testing"
func TestJoin(t *testing.T) {
testCases := []struct {
name string
patterns []string
expected string
}{
{
name: "empty",
patterns: []string{},
expected: "",
},
{
name: "empty literal",
patterns: []string{""},
expected: "(?:)",
},
{
name: "empty literals",
patterns: []string{"", ""},
expected: "(?:)",
},
{
name: "single literal",
patterns: []string{"abc"},
expected: "abc",
},
{
name: "single literal with flag",
patterns: []string{"(?i:abc)"},
expected: "(?i:ABC)",
},
{
name: "single literal with multiple flags",
patterns: []string{"(?ims:^a.b.c$)"},
expected: "(?ims:^A.B.C$)",
},
{
name: "multiple literals",
patterns: []string{"abc", "def", "ghi"},
expected: "abc|def|ghi",
},
{
name: "multiple literals with same flag",
patterns: []string{"(?i:abc)", "(?i:def)", "(?i:ghi)"},
expected: "(?i:ABC|DEF|GHI)",
},
{
name: "multiple literals with different flags",
patterns: []string{"(?i:abc)", "(?m:d.$)", "(?s:^g.i)"},
expected: "(?-s:(?i:ABC)|(?m:d.$)|(?ms:^g.i))",
},
{
name: "multiple characters with different flags",
patterns: []string{"a", "b", "(?i:c)?", "d"},
expected: "[Ca-d]?",
},
{
name: "same literals",
patterns: []string{"abc", "def", "abc", "def"},
expected: "abc|def",
},
{
name: "same prefixes with different length",
patterns: []string{"abc", "ab", "ad", "a"},
expected: "a(?:bc?|d)?",
},
{
name: "same prefixes with different length",
patterns: []string{"abcd", "abcf", "abc", "abce", "abcgh", "abdc"},
expected: "ab(?:c(?:[d-f]|gh)?|dc)",
},
{
name: "same prefixes with same length",
patterns: []string{"abcde", "abcfg", "abcgh"},
expected: "abc(?:de|fg|gh)",
},
{
name: "same prefixes in increasing length order",
patterns: []string{"a", "ab", "abc", "abcd"},
expected: "a(?:b(?:cd?)?)?",
},
{
name: "same prefixes in decreasing length order",
patterns: []string{"abcd", "abc", "ab", "a"},
expected: "a(?:b(?:cd?)?)?",
},
{
name: "same prefixes with same flag",
patterns: []string{"(?i:abc)", "(?i:ab)", "(?i:ad)", "(?i:a)"},
expected: "(?i:A(?:BC?|D)?)",
},
{
name: "same prefixes with various flags",
patterns: []string{"(?i:abc)", "(?:a.*b$)", "(?im:ad$|ae)", "(?sm:a.$)", "(?U:a.*c$)"},
expected: "(?-s:(?im:A(?:BC|D$|E))|(?m:a(?:.*b|(?s:.)|.*?c)$))",
},
{
name: "same prefix and suffix",
patterns: []string{"abcdefg", "abcfg", "abefg", "befg", "beefg"},
expected: "(?:ab(?:c(?:de)?|e)|bee?)fg",
},
{
name: "same prefix and suffix with double quests",
patterns: []string{"abcd", "abd", "acd", "ad"},
expected: "ab?c?d",
},
{
name: "same prefix and suffix with triple quests",
patterns: []string{"abcde", "acde", "abde", "abce", "abe", "ace", "ade", "ae"},
expected: "ab?c?d?e",
},
{
name: "multiple prefix groups",
patterns: []string{"abc", "ab", "abcd", "a", "bcd", "bcdef", "cdef", "cdeh"},
expected: "a(?:b(?:cd?)?)?|bcd(?:ef)?|cde[fh]",
},
{
name: "merge literal to quest",
patterns: []string{"abc(?:def)?", "abc"},
expected: "abc(?:def)?",
},
{
name: "merge literal to star",
patterns: []string{"abc(?:def)*", "abcdef", "abc"},
expected: "abc(?:def)*",
},
{
name: "merge literal to plus",
patterns: []string{"abc(?:def)+", "abcdef", "abc"},
expected: "abc(?:def)*",
},
{
name: "merge literal to alternate",
patterns: []string{"abc(?:de|f)", "abc"},
expected: "abc(?:de|f)?",
},
{
name: "merge literal to alternate with plus",
patterns: []string{"abc(?:de|[f-h]+)", "abc", "abc"},
expected: "abc(?:de|[f-h]*)",
},
{
name: "merge literal to concat",
patterns: []string{"abca*b*", "abc"},
expected: "abc(?:a*b*)?",
},
{
name: "merge literal to concat",
patterns: []string{"abca*b*", "abcde"},
expected: "abc(?:a*b*|de)",
},
{
name: "merge literal to alternate in quest",
patterns: []string{"abc(?:de|fh)?", "abcff", "abcf", "abchh"},
expected: "abc(?:de|f[fh]?|hh)?",
},
{
name: "merge literal to quest with suffix",
patterns: []string{"abc(?:def)?ghi", "abcd"},
expected: "abc(?:(?:def)?ghi|d)",
},
{
name: "merge literal to alternate with same prefix",
patterns: []string{"abcfd|def", "abcdef", "abcfe"},
expected: "abc(?:f[de]|def)|def",
},
{
name: "merge literal to alternate with different prefix",
patterns: []string{"abc|def", "ghi"},
expected: "abc|def|ghi",
},
{
name: "character class",
patterns: []string{"a", "1", "z", "2"},
expected: "[12az]",
},
{
name: "character class with prefix",
patterns: []string{"aa", "ab"},
expected: "a[ab]",
},
{
name: "character class in prefix",
patterns: []string{"abcde", "abc", "bbcde", "bbc", "cbcde", "cbc"},
expected: "[a-c]bc(?:de)?",
},
{
name: "add character class to a character",
patterns: []string{"d?", "[a-c]", "e"},
expected: "[a-e]?",
},
{
name: "add character class to a character class",
patterns: []string{"[a-c]", "[e-f]", "d"},
expected: "[a-f]",
},
{
name: "add complex character class to a complex character class",
patterns: []string{"[i-kea-c]", "[f-hd]"},
expected: "[a-k]",
},
{
name: "add character to character class negation",
patterns: []string{"[^0-9]", "3", "5"},
expected: "[^0-246-9]",
},
{
name: "add quest of character to character class negation",
patterns: []string{"[^0-9]", "3?", "5"},
expected: "[^0-246-9]?",
},
{
name: "add character to character class negation to match anything",
patterns: []string{"[^0]", "0"},
expected: "(?s:.)",
},
{
name: "merge literal prefix rather than character class",
patterns: []string{"a", "c", "e", "ab", "cd", "ef"},
expected: "ab?|cd?|ef?",
},
{
name: "add character class to a character class negation",
patterns: []string{"[a-d]", "[^c-g]", "f"},
expected: "[^eg]",
},
{
name: "successive character class",
patterns: []string{"aa", "ab", "ac"},
expected: "a[a-c]",
},
{
name: "successive character class in random order",
patterns: []string{"ac", "aa", "ae", "ab", "ad"},
expected: "a[a-e]",
},
{
name: "add case insensitive literal to a literal",
patterns: []string{"a", "(?i:b)", "c"},
expected: "[Ba-c]",
},
{
name: "add case insensitive literal to a character class",
patterns: []string{"(?i:a)", "[b-e]", "(?i:f)"},
expected: "[AFa-f]",
},
{
name: "add case insensitive character class to a character class",
patterns: []string{"[a-c]", "(?i:[d-f])", "[g-i]"},
expected: "[D-Fa-i]",
},
{
name: "add case insensitive literal to a quest of character class",
patterns: []string{"(?i:a)", "[b-e]?", "(?i:f)"},
expected: "[AFa-f]?",
},
{
name: "numbers",
patterns: []string{"1", "9", "2", "6", "3"},
expected: "[1-369]",
},
{
name: "numbers 0 to 5",
patterns: []string{"0", "4", "3", "5", "1", "2"},
expected: "[0-5]",
},
{
name: "numbers 0 to 10",
patterns: []string{"1", "9", "2", "6", "3", "7", "10", "8", "0", "5", "4"},
expected: "10?|[02-9]",
},
{
name: "numbers with prefix",
patterns: []string{"a2", "a1", "a0", "a8", "a3", "a5", "a6", "a4", "a7", "a11", "a2", "a9", "a0", "a10"},
expected: "a(?:[02-9]|1[01]?)",
},
{
name: "add empty literal to quest",
patterns: []string{"abc", "", ""},
expected: "(?:abc)?",
},
{
name: "add empty literal to plus and star",
patterns: []string{"(?:abc)+", "", ""},
expected: "(?:abc)*",
},
{
name: "add empty literal to character class",
patterns: []string{"[135]", "", "7"},
expected: "[1357]?",
},
{
name: "add empty literal to alternate with quest",
patterns: []string{"abc", "b", "", ""},
expected: "abc|b?",
},
{
name: "add empty literal to alternate with plus and star",
patterns: []string{"abc", "b+", "", ""},
expected: "abc|b*",
},
{
name: "add literal to empty literal",
patterns: []string{"", "abc", ""},
expected: "(?:abc)?",
},
{
name: "add literal with a flag to empty literal",
patterns: []string{"", "(?i:abc)", ""},
expected: "(?i:(?:ABC)?)",
},
{
name: "add quest to empty literal",
patterns: []string{"", "(?:abc)?"},
expected: "(?:abc)?",
},
{
name: "add quest to quest",
patterns: []string{"(?:abc)?", "(?:abc)?"},
expected: "(?:abc)?",
},
{
name: "add star to empty literal",
patterns: []string{"", "(?:abc)*"},
expected: "(?:abc)*",
},
{
name: "add star and plus to character class",
patterns: []string{"a[a-c]c", "a[a-c]*c", "a[a-c]+c", "a[a-d]+c"},
expected: "a(?:[a-c]*|[a-d]+)c",
},
{
name: "add quest and plus to character class",
patterns: []string{"a[a-c]c", "aac", "a[a-c]?c", "a[a-c]+c", "a[a-d]c"},
expected: "a(?:[a-c]*|[a-d])c",
},
{
name: "add quest of character class to literal",
patterns: []string{"abc", "a[a-c]?c"},
expected: "a[a-c]?c",
},
{
name: "add quest of character class to character class",
patterns: []string{"abc", "adc", "a[a-c]?c"},
expected: "a[a-d]?c",
},
{
name: "add plus to empty literal",
patterns: []string{"", "(?:abc)+"},
expected: "(?:abc)*",
},
{
name: "add plus to literal",
patterns: []string{"abc", "(?:ab)+", "(?:abc)+"},
expected: "(?:abc)+|(?:ab)+",
},
{
name: "add plus to quest",
patterns: []string{"(?:abc)?", "(?:ab)+", "(?:abc)+"},
expected: "(?:abc)*|(?:ab)+",
},
{
name: "add character class to empty literal",
patterns: []string{"", "[a-c]"},
expected: "[a-c]?",
},
{
name: "add alternate",
patterns: []string{"a", "[a-c]|bb", "cc|d"},
expected: "[a-d]|bb|cc",
},
{
name: "merge suffix",
patterns: []string{"abcde", "cde", "bde"},
expected: "(?:(?:ab)?c|b)de",
},
{
name: "merge suffix in increasing length order",
patterns: []string{"e", "de", "cde", "bcde", "abcde"},
expected: "(?:(?:(?:a?b)?c)?d)?e",
},
{
name: "merge suffix in decreasing length order",
patterns: []string{"abcde", "bcde", "cde", "de", "e"},
expected: "(?:(?:(?:a?b)?c)?d)?e",
},
{
name: "regexps matching head",
patterns: []string{"a?", "a?b*c+"},
expected: "a?(?:b*c+)?",
},
{
name: "regexps with same prefix",
patterns: []string{"a?b+cd", "a?b+c*", "a?b*c+"},
expected: "a?(?:b+(?:cd|c*)|b*c+)",
},
{
name: "regexps with same prefixes",
patterns: []string{"a?b+c*", "a?b+c*d*", "a?b+", "a?"},
expected: "a?(?:b+(?:c*d*)?)?",
},
{
name: "regexps with same prefixes and flags",
patterns: []string{"(?i:a*b+c*)", "(?i:a*b+(?-i:c*d*))", "(?i:a*)(?i:b+)", "a*", "A*"},
expected: "A*(?:(?i:B+)(?:(?i:C*)|c*d*))?|a*", // bug in regexp/syntax (golang/go#59007)
},
{
name: "regexps with same prefixes and different flags",
patterns: []string{"a?(?i:b+c*)", "(?i:a?)(?i:b+c*d*)", "(?i:a?)b+", "a?"},
expected: "a?(?i:(?:B+C*)?)|(?i:A?)(?:(?i:B+C*D*)|b+)",
},
{
name: "regexps with same literal prefix",
patterns: []string{"abcd*e*", "abcde*f*", "abefg?", "ab"},
expected: "ab(?:c(?:d*e*|de*f*)|efg?)?",
},
{
name: "regexps with same suffix",
patterns: []string{"ab*c", "c+", "bab?c", "a+c", "cbc+", "dbc+", "ab*c", "c*d+", "d+"},
expected: "(?:ab*|bab?|a+)c|(?:[cd]b)?c+|c*d+",
},
{
name: "regexps with same literal suffix",
patterns: []string{"ab*cde", "bcde", "a*de", "cde"},
expected: "(?:(?:ab*|b)?c|a*)de",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := Join(tc.patterns)
if err != nil {
t.Fatalf("got an error: %s", err)
}
if got != tc.expected {
t.Errorf("expected: %s, got: %s", tc.expected, got)
}
})
}
if _, err := Join([]string{"*"}); err == nil {
t.Fatalf("expected an error")
}
}