From 5d61cfbaf7d5af7d32f84edbd68989bdd49b4519 Mon Sep 17 00:00:00 2001 From: Mark Pulford Date: Sun, 8 May 2011 02:38:56 +0930 Subject: Convert tests to automatically verify output --- tests/test.lua | 213 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 107 insertions(+), 106 deletions(-) (limited to 'tests') diff --git a/tests/test.lua b/tests/test.lua index 51f29c4..bab2e94 100755 --- a/tests/test.lua +++ b/tests/test.lua @@ -7,136 +7,137 @@ require "common" local json = require "cjson" -local cjson_test_non_default = true - -function run_tests(tests, func) - for k, v in ipairs(tests) do - local success, msg = pcall(func, v) - if not success then - print("Error: " .. msg) - end - print() - end -end - local simple_value_tests = { - [[ "test string" ]], - [[ -5e3 ]], - [[ null ]], - [[ true ]], - [[ false ]], - [[ { "1": "one", "3": "three" } ]] + { json.decode, { '"test string"' }, true, { "test string" } }, + { json.decode, { '-5e3' }, true, { -5000 } }, + { json.decode, { 'null' }, true, { json.null } }, + { json.decode, { 'true' }, true, { true } }, + { json.decode, { 'false' }, true, { false } }, + { json.decode, { '{ "1": "one", "3": "three" }' }, + true, { { ["1"] = "one", ["3"] = "three" } } }, + { json.decode, { '[ "one", null, "three" ]' }, + true, { { "one", json.null, "three" } } } } +local Inf = math.huge; +local NaN = math.huge * 0; + local numeric_tests = { - "[ 0.0, -1, 0.3e-3, 1023.2 ]", - "[ 00123 ]", - "[ 05.2 ]", - "[ 0e10 ]", - "[ 0x6 ]", - "[ +Inf ]", - "[ Inf ]", - "[ -Inf ]", - "[ +Infinity ]", - "[ Infinity ]", - "[ -Infinity ]", - "[ +NaN ]", - "[ NaN ]", - "[ -NaN ]", - "[ Infrared ]", - "[ Noodle ]" + { json.decode, { '[ 0.0, -1, 0.3e-3, 1023.2 ]' }, + true, { { 0.0, -1, 0.0003, 1023.2 } } }, + { json.decode, { '00123' }, true, { 123 } }, + { json.decode, { '05.2' }, true, { 5.2 } }, + { json.decode, { '0e10' }, true, { 0 } }, + { json.decode, { '0x6' }, true, { 6 } }, + { json.decode, { '[ +Inf, Inf, -Inf ]' }, true, { { Inf, Inf, -Inf } } }, + { json.decode, { '[ +Infinity, Infinity, -Infinity ]' }, + true, { { Inf, Inf, -Inf } } }, + { json.decode, { '[ +NaN, NaN, -NaN ]' }, true, { { NaN, NaN, NaN } } }, + { json.decode, { 'Infrared' }, + false, { "Expected the end but found invalid token at character 4" } }, + { json.decode, { 'Noodle' }, + false, { "Expected value but found invalid token at character 1" } }, } -local object_tests = { - { [5] = "sparse test" }, - { [6] = "sparse test" }, - {{{{{{{{{{{{{{{{{{{{{ "nested" }}}}}}}}}}}}}}}}}}}}} +local nested5 = {{{{{ "nested" }}}}} + +local encode_table_tests = { + function() + cjson.encode_sparse_array(true, 2, 3) + cjson.encode_max_depth(5) + return "Setting sparse array / max depth" + end, + { json.encode, { { [3] = "sparse test" } }, + true, { '[ null, null, "sparse test" ]' } }, + + { json.encode, { { [1] = "one", [4] = "sparse test" } }, + true, { '[ "one", null, null, "sparse test" ]' } }, + + { json.encode, { { [1] = "one", [5] = "sparse test" } }, + true, { '{ "1": "one", "5": "sparse test" }' } }, + + { json.encode, { nested5 }, true, { '[ [ [ [ [ "nested" ] ] ] ] ]' } }, + { json.encode, { { nested5 } }, + false, { "Cannot serialise, excessive nesting (6)" } } } local decode_error_tests = { - '{ "unexpected eof": ', - '{ "extra data": true }, false', - [[ { "bad escape \q code" } ]], - [[ { "bad unicode \u0f6 escape" } ]], - [[ [ "bad barewood", test ] ]], - "[ -+12 ]", - "-v", - "[ 0.4eg10 ]", + { json.decode, { '{ "unexpected eof": ' }, + false, { "Expected value but found T_END at character 21" } }, + { json.decode, { '{ "extra data": true }, false' }, + false, { "Expected the end but found T_COMMA at character 23" } }, + { json.decode, { ' { "bad escape \\q code" } ' }, + false, { "Expected object key string but found invalid escape code at character 16" } }, + { json.decode, { ' { "bad unicode \\u0f6 escape" } ' }, + false, { "Expected object key string but found invalid unicode escape code at character 17" } }, + { json.decode, { ' [ "bad barewood", test ] ' }, + false, { "Expected value but found invalid token at character 20" } }, + { json.decode, { '[ -+12 ]' }, + false, { "Expected value but found invalid number at character 3" } }, + { json.decode, { '-v' }, + false, { "Expected value but found invalid number at character 1" } }, + { json.decode, { '[ 0.4eg10 ]' }, + false, { "Expected comma or array end but found invalid token at character 6" } }, } -local simple_encode_tests = { - json.null, true, false, { }, 10, "hello" +local encode_simple_tests = { + { json.encode, { json.null }, true, { 'null' } }, + { json.encode, { true }, true, { 'true' } }, + { json.encode, { false }, true, { 'false' } }, + { json.encode, { { } }, true, { '{ }' } }, + { json.encode, { 10 }, true, { '10' } }, + { json.encode, { "hello" }, true, { '"hello"' } }, } - -local function gen_ascii(max) - local chars = {} - for i = 0, max do - chars[i] = string.char(i) +function test_ascii_sweep(min, max) + local function gen_ascii() + local chars = {} + for i = min, max do + chars[i + 1] = string.char(i) + end + return table.concat(chars) end - return table.concat(chars) -end -local function decode_encode(text) - print("==JSON=> " .. text) - local obj_data = json.decode(text) - dump_value(obj_data) - local obj_json = json.encode(obj_data) - print(obj_json) -end -local function encode_decode(obj) - print("==OBJ==> ") - dump_value(obj) - local obj_json = json.encode(obj) - print(obj_json) - local obj_data = json.decode(obj_json) - dump_value(obj_data) -end - -run_tests(simple_value_tests, decode_encode) -run_tests(decode_error_tests, decode_encode) + local ascii_raw = gen_ascii() + local ascii_raw2 = json.decode(json.encode(ascii_raw)) -run_tests(numeric_tests, decode_encode) - -if cjson_test_non_default then - print("=== Disabling strict numbers ===") - json.strict_numbers(false) - run_tests(numeric_tests, decode_encode) - json.strict_numbers(true) + if ascii_raw == ascii_raw2 then + return "clean" + else + return "failed ascii sweep test" + end end -run_tests(object_tests, encode_decode) -print ("Encode tests..") - -run_tests(simple_encode_tests, encode_decode) +local escape_tests = { + { test_ascii_sweep, { 0, 255 }, true, { 'clean' } }, +} -if cjson_test_non_default then - print("=== Setting max_depth to 21, sparse_ratio to 5 ===") - json.max_depth(21) - json.sparse_ratio(5) - run_tests(object_tests, encode_decode) +function test_decode_cycle(filename) + local obj1 = json.decode(file_load(filename)) + local obj2 = json.decode(json.encode(obj1)) + return compare_values(obj1, obj2) end -local ascii1 = gen_ascii(255) -print("Unprintable escapes:") -print(json.encode(ascii1)) -print("===") -local ascii2 = json.decode(json.encode(ascii)) +run_test_group("decode simple value", simple_value_tests) +run_test_group("decode numeric", numeric_tests) + +-- INCLUDE: +-- - Sparse array exception.. +-- - .. +-- cjson.encode_sparse_array(true, 2, 3) +-- run_test_group("encode error", encode_error_tests) -print("8bit clean encode/decode: " .. tostring(ascii1 ~= ascii2)) +run_test_group("encode table", encode_table_tests) +run_test_group("decode error", decode_error_tests) +run_test_group("encode simple value", encode_simple_tests) +run_test_group("escape", escape_tests) +cjson.encode_max_depth(20) for i = 1, #arg do - local obj1 = json.decode(file_load(arg[i])) - local obj2 = json.decode(json.encode(obj1)) - if compare_values(obj, obj) then - print(arg[i] .. ": PASS") - else - print(arg[i] .. ": FAIL") - print("== obj1 ==") - dump_value(obj1) - print("== obj2 ==") - dump_value(obj2) - end + run_test("decode cycle " .. arg[i], test_decode_cycle, { arg[i] }, + true, { true }) end +cjson.refuse_invalid_numbers(true) + -- vi:ai et sw=4 ts=4: -- cgit v1.2.3-55-g6feb