| 1 |
local testframework = require 'Module:TestFramework' |
| 2 |
|
| 3 |
local str1 = "\0\127\194\128\223\191\224\160\128\239\191\191\240\144\128\128\244\143\191\191" |
| 4 |
local str2 = "foo bar főó foó baz foooo foofoo fo" |
| 5 |
local str3 = "??? foo bar főó foó baz foooo foofoo fo ok?" |
| 6 |
local str4 = {} |
| 7 |
for i = 1, 10000/4 do |
| 8 |
str4[i] = "főó " |
| 9 |
end |
| 10 |
str4 = table.concat( str4 ) |
| 11 |
|
| 12 |
local function testLongGcodepoint() |
| 13 |
local ret = {} |
| 14 |
local i = 1 |
| 15 |
for cp in mw.ustring.gcodepoint( str4 ) do |
| 16 |
if i <= 4 or i > 9996 then |
| 17 |
ret[i] = cp |
| 18 |
end |
| 19 |
i = i + 1 |
| 20 |
end |
| 21 |
return ret |
| 22 |
end |
| 23 |
|
| 24 |
return testframework.getTestProvider( { |
| 25 |
{ name = 'isutf8: valid string', func = mw.ustring.isutf8, |
| 26 |
args = { "\0 \127 \194\128 \223\191 \224\160\128 \239\191\191 \240\144\128\128 \244\143\191\191" }, |
| 27 |
expect = { true } |
| 28 |
}, |
| 29 |
{ name = 'isutf8: out of range character', func = mw.ustring.isutf8, |
| 30 |
args = { "\244\144\128\128" }, |
| 31 |
expect = { false } |
| 32 |
}, |
| 33 |
{ name = 'isutf8: insufficient continuation bytes', func = mw.ustring.isutf8, |
| 34 |
args = { "\240\128\128" }, |
| 35 |
expect = { false } |
| 36 |
}, |
| 37 |
{ name = 'isutf8: excess continuation bytes', func = mw.ustring.isutf8, |
| 38 |
args = { "\194\128\128" }, |
| 39 |
expect = { false } |
| 40 |
}, |
| 41 |
{ name = 'isutf8: bare continuation byte', func = mw.ustring.isutf8, |
| 42 |
args = { "\128" }, |
| 43 |
expect = { false } |
| 44 |
}, |
| 45 |
{ name = 'isutf8: overlong encoding', func = mw.ustring.isutf8, |
| 46 |
args = { "\192\128" }, |
| 47 |
expect = { false } |
| 48 |
}, |
| 49 |
{ name = 'isutf8: overlong encoding (2)', func = mw.ustring.isutf8, |
| 50 |
args = { "\193\191" }, |
| 51 |
expect = { false } |
| 52 |
}, |
| 53 |
|
| 54 |
{ name = 'byteoffset: (1)', func = mw.ustring.byteoffset, |
| 55 |
args = { "fóo", 1 }, |
| 56 |
expect = { 1 } |
| 57 |
}, |
| 58 |
{ name = 'byteoffset: (2)', func = mw.ustring.byteoffset, |
| 59 |
args = { "fóo", 2 }, |
| 60 |
expect = { 2 } |
| 61 |
}, |
| 62 |
{ name = 'byteoffset: (3)', func = mw.ustring.byteoffset, |
| 63 |
args = { "fóo", 3 }, |
| 64 |
expect = { 4 } |
| 65 |
}, |
| 66 |
{ name = 'byteoffset: (4)', func = mw.ustring.byteoffset, |
| 67 |
args = { "fóo", 4 }, |
| 68 |
expect = { nil } |
| 69 |
}, |
| 70 |
{ name = 'byteoffset: (0,1)', func = mw.ustring.byteoffset, |
| 71 |
args = { "fóo", 0, 1 }, |
| 72 |
expect = { 1 } |
| 73 |
}, |
| 74 |
{ name = 'byteoffset: (0,2)', func = mw.ustring.byteoffset, |
| 75 |
args = { "fóo", 0, 2 }, |
| 76 |
expect = { 2 } |
| 77 |
}, |
| 78 |
{ name = 'byteoffset: (0,3)', func = mw.ustring.byteoffset, |
| 79 |
args = { "fóo", 0, 3 }, |
| 80 |
expect = { 2 } |
| 81 |
}, |
| 82 |
{ name = 'byteoffset: (0,4)', func = mw.ustring.byteoffset, |
| 83 |
args = { "fóo", 0, 4 }, |
| 84 |
expect = { 4 } |
| 85 |
}, |
| 86 |
{ name = 'byteoffset: (0,5)', func = mw.ustring.byteoffset, |
| 87 |
args = { "fóo", 0, 5 }, |
| 88 |
expect = { nil } |
| 89 |
}, |
| 90 |
{ name = 'byteoffset: (0,-1)', func = mw.ustring.byteoffset, |
| 91 |
args = { "fóo", 0, -1 }, |
| 92 |
expect = { 4 } |
| 93 |
}, |
| 94 |
{ name = 'byteoffset: (0,-1)', func = mw.ustring.byteoffset, |
| 95 |
args = { "foó", 0, -1 }, |
| 96 |
expect = { 3 } |
| 97 |
}, |
| 98 |
{ name = 'byteoffset: (1,-1)', func = mw.ustring.byteoffset, |
| 99 |
args = { "fóo", 1, -1 }, |
| 100 |
expect = { 4 } |
| 101 |
}, |
| 102 |
{ name = 'byteoffset: (1,-1)', func = mw.ustring.byteoffset, |
| 103 |
args = { "foó", 1, -1 }, |
| 104 |
expect = { nil } |
| 105 |
}, |
| 106 |
|
| 107 |
{ name = 'codepoint: whole string', func = mw.ustring.codepoint, |
| 108 |
args = { str1, 1, -1 }, |
| 109 |
expect = { 0, 0x7f, 0x80, 0x7ff, 0x800, 0xffff, 0x10000, 0x10ffff } |
| 110 |
}, |
| 111 |
{ name = 'codepoint: substring', func = mw.ustring.codepoint, |
| 112 |
args = { str1, 5, -2 }, |
| 113 |
expect = { 0x800, 0xffff, 0x10000 } |
| 114 |
}, |
| 115 |
{ name = 'codepoint: (5,4)', func = mw.ustring.codepoint, |
| 116 |
args = { str1, 5, 4 }, |
| 117 |
expect = {} |
| 118 |
}, |
| 119 |
{ name = 'codepoint: (1,0)', func = mw.ustring.codepoint, |
| 120 |
args = { str1, 1, 0 }, |
| 121 |
expect = {} |
| 122 |
}, |
| 123 |
{ name = 'codepoint: (9,9)', func = mw.ustring.codepoint, |
| 124 |
args = { str1, 9, 9 }, |
| 125 |
expect = {} |
| 126 |
}, |
| 127 |
{ name = 'codepoint: end of a really long string', func = mw.ustring.codepoint, |
| 128 |
args = { str4, 9000, 9004 }, |
| 129 |
expect = { 0x20, 0x66, 0x151, 0xf3, 0x20 } |
| 130 |
}, |
| 131 |
|
| 132 |
{ name = 'char: basic test', func = mw.ustring.char, |
| 133 |
args = { 0, 0x7f, 0x80, 0x7ff, 0x800, 0xffff, 0x10000, 0x10ffff }, |
| 134 |
expect = { str1 } |
| 135 |
}, |
| 136 |
{ name = 'char: invalid codepoint', func = mw.ustring.char, |
| 137 |
args = { 0x110000 }, |
| 138 |
expect = "bad argument #1 to 'char' (value out of range)" |
| 139 |
}, |
| 140 |
{ name = 'char: invalid value', func = mw.ustring.char, |
| 141 |
args = { 'foo' }, |
| 142 |
expect = "bad argument #1 to 'char' (number expected, got string)" |
| 143 |
}, |
| 144 |
|
| 145 |
{ name = 'len: basic test', func = mw.ustring.len, |
| 146 |
args = { str1 }, |
| 147 |
expect = { 8 } |
| 148 |
}, |
| 149 |
{ name = 'len: invalid string', func = mw.ustring.len, |
| 150 |
args = { "\244\144\128\128" }, |
| 151 |
expect = { nil } |
| 152 |
}, |
| 153 |
|
| 154 |
{ name = 'sub: (4)', func = mw.ustring.sub, |
| 155 |
args = { str1, 4 }, |
| 156 |
expect = { "\223\191\224\160\128\239\191\191\240\144\128\128\244\143\191\191" } |
| 157 |
}, |
| 158 |
{ name = 'sub: (4,7)', func = mw.ustring.sub, |
| 159 |
args = { str1, 4, 7 }, |
| 160 |
expect = { "\223\191\224\160\128\239\191\191\240\144\128\128" } |
| 161 |
}, |
| 162 |
{ name = 'sub: (4,-1)', func = mw.ustring.sub, |
| 163 |
args = { str1, 4, -1 }, |
| 164 |
expect = { "\223\191\224\160\128\239\191\191\240\144\128\128\244\143\191\191" } |
| 165 |
}, |
| 166 |
{ name = 'sub: (4,-2)', func = mw.ustring.sub, |
| 167 |
args = { str1, 4, -2 }, |
| 168 |
expect = { "\223\191\224\160\128\239\191\191\240\144\128\128" } |
| 169 |
}, |
| 170 |
{ name = 'sub: (-2)', func = mw.ustring.sub, |
| 171 |
args = { str1, -2 }, |
| 172 |
expect = { "\240\144\128\128\244\143\191\191" } |
| 173 |
}, |
| 174 |
{ name = 'sub: (9)', func = mw.ustring.sub, |
| 175 |
args = { str1, 9 }, |
| 176 |
expect = { "" } |
| 177 |
}, |
| 178 |
{ name = 'sub: (0)', func = mw.ustring.sub, |
| 179 |
args = { str1, 0 }, |
| 180 |
expect = { str1 } |
| 181 |
}, |
| 182 |
{ name = 'sub: (4,3)', func = mw.ustring.sub, |
| 183 |
args = { str1, 4, 3 }, |
| 184 |
expect = { "" } |
| 185 |
}, |
| 186 |
{ name = 'sub: (1,0)', func = mw.ustring.sub, |
| 187 |
args = { str2, 1, 0 }, |
| 188 |
expect = { "" } |
| 189 |
}, |
| 190 |
{ name = 'sub: (5,5)', func = mw.ustring.sub, |
| 191 |
args = { str1, 5, 5 }, |
| 192 |
expect = { "\224\160\128" } |
| 193 |
}, |
| 194 |
{ name = 'sub: (9,9)', func = mw.ustring.sub, |
| 195 |
args = { str1, 9, 9 }, |
| 196 |
expect = { "" } |
| 197 |
}, |
| 198 |
{ name = 'sub: empty string', func = mw.ustring.sub, |
| 199 |
args = { '', 5 }, |
| 200 |
expect = { "" } |
| 201 |
}, |
| 202 |
|
| 203 |
{ name = 'upper: basic test', func = mw.ustring.upper, |
| 204 |
args = { "fóó?" }, |
| 205 |
expect = { "FÓÓ?" } |
| 206 |
}, |
| 207 |
{ name = 'lower: basic test', func = mw.ustring.lower, |
| 208 |
args = { "FÓÓ?" }, |
| 209 |
expect = { "fóó?" } |
| 210 |
}, |
| 211 |
|
| 212 |
{ name = 'find: (simple)', func = mw.ustring.find, |
| 213 |
args = { "bar ¡foo bar", '¡foo' }, |
| 214 |
expect = { 5, 8 } |
| 215 |
}, |
| 216 |
{ name = 'find: (%)', func = mw.ustring.find, |
| 217 |
args = { "bar ¡foo bar", '¡fo%+' }, |
| 218 |
expect = { } |
| 219 |
}, |
| 220 |
{ name = 'find: (%)', func = mw.ustring.find, |
| 221 |
args = { "bar ¡fo+ bar", '¡fo%+' }, |
| 222 |
expect = { 5, 8 } |
| 223 |
}, |
| 224 |
{ name = 'find: (+)', func = mw.ustring.find, |
| 225 |
args = { "bar ¡foo bar", '¡fo+' }, |
| 226 |
expect = { 5, 8 } |
| 227 |
}, |
| 228 |
{ name = 'find: (+) (2)', func = mw.ustring.find, |
| 229 |
args = { "bar ¡foo bar", '¡fx+o+' }, |
| 230 |
expect = {} |
| 231 |
}, |
| 232 |
{ name = 'find: (?)', func = mw.ustring.find, |
| 233 |
args = { "bar ¡foox bar", '¡foox?' }, |
| 234 |
expect = { 5, 9 } |
| 235 |
}, |
| 236 |
{ name = 'find: (?) (2)', func = mw.ustring.find, |
| 237 |
args = { "bar ¡foo bar", '¡foox?' }, |
| 238 |
expect = { 5, 8 } |
| 239 |
}, |
| 240 |
{ name = 'find: (*)', func = mw.ustring.find, |
| 241 |
args = { "bar ¡foo bar", '¡fx*oo' }, |
| 242 |
expect = { 5, 8 } |
| 243 |
}, |
| 244 |
{ name = 'find: (-)', func = mw.ustring.find, |
| 245 |
args = { "bar ¡foo bar", '¡fo-' }, |
| 246 |
expect = { 5, 6 } |
| 247 |
}, |
| 248 |
{ name = 'find: (-)', func = mw.ustring.find, |
| 249 |
args = { "bar ¡foo bar", '¡fo-o' }, |
| 250 |
expect = { 5, 7 } |
| 251 |
}, |
| 252 |
{ name = 'find: (-)', func = mw.ustring.find, |
| 253 |
args = { "bar ¡foox bar", '¡fo-x' }, |
| 254 |
expect = { 5, 9 } |
| 255 |
}, |
| 256 |
{ name = 'find: (%a)', func = mw.ustring.find, |
| 257 |
args = { "bar ¡foo bar", '¡f%a' }, |
| 258 |
expect = { 5, 7 } |
| 259 |
}, |
| 260 |
{ name = 'find: (%a, utf8)', func = mw.ustring.find, |
| 261 |
args = { "bar ¡fóó bar", '¡f%a' }, |
| 262 |
expect = { 5, 7 } |
| 263 |
}, |
| 264 |
{ name = 'find: (%a, utf8 2)', func = mw.ustring.find, |
| 265 |
args = { "bar ¡fóó bar", 'f%a' }, |
| 266 |
expect = { 6, 7 } |
| 267 |
}, |
| 268 |
{ name = 'find: (%a+)', func = mw.ustring.find, |
| 269 |
args = { "bar ¡fóó bar", '¡f%a+' }, |
| 270 |
expect = { 5, 8 } |
| 271 |
}, |
| 272 |
{ name = 'find: ([]+)', func = mw.ustring.find, |
| 273 |
args = { "bar ¡fóo bar", '¡f[oó]+' }, |
| 274 |
expect = { 5, 8 } |
| 275 |
}, |
| 276 |
{ name = 'find: ([-]+)', func = mw.ustring.find, |
| 277 |
args = { "bar ¡fóo bar", '¡f[a-uá-ú]+' }, |
| 278 |
expect = { 5, 8 } |
| 279 |
}, |
| 280 |
{ name = 'find: ([-]+ 2)', func = mw.ustring.find, |
| 281 |
args = { "bar ¡fóo bar", '¡f[a-ú]+' }, |
| 282 |
expect = { 5, 8 } |
| 283 |
}, |
| 284 |
{ name = 'find: (%b)', func = mw.ustring.find, |
| 285 |
args = { "bar ¡<foo <foo> foo> bar", '¡%b<>' }, |
| 286 |
expect = { 5, 20 } |
| 287 |
}, |
| 288 |
{ name = 'find: (%b 2)', func = mw.ustring.find, |
| 289 |
args = { "bar ¡(foo (foo) foo) bar", '¡%b()' }, |
| 290 |
expect = { 5, 20 } |
| 291 |
}, |
| 292 |
{ name = 'find: (%b 3)', func = mw.ustring.find, |
| 293 |
args = { "bar ¡-foo-foo- bar", '¡%b--' }, |
| 294 |
expect = { 5, 10 } |
| 295 |
}, |
| 296 |
{ name = 'find: (%b 4)', func = mw.ustring.find, |
| 297 |
args = { "bar «foo «foo» foo» bar", '%b«»' }, |
| 298 |
expect = { 5, 19 } |
| 299 |
}, |
| 300 |
{ name = 'find: (%b 5)', func = mw.ustring.find, |
| 301 |
args = { "bar !foo !foo¡ foo¡ bar", '%b!¡' }, |
| 302 |
expect = { 5, 19 } |
| 303 |
}, |
| 304 |
{ name = 'find: (%b 6)', func = mw.ustring.find, |
| 305 |
args = { "bar ¡foo ¡foo! foo! bar", '%b¡!' }, |
| 306 |
expect = { 5, 19 } |
| 307 |
}, |
| 308 |
{ name = 'find: (%b 7)', func = mw.ustring.find, |
| 309 |
args = { "bar ¡foo¡foo¡ bar", '%b¡¡' }, |
| 310 |
expect = { 5, 9 } |
| 311 |
}, |
| 312 |
{ name = 'find: (%f)', func = mw.ustring.find, |
| 313 |
args = { "foo ¡foobar ¡foo bar baz", '¡.-%f[%s]' }, |
| 314 |
expect = { 5, 11 } |
| 315 |
}, |
| 316 |
{ name = 'find: (%f 2)', func = mw.ustring.find, |
| 317 |
args = { "foo ¡foobar ¡foo bar baz", '¡foo%f[%s]' }, |
| 318 |
expect = { 13, 16 } |
| 319 |
}, |
| 320 |
{ name = 'find: (%f 3)', func = mw.ustring.find, |
| 321 |
args = { "foo foo¡foobar ¡foo bar baz", '%f[%S]¡.-%f[%s]' }, |
| 322 |
expect = { 16, 19 } |
| 323 |
}, |
| 324 |
{ name = 'find: (%f 4)', func = mw.ustring.find, |
| 325 |
args = { "foo foo¡foobar ¡foo bar baz", '%f[%S]¡.-%f[%s]', 16 }, |
| 326 |
expect = { 16, 19 } |
| 327 |
}, |
| 328 |
{ name = 'find: (%f 5)', func = mw.ustring.find, |
| 329 |
args = { "foo ¡bar baz", '%f[%Z]' }, |
| 330 |
expect = { 1, 0 } |
| 331 |
}, |
| 332 |
{ name = 'find: (%f 6)', func = mw.ustring.find, |
| 333 |
args = { "foo ¡bar baz", '%f[%z]' }, |
| 334 |
expect = { 13, 12 } |
| 335 |
}, |
| 336 |
{ name = 'find: (%f 7)', func = mw.ustring.find, |
| 337 |
args = { "foo ¡b\0r baz", '%f[%Z]', 2 }, |
| 338 |
expect = { 8, 7 } |
| 339 |
}, |
| 340 |
{ name = 'find: (%f 8)', func = mw.ustring.find, |
| 341 |
args = { "\0foo ¡b\0r baz", '%f[%z]' }, |
| 342 |
expect = { 8, 7 } |
| 343 |
}, |
| 344 |
{ name = 'find: (%f 9)', func = mw.ustring.find, |
| 345 |
args = { "\0foo ¡b\0r baz", '%f[%Z]' }, |
| 346 |
expect = { 2, 1 } |
| 347 |
}, |
| 348 |
{ name = 'find: (%A)', func = mw.ustring.find, |
| 349 |
args = { "fóó? bar", '%A+' }, |
| 350 |
expect = { 4, 5 } |
| 351 |
}, |
| 352 |
{ name = 'find: (%W)', func = mw.ustring.find, |
| 353 |
args = { "fóó? bar", '%W+' }, |
| 354 |
expect = { 4, 5 } |
| 355 |
}, |
| 356 |
{ name = 'find: ([^])', func = mw.ustring.find, |
| 357 |
args = { "fóó? bar", '[^a-zó]+' }, |
| 358 |
expect = { 4, 5 } |
| 359 |
}, |
| 360 |
{ name = 'find: ([^] 2)', func = mw.ustring.find, |
| 361 |
args = { "fó0? bar", '[^%a0-9]+' }, |
| 362 |
expect = { 4, 5 } |
| 363 |
}, |
| 364 |
{ name = 'find: ([^] 3)', func = mw.ustring.find, |
| 365 |
args = { "¡fó0% bar", '¡[^%%]+' }, |
| 366 |
expect = { 1, 4 } |
| 367 |
}, |
| 368 |
{ name = 'find: ($)', func = mw.ustring.find, |
| 369 |
args = { "¡foo1 ¡foo2 ¡foo3", '¡foo[0-9]+$' }, |
| 370 |
expect = { 13, 17 } |
| 371 |
}, |
| 372 |
{ name = 'find: (.*)', func = mw.ustring.find, |
| 373 |
args = { "¡foo¡ ¡bar¡ baz", '¡.*¡' }, |
| 374 |
expect = { 1, 11 } |
| 375 |
}, |
| 376 |
{ name = 'find: (.-)', func = mw.ustring.find, |
| 377 |
args = { "¡foo¡ ¡bar¡ baz", '¡.-¡' }, |
| 378 |
expect = { 1, 5 } |
| 379 |
}, |
| 380 |
{ name = 'find: plain', func = mw.ustring.find, |
| 381 |
args = { "¡a¡ ¡.¡", '¡.¡', 1, true }, |
| 382 |
expect = { 5, 7 } |
| 383 |
}, |
| 384 |
{ name = 'find: empty delimiter', func = mw.ustring.find, |
| 385 |
args = { "¡a¡ ¡.¡", '' }, |
| 386 |
expect = { 1, 0 } |
| 387 |
}, |
| 388 |
{ name = 'find: empty delimiter (2)', func = mw.ustring.find, |
| 389 |
args = { "¡a¡ ¡.¡", '', 2 }, |
| 390 |
expect = { 2, 1 } |
| 391 |
}, |
| 392 |
{ name = 'find: plain + empty delimiter', func = mw.ustring.find, |
| 393 |
args = { "¡a¡ ¡.¡", '', 1, true }, |
| 394 |
expect = { 1, 0 } |
| 395 |
}, |
| 396 |
{ name = 'find: plain + empty delimiter (2)', func = mw.ustring.find, |
| 397 |
args = { "¡a¡ ¡.¡", '', 2, true }, |
| 398 |
expect = { 2, 1 } |
| 399 |
}, |
| 400 |
{ name = 'find: excessive init', func = mw.ustring.find, |
| 401 |
args = { "¡a¡ ¡.¡", '()', 20 }, |
| 402 |
expect = { 8, 7, 8 } |
| 403 |
}, |
| 404 |
{ name = 'find: excessive init (2)', func = mw.ustring.find, |
| 405 |
args = { "¡a¡ ¡.¡", '()', -20 }, |
| 406 |
expect = { 1, 0, 1 } |
| 407 |
}, |
| 408 |
{ name = 'find: plain + excessive init', func = mw.ustring.find, |
| 409 |
args = { "¡a¡ ¡.¡", '', 20, true }, |
| 410 |
expect = { 8, 7 } |
| 411 |
}, |
| 412 |
{ name = 'find: plain + excessive init', func = mw.ustring.find, |
| 413 |
args = { "¡a¡ ¡.¡", '', -20, true }, |
| 414 |
expect = { 1, 0 } |
| 415 |
}, |
| 416 |
|
| 417 |
{ name = 'find: capture (1)', func = mw.ustring.find, |
| 418 |
args = { "bar ¡foo bar", '(¡foo)' }, |
| 419 |
expect = { 5, 8, '¡foo' } |
| 420 |
}, |
| 421 |
{ name = 'find: capture (2)', func = mw.ustring.find, |
| 422 |
args = { "bar ¡fóo bar", '(¡f%a+)' }, |
| 423 |
expect = { 5, 8, '¡fóo' } |
| 424 |
}, |
| 425 |
{ name = 'find: capture (3)', func = mw.ustring.find, |
| 426 |
args = { "bar ¡fóo bar", '(¡f(%a)%a)' }, |
| 427 |
expect = { 5, 8, '¡fóo', 'ó' } |
| 428 |
}, |
| 429 |
{ name = 'find: capture (4)', func = mw.ustring.find, |
| 430 |
args = { "bar ¡fóo bar", '(¡f(%a-)%a)' }, |
| 431 |
expect = { 5, 7, '¡fó', '' } |
| 432 |
}, |
| 433 |
{ name = 'find: capture (5)', func = mw.ustring.find, |
| 434 |
args = { "bar ¡fóo bar", '()(()¡f()(%a)()%a())()' }, |
| 435 |
expect = { 5, 8, 5, '¡fóo', 5, 7, 'ó', 8, 9, 9 } |
| 436 |
}, |
| 437 |
{ name = 'find: capture (6)', func = mw.ustring.find, |
| 438 |
args = { "fóó", "()(f)()(óó)()" }, |
| 439 |
expect = { 1, 3, 1, 'f', 2, 'óó', 4 } |
| 440 |
}, |
| 441 |
{ name = 'find: capture (7)', func = mw.ustring.find, |
| 442 |
args = { "fóó fóó", "()(f)()(óó)()", 2 }, |
| 443 |
expect = { 5, 7, 5, 'f', 6, 'óó', 8 } |
| 444 |
}, |
| 445 |
{ name = 'find: (%1)', func = mw.ustring.find, |
| 446 |
args = { "foo foofóó foófoó bar", '(f%a+)%1' }, |
| 447 |
expect = { 12, 17, 'foó' } |
| 448 |
}, |
| 449 |
{ name = 'find: deceptively-simple pattern', func = mw.ustring.find, |
| 450 |
args = { "fóó", '([^a-z])' }, |
| 451 |
expect = { 2, 2, 'ó' } |
| 452 |
}, |
| 453 |
{ name = 'find: Bracket at start of a character set doesn\'t close', func = mw.ustring.find, |
| 454 |
args = { "fóó", '()[]' }, |
| 455 |
expect = "Missing close-bracket for character set beginning at pattern character 3" |
| 456 |
}, |
| 457 |
{ name = 'find: Bracket at start of a negated character set doesn\'t close', func = mw.ustring.find, |
| 458 |
args = { "fóó", '()[^]' }, |
| 459 |
expect = "Missing close-bracket for character set beginning at pattern character 3" |
| 460 |
}, |
| 461 |
{ name = 'find: Bracket at start of a character set is literal', func = mw.ustring.find, |
| 462 |
args = { "foo]bar¿", '()([]])' }, |
| 463 |
expect = { 4, 4, 4, ']' } |
| 464 |
}, |
| 465 |
{ name = 'find: Bracket at start of a negated character set is literal', func = mw.ustring.find, |
| 466 |
args = { "]bar¿", '()([^]])' }, |
| 467 |
expect = { 2, 2, 2, 'b' } |
| 468 |
}, |
| 469 |
{ name = 'find: Bracket at start of a character set can be a range endpoint', func = mw.ustring.find, |
| 470 |
args = { "foo]bar¿", '()([]-z]+)' }, |
| 471 |
expect = { 1, 7, 1, 'foo]bar' } |
| 472 |
}, |
| 473 |
{ name = 'find: Bracket at start of a negated character can be a range endpoint', func = mw.ustring.find, |
| 474 |
args = { "fOO]bar¿", '()([^]-z]+)' }, |
| 475 |
expect = { 2, 3, 2, 'OO' } |
| 476 |
}, |
| 477 |
{ name = 'find: Weird edge-case that was failing (1)', func = mw.ustring.find, |
| 478 |
args = { "foo]ba-]r¿", '()([a]-%]+)' }, |
| 479 |
expect = { 4, 4, 4, ']' } |
| 480 |
}, |
| 481 |
{ name = 'find: Weird edge-case that was failing (2)', func = mw.ustring.find, |
| 482 |
args = { "foo¿", '()[!-%]' }, |
| 483 |
expect = "Missing close-bracket for character set beginning at pattern character 3" |
| 484 |
}, |
| 485 |
{ name = 'find: Inverted range (1)', func = mw.ustring.find, |
| 486 |
args = { "foo¿", '()([z-a]+)' }, |
| 487 |
expect = { nil } |
| 488 |
}, |
| 489 |
{ name = 'find: Inverted range (2)', func = mw.ustring.find, |
| 490 |
args = { "foo¿", '()([^z-a]+)' }, |
| 491 |
expect = { 1, 4, 1, 'foo¿' } |
| 492 |
}, |
| 493 |
{ name = 'find: Inverted range (3)', func = mw.ustring.find, |
| 494 |
args = { "foo¿", '()(f[z-a]o)' }, |
| 495 |
expect = { nil } |
| 496 |
}, |
| 497 |
{ name = 'find: Inverted range (4)', func = mw.ustring.find, |
| 498 |
args = { "foo¿", '()(f[z-a]*o)' }, |
| 499 |
expect = { 1, 2, 1, 'fo' } |
| 500 |
}, |
| 501 |
|
| 502 |
{ name = 'match: (1)', func = mw.ustring.match, |
| 503 |
args = { "bar fóo bar", 'f%a+' }, |
| 504 |
expect = { 'fóo' } |
| 505 |
}, |
| 506 |
{ name = 'match: (2)', func = mw.ustring.match, |
| 507 |
args = { "bar fóo bar", 'f(%a+)' }, |
| 508 |
expect = { 'óo' } |
| 509 |
}, |
| 510 |
{ name = 'match: empty pattern', func = mw.ustring.match, |
| 511 |
args = { "¡a¡ ¡.¡", '()' }, |
| 512 |
expect = { 1 } |
| 513 |
}, |
| 514 |
{ name = 'match: empty pattern (2)', func = mw.ustring.match, |
| 515 |
args = { "¡a¡ ¡.¡", '()', 2 }, |
| 516 |
expect = { 2 } |
| 517 |
}, |
| 518 |
{ name = 'match: excessive init', func = mw.ustring.match, |
| 519 |
args = { "¡a¡ ¡.¡", '()', 20 }, |
| 520 |
expect = { 8 } |
| 521 |
}, |
| 522 |
{ name = 'match: excessive init (2)', func = mw.ustring.match, |
| 523 |
args = { "¡a¡ ¡.¡", '()', -20 }, |
| 524 |
expect = { 1 } |
| 525 |
}, |
| 526 |
|
| 527 |
{ name = 'gsub: (empty string, empty pattern)', func = mw.ustring.gsub, |
| 528 |
args = { '', '', 'X' }, |
| 529 |
expect = { 'X', 1 } |
| 530 |
}, |
| 531 |
{ name = 'gsub: (empty string, one char pattern)', func = mw.ustring.gsub, |
| 532 |
args = { '', 'á', 'X' }, |
| 533 |
expect = { '', 0 } |
| 534 |
}, |
| 535 |
{ name = 'gsub: (one char string, one char pattern)', func = mw.ustring.gsub, |
| 536 |
args = { 'á', 'á', 'X' }, |
| 537 |
expect = { 'X', 1 } |
| 538 |
}, |
| 539 |
{ name = 'gsub: (one char string, empty pattern)', func = mw.ustring.gsub, |
| 540 |
args = { 'á', '', 'X' }, |
| 541 |
expect = { 'XáX', 2 } |
| 542 |
}, |
| 543 |
{ name = 'gsub: (empty pattern with position captures)', func = mw.ustring.gsub, |
| 544 |
args = { 'ábć', '()', '%1' }, |
| 545 |
expect = { '1á2b3ć4', 4 } |
| 546 |
}, |
| 547 |
{ name = 'gsub: (limited to 1 replacement)', func = mw.ustring.gsub, |
| 548 |
args = { 'áá', 'á', 'X', 1 }, |
| 549 |
expect = { 'Xá', 1 } |
| 550 |
}, |
| 551 |
{ name = 'gsub: (limited to 0 replacements)', func = mw.ustring.gsub, |
| 552 |
args = { 'áá', 'á', 'X', 0 }, |
| 553 |
expect = { 'áá', 0 } |
| 554 |
}, |
| 555 |
{ name = 'gsub: (string 1)', func = mw.ustring.gsub, |
| 556 |
args = { str2, 'f%a+', 'X' }, |
| 557 |
expect = { 'X bar X X baz X X X', 6 } |
| 558 |
}, |
| 559 |
{ name = 'gsub: (string 2)', func = mw.ustring.gsub, |
| 560 |
args = { str3, 'f%a+', 'X' }, |
| 561 |
expect = { '??? X bar X X baz X X X ok?', 6 } |
| 562 |
}, |
| 563 |
{ name = 'gsub: (string 3)', func = mw.ustring.gsub, |
| 564 |
args = { str2, 'f%a+', 'X', 3 }, |
| 565 |
expect = { 'X bar X X baz foooo foofoo fo', 3 } |
| 566 |
}, |
| 567 |
{ name = 'gsub: (string 4)', func = mw.ustring.gsub, |
| 568 |
args = { str3, 'f%a+', 'X', 3 }, |
| 569 |
expect = { '??? X bar X X baz foooo foofoo fo ok?', 3 } |
| 570 |
}, |
| 571 |
{ name = 'gsub: (string 5)', func = mw.ustring.gsub, |
| 572 |
args = { 'foo; fóó', '(f)(%a+)', '%%0=%0 %%1=%1 %%2=%2' }, |
| 573 |
expect = { '%0=foo %1=f %2=oo; %0=fóó %1=f %2=óó', 2 } |
| 574 |
}, |
| 575 |
{ name = 'gsub: string, undocumented behavior where %1 works as %0 if there are no captures', func = mw.ustring.gsub, |
| 576 |
args = { 'foo; fóó', '%a+', '%1!' }, |
| 577 |
expect = { 'foo!; fóó!', 2 } |
| 578 |
}, |
| 579 |
{ name = 'gsub: (anchored)', func = mw.ustring.gsub, |
| 580 |
args = { 'foofoofoo foo', '^foo', 'X' }, |
| 581 |
expect = { 'Xfoofoo foo', 1 } |
| 582 |
}, |
| 583 |
{ name = 'gsub: (table 1)', func = mw.ustring.gsub, |
| 584 |
args = { str2, 'f%a+', { foo = 'X', ['főó'] = 'Y', ['foó'] = 'Z' } }, |
| 585 |
expect = { 'X bar Y Z baz foooo foofoo fo', 6 } |
| 586 |
}, |
| 587 |
{ name = 'gsub: (table 2)', func = mw.ustring.gsub, |
| 588 |
args = { str3, 'f%a+', { foo = 'X', ['főó'] = 'Y', ['foó'] = 'Z' } }, |
| 589 |
expect = { '??? X bar Y Z baz foooo foofoo fo ok?', 6 } |
| 590 |
}, |
| 591 |
{ name = 'gsub: (table 3)', func = mw.ustring.gsub, |
| 592 |
args = { str2, 'f%a+', { ['főó'] = 'Y', ['foó'] = 'Z' }, 1 }, |
| 593 |
expect = { str2, 1 } |
| 594 |
}, |
| 595 |
{ name = 'gsub: (table 4)', func = mw.ustring.gsub, |
| 596 |
args = { str3, 'f(%a+)', { oo = 'X', ['őó'] = 'Y', ['oó'] = 'Z' } }, |
| 597 |
expect = { '??? X bar Y Z baz foooo foofoo fo ok?', 6 } |
| 598 |
}, |
| 599 |
{ name = 'gsub: (table 5)', func = mw.ustring.gsub, |
| 600 |
args = { str3, '(f)(%a+)', { f = 'F', oo = 'X', ['őó'] = 'Y', ['oó'] = 'Z' } }, |
| 601 |
expect = { '??? F bar F F baz F F F ok?', 6 } |
| 602 |
}, |
| 603 |
{ name = 'gsub: (inverted zero character class)', func = mw.ustring.gsub, |
| 604 |
args = { "ó", '%Z', 'repl' }, |
| 605 |
expect = { 'repl', 1 } |
| 606 |
}, |
| 607 |
{ name = 'gsub: (single dot pattern at end)', func = mw.ustring.gsub, |
| 608 |
args = { "ó", '.', 'repl' }, |
| 609 |
expect = { 'repl', 1 } |
| 610 |
}, |
| 611 |
{ name = 'gsub: (single dot pattern at end + leading)', func = mw.ustring.gsub, |
| 612 |
args = { 'fó', 'f.', 'repl' }, |
| 613 |
expect = { 'repl', 1 } |
| 614 |
}, |
| 615 |
{ name = 'gsub: (dot pattern)', func = mw.ustring.gsub, |
| 616 |
args = { 'f ó b', 'f . b', 'repl' }, |
| 617 |
expect = { 'repl', 1 } |
| 618 |
}, |
| 619 |
{ name = 'gsub: (dot pattern with +)', func = mw.ustring.gsub, |
| 620 |
args = { 'f óóó b', 'f .+ b', 'repl' }, |
| 621 |
expect = { 'repl', 1 } |
| 622 |
}, |
| 623 |
{ name = 'gsub: (dot pattern with -)', func = mw.ustring.gsub, |
| 624 |
args = { 'f óóó b', 'f .- b', 'repl' }, |
| 625 |
expect = { 'repl', 1 } |
| 626 |
}, |
| 627 |
{ name = 'gsub: (dot pattern with *)', func = mw.ustring.gsub, |
| 628 |
args = { 'f óóó b', 'f .* b', 'repl' }, |
| 629 |
expect = { 'repl', 1 } |
| 630 |
}, |
| 631 |
{ name = 'gsub: (function 1)', func = mw.ustring.gsub, |
| 632 |
args = { str2, 'f%a+', function(m) if m == 'fo' then return nil end return '-' .. mw.ustring.upper(m) .. '-' end }, |
| 633 |
expect = { '-FOO- bar -FŐÓ- -FOÓ- baz -FOOOO- -FOOFOO- fo', 6 } |
| 634 |
}, |
| 635 |
{ name = 'gsub: (function 2)', func = mw.ustring.gsub, |
| 636 |
args = { str3, 'f%a+', function(m) if m == 'fo' then return nil end return '-' .. mw.ustring.upper(m) .. '-' end }, |
| 637 |
expect = { '??? -FOO- bar -FŐÓ- -FOÓ- baz -FOOOO- -FOOFOO- fo ok?', 6 } |
| 638 |
}, |
| 639 |
{ name = 'gsub: (function 3)', func = mw.ustring.gsub, |
| 640 |
args = { str3, '(f)(%a+)', function(m1, m2) if m2 == 'o' then return nil end return '-' .. m1 .. mw.ustring.upper(m2) .. '-' end }, |
| 641 |
expect = { '??? -fOO- bar -fŐÓ- -fOÓ- baz -fOOOO- -fOOFOO- fo ok?', 6 } |
| 642 |
}, |
| 643 |
{ name = 'gsub: invalid replacement string', func = mw.ustring.gsub, |
| 644 |
args = { 'foo; fóó', '(%a+)', '%2' }, |
| 645 |
expect = "invalid capture index %2 in replacement string" |
| 646 |
}, |
| 647 |
{ name = 'gsub: passing numbers instead of strings (1)', func = mw.ustring.gsub, |
| 648 |
args = { 12345, '[33]', '9' }, |
| 649 |
expect = { '12945', 1 } |
| 650 |
}, |
| 651 |
{ name = 'gsub: passing numbers instead of strings (2)', func = mw.ustring.gsub, |
| 652 |
args = { '12345', 3, '9' }, |
| 653 |
expect = { '12945', 1 } |
| 654 |
}, |
| 655 |
{ name = 'gsub: passing numbers instead of strings (3)', func = mw.ustring.gsub, |
| 656 |
args = { '12345', '[33]', 9 }, |
| 657 |
expect = { '12945', 1 } |
| 658 |
}, |
| 659 |
{ name = 'gsub: table replacement with a bad type (boolean)', func = mw.ustring.gsub, |
| 660 |
args = { 'abc', 'b', { b = true } }, |
| 661 |
expect = 'invalid replacement value (a boolean)' |
| 662 |
}, |
| 663 |
{ name = 'gsub: table replacement with a bad type (table)', func = mw.ustring.gsub, |
| 664 |
args = { 'abc', 'b', { b = {} } }, |
| 665 |
expect = 'invalid replacement value (a table)' |
| 666 |
}, |
| 667 |
{ name = 'gsub: table replacement with a bad type (function)', func = mw.ustring.gsub, |
| 668 |
args = { 'abc', 'b', { b = function () end } }, |
| 669 |
expect = 'invalid replacement value (a function)' |
| 670 |
}, |
| 671 |
{ name = 'gsub: function replacement with a bad type (boolean)', func = mw.ustring.gsub, |
| 672 |
args = { 'abc', 'b', function () return true end }, |
| 673 |
expect = 'invalid replacement value (a boolean)' |
| 674 |
}, |
| 675 |
{ name = 'gsub: function replacement with a bad type (table)', func = mw.ustring.gsub, |
| 676 |
args = { 'abc', 'b', function () return {} end }, |
| 677 |
expect = 'invalid replacement value (a table)' |
| 678 |
}, |
| 679 |
{ name = 'gsub: function replacement with a bad type (function)', func = mw.ustring.gsub, |
| 680 |
args = { 'abc', 'b', function () return function () end end }, |
| 681 |
expect = 'invalid replacement value (a function)' |
| 682 |
}, |
| 683 |
|
| 684 |
{ name = 'gcodepoint: basic test', func = mw.ustring.gcodepoint, |
| 685 |
args = { str1 }, |
| 686 |
expect = { { 0 }, { 0x7f }, { 0x80 }, { 0x7ff }, { 0x800 }, { 0xffff }, { 0x10000 }, { 0x10ffff } }, |
| 687 |
type = 'Iterator' |
| 688 |
}, |
| 689 |
{ name = 'gcodepoint: (4)', func = mw.ustring.gcodepoint, |
| 690 |
args = { str1, 4 }, |
| 691 |
expect = { { 0x7ff }, { 0x800 }, { 0xffff }, { 0x10000 }, { 0x10ffff } }, |
| 692 |
type = 'Iterator' |
| 693 |
}, |
| 694 |
{ name = 'gcodepoint: (4, -2)', func = mw.ustring.gcodepoint, |
| 695 |
args = { str1, 4, -2 }, |
| 696 |
expect = { { 0x7ff }, { 0x800 }, { 0xffff }, { 0x10000 } }, |
| 697 |
type = 'Iterator' |
| 698 |
}, |
| 699 |
{ name = 'gcodepoint: (4, 3)', func = mw.ustring.gcodepoint, |
| 700 |
args = { str1, 4, 3 }, |
| 701 |
expect = {}, |
| 702 |
type = 'Iterator' |
| 703 |
}, |
| 704 |
{ name = 'gcodepoint: (1, 0)', func = mw.ustring.gcodepoint, |
| 705 |
args = { str1, 1, 0 }, |
| 706 |
expect = {}, |
| 707 |
type = 'Iterator' |
| 708 |
}, |
| 709 |
{ name = 'gcodepoint: (9, 9)', func = mw.ustring.gcodepoint, |
| 710 |
args = { str1, 9, 9 }, |
| 711 |
expect = {}, |
| 712 |
type = 'Iterator' |
| 713 |
}, |
| 714 |
{ name = 'gcodepoint: really long string', func = testLongGcodepoint, |
| 715 |
args = {}, |
| 716 |
expect = { { |
| 717 |
[1] = 0x66, [2] = 0x151, [3] = 0xf3, [4] = 0x20, |
| 718 |
[9997] = 0x66, [9998] = 0x151, [9999] = 0xf3, [10000] = 0x20, |
| 719 |
} }, |
| 720 |
}, |
| 721 |
|
| 722 |
{ name = 'gmatch: test string 1', func = mw.ustring.gmatch, |
| 723 |
args = { str2, 'f%a+' }, |
| 724 |
expect = { { 'foo' }, { 'főó' }, { 'foó' }, { 'foooo' }, { 'foofoo' }, { 'fo' } }, |
| 725 |
type = 'Iterator' |
| 726 |
}, |
| 727 |
{ name = 'gmatch: test string 2', func = mw.ustring.gmatch, |
| 728 |
args = { str3, 'f%a+' }, |
| 729 |
expect = { { 'foo' }, { 'főó' }, { 'foó' }, { 'foooo' }, { 'foofoo' }, { 'fo' } }, |
| 730 |
type = 'Iterator' |
| 731 |
}, |
| 732 |
{ name = 'gmatch: anchored', func = mw.ustring.gmatch, |
| 733 |
args = { "fóó1 ^fóó2 fóó3 ^fóó4", '^fóó%d+' }, |
| 734 |
expect = { { "^fóó2" }, { "^fóó4" } }, |
| 735 |
type = 'Iterator' |
| 736 |
}, |
| 737 |
|
| 738 |
{ name = 'find: Pure-lua version, non-native error message', func = mw.ustring.find, |
| 739 |
args = { "fóó", '[]' }, |
| 740 |
expect = "Missing close-bracket for character set beginning at pattern character 1" |
| 741 |
}, |
| 742 |
{ name = 'match: Pure-lua version, non-native error message', func = mw.ustring.match, |
| 743 |
args = { "fóó", '[]' }, |
| 744 |
expect = "Missing close-bracket for character set beginning at pattern character 1" |
| 745 |
}, |
| 746 |
{ name = 'gsub: Pure-lua version, non-native error message', func = mw.ustring.gsub, |
| 747 |
args = { "fóó", '[]', '' }, |
| 748 |
expect = "Missing close-bracket for character set beginning at pattern character 1" |
| 749 |
}, |
| 750 |
|
| 751 |
{ name = 'string length limit', |
| 752 |
func = function () |
| 753 |
local s = string.rep( "x", mw.ustring.maxStringLength + 1 ) |
| 754 |
local ret = { mw.ustring.gsub( s, 'a', 'b' ) } |
| 755 |
|
| 756 |
ret[1] = string.gsub( ret[1], 'xxxxx(x*)', function ( m ) |
| 757 |
return 'xxxxx[snip ' .. #m .. ' more]' |
| 758 |
end ) |
| 759 |
return unpack( ret ) |
| 760 |
end, |
| 761 |
expect = "bad argument #1 to 'gsub' (string is longer than " .. mw.ustring.maxStringLength .. " bytes)" |
| 762 |
}, |
| 763 |
{ name = 'pattern length limit', |
| 764 |
func = function () |
| 765 |
local pattern = string.rep( "x", mw.ustring.maxPatternLength + 1 ) |
| 766 |
return mw.ustring.gsub( 'a', pattern, 'b' ) |
| 767 |
end, |
| 768 |
expect = "bad argument #2 to 'gsub' (pattern is longer than " .. mw.ustring.maxPatternLength .. " bytes)" |
| 769 |
}, |
| 770 |
} ) |