diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-05-08 02:38:56 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-05-08 02:38:56 +0930 |
commit | 5d61cfbaf7d5af7d32f84edbd68989bdd49b4519 (patch) | |
tree | d6b206b758bee7f1603cfbf6c1c0841e8f560ef0 | |
parent | 0d56e3bebecb7008f0baa7eaccf3dc9b2a39e360 (diff) | |
download | lua-cjson-5d61cfbaf7d5af7d32f84edbd68989bdd49b4519.tar.gz lua-cjson-5d61cfbaf7d5af7d32f84edbd68989bdd49b4519.tar.bz2 lua-cjson-5d61cfbaf7d5af7d32f84edbd68989bdd49b4519.zip |
Convert tests to automatically verify output
-rwxr-xr-x | tests/test.lua | 213 |
1 files changed, 107 insertions, 106 deletions
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 @@ | |||
7 | require "common" | 7 | require "common" |
8 | local json = require "cjson" | 8 | local json = require "cjson" |
9 | 9 | ||
10 | local cjson_test_non_default = true | ||
11 | |||
12 | function run_tests(tests, func) | ||
13 | for k, v in ipairs(tests) do | ||
14 | local success, msg = pcall(func, v) | ||
15 | if not success then | ||
16 | print("Error: " .. msg) | ||
17 | end | ||
18 | print() | ||
19 | end | ||
20 | end | ||
21 | |||
22 | local simple_value_tests = { | 10 | local simple_value_tests = { |
23 | [[ "test string" ]], | 11 | { json.decode, { '"test string"' }, true, { "test string" } }, |
24 | [[ -5e3 ]], | 12 | { json.decode, { '-5e3' }, true, { -5000 } }, |
25 | [[ null ]], | 13 | { json.decode, { 'null' }, true, { json.null } }, |
26 | [[ true ]], | 14 | { json.decode, { 'true' }, true, { true } }, |
27 | [[ false ]], | 15 | { json.decode, { 'false' }, true, { false } }, |
28 | [[ { "1": "one", "3": "three" } ]] | 16 | { json.decode, { '{ "1": "one", "3": "three" }' }, |
17 | true, { { ["1"] = "one", ["3"] = "three" } } }, | ||
18 | { json.decode, { '[ "one", null, "three" ]' }, | ||
19 | true, { { "one", json.null, "three" } } } | ||
29 | } | 20 | } |
30 | 21 | ||
22 | local Inf = math.huge; | ||
23 | local NaN = math.huge * 0; | ||
24 | |||
31 | local numeric_tests = { | 25 | local numeric_tests = { |
32 | "[ 0.0, -1, 0.3e-3, 1023.2 ]", | 26 | { json.decode, { '[ 0.0, -1, 0.3e-3, 1023.2 ]' }, |
33 | "[ 00123 ]", | 27 | true, { { 0.0, -1, 0.0003, 1023.2 } } }, |
34 | "[ 05.2 ]", | 28 | { json.decode, { '00123' }, true, { 123 } }, |
35 | "[ 0e10 ]", | 29 | { json.decode, { '05.2' }, true, { 5.2 } }, |
36 | "[ 0x6 ]", | 30 | { json.decode, { '0e10' }, true, { 0 } }, |
37 | "[ +Inf ]", | 31 | { json.decode, { '0x6' }, true, { 6 } }, |
38 | "[ Inf ]", | 32 | { json.decode, { '[ +Inf, Inf, -Inf ]' }, true, { { Inf, Inf, -Inf } } }, |
39 | "[ -Inf ]", | 33 | { json.decode, { '[ +Infinity, Infinity, -Infinity ]' }, |
40 | "[ +Infinity ]", | 34 | true, { { Inf, Inf, -Inf } } }, |
41 | "[ Infinity ]", | 35 | { json.decode, { '[ +NaN, NaN, -NaN ]' }, true, { { NaN, NaN, NaN } } }, |
42 | "[ -Infinity ]", | 36 | { json.decode, { 'Infrared' }, |
43 | "[ +NaN ]", | 37 | false, { "Expected the end but found invalid token at character 4" } }, |
44 | "[ NaN ]", | 38 | { json.decode, { 'Noodle' }, |
45 | "[ -NaN ]", | 39 | false, { "Expected value but found invalid token at character 1" } }, |
46 | "[ Infrared ]", | ||
47 | "[ Noodle ]" | ||
48 | } | 40 | } |
49 | 41 | ||
50 | local object_tests = { | 42 | local nested5 = {{{{{ "nested" }}}}} |
51 | { [5] = "sparse test" }, | 43 | |
52 | { [6] = "sparse test" }, | 44 | local encode_table_tests = { |
53 | {{{{{{{{{{{{{{{{{{{{{ "nested" }}}}}}}}}}}}}}}}}}}}} | 45 | function() |
46 | cjson.encode_sparse_array(true, 2, 3) | ||
47 | cjson.encode_max_depth(5) | ||
48 | return "Setting sparse array / max depth" | ||
49 | end, | ||
50 | { json.encode, { { [3] = "sparse test" } }, | ||
51 | true, { '[ null, null, "sparse test" ]' } }, | ||
52 | |||
53 | { json.encode, { { [1] = "one", [4] = "sparse test" } }, | ||
54 | true, { '[ "one", null, null, "sparse test" ]' } }, | ||
55 | |||
56 | { json.encode, { { [1] = "one", [5] = "sparse test" } }, | ||
57 | true, { '{ "1": "one", "5": "sparse test" }' } }, | ||
58 | |||
59 | { json.encode, { nested5 }, true, { '[ [ [ [ [ "nested" ] ] ] ] ]' } }, | ||
60 | { json.encode, { { nested5 } }, | ||
61 | false, { "Cannot serialise, excessive nesting (6)" } } | ||
54 | } | 62 | } |
55 | 63 | ||
56 | local decode_error_tests = { | 64 | local decode_error_tests = { |
57 | '{ "unexpected eof": ', | 65 | { json.decode, { '{ "unexpected eof": ' }, |
58 | '{ "extra data": true }, false', | 66 | false, { "Expected value but found T_END at character 21" } }, |
59 | [[ { "bad escape \q code" } ]], | 67 | { json.decode, { '{ "extra data": true }, false' }, |
60 | [[ { "bad unicode \u0f6 escape" } ]], | 68 | false, { "Expected the end but found T_COMMA at character 23" } }, |
61 | [[ [ "bad barewood", test ] ]], | 69 | { json.decode, { ' { "bad escape \\q code" } ' }, |
62 | "[ -+12 ]", | 70 | false, { "Expected object key string but found invalid escape code at character 16" } }, |
63 | "-v", | 71 | { json.decode, { ' { "bad unicode \\u0f6 escape" } ' }, |
64 | "[ 0.4eg10 ]", | 72 | false, { "Expected object key string but found invalid unicode escape code at character 17" } }, |
73 | { json.decode, { ' [ "bad barewood", test ] ' }, | ||
74 | false, { "Expected value but found invalid token at character 20" } }, | ||
75 | { json.decode, { '[ -+12 ]' }, | ||
76 | false, { "Expected value but found invalid number at character 3" } }, | ||
77 | { json.decode, { '-v' }, | ||
78 | false, { "Expected value but found invalid number at character 1" } }, | ||
79 | { json.decode, { '[ 0.4eg10 ]' }, | ||
80 | false, { "Expected comma or array end but found invalid token at character 6" } }, | ||
65 | } | 81 | } |
66 | 82 | ||
67 | local simple_encode_tests = { | 83 | local encode_simple_tests = { |
68 | json.null, true, false, { }, 10, "hello" | 84 | { json.encode, { json.null }, true, { 'null' } }, |
85 | { json.encode, { true }, true, { 'true' } }, | ||
86 | { json.encode, { false }, true, { 'false' } }, | ||
87 | { json.encode, { { } }, true, { '{ }' } }, | ||
88 | { json.encode, { 10 }, true, { '10' } }, | ||
89 | { json.encode, { "hello" }, true, { '"hello"' } }, | ||
69 | } | 90 | } |
70 | 91 | ||
71 | 92 | function test_ascii_sweep(min, max) | |
72 | local function gen_ascii(max) | 93 | local function gen_ascii() |
73 | local chars = {} | 94 | local chars = {} |
74 | for i = 0, max do | 95 | for i = min, max do |
75 | chars[i] = string.char(i) | 96 | chars[i + 1] = string.char(i) |
97 | end | ||
98 | return table.concat(chars) | ||
76 | end | 99 | end |
77 | return table.concat(chars) | ||
78 | end | ||
79 | 100 | ||
80 | local function decode_encode(text) | 101 | local ascii_raw = gen_ascii() |
81 | print("==JSON=> " .. text) | 102 | local ascii_raw2 = json.decode(json.encode(ascii_raw)) |
82 | local obj_data = json.decode(text) | ||
83 | dump_value(obj_data) | ||
84 | local obj_json = json.encode(obj_data) | ||
85 | print(obj_json) | ||
86 | end | ||
87 | local function encode_decode(obj) | ||
88 | print("==OBJ==> ") | ||
89 | dump_value(obj) | ||
90 | local obj_json = json.encode(obj) | ||
91 | print(obj_json) | ||
92 | local obj_data = json.decode(obj_json) | ||
93 | dump_value(obj_data) | ||
94 | end | ||
95 | |||
96 | run_tests(simple_value_tests, decode_encode) | ||
97 | run_tests(decode_error_tests, decode_encode) | ||
98 | 103 | ||
99 | run_tests(numeric_tests, decode_encode) | 104 | if ascii_raw == ascii_raw2 then |
100 | 105 | return "clean" | |
101 | if cjson_test_non_default then | 106 | else |
102 | print("=== Disabling strict numbers ===") | 107 | return "failed ascii sweep test" |
103 | json.strict_numbers(false) | 108 | end |
104 | run_tests(numeric_tests, decode_encode) | ||
105 | json.strict_numbers(true) | ||
106 | end | 109 | end |
107 | 110 | ||
108 | run_tests(object_tests, encode_decode) | 111 | local escape_tests = { |
109 | print ("Encode tests..") | 112 | { test_ascii_sweep, { 0, 255 }, true, { 'clean' } }, |
110 | 113 | } | |
111 | run_tests(simple_encode_tests, encode_decode) | ||
112 | 114 | ||
113 | if cjson_test_non_default then | 115 | function test_decode_cycle(filename) |
114 | print("=== Setting max_depth to 21, sparse_ratio to 5 ===") | 116 | local obj1 = json.decode(file_load(filename)) |
115 | json.max_depth(21) | 117 | local obj2 = json.decode(json.encode(obj1)) |
116 | json.sparse_ratio(5) | 118 | return compare_values(obj1, obj2) |
117 | run_tests(object_tests, encode_decode) | ||
118 | end | 119 | end |
119 | 120 | ||
120 | local ascii1 = gen_ascii(255) | 121 | run_test_group("decode simple value", simple_value_tests) |
121 | print("Unprintable escapes:") | 122 | run_test_group("decode numeric", numeric_tests) |
122 | print(json.encode(ascii1)) | 123 | |
123 | print("===") | 124 | -- INCLUDE: |
124 | local ascii2 = json.decode(json.encode(ascii)) | 125 | -- - Sparse array exception.. |
126 | -- - .. | ||
127 | -- cjson.encode_sparse_array(true, 2, 3) | ||
128 | -- run_test_group("encode error", encode_error_tests) | ||
125 | 129 | ||
126 | print("8bit clean encode/decode: " .. tostring(ascii1 ~= ascii2)) | 130 | run_test_group("encode table", encode_table_tests) |
131 | run_test_group("decode error", decode_error_tests) | ||
132 | run_test_group("encode simple value", encode_simple_tests) | ||
133 | run_test_group("escape", escape_tests) | ||
127 | 134 | ||
135 | cjson.encode_max_depth(20) | ||
128 | for i = 1, #arg do | 136 | for i = 1, #arg do |
129 | local obj1 = json.decode(file_load(arg[i])) | 137 | run_test("decode cycle " .. arg[i], test_decode_cycle, { arg[i] }, |
130 | local obj2 = json.decode(json.encode(obj1)) | 138 | true, { true }) |
131 | if compare_values(obj, obj) then | ||
132 | print(arg[i] .. ": PASS") | ||
133 | else | ||
134 | print(arg[i] .. ": FAIL") | ||
135 | print("== obj1 ==") | ||
136 | dump_value(obj1) | ||
137 | print("== obj2 ==") | ||
138 | dump_value(obj2) | ||
139 | end | ||
140 | end | 139 | end |
141 | 140 | ||
141 | cjson.refuse_invalid_numbers(true) | ||
142 | |||
142 | -- vi:ai et sw=4 ts=4: | 143 | -- vi:ai et sw=4 ts=4: |