1 -- See Copyright Notice in the file LICENSE
3 -- This file should contain only test sets that behave identically
4 -- when being run with pcre or posix regex libraries.
6 local luatest = require "luatest"
9 local function norm(a) return a==nil and N or a end
11 local function get_gsub (lib)
13 function (subj, pattern, repl, n)
14 return lib.new (pattern) : gsub (subj, repl, n)
18 local function set_f_gmatch (lib, flg)
19 -- gmatch (s, p, [cf], [ef])
20 local function test_gmatch (subj, patt)
21 local out, guard = {}, 10
22 for a, b in lib.gmatch (subj, patt) do
23 table.insert (out, { norm(a), norm(b) })
25 if guard == 0 then break end
30 Name = "Function gmatch",
32 --{ subj patt results }
33 { {"ab", lib.new"."}, {{"a",N}, {"b",N} } },
34 { {("abcd"):rep(3), "(.)b.(d)"}, {{"a","d"},{"a","d"},{"a","d"}} },
35 { {"abcd", ".*" }, {{"abcd",N},{"",N} } },--zero-length match
36 { {"abc", "^." }, {{"a",N}} },--anchored pattern
40 local function set_f_split (lib, flg)
41 -- split (s, p, [cf], [ef])
42 local function test_split (subj, patt)
43 local out, guard = {}, 10
44 for a, b, c in lib.split (subj, patt) do
45 table.insert (out, { norm(a), norm(b), norm(c) })
47 if guard == 0 then break end
52 Name = "Function split",
54 --{ subj patt results }
55 { {"ab", lib.new","}, {{"ab",N,N}, } },
56 { {"ab", ","}, {{"ab",N,N}, } },
57 { {",", ","}, {{"",",",N}, {"", N, N}, } },
58 { {",,", ","}, {{"",",",N}, {"",",",N}, {"",N,N} } },
59 { {"a,b", ","}, {{"a",",",N}, {"b",N,N}, } },
60 { {",a,b", ","}, {{"",",",N}, {"a",",",N}, {"b",N,N}} },
61 { {"a,b,", ","}, {{"a",",",N}, {"b",",",N}, {"",N,N} } },
62 { {"a,,b", ","}, {{"a",",",N}, {"",",",N}, {"b",N,N}} },
63 { {"ab<78>c", "<(.)(.)>"}, {{"ab","7","8"}, {"c",N,N}, } },
64 { {"abc", "^."}, {{"", "a",N}, {"bc",N,N}, } },--anchored pattern
65 { {"abc", "^"}, {{"", "", N}, {"abc",N,N}, } },
66 -- { {"abc", "$"}, {{"abc","",N}, {"",N,N}, } },
67 -- { {"abc", "^|$"}, {{"", "", N}, {"abc","",N},{"",N,N},} },
71 local function set_f_find (lib, flg)
73 Name = "Function find",
75 -- {subj, patt, st}, { results }
76 { {"abcd", lib.new".+"}, { 1,4 } }, -- [none]
77 { {"abcd", ".+"}, { 1,4 } }, -- [none]
78 { {"abcd", ".+", 2}, { 2,4 } }, -- positive st
79 { {"abcd", ".+", -2}, { 3,4 } }, -- negative st
80 { {"abcd", ".*"}, { 1,4 } }, -- [none]
81 { {"abc", "bc"}, { 2,3 } }, -- [none]
82 { {"abcd", "(.)b.(d)"}, { 1,4,"a","d" }}, -- [captures]
86 local function set_f_match (lib, flg)
88 Name = "Function match",
90 -- {subj, patt, st}, { results }
91 { {"abcd", lib.new".+"}, {"abcd"} }, -- [none]
92 { {"abcd", ".+"}, {"abcd"} }, -- [none]
93 { {"abcd", ".+", 2}, {"bcd"} }, -- positive st
94 { {"abcd", ".+", -2}, {"cd"} }, -- negative st
95 { {"abcd", ".*"}, {"abcd"} }, -- [none]
96 { {"abc", "bc"}, {"bc"} }, -- [none]
97 { {"abcd", "(.)b.(d)"}, {"a","d"} }, -- [captures]
101 local function set_m_exec (lib, flg)
103 Name = "Method exec",
105 --{patt}, {subj, st} { results }
106 { {".+"}, {"abcd"}, {1,4,{}} }, -- [none]
107 { {".+"}, {"abcd",2}, {2,4,{}} }, -- positive st
108 { {".+"}, {"abcd",-2}, {3,4,{}} }, -- negative st
109 { {".*"}, {"abcd"}, {1,4,{}} }, -- [none]
110 { {"bc"}, {"abc"}, {2,3,{}} }, -- [none]
111 { { "(.)b.(d)"}, {"abcd"}, {1,4,{1,1,4,4}}},--[captures]
112 { {"(a+)6+(b+)"}, {"Taa66bbT",2}, {2,7,{2,3,6,7}}},--[st+captures]
116 local function set_m_tfind (lib, flg)
118 Name = "Method tfind",
120 --{patt}, {subj, st} { results }
121 { {".+"}, {"abcd"}, {1,4,{}} }, -- [none]
122 { {".+"}, {"abcd",2}, {2,4,{}} }, -- positive st
123 { {".+"}, {"abcd",-2}, {3,4,{}} }, -- negative st
124 { {".*"}, {"abcd"}, {1,4,{}} }, -- [none]
125 { {"bc"}, {"abc"}, {2,3,{}} }, -- [none]
126 { {"(.)b.(d)"}, {"abcd"}, {1,4,{"a","d"}}},--[captures]
130 local function set_m_find (lib, flg)
132 Name = "Method find",
134 --{patt}, {subj, st} { results }
135 { {".+"}, {"abcd"}, {1,4} }, -- [none]
136 { {".+"}, {"abcd",2}, {2,4} }, -- positive st
137 { {".+"}, {"abcd",-2}, {3,4} }, -- negative st
138 { {".*"}, {"abcd"}, {1,4} }, -- [none]
139 { {"bc"}, {"abc"}, {2,3} }, -- [none]
140 { {"(.)b.(d)"}, {"abcd"}, {1,4,"a","d"}},--[captures]
144 local function set_m_match (lib, flg)
146 Name = "Method match",
148 --{patt}, {subj, st} { results }
149 { {".+"}, {"abcd"}, {"abcd"} }, -- [none]
150 { {".+"}, {"abcd",2}, {"bcd" } }, -- positive st
151 { {".+"}, {"abcd",-2}, {"cd" } }, -- negative st
152 { {".*"}, {"abcd"}, {"abcd"} }, -- [none]
153 { {"bc"}, {"abc"}, {"bc" } }, -- [none]
154 {{ "(.)b.(d)"}, {"abcd"}, {"a","d"} }, --[captures]
158 local function set_f_gsub1 (lib, flg)
159 local subj, pat = "abcdef", "[abef]+"
160 local cpat = lib.new(pat)
162 Name = "Function gsub, set1",
163 Func = get_gsub (lib),
164 --{ s, p, f, n, res1, res2, res3 },
165 { {subj, cpat, "", 0}, {subj, 0, 0} }, -- test "n" + empty_replace
166 { {subj, pat, "", 0}, {subj, 0, 0} }, -- test "n" + empty_replace
167 { {subj, pat, "", -1}, {subj, 0, 0} }, -- test "n" + empty_replace
168 { {subj, pat, "", 1}, {"cdef", 1, 1} },
169 { {subj, pat, "", 2}, {"cd", 2, 2} },
170 { {subj, pat, "", 3}, {"cd", 2, 2} },
171 { {subj, pat, "" }, {"cd", 2, 2} },
172 { {subj, pat, "#", 0}, {subj, 0, 0} }, -- test "n" + non-empty_replace
173 { {subj, pat, "#", 1}, {"#cdef", 1, 1} },
174 { {subj, pat, "#", 2}, {"#cd#", 2, 2} },
175 { {subj, pat, "#", 3}, {"#cd#", 2, 2} },
176 { {subj, pat, "#" }, {"#cd#", 2, 2} },
177 { {"abc", "^.", "#" }, {"#bc", 1, 1} }, -- anchored pattern
181 local function set_f_gsub2 (lib, flg)
182 local subj, pat = "abc", "([ac])"
184 Name = "Function gsub, set2",
185 Func = get_gsub (lib),
186 --{ s, p, f, n, res1, res2, res3 },
187 { {subj, pat, "<%1>" }, {"<a>b<c>", 2, 2} }, -- test non-escaped chars in f
188 { {subj, pat, "%<%1%>" }, {"<a>b<c>", 2, 2} }, -- test escaped chars in f
189 { {subj, pat, "" }, {"b", 2, 2} }, -- test empty replace
190 { {subj, pat, "1" }, {"1b1", 2, 2} }, -- test odd and even %'s in f
191 { {subj, pat, "%1" }, {"abc", 2, 2} },
192 { {subj, pat, "%%1" }, {"%1b%1", 2, 2} },
193 { {subj, pat, "%%%1" }, {"%ab%c", 2, 2} },
194 { {subj, pat, "%%%%1" }, {"%%1b%%1", 2, 2} },
195 { {subj, pat, "%%%%%1" }, {"%%ab%%c", 2, 2} },
199 local function set_f_gsub3 (lib, flg)
201 Name = "Function gsub, set3",
202 Func = get_gsub (lib),
203 --{ s, p, f, n, res1,res2,res3 },
204 { {"abc", "a", "%0" }, {"abc", 1, 1} }, -- test (in)valid capture index
205 { {"abc", "a", "%1" }, {"abc", 1, 1} },
206 { {"abc", "[ac]", "%1" }, {"abc", 2, 2} },
207 { {"abc", "(a)", "%1" }, {"abc", 1, 1} },
208 { {"abc", "(a)", "%2" }, "invalid capture index" },
212 local function set_f_gsub4 (lib, flg)
214 Name = "Function gsub, set4",
215 Func = get_gsub (lib),
216 --{ s, p, f, n, res1, res2, res3 },
217 { {"a2c3", ".", "#" }, {"####", 4, 4} }, -- test .
218 { {"a2c3", ".+", "#" }, {"#", 1, 1} }, -- test .+
219 { {"a2c3", ".*", "#" }, {"##", 2, 2} }, -- test .*
220 { {"/* */ */", "\\/\\*(.*)\\*\\/", "#" }, {"#", 1, 1} },
221 { {"a2c3", "[0-9]", "#" }, {"a#c#", 2, 2} }, -- test %d
222 { {"a2c3", "[^0-9]", "#" }, {"#2#3", 2, 2} }, -- test %D
223 { {"a \t\nb", "[ \t\n]", "#" }, {"a###b", 3, 3} }, -- test %s
224 { {"a \t\nb", "[^ \t\n]", "#" }, {"# \t\n#", 2, 2} }, -- test %S
228 local function set_f_gsub5 (lib, flg)
229 local function frep1 () end -- returns nothing
230 local function frep2 () return "#" end -- ignores arguments
231 local function frep3 (...) return table.concat({...}, ",") end -- "normal"
232 local function frep4 () return {} end -- invalid return type
233 local function frep5 () return "7", "a" end -- 2-nd return is "a"
234 local function frep6 () return "7", "break" end -- 2-nd return is "break"
237 Name = "Function gsub, set5",
238 Func = get_gsub (lib),
239 --{ s, p, f, n, res1, res2, res3 },
240 { {subj, "a(.)c(.)", frep1 }, {subj, 1, 0} },
241 { {subj, "a(.)c(.)", frep2 }, {"#", 1, 1} },
242 { {subj, "a(.)c(.)", frep3 }, {"2,3", 1, 1} },
243 { {subj, "a.c.", frep3 }, {subj, 1, 1} },
244 { {subj, "z*", frep1 }, {subj, 5, 0} },
245 { {subj, "z*", frep2 }, {"#a#2#c#3#", 5, 5} },
246 { {subj, "z*", frep3 }, {subj, 5, 5} },
247 { {subj, subj, frep4 }, "invalid return type" },
248 { {"abc",".", frep5 }, {"777", 3, 3} },
249 { {"abc",".", frep6 }, {"777", 3, 3} },
253 local function set_f_gsub6 (lib, flg)
254 local tab1, tab2, tab3 = {}, { ["2"] = 56 }, { ["2"] = {} }
257 Name = "Function gsub, set6",
258 Func = get_gsub (lib),
259 --{ s, p, f, n, res1,res2,res3 },
260 { {subj, "a(.)c(.)", tab1 }, {subj, 1, 0} },
261 { {subj, "a(.)c(.)", tab2 }, {"56", 1, 1} },
262 { {subj, "a(.)c(.)", tab3 }, "invalid replacement type" },
263 { {subj, "a.c.", tab1 }, {subj, 1, 0} },
264 { {subj, "a.c.", tab2 }, {subj, 1, 0} },
265 { {subj, "a.c.", tab3 }, {subj, 1, 0} },
269 local function set_f_gsub8 (lib, flg)
270 local subj, patt, repl = "abcdef", "..", "*"
272 Name = "Function gsub, set8",
273 Func = get_gsub (lib),
274 --{ s, p, f, n, res1, res2, res3 },
275 { {subj, patt, repl, function() end }, {"abcdef", 3, 0} },
276 { {subj, patt, repl, function() return nil end }, {"abcdef", 3, 0} },
277 { {subj, patt, repl, function() return false end }, {"abcdef", 3, 0} },
278 { {subj, patt, repl, function() return true end }, {"***", 3, 3} },
279 { {subj, patt, repl, function() return {} end }, {"***", 3, 3} },
280 { {subj, patt, repl, function() return "#" end }, {"###", 3, 3} },
281 { {subj, patt, repl, function() return 57 end }, {"575757", 3, 3} },
282 { {subj, patt, repl, function (from) return from end }, {"135", 3, 3} },
283 { {subj, patt, repl, function (from, to) return to end }, {"246", 3, 3} },
284 { {subj, patt, repl, function (from,to,rep) return rep end },
286 { {subj, patt, repl, function (from, to, rep) return rep..to..from end },
287 {"*21*43*65", 3, 3} },
288 { {subj, patt, repl, function() return nil end }, {"abcdef", 3, 0} },
289 { {subj, patt, repl, function() return nil, nil end }, {"abcdef", 3, 0} },
290 { {subj, patt, repl, function() return nil, false end }, {"abcdef", 3, 0} },
291 { {subj, patt, repl, function() return nil, true end }, {"ab**", 3, 2} },
292 { {subj, patt, repl, function() return true, true end }, {"***", 3, 3} },
293 { {subj, patt, repl, function() return nil, 0 end }, {"abcdef", 1, 0} },
294 { {subj, patt, repl, function() return true, 0 end }, {"*cdef", 1, 1} },
295 { {subj, patt, repl, function() return nil, 1 end }, {"ab*ef", 2, 1} },
296 { {subj, patt, repl, function() return true, 1 end }, {"**ef", 2, 2} },
300 return function (libname, isglobal)
301 local lib = isglobal and _G[libname] or require (libname)