From 96187b423dbec0d502bb1c4200094527458d83c2 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 21 Sep 2019 19:22:24 -0700 Subject: support error message testing for modern versions of lua --- tests/test.lua | 68 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file 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 = { { "Encode sparse array as object", json.encode, { { [1] = "one", [5] = "sparse test" } }, true, { - util.one_of{ + util.one_of { '{"5":"sparse test","1":"one"}', '{"1":"one","5":"sparse test"}' } @@ -363,32 +363,71 @@ local cjson_tests = { json.encode_keep_buffer, { true }, true, { true } }, -- Test config API errors - -- Function is listed as '?' due to pcall + -- Function is listed as '?' due to pcall for older versions of Lua { "Set encode_number_precision(0) [throw error]", json.encode_number_precision, { 0 }, - false, { "bad argument #1 to '?' (expected integer between 1 and 16)" } }, + false, { + util.one_of { + "bad argument #1 to '?' (expected integer between 1 and 16)", + "bad argument #1 to 'cjson.encode_number_precision' (expected integer between 1 and 16)" + } + } }, { "Set encode_number_precision(\"five\") [throw error]", json.encode_number_precision, { "five" }, - false, { "bad argument #1 to '?' (number expected, got string)" } }, + false, { + util.one_of { + "bad argument #1 to '?' (number expected, got string)", + "bad argument #1 to 'cjson.encode_number_precision' (number expected, got string)" + } + } }, { "Set encode_keep_buffer(nil, true) [throw error]", json.encode_keep_buffer, { nil, true }, - false, { "bad argument #2 to '?' (found too many arguments)" } }, + false, { + util.one_of { + "bad argument #2 to '?' (found too many arguments)", + "bad argument #2 to 'cjson.encode_keep_buffer' (found too many arguments)" + } + } }, { "Set encode_max_depth(\"wrong\") [throw error]", json.encode_max_depth, { "wrong" }, - false, { "bad argument #1 to '?' (number expected, got string)" } }, + false, { + util.one_of { + "bad argument #1 to '?' (number expected, got string)", + "bad argument #1 to 'cjson.encode_max_depth' (number expected, got string)" + } + } }, { "Set decode_max_depth(0) [throw error]", json.decode_max_depth, { "0" }, - false, { "bad argument #1 to '?' (expected integer between 1 and 2147483647)" } }, + false, { + util.one_of { + "bad argument #1 to '?' (expected integer between 1 and 2147483647)", + "bad argument #1 to 'cjson.decode_max_depth' (expected integer between 1 and 2147483647)" + } + } }, { "Set encode_invalid_numbers(-2) [throw error]", json.encode_invalid_numbers, { -2 }, - false, { "bad argument #1 to '?' (invalid option '-2')" } }, + false, { + util.one_of { + "bad argument #1 to '?' (invalid option '-2')", + "bad argument #1 to 'cjson.encode_invalid_numbers' (invalid option '-2')" + } + } }, { "Set decode_invalid_numbers(true, false) [throw error]", json.decode_invalid_numbers, { true, false }, - false, { "bad argument #2 to '?' (found too many arguments)" } }, + false, { + util.one_of { + "bad argument #2 to '?' (found too many arguments)", + "bad argument #2 to 'cjson.decode_invalid_numbers' (found too many arguments)" + } + } }, { "Set encode_sparse_array(\"not quite on\") [throw error]", json.encode_sparse_array, { "not quite on" }, - false, { "bad argument #1 to '?' (invalid option 'not quite on')" } }, - + false, { + util.one_of { + "bad argument #1 to '?' (invalid option 'not quite on')", + "bad argument #1 to 'cjson.encode_sparse_array' (invalid option 'not quite on')" + } + } }, { "Reset Lua CJSON configuration", function () json = json.new() end }, -- Wrap in a function to ensure the table returned by json.new() is used { "Check encode_sparse_array()", @@ -400,7 +439,12 @@ local cjson_tests = { true, { "true" } }, { "Encode (safe) argument validation [throw error]", json_safe.encode, { "arg1", "arg2" }, - false, { "bad argument #1 to '?' (expected 1 argument)" } }, + false, { + util.one_of { + "bad argument #1 to '?' (expected 1 argument)", + "bad argument #1 to 'cjson.safe.encode' (expected 1 argument)" + } + } }, { "Decode (safe) error generation", json_safe.decode, { "Oops" }, true, { nil, "Expected value but found invalid token at character 1" } }, -- cgit v1.2.3-55-g6feb