aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-05-08 02:38:56 +0930
committerMark Pulford <mark@kyne.com.au>2011-05-08 02:38:56 +0930
commit5d61cfbaf7d5af7d32f84edbd68989bdd49b4519 (patch)
treed6b206b758bee7f1603cfbf6c1c0841e8f560ef0
parent0d56e3bebecb7008f0baa7eaccf3dc9b2a39e360 (diff)
downloadlua-cjson-5d61cfbaf7d5af7d32f84edbd68989bdd49b4519.tar.gz
lua-cjson-5d61cfbaf7d5af7d32f84edbd68989bdd49b4519.tar.bz2
lua-cjson-5d61cfbaf7d5af7d32f84edbd68989bdd49b4519.zip
Convert tests to automatically verify output
-rwxr-xr-xtests/test.lua213
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 @@
7require "common" 7require "common"
8local json = require "cjson" 8local json = require "cjson"
9 9
10local cjson_test_non_default = true
11
12function 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
20end
21
22local simple_value_tests = { 10local 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
22local Inf = math.huge;
23local NaN = math.huge * 0;
24
31local numeric_tests = { 25local 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
50local object_tests = { 42local nested5 = {{{{{ "nested" }}}}}
51 { [5] = "sparse test" }, 43
52 { [6] = "sparse test" }, 44local 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
56local decode_error_tests = { 64local 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
67local simple_encode_tests = { 83local 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 92function test_ascii_sweep(min, max)
72local 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)
78end
79 100
80local 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)
86end
87local 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)
94end
95
96run_tests(simple_value_tests, decode_encode)
97run_tests(decode_error_tests, decode_encode)
98 103
99run_tests(numeric_tests, decode_encode) 104 if ascii_raw == ascii_raw2 then
100 105 return "clean"
101if 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)
106end 109end
107 110
108run_tests(object_tests, encode_decode) 111local escape_tests = {
109print ("Encode tests..") 112 { test_ascii_sweep, { 0, 255 }, true, { 'clean' } },
110 113}
111run_tests(simple_encode_tests, encode_decode)
112 114
113if cjson_test_non_default then 115function 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)
118end 119end
119 120
120local ascii1 = gen_ascii(255) 121run_test_group("decode simple value", simple_value_tests)
121print("Unprintable escapes:") 122run_test_group("decode numeric", numeric_tests)
122print(json.encode(ascii1)) 123
123print("===") 124-- INCLUDE:
124local 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
126print("8bit clean encode/decode: " .. tostring(ascii1 ~= ascii2)) 130run_test_group("encode table", encode_table_tests)
131run_test_group("decode error", decode_error_tests)
132run_test_group("encode simple value", encode_simple_tests)
133run_test_group("escape", escape_tests)
127 134
135cjson.encode_max_depth(20)
128for i = 1, #arg do 136for 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
140end 139end
141 140
141cjson.refuse_invalid_numbers(true)
142
142-- vi:ai et sw=4 ts=4: 143-- vi:ai et sw=4 ts=4: