diff options
| author | leaf corcoran <leafot@gmail.com> | 2019-09-21 19:22:24 -0700 |
|---|---|---|
| committer | leaf <leafot@gmail.com> | 2020-04-25 14:24:18 -0700 |
| commit | 96187b423dbec0d502bb1c4200094527458d83c2 (patch) | |
| tree | d8e768f595626b2bdbf11f966d770d4663b516a9 | |
| parent | 3882fac81a08c24c948faafe980dd181a906fa00 (diff) | |
| download | lua-cjson-96187b423dbec0d502bb1c4200094527458d83c2.tar.gz lua-cjson-96187b423dbec0d502bb1c4200094527458d83c2.tar.bz2 lua-cjson-96187b423dbec0d502bb1c4200094527458d83c2.zip | |
support error message testing for modern versions of lua
| -rwxr-xr-x | tests/test.lua | 68 |
1 files changed, 56 insertions, 12 deletions
diff --git a/tests/test.lua b/tests/test.lua index b2162b9..aeded76 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
| @@ -294,7 +294,7 @@ local cjson_tests = { | |||
| 294 | { "Encode sparse array as object", | 294 | { "Encode sparse array as object", |
| 295 | json.encode, { { [1] = "one", [5] = "sparse test" } }, | 295 | json.encode, { { [1] = "one", [5] = "sparse test" } }, |
| 296 | true, { | 296 | true, { |
| 297 | util.one_of{ | 297 | util.one_of { |
| 298 | '{"5":"sparse test","1":"one"}', | 298 | '{"5":"sparse test","1":"one"}', |
| 299 | '{"1":"one","5":"sparse test"}' | 299 | '{"1":"one","5":"sparse test"}' |
| 300 | } | 300 | } |
| @@ -363,32 +363,71 @@ local cjson_tests = { | |||
| 363 | json.encode_keep_buffer, { true }, true, { true } }, | 363 | json.encode_keep_buffer, { true }, true, { true } }, |
| 364 | 364 | ||
| 365 | -- Test config API errors | 365 | -- Test config API errors |
| 366 | -- Function is listed as '?' due to pcall | 366 | -- Function is listed as '?' due to pcall for older versions of Lua |
| 367 | { "Set encode_number_precision(0) [throw error]", | 367 | { "Set encode_number_precision(0) [throw error]", |
| 368 | json.encode_number_precision, { 0 }, | 368 | json.encode_number_precision, { 0 }, |
| 369 | false, { "bad argument #1 to '?' (expected integer between 1 and 16)" } }, | 369 | false, { |
| 370 | util.one_of { | ||
| 371 | "bad argument #1 to '?' (expected integer between 1 and 16)", | ||
| 372 | "bad argument #1 to 'cjson.encode_number_precision' (expected integer between 1 and 16)" | ||
| 373 | } | ||
| 374 | } }, | ||
| 370 | { "Set encode_number_precision(\"five\") [throw error]", | 375 | { "Set encode_number_precision(\"five\") [throw error]", |
| 371 | json.encode_number_precision, { "five" }, | 376 | json.encode_number_precision, { "five" }, |
| 372 | false, { "bad argument #1 to '?' (number expected, got string)" } }, | 377 | false, { |
| 378 | util.one_of { | ||
| 379 | "bad argument #1 to '?' (number expected, got string)", | ||
| 380 | "bad argument #1 to 'cjson.encode_number_precision' (number expected, got string)" | ||
| 381 | } | ||
| 382 | } }, | ||
| 373 | { "Set encode_keep_buffer(nil, true) [throw error]", | 383 | { "Set encode_keep_buffer(nil, true) [throw error]", |
| 374 | json.encode_keep_buffer, { nil, true }, | 384 | json.encode_keep_buffer, { nil, true }, |
| 375 | false, { "bad argument #2 to '?' (found too many arguments)" } }, | 385 | false, { |
| 386 | util.one_of { | ||
| 387 | "bad argument #2 to '?' (found too many arguments)", | ||
| 388 | "bad argument #2 to 'cjson.encode_keep_buffer' (found too many arguments)" | ||
| 389 | } | ||
| 390 | } }, | ||
| 376 | { "Set encode_max_depth(\"wrong\") [throw error]", | 391 | { "Set encode_max_depth(\"wrong\") [throw error]", |
| 377 | json.encode_max_depth, { "wrong" }, | 392 | json.encode_max_depth, { "wrong" }, |
| 378 | false, { "bad argument #1 to '?' (number expected, got string)" } }, | 393 | false, { |
| 394 | util.one_of { | ||
| 395 | "bad argument #1 to '?' (number expected, got string)", | ||
| 396 | "bad argument #1 to 'cjson.encode_max_depth' (number expected, got string)" | ||
| 397 | } | ||
| 398 | } }, | ||
| 379 | { "Set decode_max_depth(0) [throw error]", | 399 | { "Set decode_max_depth(0) [throw error]", |
| 380 | json.decode_max_depth, { "0" }, | 400 | json.decode_max_depth, { "0" }, |
| 381 | false, { "bad argument #1 to '?' (expected integer between 1 and 2147483647)" } }, | 401 | false, { |
| 402 | util.one_of { | ||
| 403 | "bad argument #1 to '?' (expected integer between 1 and 2147483647)", | ||
| 404 | "bad argument #1 to 'cjson.decode_max_depth' (expected integer between 1 and 2147483647)" | ||
| 405 | } | ||
| 406 | } }, | ||
| 382 | { "Set encode_invalid_numbers(-2) [throw error]", | 407 | { "Set encode_invalid_numbers(-2) [throw error]", |
| 383 | json.encode_invalid_numbers, { -2 }, | 408 | json.encode_invalid_numbers, { -2 }, |
| 384 | false, { "bad argument #1 to '?' (invalid option '-2')" } }, | 409 | false, { |
| 410 | util.one_of { | ||
| 411 | "bad argument #1 to '?' (invalid option '-2')", | ||
| 412 | "bad argument #1 to 'cjson.encode_invalid_numbers' (invalid option '-2')" | ||
| 413 | } | ||
| 414 | } }, | ||
| 385 | { "Set decode_invalid_numbers(true, false) [throw error]", | 415 | { "Set decode_invalid_numbers(true, false) [throw error]", |
| 386 | json.decode_invalid_numbers, { true, false }, | 416 | json.decode_invalid_numbers, { true, false }, |
| 387 | false, { "bad argument #2 to '?' (found too many arguments)" } }, | 417 | false, { |
| 418 | util.one_of { | ||
| 419 | "bad argument #2 to '?' (found too many arguments)", | ||
| 420 | "bad argument #2 to 'cjson.decode_invalid_numbers' (found too many arguments)" | ||
| 421 | } | ||
| 422 | } }, | ||
| 388 | { "Set encode_sparse_array(\"not quite on\") [throw error]", | 423 | { "Set encode_sparse_array(\"not quite on\") [throw error]", |
| 389 | json.encode_sparse_array, { "not quite on" }, | 424 | json.encode_sparse_array, { "not quite on" }, |
| 390 | false, { "bad argument #1 to '?' (invalid option 'not quite on')" } }, | 425 | false, { |
| 391 | 426 | util.one_of { | |
| 427 | "bad argument #1 to '?' (invalid option 'not quite on')", | ||
| 428 | "bad argument #1 to 'cjson.encode_sparse_array' (invalid option 'not quite on')" | ||
| 429 | } | ||
| 430 | } }, | ||
| 392 | { "Reset Lua CJSON configuration", function () json = json.new() end }, | 431 | { "Reset Lua CJSON configuration", function () json = json.new() end }, |
| 393 | -- Wrap in a function to ensure the table returned by json.new() is used | 432 | -- Wrap in a function to ensure the table returned by json.new() is used |
| 394 | { "Check encode_sparse_array()", | 433 | { "Check encode_sparse_array()", |
| @@ -400,7 +439,12 @@ local cjson_tests = { | |||
| 400 | true, { "true" } }, | 439 | true, { "true" } }, |
| 401 | { "Encode (safe) argument validation [throw error]", | 440 | { "Encode (safe) argument validation [throw error]", |
| 402 | json_safe.encode, { "arg1", "arg2" }, | 441 | json_safe.encode, { "arg1", "arg2" }, |
| 403 | false, { "bad argument #1 to '?' (expected 1 argument)" } }, | 442 | false, { |
| 443 | util.one_of { | ||
| 444 | "bad argument #1 to '?' (expected 1 argument)", | ||
| 445 | "bad argument #1 to 'cjson.safe.encode' (expected 1 argument)" | ||
| 446 | } | ||
| 447 | } }, | ||
| 404 | { "Decode (safe) error generation", | 448 | { "Decode (safe) error generation", |
| 405 | json_safe.decode, { "Oops" }, | 449 | json_safe.decode, { "Oops" }, |
| 406 | true, { nil, "Expected value but found invalid token at character 1" } }, | 450 | true, { nil, "Expected value but found invalid token at character 1" } }, |
