1
# vim:set ft= ts=4 sw=4 et fdm=marker:
3
use Test::Nginx::Socket;
5
#worker_connections(1014);
12
plan tests => repeat_each() * (blocks() * 2);
20
=== TEST 1: matched but w/o variables
24
local s, n = ngx.re.sub("hello, world", "[a-z]+", "howdy")
37
=== TEST 2: not matched
41
local s, n = ngx.re.sub("hello, world", "[A-Z]+", "howdy")
54
=== TEST 3: matched and with variables
58
local s, n = ngx.re.sub("a b c d", "(b) (c)", "[$0] [$1] [$2] [$3] [$134]")
66
a [b c] [b] [c] [] [] d
71
=== TEST 4: matched and with named variables
75
local rc, s, n = pcall(ngx.re.sub, "a b c d",
76
"(b) (c)", "[$0] [$1] [$2] [$3] [$hello]")
86
bad template for substitution: "[$0] [$1] [$2] [$3] [$hello]"
91
=== TEST 5: matched and with named variables (bracketed)
95
local rc, s, n = pcall(ngx.re.sub, "a b c d",
96
"(b) (c)", "[$0] [$1] [$2] [$3] [${hello}]")
106
bad template for substitution: "[$0] [$1] [$2] [$3] [${hello}]"
111
=== TEST 6: matched and with bracketed variables
115
local s, n = ngx.re.sub("b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${134}]")
123
[b c] [b] [c] [] [] d
128
=== TEST 7: matched and with bracketed variables (unmatched brackets)
132
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${134]")
142
bad template for substitution: "[$0] [$1] [${2}] [$3] [${134]"
147
=== TEST 8: matched and with bracketed variables (unmatched brackets)
151
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${134")
161
bad template for substitution: "[$0] [$1] [${2}] [$3] [${134"
166
=== TEST 9: matched and with bracketed variables (unmatched brackets)
170
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [${")
180
bad template for substitution: "[$0] [$1] [${2}] [$3] [${"
185
=== TEST 10: trailing $
189
local rc, s, n = pcall(ngx.re.sub, "b c d", "(b) (c)", "[$0] [$1] [${2}] [$3] [$")
199
bad template for substitution: "[$0] [$1] [${2}] [$3] [$"
204
=== TEST 11: matched but w/o variables and with literal $
208
local s, n = ngx.re.sub("hello, world", "[a-z]+", "ho$$wdy")
221
=== TEST 12: non-anchored match
225
local s, n = ngx.re.sub("hello, 1234", "[0-9]", "x")
238
=== TEST 13: anchored match
242
local s, n = ngx.re.sub("hello, 1234", "[0-9]", "x", "a")
255
=== TEST 14: function replace
259
local repl = function (m)
260
return "[" .. m[0] .. "] [" .. m[1] .. "]"
263
local s, n = ngx.re.sub("hello, 34", "([0-9])", repl)
276
=== TEST 15: function replace (failed)
280
local repl = function (m)
281
return "[" .. m[0] .. "] [" .. m[1] .. "]"
284
local s, n = ngx.re.sub("hello, 34", "([A-Z])", repl)
297
=== TEST 16: bad repl arg type
301
local rc, s, n = pcall(ngx.re.sub, "hello, 34", "([A-Z])", true)
311
bad argument #3 to '?' (string, number, or function expected, got boolean)
316
=== TEST 17: use number to replace
320
local rc, s, n = pcall(ngx.re.sub, "hello, 34", "([0-9])", 72)
335
=== TEST 18: bad function return value type
339
local f = function (m) end
340
local rc, s, n = pcall(ngx.re.sub, "hello, 34", "([0-9])", f)
350
bad argument #3 to '?' (string or number expected to be returned by the replace function, got nil)
355
=== TEST 19: matched but w/o variables (set_by_lua)
359
local s, n = ngx.re.sub("hello, world", "[a-z]+", "howdy")