-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.lua
More file actions
430 lines (399 loc) · 12.3 KB
/
Copy pathtest.lua
File metadata and controls
430 lines (399 loc) · 12.3 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
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
local jsregexp = require("jsregexp")
local unpack = unpack or table.unpack
local tests = 0
local fails = 0
local successes = 0
-- all other tests count not-compilation as failure
local function test_compile(str, regex, flags, want)
local function fail(...)
print(str, regex, flags, want)
print(...)
fails = fails + 1
end
tests = tests + 1
local r = jsregexp.compile_safe(regex, flags)
if not r and want then
return fail("compilation error")
elseif r and not want then
return fail("should not compile")
end
successes = successes + 1
end
local function test_exec(str, regex, flags, want)
local function fail(...)
print(str, regex, flags, want)
print(...)
fails = fails + 1
end
tests = tests + 1
local r = jsregexp.compile(regex, flags)
if not r then
return fail("compilation error")
end
for _, match_wanted in ipairs(want) do
local match = r:exec(str)
if match and not match_wanted then
return fail(string.format("no match expected, got %s", match))
end
if not match then
return fail(string.format("match expected, wanted %s", match_wanted))
end
if #match_wanted ~= #match then
return fail(string.format("match group count mismatch, wanted: %d, got: %d", #match_wanted, #match))
end
if match_wanted[0] ~= match[0] then
return fail(string.format("global mismatch, wanted: %s, got: %s", match_wanted[0], match[0]))
end
for i, val in ipairs(match_wanted) do
if val ~= match[i] then
return fail(string.format("group %d mismatch, wanted: %s, got: %s", i, match_wanted[i], match[i]))
end
end
if match_wanted.groups and not match.groups then
return fail("expected named groups")
end
if not match_wanted.groups and match.groups then
return fail("expected no named groups")
end
if match_wanted.groups then
for key, val in pairs(match_wanted.groups) do
if val ~= match.groups[key] then
return fail(
string.format("named group %s mismatch, wanted %s, got %s", key, val, match.groups[key])
)
end
end
end
if match_wanted.indices and not match.indices then
return fail("expected indices table")
end
if not match_wanted.indices and match.indices then
return fail("expected no indices table")
end
if match_wanted.indices then
if match_wanted.indices.groups and not match.indices.groups then
return fail("expected indices.groups table")
end
if not match_wanted.indices.groups and match.indices.groups then
return fail("expected no indices.groups table")
end
for i = 0, #match.indices do
local a, b = unpack(match_wanted.indices[i])
local c, d = unpack(match.indices[i])
if a ~= c or b ~= d then
return fail(
string.format("wrong indices for group %d, expected {%d, %d}, got {%d, %d}", i, a, b, c, d)
)
end
end
if match_wanted.indices.groups then
for key, val in pairs(match_wanted.indices.groups) do
if not match_wanted.indices.groups[key] then
return fail(string.format("unexpected key in indices.groups: %s", key))
end
local a, b = unpack(match_wanted.indices.groups[key])
local c, d = unpack(val)
if a ~= c or b ~= d then
return fail(
string.format(
"wrong indices for group %s, expected {%d, %d}, got {%d, %d}",
key,
a,
b,
c,
d
)
)
end
end
end
end
end
local match = r:exec(str)
if r.global and match then
return fail(string.format("surplus match: %s", match))
end
successes = successes + 1
end
local function test_escape(str, want)
local function fail(...)
print(str, want)
print(...)
fails = fails + 1
end
tests = tests + 1
local res = jsregexp.escape(str)
if res ~= want then
return fail(string.format("escape: wanted: %s, got: %s", want, res))
end
successes = successes + 1
end
local function test_test(str, regex, flags, want)
local function fail(...)
print(str, regex, flags, want)
print(...)
fails = fails + 1
end
tests = tests + 1
local r = jsregexp.compile(regex, flags)
if not r then
return fail("compilation error")
end
for _, match_wanted in ipairs(want) do
local match = r:test(str)
if match ~= match_wanted then
return fail(string.format("test mismatch, wanted: %s, got: %s", tostring(match_wanted), tostring(match)))
end
end
local match = r:test(str)
if r.global and match then
return fail(string.format("surplus match: %s", tostring(match)))
end
successes = successes + 1
end
local function test_match(str, regex, flags, want)
local function fail(fmt, ...)
print(str, regex, flags, want)
print(string.format(fmt, ...))
fails = fails + 1
end
tests = tests + 1
local r = jsregexp.compile_safe(regex, flags)
if not r then
return fail("compilation error")
end
local matches = r:match(str)
if want and not matches then
return fail("no matches found")
end
if not want and matches then
return fail("matches found, none wanted")
end
if want and matches then
if #want ~= #matches then
return fail("number of matches mismatch, wanted %d, got %d", #want, #matches)
end
if r.global then
for i, match_want in ipairs(want) do
local match = matches[i]
if match ~= match_want then
return fail("match mismatch, wanted %s, got %s", match_want, match)
end
end
else
-- TODO: compare match object
end
end
successes = successes + 1
end
local function test_match_all_list(str, regex, flags, want)
local function fail(fmt, ...)
print(str, regex, flags, want)
print(string.format(fmt, ...))
fails = fails + 1
end
tests = tests + 1
local r = jsregexp.compile_safe(regex, flags)
if not r then
return fail("compilation error")
end
local matches = r:match_all_list(str)
if want and not matches then
return fail("no matches found")
end
if not want and matches then
return fail("matches found, none wanted")
end
if want and matches then
if #want ~= #matches then
return fail("number of matches mismatch, wanted %d, got %d", #want, #matches)
end
for i, match_want in ipairs(want) do
local match = matches[i][0]
if match ~= match_want then
return fail("match mismatch, wanted %s, got %s", match_want, match)
end
end
end
successes = successes + 1
end
local function test_search(str, regex, flags, want)
local function fail(fmt, ...)
print(str, regex, flags, want)
print(string.format(fmt, ...))
fails = fails + 1
end
tests = tests + 1
local r = jsregexp.compile_safe(regex, flags)
if not r then
return fail("compilation error")
end
local idx = r:search(str)
if idx ~= want then
return fail("search failed: wanted %d, got %d", want, idx)
end
successes = successes + 1
end
local function test_split(str, regex, flags, want)
local function fail(fmt, ...)
print(str, regex, flags, want)
print(string.format(fmt, ...))
fails = fails + 1
end
tests = tests + 1
local r = jsregexp.compile_safe(regex, flags)
if not r then
return fail("compilation error")
end
local split = r:split(str)
local min = math.min(#split, #want)
for i = 1, min do
local w = want[i]
if w ~= split[i] then
return fail("split mismatch, wanted %s, got %s", w, split[i])
end
end
if #split ~= #want then
return fail("number of splits mismatch, wanted %d, got %d", #want, #split)
end
successes = successes + 1
end
local function test_replace(str, regex, flags, replacement, want)
local function fail(fmt, ...)
print(str, regex, flags, want)
print(string.format(fmt, ...))
fails = fails + 1
end
tests = tests + 1
local r = jsregexp.compile_safe(regex, flags)
if not r then
return fail("compilation error")
end
local res = r:replace(str, replacement)
if res ~= want then
return fail("replacement mismatch, wanted %s, got %s", want, res)
end
successes = successes + 1
end
local function test_replace_all(str, regex, flags, replacement, want)
local function fail(fmt, ...)
print(str, regex, flags, want)
print(string.format(fmt, ...))
fails = fails + 1
end
tests = tests + 1
local r = jsregexp.compile_safe(regex, flags)
if not r then
return fail("compilation error")
end
local res = r:replace(str, replacement)
if res ~= want then
return fail("replacement mismatch, wanted %s, got %s", want, res)
end
successes = successes + 1
end
test_compile("dummy", "(.*", "", nil)
test_compile("dummy", "[", "", nil)
-- 0xfd (together with other weird chars) crashes lre_compile if not caught
-- (luajit at least..)
test_compile("dummy", string.char(0xfd, 166, 178, 165, 138, 183), "", nil)
test_exec("wut", "wot", "", {})
test_exec("The quick brown", "\\w+", "g", { { [0] = "The" }, { [0] = "quick" }, { [0] = "brown" } })
test_exec(
"The quick brown fox",
"(\\w+) (\\w+)",
"g",
{ { [0] = "The quick", "The", "quick" }, { [0] = "brown fox", "brown", "fox" } }
)
test_exec("The quick brown fox", "(?<word1>\\w+) (\\w+)", "g", {
{ [0] = "The quick", "The", "quick", groups = { word1 = "The" } },
{ [0] = "brown fox", "brown", "fox", groups = { word1 = "brown" } },
})
test_exec("The Quick Brown Fox Jumps Over The Lazy Dog", "quick\\s(?<color>brown).+?(jumps)", "di", {
{
[0] = "Quick Brown Fox Jumps",
[1] = "Brown",
[2] = "Jumps",
indices = {
[0] = { 5, 25 },
[1] = { 11, 15 },
[2] = { 21, 25 },
groups = {
color = { 11, 15 },
},
},
index = 4,
input = "The Quick Brown Fox Jumps Over The Lazy Dog",
groups = {
color = "Brown",
},
},
})
test_escape("foo.bar", "\\x66oo\\.bar")
test_escape("foo-bar", "\\x66oo\\x2dbar")
test_escape("foo\nbar", "\\x66oo\\nbar")
test_escape("0", "\\x30")
test_escape("(foo)", "\\(foo\\)")
test_escape(" ", "\\x20")
test_escape("\\d \\D (?:)", "\\\\d\\x20\\\\D\\x20\\(\\?\\x3a\\)")
test_test("abc", "a.c", "", { true })
test_test("abc", jsregexp.escape("a.c"), "", { false })
test_test("a.c", jsregexp.escape("a.c"), "", { true })
test_test("The quick brown", "\\w+", "", { true })
test_test("The quick brown", "\\d+", "", { false })
test_test("The quick brown", "\\w+", "g", { true, true, true })
test_test("π", "\\p{Script_Extensions=Greek}", "u", { true })
test_test("π", "[\\p{Script_Extensions=Greek}--π]", "v", { false })
test_test("α", "[\\p{Script_Extensions=Greek}--π]", "v", { true })
-- ⚽ consists of one code point, the other emojis of two
test_test("⚽", "^\\p{Emoji}$", "u", { true })
test_test("👨🏾⚕️", "^\\p{Emoji}$", "u", { false })
test_test("⚽", "^\\p{RGI_Emoji}$", "v", { true })
test_test("👨🏾⚕️", "^\\p{RGI_Emoji}$", "v", { true })
test_test("😄", "^\\p{RGI_Emoji}$", "v", { true })
test_match("The quick brown", "\\d+", "g", nil)
test_match("The quick brown", "\\w+", "g", { "The", "quick", "brown" })
test_match_all_list("The quick brown", "\\d+", "g", {})
test_match_all_list("The quick brown", "\\w+", "g", { "The", "quick", "brown" })
test_match_all_list("𝄞𝄞𐐷𝄞𝄞", "𝄞*", "g", { "𝄞𝄞", "", "𝄞𝄞", "" })
test_search("The quick brown", "nothing", "g", -1)
test_search("The quick brown", "quick", "g", 5)
test_split("abc", "x", "g", { "abc" })
test_split("", "a?", "g", {})
test_split("", "a", "g", { "" })
test_split("1-2-3", "-", "g", { "1", "2", "3" })
test_split("1-2-", "-", "g", { "1", "2", "" })
test_split("-2-3", "-", "g", { "", "2", "3" })
test_split("--", "-", "g", { "", "", "" })
test_split("Hello 1 word. Sentence number 2.", "(\\d)", "g", { "Hello ", "1", " word. Sentence number ", "2", "." })
test_replace("a b", "\\w+", "", "_", "_ b")
test_replace("a b", "\\w+", "", function()
return "_"
end, "_ b")
test_replace("12 34", "\\d+", "", "_", "_ 34")
test_replace("123 456", "\\d+", "", "_", "_ 456")
test_replace("a1b2c", "X", "g", "_", "a1b2c")
test_replace("a1b2c", "\\d", "", "_", "a_b2c")
test_replace("a1b2c", "\\d", "g", "_", "a_b_c")
test_replace("a1b2c", "(\\d)(.)", "g", "$1", "a12")
test_replace("a1b2c", "(\\d)(.)", "g", "$2", "abc")
test_replace_all("a b", "\\w+", "g", "_", "_ _")
test_replace_all("a b", "\\w+", "g", function()
return "_"
end, "_ _")
test_replace_all("12 34", "\\d+", "g", "_", "_ _")
test_replace_all("123 456", "\\d+", "g", "_", "_ _")
test_replace_all("a1b2c", "X", "g", "_", "a1b2c")
test_replace_all("a1b2c", "\\d", "g", "_", "a_b_c")
test_replace_all("a1b2c", "\\d", "g", "_", "a_b_c")
test_replace_all("a1b2c", "(\\d)(.)", "g", "$1", "a12")
test_replace_all("a1b2c", "(\\d)(.)", "g", "$2", "abc")
local bold_green = "\27[1;32m"
local bold_red = "\27[1;31m"
local normal = "\27[0m"
local color = fails == 0 and bold_green or bold_red
print(string.format("%s%d tests run, %d successes, %d failed%s", color, tests, successes, fails, normal))
if fails > 0 then
collectgarbage()
os.exit(1)
end