| 1 |
local testframework = require 'Module:TestFramework' |
| 2 |
|
| 3 |
|
| 4 |
local tagattrs = { absent = false, present = true, key = 'value', n = 42 } |
| 5 |
setmetatable( tagattrs, { __pairs = function ( t ) |
| 6 |
local keys = { 'absent', 'present', 'key', 'n' } |
| 7 |
local i = 0 |
| 8 |
return function() |
| 9 |
i = i + 1 |
| 10 |
if i <= #keys then |
| 11 |
return keys[i], t[keys[i]] |
| 12 |
end |
| 13 |
end |
| 14 |
end } ) |
| 15 |
|
| 16 |
|
| 17 |
mw.text.stripTest = mw.text.stripTest or { nowiki = '!!!', general = '!!!', ppnowiki = '!!!' } |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
local function stripTest( func, marker, arg ) |
| 22 |
local result = func( marker, arg ) |
| 23 |
if result == marker then |
| 24 |
result = 'strip-marker' |
| 25 |
end |
| 26 |
return result |
| 27 |
end |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
function jsonRoundTripTest( tree ) |
| 32 |
return mw.text.jsonDecode( mw.text.jsonEncode( tree ) ) |
| 33 |
end |
| 34 |
|
| 35 |
local recursiveTable = {} |
| 36 |
recursiveTable.recursiveTable = recursiveTable |
| 37 |
|
| 38 |
|
| 39 |
local tests = { |
| 40 |
{ name = 'trim', |
| 41 |
func = mw.text.trim, args = { ' foo bar ' }, |
| 42 |
expect = { 'foo bar' } |
| 43 |
}, |
| 44 |
{ name = 'trim right', |
| 45 |
func = mw.text.trim, args = { 'foo bar ' }, |
| 46 |
expect = { 'foo bar' } |
| 47 |
}, |
| 48 |
{ name = 'trim left', |
| 49 |
func = mw.text.trim, args = { ' foo bar' }, |
| 50 |
expect = { 'foo bar' } |
| 51 |
}, |
| 52 |
{ name = 'trim none', |
| 53 |
func = mw.text.trim, args = { 'foo bar' }, |
| 54 |
expect = { 'foo bar' } |
| 55 |
}, |
| 56 |
{ name = 'trim charset', |
| 57 |
func = mw.text.trim, args = { 'xxx foo bar xxx', 'x' }, |
| 58 |
expect = { ' foo bar ' } |
| 59 |
}, |
| 60 |
|
| 61 |
{ name = 'encode', |
| 62 |
func = mw.text.encode, args = { '<b>foo\194\160"bar"</b> & \'baz\'' }, |
| 63 |
expect = { '<b>foo "bar"</b> & 'baz'' } |
| 64 |
}, |
| 65 |
{ name = 'encode charset', |
| 66 |
func = mw.text.encode, args = { '<b>foo\194\160"bar"</b> & \'baz\'', 'aeiou' }, |
| 67 |
expect = { '<b>foo\194\160"bar"</b> & \'baz\'' } |
| 68 |
}, |
| 69 |
|
| 70 |
{ name = 'decode', |
| 71 |
func = mw.text.decode, |
| 72 |
args = { '<>&" foo foo ♥ &quot;' }, |
| 73 |
expect = { '<>&" foo foo ♥ "' } |
| 74 |
}, |
| 75 |
{ name = 'decode named', |
| 76 |
func = mw.text.decode, |
| 77 |
args = { '<>&" foo foo ♥ &quot;', true }, |
| 78 |
expect = { '<>&" foo foo ♥ "' } |
| 79 |
}, |
| 80 |
|
| 81 |
{ name = 'nowiki', |
| 82 |
func = mw.text.nowiki, |
| 83 |
args = { '*"&\'<=>[]{|}#*:;\n*\n#\n:\n;\nhttp://example.com:80/\nRFC 123, ISBN 456' }, |
| 84 |
expect = { |
| 85 |
'*"&'<=>[]{|}#*:;' .. |
| 86 |
'\n*\n#\n:\n;\nhttp://example.com:80/' .. |
| 87 |
'\nRFC 123, ISBN 456' |
| 88 |
} |
| 89 |
}, |
| 90 |
|
| 91 |
|
| 92 |
{ name = 'nowiki noescapes', |
| 93 |
func = mw.text.nowiki, |
| 94 |
args = { 'a' }, |
| 95 |
expect = { |
| 96 |
'a' |
| 97 |
} |
| 98 |
}, |
| 99 |
{ name = 'nowiki braces and brackets', |
| 100 |
func = mw.text.nowiki, |
| 101 |
args = { '[[WikiLink]] {{Template}} <html>' }, |
| 102 |
expect = { |
| 103 |
'[[WikiLink]] {{Template}} <html>' |
| 104 |
} |
| 105 |
}, |
| 106 |
{ name = 'nowiki quotes', |
| 107 |
func = mw.text.nowiki, |
| 108 |
args = { '"' .. "'" }, |
| 109 |
expect = { '"'' }, |
| 110 |
}, |
| 111 |
{ name = 'nowiki tokens', |
| 112 |
func = mw.text.nowiki, |
| 113 |
args = { '{| {- {+ !! ~~~~~ __FOO__' }, |
| 114 |
expect = { |
| 115 |
'{| {- {+ !! ~~~~~ __FOO__' |
| 116 |
}, |
| 117 |
}, |
| 118 |
{ name = 'nowiki start of line', |
| 119 |
func = mw.text.nowiki, |
| 120 |
args = { '* foo\n! bar\n# bat\n:baz\n pre\n----' }, |
| 121 |
expect = { |
| 122 |
'* foo\n! bar\n# bat\n:baz\n pre\n----', |
| 123 |
}, |
| 124 |
}, |
| 125 |
{ name = 'nowiki paragraph separators', |
| 126 |
func = mw.text.nowiki, |
| 127 |
args = { 'a\n\n\n\nb' }, |
| 128 |
expect = { 'a\n \n b' }, |
| 129 |
}, |
| 130 |
{ name = 'nowiki language converter', |
| 131 |
func = mw.text.nowiki, |
| 132 |
args = { '-{ foo ; bar }-' }, |
| 133 |
expect = { '-{ foo ; bar }-' }, |
| 134 |
}, |
| 135 |
{ name = 'nowiki left-side context: |+', |
| 136 |
func = mw.text.nowiki, |
| 137 |
args = { '+ foo + bar' }, |
| 138 |
expect = { '+ foo + bar' }, |
| 139 |
}, |
| 140 |
{ name = 'nowiki left-side context: |-', |
| 141 |
func = mw.text.nowiki, |
| 142 |
args = { '- foo - bar' }, |
| 143 |
expect = { '- foo - bar' }, |
| 144 |
}, |
| 145 |
{ name = 'nowiki left-side context: __FOO__', |
| 146 |
func = mw.text.nowiki, |
| 147 |
args = { '_FOO__' }, |
| 148 |
expect = { '_FOO__' }, |
| 149 |
}, |
| 150 |
{ name = 'nowiki left-side context: ~~~', |
| 151 |
func = mw.text.nowiki, |
| 152 |
args = { '~~ long string here' }, |
| 153 |
expect = { '~~ long string here' }, |
| 154 |
}, |
| 155 |
{ name = 'nowiki left-side context: newlines', |
| 156 |
func = mw.text.nowiki, |
| 157 |
args = { '\n\n\nFoo' }, |
| 158 |
expect = { ' \n Foo' }, |
| 159 |
}, |
| 160 |
{ name = 'nowiki right-side context: ~~~', |
| 161 |
func = mw.text.nowiki, |
| 162 |
args = { 'long string here ~~' }, |
| 163 |
expect = { 'long string here ~~' }, |
| 164 |
}, |
| 165 |
{ name = 'nowiki right-side context: __FOO__', |
| 166 |
func = mw.text.nowiki, |
| 167 |
args = { '__FOO_' }, |
| 168 |
expect = { '__FOO_' }, |
| 169 |
}, |
| 170 |
{ name = 'nowiki right-side context: newlines', |
| 171 |
func = mw.text.nowiki, |
| 172 |
args = { 'foo\n\n\n' }, |
| 173 |
expect = { 'foo\n ' }, |
| 174 |
}, |
| 175 |
|
| 176 |
{ name = 'tag, simple', |
| 177 |
func = mw.text.tag, |
| 178 |
args = { { name = 'b' } }, |
| 179 |
expect = { '<b>' } |
| 180 |
}, |
| 181 |
{ name = 'tag, simple with content', |
| 182 |
func = mw.text.tag, |
| 183 |
args = { { name = 'b', content = 'foo' } }, |
| 184 |
expect = { '<b>foo</b>' } |
| 185 |
}, |
| 186 |
{ name = 'tag, simple self-closing', |
| 187 |
func = mw.text.tag, |
| 188 |
args = { { name = 'br', content = false } }, |
| 189 |
expect = { '<br />' } |
| 190 |
}, |
| 191 |
{ name = 'tag, args', |
| 192 |
func = mw.text.tag, |
| 193 |
args = { { name = 'b', attrs = tagattrs } }, |
| 194 |
expect = { '<b present key="value" n="42">' } |
| 195 |
}, |
| 196 |
{ name = 'tag, args with content', |
| 197 |
func = mw.text.tag, |
| 198 |
args = { { name = 'b', attrs = tagattrs, content = 'foo' } }, |
| 199 |
expect = { '<b present key="value" n="42">foo</b>' } |
| 200 |
}, |
| 201 |
{ name = 'tag, args self-closing', |
| 202 |
func = mw.text.tag, |
| 203 |
args = { { name = 'br', attrs = tagattrs, content = false } }, |
| 204 |
expect = { '<br present key="value" n="42" />' } |
| 205 |
}, |
| 206 |
{ name = 'tag, args, positional params', |
| 207 |
func = mw.text.tag, |
| 208 |
args = { 'b', tagattrs }, |
| 209 |
expect = { '<b present key="value" n="42">' } |
| 210 |
}, |
| 211 |
{ name = 'tag, args with content, positional params', |
| 212 |
func = mw.text.tag, |
| 213 |
args = { 'b', tagattrs, 'foo' }, |
| 214 |
expect = { '<b present key="value" n="42">foo</b>' } |
| 215 |
}, |
| 216 |
|
| 217 |
{ name = 'unstrip (nowiki)', |
| 218 |
func = stripTest, |
| 219 |
args = { mw.text.unstrip, mw.text.stripTest.nowiki }, |
| 220 |
expect = { 'NoWiki' } |
| 221 |
}, |
| 222 |
{ name = 'unstrip (general)', |
| 223 |
func = stripTest, |
| 224 |
args = { mw.text.unstrip, mw.text.stripTest.general }, |
| 225 |
expect = { '' } |
| 226 |
}, |
| 227 |
{ name = 'unstrip (pp-nowiki)', |
| 228 |
func = stripTest, |
| 229 |
args = { mw.text.unstrip, mw.text.stripTest.ppnowiki }, |
| 230 |
expect = { '<nowiki>PP-NoWiki</nowiki>' } |
| 231 |
}, |
| 232 |
|
| 233 |
{ name = 'unstripNoWiki (nowiki)', |
| 234 |
func = stripTest, |
| 235 |
args = { mw.text.unstripNoWiki, mw.text.stripTest.nowiki }, |
| 236 |
expect = { 'NoWiki' } |
| 237 |
}, |
| 238 |
{ name = 'unstripNoWiki (pp-nowiki)', |
| 239 |
func = stripTest, |
| 240 |
args = { mw.text.unstripNoWiki, mw.text.stripTest.ppnowiki }, |
| 241 |
expect = { 'PP-NoWiki' } |
| 242 |
}, |
| 243 |
{ name = 'unstripNoWiki (pp-nowiki-original)', |
| 244 |
func = stripTest, |
| 245 |
args = { mw.text.unstripNoWiki, mw.text.stripTest.ppnowiki, true }, |
| 246 |
expect = { '<nowiki>PP-NoWiki</nowiki>' } |
| 247 |
}, |
| 248 |
{ name = 'unstripNoWiki (general)', |
| 249 |
func = stripTest, |
| 250 |
args = { mw.text.unstripNoWiki, mw.text.stripTest.general }, |
| 251 |
expect = { 'strip-marker' } |
| 252 |
}, |
| 253 |
|
| 254 |
{ name = 'killMarkers', |
| 255 |
func = mw.text.killMarkers, |
| 256 |
args = { 'a' .. mw.text.stripTest.nowiki .. 'b' .. mw.text.stripTest.general .. 'c' }, |
| 257 |
expect = { 'abc' } |
| 258 |
}, |
| 259 |
|
| 260 |
{ name = 'split, simple', |
| 261 |
func = mw.text.split, args = { 'a,b,c,d', ',' }, |
| 262 |
expect = { { 'a', 'b', 'c', 'd' } } |
| 263 |
}, |
| 264 |
{ name = 'split, no separator', |
| 265 |
func = mw.text.split, args = { 'xxx', ',' }, |
| 266 |
expect = { { 'xxx' } } |
| 267 |
}, |
| 268 |
{ name = 'split, empty string', |
| 269 |
func = mw.text.split, args = { '', ',' }, |
| 270 |
expect = { { '' } } |
| 271 |
}, |
| 272 |
{ name = 'split, with empty items', |
| 273 |
func = mw.text.split, args = { ',,', ',' }, |
| 274 |
expect = { { '', '', '' } } |
| 275 |
}, |
| 276 |
{ name = 'split, with empty items (1)', |
| 277 |
func = mw.text.split, args = { 'x,,', ',' }, |
| 278 |
expect = { { 'x', '', '' } } |
| 279 |
}, |
| 280 |
{ name = 'split, with empty items (2)', |
| 281 |
func = mw.text.split, args = { ',x,', ',' }, |
| 282 |
expect = { { '', 'x', '' } } |
| 283 |
}, |
| 284 |
{ name = 'split, with empty items (3)', |
| 285 |
func = mw.text.split, args = { ',,x', ',' }, |
| 286 |
expect = { { '', '', 'x' } } |
| 287 |
}, |
| 288 |
{ name = 'split, with empty items (4)', |
| 289 |
func = mw.text.split, args = { ',x,x', ',' }, |
| 290 |
expect = { { '', 'x', 'x' } } |
| 291 |
}, |
| 292 |
{ name = 'split, with empty items (5)', |
| 293 |
func = mw.text.split, args = { 'x,,x', ',' }, |
| 294 |
expect = { { 'x', '', 'x' } } |
| 295 |
}, |
| 296 |
{ name = 'split, with empty items (7)', |
| 297 |
func = mw.text.split, args = { 'x,x,', ',' }, |
| 298 |
expect = { { 'x', 'x', '' } } |
| 299 |
}, |
| 300 |
{ name = 'split, with empty pattern', |
| 301 |
func = mw.text.split, args = { 'xxx', '' }, |
| 302 |
expect = { { 'x', 'x', 'x' } } |
| 303 |
}, |
| 304 |
{ name = 'split, with empty pattern (2)', |
| 305 |
func = mw.text.split, args = { 'xxx', ',?' }, |
| 306 |
expect = { { 'x', 'x', 'x' } } |
| 307 |
}, |
| 308 |
|
| 309 |
{ name = 'listToText (0)', |
| 310 |
func = mw.text.listToText, args = { {} }, |
| 311 |
expect = { '' } |
| 312 |
}, |
| 313 |
{ name = 'listToText (1)', |
| 314 |
func = mw.text.listToText, args = { { 1 } }, |
| 315 |
expect = { '1' } |
| 316 |
}, |
| 317 |
{ name = 'listToText (2)', |
| 318 |
func = mw.text.listToText, args = { { 1, 2 } }, |
| 319 |
expect = { '1 and 2' } |
| 320 |
}, |
| 321 |
{ name = 'listToText (3)', |
| 322 |
func = mw.text.listToText, args = { { 1, 2, 3 } }, |
| 323 |
expect = { '1, 2 and 3' } |
| 324 |
}, |
| 325 |
{ name = 'listToText (4)', |
| 326 |
func = mw.text.listToText, args = { { 1, 2, 3, 4 } }, |
| 327 |
expect = { '1, 2, 3 and 4' } |
| 328 |
}, |
| 329 |
{ name = 'listToText, alternate separator', |
| 330 |
func = mw.text.listToText, args = { { 1, 2, 3, 4 }, '; ' }, |
| 331 |
expect = { '1; 2; 3 and 4' } |
| 332 |
}, |
| 333 |
{ name = 'listToText, alternate conjunction', |
| 334 |
func = mw.text.listToText, args = { { 1, 2, 3, 4 }, nil, ' or ' }, |
| 335 |
expect = { '1, 2, 3 or 4' } |
| 336 |
}, |
| 337 |
|
| 338 |
{ name = 'truncate, no truncation', |
| 339 |
func = mw.text.truncate, args = { 'foobarbaz', 9 }, |
| 340 |
expect = { 'foobarbaz' } |
| 341 |
}, |
| 342 |
{ name = 'truncate, no truncation (2)', |
| 343 |
func = mw.text.truncate, args = { 'foobarbaz', -9 }, |
| 344 |
expect = { 'foobarbaz' } |
| 345 |
}, |
| 346 |
{ name = 'truncate, tail truncation', |
| 347 |
func = mw.text.truncate, args = { 'foobarbaz', 3 }, |
| 348 |
expect = { 'foo...' } |
| 349 |
}, |
| 350 |
{ name = 'truncate, head truncation', |
| 351 |
func = mw.text.truncate, args = { 'foobarbaz', -3 }, |
| 352 |
expect = { '...baz' } |
| 353 |
}, |
| 354 |
{ name = 'truncate, avoid silly truncation', |
| 355 |
func = mw.text.truncate, args = { 'foobarbaz', 8 }, |
| 356 |
expect = { 'foobarbaz' } |
| 357 |
}, |
| 358 |
{ name = 'truncate, avoid silly truncation (2)', |
| 359 |
func = mw.text.truncate, args = { 'foobarbaz', 6 }, |
| 360 |
expect = { 'foobarbaz' } |
| 361 |
}, |
| 362 |
{ name = 'truncate, alternate ellipsis', |
| 363 |
func = mw.text.truncate, args = { 'foobarbaz', 3, '!' }, |
| 364 |
expect = { 'foo!' } |
| 365 |
}, |
| 366 |
{ name = 'truncate, with adjusted length', |
| 367 |
func = mw.text.truncate, args = { 'foobarbaz', 6, nil, true }, |
| 368 |
expect = { 'foo...' } |
| 369 |
}, |
| 370 |
{ name = 'truncate, with adjusted length (2)', |
| 371 |
func = mw.text.truncate, args = { 'foobarbaz', -6, nil, true }, |
| 372 |
expect = { '...baz' } |
| 373 |
}, |
| 374 |
{ name = 'truncate, ridiculously short', |
| 375 |
func = mw.text.truncate, args = { 'foobarbaz', 1, nil, true }, |
| 376 |
expect = { '...' } |
| 377 |
}, |
| 378 |
{ name = 'truncate, ridiculously short (2)', |
| 379 |
func = mw.text.truncate, args = { 'foobarbaz', -1, nil, true }, |
| 380 |
expect = { '...' } |
| 381 |
}, |
| 382 |
|
| 383 |
{ name = 'json encode-decode round trip, simple object', |
| 384 |
func = jsonRoundTripTest, |
| 385 |
args = { { |
| 386 |
int = 2, |
| 387 |
string = "foo", |
| 388 |
['true'] = true, |
| 389 |
['false'] = false, |
| 390 |
} }, |
| 391 |
expect = { { |
| 392 |
int = 2, |
| 393 |
string = "foo", |
| 394 |
['true'] = true, |
| 395 |
['false'] = false, |
| 396 |
} }, |
| 397 |
}, |
| 398 |
{ name = 'json decode, simple object', |
| 399 |
func = mw.text.jsonDecode, |
| 400 |
args = { '{"int":2,"string":"foo","true":true,"false":false}' }, |
| 401 |
expect = { { |
| 402 |
int = 2, |
| 403 |
string = "foo", |
| 404 |
['true'] = true, |
| 405 |
['false'] = false, |
| 406 |
} }, |
| 407 |
}, |
| 408 |
{ name = 'json encode, simple array', |
| 409 |
func = mw.text.jsonEncode, |
| 410 |
args = { { 1, "foo", true, false } }, |
| 411 |
expect = { '[1,"foo",true,false]' } |
| 412 |
}, |
| 413 |
{ name = 'json decode, simple array', |
| 414 |
func = mw.text.jsonDecode, |
| 415 |
args = { '[1,"foo",true,false]' }, |
| 416 |
expect = { { 1, "foo", true, false } } |
| 417 |
}, |
| 418 |
{ name = 'json encode-decode round trip, object with numeric keys', |
| 419 |
func = jsonRoundTripTest, |
| 420 |
args = { { x = "x", [1] = 1, [2] = 2 } }, |
| 421 |
expect = { { x = "x", [1] = 1, [2] = 2 } } |
| 422 |
}, |
| 423 |
{ name = 'json decode, object with numeric keys', |
| 424 |
func = mw.text.jsonDecode, |
| 425 |
args = { '{"x":"x","1":1,"2":2}' }, |
| 426 |
expect = { { x = "x", [1] = 1, [2] = 2 } } |
| 427 |
}, |
| 428 |
{ name = 'json encode, simple array, preserve keys', |
| 429 |
func = mw.text.jsonEncode, |
| 430 |
args = { { 1, "foo", true, false }, mw.text.JSON_PRESERVE_KEYS }, |
| 431 |
expect = { '{"1":1,"2":"foo","3":true,"4":false}' } |
| 432 |
}, |
| 433 |
{ name = 'json decode, simple array, preserve keys', |
| 434 |
func = mw.text.jsonDecode, |
| 435 |
args = { '[1,"foo",true,false]', mw.text.JSON_PRESERVE_KEYS }, |
| 436 |
expect = { { [0] = 1, "foo", true, false } } |
| 437 |
}, |
| 438 |
{ name = 'json encode, nested arrays', |
| 439 |
func = mw.text.jsonEncode, |
| 440 |
args = { { 1, 2, 3, { 4, 5, { 6, 7, 8 } } } }, |
| 441 |
expect = { '[1,2,3,[4,5,[6,7,8]]]' } |
| 442 |
}, |
| 443 |
{ name = 'json decode, nested arrays', |
| 444 |
func = mw.text.jsonDecode, |
| 445 |
args = { '[1,2,3,[4,5,[6,7,8]]]' }, |
| 446 |
expect = { { 1, 2, 3, { 4, 5, { 6, 7, 8 } } } } |
| 447 |
}, |
| 448 |
{ name = 'json encode, array in object', |
| 449 |
func = mw.text.jsonEncode, |
| 450 |
args = { { x = { 1, 2, { y = { 3, 4 } } } } }, |
| 451 |
expect = { '{"x":[1,2,{"y":[3,4]}]}' } |
| 452 |
}, |
| 453 |
{ name = 'json decode, array in object', |
| 454 |
func = mw.text.jsonDecode, |
| 455 |
args = { '{"x":[1,2,{"y":[3,4]}],"z":[5,6]}' }, |
| 456 |
expect = { { x = { 1, 2, { y = { 3, 4 } } }, z = { 5, 6 } } } |
| 457 |
}, |
| 458 |
{ name = 'json decode, empty array', |
| 459 |
func = mw.text.jsonDecode, |
| 460 |
args = { '[]' }, |
| 461 |
expect = { {} } |
| 462 |
}, |
| 463 |
{ name = 'json decode, empty object', |
| 464 |
func = mw.text.jsonDecode, |
| 465 |
args = { '{}' }, |
| 466 |
expect = { {} } |
| 467 |
}, |
| 468 |
{ name = 'json encode, object with one large numeric index', |
| 469 |
func = mw.text.jsonEncode, |
| 470 |
args = { { [1000] = 1 } }, |
| 471 |
expect = { '{"1000":1}' } |
| 472 |
}, |
| 473 |
{ name = 'json decode, object with one large numeric index', |
| 474 |
func = mw.text.jsonDecode, |
| 475 |
args = { '{"1000":1}' }, |
| 476 |
expect = { { [1000] = 1 } } |
| 477 |
}, |
| 478 |
{ name = 'json encode, array with holes (ideally would be "[1,2,nil,4]", but probably not worth worrying about)', |
| 479 |
func = mw.text.jsonEncode, |
| 480 |
args = { { 1, 2, nil, 4 } }, |
| 481 |
expect = { '{"1":1,"2":2,"4":4}' } |
| 482 |
}, |
| 483 |
{ name = 'json decode, array with null (ideally would somehow insist on having a [3] = nil element, but that\'s not easily possible)', |
| 484 |
func = mw.text.jsonDecode, |
| 485 |
args = { '[1,2,null,4]' }, |
| 486 |
expect = { { 1, 2, [4] = 4 } } |
| 487 |
}, |
| 488 |
{ name = 'json encode, empty table (could be either [] or {}, but change should be announced)', |
| 489 |
func = mw.text.jsonEncode, |
| 490 |
args = { {} }, |
| 491 |
expect = { '[]' } |
| 492 |
}, |
| 493 |
{ name = 'json encode, table with index 0 (technically wrong, but probably not worth working around)', |
| 494 |
func = mw.text.jsonEncode, |
| 495 |
args = { { [0] = "zero" } }, |
| 496 |
expect = { '["zero"]' } |
| 497 |
}, |
| 498 |
{ name = 'json decode, object with index 1 (technically wrong, but probably not worth working around)', |
| 499 |
func = mw.text.jsonDecode, |
| 500 |
args = { '{"1":"one"}' }, |
| 501 |
expect = { { 'one' } } |
| 502 |
}, |
| 503 |
{ name = 'json encode, pretty', |
| 504 |
func = mw.text.jsonEncode, |
| 505 |
args = { { 1, 2, 3, { 4, 5, { 6, 7, { x = 8 } } } }, mw.text.JSON_PRETTY }, |
| 506 |
expect = { [=[[ |
| 507 |
1, |
| 508 |
2, |
| 509 |
3, |
| 510 |
[ |
| 511 |
4, |
| 512 |
5, |
| 513 |
[ |
| 514 |
6, |
| 515 |
7, |
| 516 |
{ |
| 517 |
"x": 8 |
| 518 |
} |
| 519 |
] |
| 520 |
] |
| 521 |
]]=] } |
| 522 |
}, |
| 523 |
{ name = 'json encode, raw value (technically not allowed, but a common extension)', |
| 524 |
func = mw.text.jsonEncode, |
| 525 |
args = { "foo" }, |
| 526 |
expect = { '"foo"' } |
| 527 |
}, |
| 528 |
{ name = 'json decode, raw value (technically not allowed, but a common extension)', |
| 529 |
func = mw.text.jsonDecode, |
| 530 |
args = { '"foo"' }, |
| 531 |
expect = { 'foo' } |
| 532 |
}, |
| 533 |
{ name = 'json encode, sneaky nil injection (object)', |
| 534 |
func = mw.text.jsonEncode, |
| 535 |
args = { setmetatable( {}, { |
| 536 |
__pairs = function ( t ) |
| 537 |
return function ( t, k ) |
| 538 |
if k ~= "foo" then |
| 539 |
return "foo", nil |
| 540 |
end |
| 541 |
end, t, nil |
| 542 |
end, |
| 543 |
} ) }, |
| 544 |
expect = { '{"foo":null}' } |
| 545 |
}, |
| 546 |
{ name = 'json encode, sneaky nil injection (array)', |
| 547 |
func = mw.text.jsonEncode, |
| 548 |
args = { setmetatable( { "one", "two", nil, "four" }, { |
| 549 |
__pairs = function ( t ) |
| 550 |
return function ( t, k ) |
| 551 |
k = k and k + 1 or 1 |
| 552 |
if k <= 4 then |
| 553 |
return k, t[k] |
| 554 |
end |
| 555 |
end, t, nil |
| 556 |
end, |
| 557 |
} ) }, |
| 558 |
expect = { '["one","two",null,"four"]' } |
| 559 |
}, |
| 560 |
|
| 561 |
{ name = 'json encode, invalid values (inf)', |
| 562 |
func = mw.text.jsonEncode, |
| 563 |
args = { { 1/0 } }, |
| 564 |
expect = 'mw.text.jsonEncode: Cannot encode non-finite numbers' |
| 565 |
}, |
| 566 |
{ name = 'json encode, invalid values (nan)', |
| 567 |
func = mw.text.jsonEncode, |
| 568 |
args = { { 0/0 } }, |
| 569 |
expect = 'mw.text.jsonEncode: Cannot encode non-finite numbers' |
| 570 |
}, |
| 571 |
{ name = 'json encode, invalid values (function)', |
| 572 |
func = mw.text.jsonEncode, |
| 573 |
args = { { function () end } }, |
| 574 |
expect = 'mw.text.jsonEncode: Cannot encode type \'function\'' |
| 575 |
}, |
| 576 |
{ name = 'json encode, invalid values (recursive table)', |
| 577 |
func = mw.text.jsonEncode, |
| 578 |
args = { { recursiveTable } }, |
| 579 |
expect = 'mw.text.jsonEncode: Cannot use recursive tables' |
| 580 |
}, |
| 581 |
{ name = 'json encode, invalid values (table with bool key)', |
| 582 |
func = mw.text.jsonEncode, |
| 583 |
args = { { [true] = 1 } }, |
| 584 |
expect = 'mw.text.jsonEncode: Cannot use type \'boolean\' as a table key' |
| 585 |
}, |
| 586 |
{ name = 'json encode, invalid values (table with function key)', |
| 587 |
func = mw.text.jsonEncode, |
| 588 |
args = { { [function() end] = 1 } }, |
| 589 |
expect = 'mw.text.jsonEncode: Cannot use type \'function\' as a table key' |
| 590 |
}, |
| 591 |
{ name = 'json encode, invalid values (table with inf key)', |
| 592 |
func = mw.text.jsonEncode, |
| 593 |
args = { { [1/0] = 1 } }, |
| 594 |
expect = 'mw.text.jsonEncode: Cannot use \'inf\' as a table key' |
| 595 |
}, |
| 596 |
|
| 597 |
{ name = 'json decode, invalid values (trailing comma)', |
| 598 |
func = mw.text.jsonDecode, |
| 599 |
args = { '{"x":1,}' }, |
| 600 |
expect = 'mw.text.jsonDecode: Syntax error' |
| 601 |
}, |
| 602 |
{ name = 'json decode, trailing comma with JSON_TRY_FIXING', |
| 603 |
func = mw.text.jsonDecode, |
| 604 |
args = { '{"x":1,}', mw.text.JSON_TRY_FIXING }, |
| 605 |
expect = { { x = 1 } } |
| 606 |
}, |
| 607 |
} |
| 608 |
|
| 609 |
return testframework.getTestProvider( tests ) |