@@ -39,7 +39,7 @@ local function test_exec(str, regex, flags, want)
39
39
if match and not match_wanted then
40
40
return fail (string.format (" no match expected, got %s" , match ))
41
41
end
42
- if not match and match_wanted then
42
+ if not match then
43
43
return fail (string.format (" match expected, wanted %s" , match_wanted ))
44
44
end
45
45
if # match_wanted ~= # match then
@@ -167,18 +167,21 @@ local function test_match(str, regex, flags, want)
167
167
if # want ~= # matches then
168
168
return fail (" number of matches mismatch, wanted %d, got %d" , # want , # matches )
169
169
end
170
- for i , match_want in ipairs (want ) do
171
- local match = matches [i ][0 ]
172
- if match ~= match_want then
173
- return fail (" match mismatch, wanted %s, got %s" , match_want , match )
170
+ if r .global then
171
+ for i , match_want in ipairs (want ) do
172
+ local match = matches [i ]
173
+ if match ~= match_want then
174
+ return fail (" match mismatch, wanted %s, got %s" , match_want , match )
175
+ end
174
176
end
177
+ else
178
+ -- TODO: compare match object
175
179
end
176
180
end
177
181
successes = successes + 1
178
182
end
179
183
180
184
local function test_match_all_list (str , regex , flags , want )
181
- print (str , " ~" , regex , " flags" )
182
185
local function fail (fmt , ...)
183
186
print (str , regex , flags , want )
184
187
print (string.format (fmt , ... ))
@@ -271,13 +274,32 @@ local function test_replace(str, regex, flags, replacement, want)
271
274
successes = successes + 1
272
275
end
273
276
277
+ local function test_replace_all (str , regex , flags , replacement , want )
278
+ local function fail (fmt , ...)
279
+ print (str , regex , flags , want )
280
+ print (string.format (fmt , ... ))
281
+ fails = fails + 1
282
+ end
283
+ tests = tests + 1
284
+ local r = jsregexp .compile_safe (regex , flags )
285
+ if not r then
286
+ return fail (" compilation error" )
287
+ end
288
+ local res = r :replace (str , replacement )
289
+ if res ~= want then
290
+ return fail (" replacement mismatch, wanted %s, got %s" , want , res )
291
+ end
292
+ successes = successes + 1
293
+ end
294
+
274
295
test_compile (" dummy" , " (.*" , " " , nil )
275
296
test_compile (" dummy" , " [" , " " , nil )
276
297
277
- -- 0xfd (together with other wird chars) crashes lre_compile if not caught
298
+ -- 0xfd (together with other weird chars) crashes lre_compile if not caught
278
299
-- (luajit at least..)
279
300
test_compile (" dummy" , string.char (0xfd , 166 , 178 , 165 , 138 , 183 ), " " , nil )
280
301
302
+ test_exec (" wut" , " wot" , " " , {})
281
303
test_exec (" The quick brown" , " \\ w+" , " g" , { { [0 ] = " The" }, { [0 ] = " quick" }, { [0 ] = " brown" } })
282
304
test_exec (
283
305
" The quick brown fox" ,
@@ -320,6 +342,7 @@ test_match("The quick brown", "\\w+", "g", { "The", "quick", "brown" })
320
342
321
343
test_match_all_list (" The quick brown" , " \\ d+" , " g" , {})
322
344
test_match_all_list (" The quick brown" , " \\ w+" , " g" , { " The" , " quick" , " brown" })
345
+ test_match_all_list (" 𝄞𝄞𐐷𝄞𝄞" , " 𝄞*" , " g" , { " 𝄞𝄞" , " " , " 𝄞𝄞" , " " })
323
346
324
347
test_search (" The quick brown" , " nothing" , " g" , - 1 )
325
348
test_search (" The quick brown" , " quick" , " g" , 5 )
@@ -333,12 +356,30 @@ test_split("-2-3", "-", "g", { "", "2", "3" })
333
356
test_split (" --" , " -" , " g" , { " " , " " , " " })
334
357
test_split (" Hello 1 word. Sentence number 2." , " (\\ d)" , " g" , { " Hello " , " 1" , " word. Sentence number " , " 2" , " ." })
335
358
359
+ test_replace (" a b" , " \\ w+" , " " , " _" , " _ b" )
360
+ test_replace (" a b" , " \\ w+" , " " , function ()
361
+ return " _"
362
+ end , " _ b" )
363
+ test_replace (" 12 34" , " \\ d+" , " " , " _" , " _ 34" )
364
+ test_replace (" 123 456" , " \\ d+" , " " , " _" , " _ 456" )
336
365
test_replace (" a1b2c" , " X" , " g" , " _" , " a1b2c" )
337
366
test_replace (" a1b2c" , " \\ d" , " " , " _" , " a_b2c" )
338
367
test_replace (" a1b2c" , " \\ d" , " g" , " _" , " a_b_c" )
339
368
test_replace (" a1b2c" , " (\\ d)(.)" , " g" , " $1" , " a12" )
340
369
test_replace (" a1b2c" , " (\\ d)(.)" , " g" , " $2" , " abc" )
341
370
371
+ test_replace_all (" a b" , " \\ w+" , " g" , " _" , " _ _" )
372
+ test_replace_all (" a b" , " \\ w+" , " g" , function ()
373
+ return " _"
374
+ end , " _ _" )
375
+ test_replace_all (" 12 34" , " \\ d+" , " g" , " _" , " _ _" )
376
+ test_replace_all (" 123 456" , " \\ d+" , " g" , " _" , " _ _" )
377
+ test_replace_all (" a1b2c" , " X" , " g" , " _" , " a1b2c" )
378
+ test_replace_all (" a1b2c" , " \\ d" , " g" , " _" , " a_b_c" )
379
+ test_replace_all (" a1b2c" , " \\ d" , " g" , " _" , " a_b_c" )
380
+ test_replace_all (" a1b2c" , " (\\ d)(.)" , " g" , " $1" , " a12" )
381
+ test_replace_all (" a1b2c" , " (\\ d)(.)" , " g" , " $2" , " abc" )
382
+
342
383
local bold_green = " \27 [1;32m"
343
384
local bold_red = " \27 [1;31m"
344
385
local normal = " \27 [0m"
0 commit comments