aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2012-01-14 01:29:07 +1030
committerMark Pulford <mark@kyne.com.au>2012-03-04 18:54:34 +1030
commit8e1b49f0dc0819991783d262faf33d5e53d6f621 (patch)
tree0c7aeeb5b3070b78a50a16e91293c7c50cb282f4 /tests
parenta55b836b3152cce4bbb96699dc4b7cb49967c6bc (diff)
downloadlua-cjson-8e1b49f0dc0819991783d262faf33d5e53d6f621.tar.gz
lua-cjson-8e1b49f0dc0819991783d262faf33d5e53d6f621.tar.bz2
lua-cjson-8e1b49f0dc0819991783d262faf33d5e53d6f621.zip
Add descriptions to all tests
Rewrite test framework and add descriptions for all tests.
Diffstat (limited to 'tests')
-rwxr-xr-xtests/test.lua335
1 files changed, 192 insertions, 143 deletions
diff --git a/tests/test.lua b/tests/test.lua
index 4b8525b..b1212af 100755
--- a/tests/test.lua
+++ b/tests/test.lua
@@ -9,7 +9,7 @@
9local json = require "cjson" 9local json = require "cjson"
10local util = require "cjson.util" 10local util = require "cjson.util"
11 11
12local function gen_ascii() 12local function gen_raw_octets()
13 local chars = {} 13 local chars = {}
14 for i = 0, 255 do chars[i + 1] = string.char(i) end 14 for i = 0, 255 do chars[i + 1] = string.char(i) end
15 return table.concat(chars) 15 return table.concat(chars)
@@ -53,205 +53,254 @@ function test_decode_cycle(filename)
53 return util.compare_values(obj1, obj2) 53 return util.compare_values(obj1, obj2)
54end 54end
55 55
56-- Set up data used in tests
56local Inf = math.huge; 57local Inf = math.huge;
57local NaN = math.huge * 0; 58local NaN = math.huge * 0;
58local octets_raw = gen_ascii() 59
60local octets_raw = gen_raw_octets()
59local octets_escaped = util.file_load("octets-escaped.dat") 61local octets_escaped = util.file_load("octets-escaped.dat")
62
60local utf8_loaded, utf8_raw = pcall(util.file_load, "utf8.dat") 63local utf8_loaded, utf8_raw = pcall(util.file_load, "utf8.dat")
61if not utf8_loaded then 64if not utf8_loaded then
62 utf8_raw = "Failed to load utf8.dat" 65 utf8_raw = "Failed to load utf8.dat"
63end 66end
64local utf16_escaped = gen_utf16_escaped() 67local utf16_escaped = gen_utf16_escaped()
68
65local nested5 = {{{{{ "nested" }}}}} 69local nested5 = {{{{{ "nested" }}}}}
70
66local table_cycle = {} 71local table_cycle = {}
67local table_cycle2 = { table_cycle } 72local table_cycle2 = { table_cycle }
68table_cycle[1] = table_cycle2 73table_cycle[1] = table_cycle2
69 74
70local decode_simple_tests = { 75local all_tests = {
71 { json.decode, { '"test string"' }, true, { "test string" } }, 76 -- Simple decode tests
72 { json.decode, { '-5e3' }, true, { -5000 } }, 77 { "Decode string",
73 { json.decode, { 'null' }, true, { json.null } }, 78 json.decode, { '"test string"' }, true, { "test string" } },
74 { json.decode, { 'true' }, true, { true } }, 79 { "Decode number with exponent",
75 { json.decode, { 'false' }, true, { false } }, 80 json.decode, { '-5e3' }, true, { -5000 } },
76 { json.decode, { '{ "1": "one", "3": "three" }' }, 81 { "Decode null",
82 json.decode, { 'null' }, true, { json.null } },
83 { "Decode true",
84 json.decode, { 'true' }, true, { true } },
85 { "Decode false",
86 json.decode, { 'false' }, true, { false } },
87 { "Decode object with numeric keys",
88 json.decode, { '{ "1": "one", "3": "three" }' },
77 true, { { ["1"] = "one", ["3"] = "three" } } }, 89 true, { { ["1"] = "one", ["3"] = "three" } } },
78 { json.decode, { '[ "one", null, "three" ]' }, 90 { "Decode array",
79 true, { { "one", json.null, "three" } } } 91 json.decode, { '[ "one", null, "three" ]' },
80} 92 true, { { "one", json.null, "three" } } },
81
82local encode_simple_tests = {
83 { json.encode, { json.null }, true, { 'null' } },
84 { json.encode, { true }, true, { 'true' } },
85 { json.encode, { false }, true, { 'false' } },
86 { json.encode, { { } }, true, { '{}' } },
87 { json.encode, { 10 }, true, { '10' } },
88 { json.encode, { NaN },
89 false, { "Cannot serialise number: must not be NaN or Inf" } },
90 { json.encode, { Inf },
91 false, { "Cannot serialise number: must not be NaN or Inf" } },
92 { json.encode, { "hello" }, true, { '"hello"' } },
93}
94 93
95local decode_numeric_tests = { 94 -- Numeric decode tests
96 { json.decode, { '[ 0.0, -1, 0.3e-3, 1023.2 ]' }, 95 { "Decode various numbers",
96 json.decode, { '[ 0.0, -1, 0.3e-3, 1023.2 ]' },
97 true, { { 0.0, -1, 0.0003, 1023.2 } } }, 97 true, { { 0.0, -1, 0.0003, 1023.2 } } },
98 { json.decode, { '00123' }, true, { 123 } }, 98 { "Decode integer with leading zeros",
99 { json.decode, { '05.2' }, true, { 5.2 } }, 99 json.decode, { '00123' }, true, { 123 } },
100 { json.decode, { '0e10' }, true, { 0 } }, 100 { "Decode floating point with leading zero",
101 { json.decode, { '0x6' }, true, { 6 } }, 101 json.decode, { '05.2' }, true, { 5.2 } },
102 { json.decode, { '[ +Inf, Inf, -Inf ]' }, true, { { Inf, Inf, -Inf } } }, 102 { "Decode zero with exponent",
103 { json.decode, { '[ +Infinity, Infinity, -Infinity ]' }, 103 json.decode, { '0e10' }, true, { 0 } },
104 { "Decode hexadecimal",
105 json.decode, { '0x6' }, true, { 6 } },
106 { "Decode +-Inf",
107 json.decode, { '[ +Inf, Inf, -Inf ]' }, true, { { Inf, Inf, -Inf } } },
108 { "Decode +-Infinity",
109 json.decode, { '[ +Infinity, Infinity, -Infinity ]' },
104 true, { { Inf, Inf, -Inf } } }, 110 true, { { Inf, Inf, -Inf } } },
105 { json.decode, { '[ +NaN, NaN, -NaN ]' }, true, { { NaN, NaN, NaN } } }, 111 { "Decode +-NaN",
106 { json.decode, { 'Infrared' }, 112 json.decode, { '[ +NaN, NaN, -NaN ]' }, true, { { NaN, NaN, NaN } } },
113 { "Decode Infrared (not infinity)",
114 json.decode, { 'Infrared' },
107 false, { "Expected the end but found invalid token at character 4" } }, 115 false, { "Expected the end but found invalid token at character 4" } },
108 { json.decode, { 'Noodle' }, 116 { "Decode Noodle (not NaN)",
117 json.decode, { 'Noodle' },
109 false, { "Expected value but found invalid token at character 1" } }, 118 false, { "Expected value but found invalid token at character 1" } },
110}
111 119
112local encode_table_tests = { 120 -- Decode error tests
113 function() 121 { "Decode UTF-16BE",
122 json.decode, { '\0"\0"' },
123 false, { "JSON parser does not support UTF-16 or UTF-32" } },
124 { "Decode UTF-16LE",
125 json.decode, { '"\0"\0' },
126 false, { "JSON parser does not support UTF-16 or UTF-32" } },
127 { "Decode UTF-32BE",
128 json.decode, { '\0\0\0"' },
129 false, { "JSON parser does not support UTF-16 or UTF-32" } },
130 { "Decode UTF-32LE",
131 json.decode, { '"\0\0\0' },
132 false, { "JSON parser does not support UTF-16 or UTF-32" } },
133 { "Decode partial JSON",
134 json.decode, { '{ "unexpected eof": ' },
135 false, { "Expected value but found T_END at character 21" } },
136 { "Decode with extra comma",
137 json.decode, { '{ "extra data": true }, false' },
138 false, { "Expected the end but found T_COMMA at character 23" } },
139 { "Decode invalid escape code",
140 json.decode, { [[ { "bad escape \q code" } ]] },
141 false, { "Expected object key string but found invalid escape code at character 16" } },
142 { "Decode invalid unicode escape",
143 json.decode, { [[ { "bad unicode \u0f6 escape" } ]] },
144 false, { "Expected object key string but found invalid unicode escape code at character 17" } },
145 { "Decode invalid keyword",
146 json.decode, { ' [ "bad barewood", test ] ' },
147 false, { "Expected value but found invalid token at character 20" } },
148 { "Decode invalid number #1",
149 json.decode, { '[ -+12 ]' },
150 false, { "Expected value but found invalid number at character 3" } },
151 { "Decode invalid number #2",
152 json.decode, { '-v' },
153 false, { "Expected value but found invalid number at character 1" } },
154 { "Decode invalid number exponent",
155 json.decode, { '[ 0.4eg10 ]' },
156 false, { "Expected comma or array end but found invalid token at character 6" } },
157 { "Setting decode_max_depth(5)", function ()
158 json.decode_max_depth(5)
159 end },
160 { "Decode array at nested limit",
161 json.decode, { '[[[[[ "nested" ]]]]]' },
162 true, { {{{{{ "nested" }}}}} } },
163 { "Decode array over nested limit",
164 json.decode, { '[[[[[[ "nested" ]]]]]]' },
165 false, { "Too many nested data structures" } },
166 { "Setting decode_max_depth(1000)", function ()
167 json.decode_max_depth(1000)
168 end },
169
170 -- Simple encode tests
171 { "Encode null",
172 json.encode, { json.null }, true, { 'null' } },
173 { "Encode true",
174 json.encode, { true }, true, { 'true' } },
175 { "Encode false",
176 json.encode, { false }, true, { 'false' } },
177 { "Encode empty object",
178 json.encode, { { } }, true, { '{}' } },
179 { "Encode integer",
180 json.encode, { 10 }, true, { '10' } },
181 { "Encode NaN (invalid numbers disabled)",
182 json.encode, { NaN },
183 false, { "Cannot serialise number: must not be NaN or Inf" } },
184 { "Encode Infinity (invalid numbers disabled)",
185 json.encode, { Inf },
186 false, { "Cannot serialise number: must not be NaN or Inf" } },
187 { "Encode string",
188 json.encode, { "hello" }, true, { '"hello"' } },
189
190 -- Table encode tests
191 { "Setting sparse array (true, 2, 3) / max depth (5)", function()
114 json.encode_sparse_array(true, 2, 3) 192 json.encode_sparse_array(true, 2, 3)
115 json.encode_max_depth(5) 193 json.encode_max_depth(5)
116 return "Setting sparse array (true, 2, 3) / max depth (5)" 194 end },
117 end, 195 { "Encode sparse table as array #1",
118 { json.encode, { { [3] = "sparse test" } }, 196 json.encode, { { [3] = "sparse test" } },
119 true, { '[null,null,"sparse test"]' } }, 197 true, { '[null,null,"sparse test"]' } },
120 { json.encode, { { [1] = "one", [4] = "sparse test" } }, 198 { "Encode sparse table as array #2",
199 json.encode, { { [1] = "one", [4] = "sparse test" } },
121 true, { '["one",null,null,"sparse test"]' } }, 200 true, { '["one",null,null,"sparse test"]' } },
122 { json.encode, { { [1] = "one", [5] = "sparse test" } }, 201 { "Encode sparse array as object",
202 json.encode, { { [1] = "one", [5] = "sparse test" } },
123 true, { '{"1":"one","5":"sparse test"}' } }, 203 true, { '{"1":"one","5":"sparse test"}' } },
124 204
125 { json.encode, { { ["2"] = "numeric string key test" } }, 205 { "Encode table with numeric string key as object",
206 json.encode, { { ["2"] = "numeric string key test" } },
126 true, { '{"2":"numeric string key test"}' } }, 207 true, { '{"2":"numeric string key test"}' } },
127 208
128 { json.encode, { nested5 }, true, { '[[[[["nested"]]]]]' } }, 209 { "Encode nested table",
129 { json.encode, { { nested5 } }, 210 json.encode, { nested5 }, true, { '[[[[["nested"]]]]]' } },
211 { "Encode nested table (throw error)",
212 json.encode, { { nested5 } },
213 false, { "Cannot serialise, excessive nesting (6)" } },
214 { "Encode table with cycle",
215 json.encode, { table_cycle },
130 false, { "Cannot serialise, excessive nesting (6)" } }, 216 false, { "Cannot serialise, excessive nesting (6)" } },
131 { json.encode, { table_cycle },
132 false, { "Cannot serialise, excessive nesting (6)" } }
133}
134 217
135local encode_error_tests = { 218 -- Encode error tests
136 { json.encode, { { [false] = "wrong" } }, 219 { "Encode table with incompatible key",
220 json.encode, { { [false] = "wrong" } },
137 false, { "Cannot serialise boolean: table key must be a number or string" } }, 221 false, { "Cannot serialise boolean: table key must be a number or string" } },
138 { json.encode, { function () end }, 222 { "Encode Lua function",
223 json.encode, { function () end },
139 false, { "Cannot serialise function: type not supported" } }, 224 false, { "Cannot serialise function: type not supported" } },
140 function () 225 { "Setting encode_invalid_numbers(false)", function ()
141 json.encode_invalid_numbers(false) 226 json.encode_invalid_numbers(false)
142 return 'Setting encode_invalid_numbers(false)' 227 end },
143 end, 228 { "Encode NaN (invalid numbers disabled)",
144 { json.encode, { NaN }, 229 json.encode, { NaN },
145 false, { "Cannot serialise number: must not be NaN or Inf" } }, 230 false, { "Cannot serialise number: must not be NaN or Inf" } },
146 { json.encode, { Inf }, 231 { "Encode Infinity (invalid numbers disabled)",
232 json.encode, { Inf },
147 false, { "Cannot serialise number: must not be NaN or Inf" } }, 233 false, { "Cannot serialise number: must not be NaN or Inf" } },
148 function () 234 { 'Setting encode_invalid_numbers("null").', function ()
149 json.encode_invalid_numbers("null") 235 json.encode_invalid_numbers("null")
150 return 'Setting encode_invalid_numbers("null").' 236 end },
151 end, 237 { "Encode NaN as null",
152 { json.encode, { NaN }, true, { "null" } }, 238 json.encode, { NaN }, true, { "null" } },
153 { json.encode, { Inf }, true, { "null" } }, 239 { "Encode Infinity as null",
154 function () 240 json.encode, { Inf }, true, { "null" } },
241 { 'Setting encode_invalid_numbers(true).', function ()
155 json.encode_invalid_numbers(true) 242 json.encode_invalid_numbers(true)
156 return 'Setting encode_invalid_numbers(true).' 243 end },
157 end, 244 { "Encode NaN",
158 { json.encode, { NaN }, true, { "nan" } }, 245 json.encode, { NaN }, true, { "nan" } },
159 { json.encode, { Inf }, true, { "inf" } }, 246 { "Encode Infinity",
160 function () 247 json.encode, { Inf }, true, { "inf" } },
248 { 'Setting encode_invalid_numbers(false)', function ()
161 json.encode_invalid_numbers(false) 249 json.encode_invalid_numbers(false)
162 return 'Setting encode_invalid_numbers(false)' 250 end },
163 end,
164}
165
166local decode_error_tests = {
167 { json.decode, { '\0"\0"' },
168 false, { "JSON parser does not support UTF-16 or UTF-32" } },
169 { json.decode, { '"\0"\0' },
170 false, { "JSON parser does not support UTF-16 or UTF-32" } },
171 { json.decode, { '{ "unexpected eof": ' },
172 false, { "Expected value but found T_END at character 21" } },
173 { json.decode, { '{ "extra data": true }, false' },
174 false, { "Expected the end but found T_COMMA at character 23" } },
175 { json.decode, { ' { "bad escape \\q code" } ' },
176 false, { "Expected object key string but found invalid escape code at character 16" } },
177 { json.decode, { ' { "bad unicode \\u0f6 escape" } ' },
178 false, { "Expected object key string but found invalid unicode escape code at character 17" } },
179 { json.decode, { ' [ "bad barewood", test ] ' },
180 false, { "Expected value but found invalid token at character 20" } },
181 { json.decode, { '[ -+12 ]' },
182 false, { "Expected value but found invalid number at character 3" } },
183 { json.decode, { '-v' },
184 false, { "Expected value but found invalid number at character 1" } },
185 { json.decode, { '[ 0.4eg10 ]' },
186 false, { "Expected comma or array end but found invalid token at character 6" } },
187 function ()
188 json.decode_max_depth(5)
189 return "Setting decode_max_depth(5)"
190 end,
191 { json.decode, { '[[[[[ "nested" ]]]]]' },
192 true, { {{{{{ "nested" }}}}} } },
193 { json.decode, { '[[[[[[ "nested" ]]]]]]' },
194 false, { "Too many nested data structures" } },
195 function ()
196 json.decode_max_depth(1000)
197 return "Setting decode_max_depth(1000)"
198 end
199}
200 251
201local escape_tests = { 252 -- Escaping tests
202 -- Test 8bit clean 253 { "Encode all octets (8-bit clean)",
203 { json.encode, { octets_raw }, true, { octets_escaped } }, 254 json.encode, { octets_raw }, true, { octets_escaped } },
204 { json.decode, { octets_escaped }, true, { octets_raw } }, 255 { "Decode all escaped octets",
205 -- Ensure high bits are removed from surrogate codes 256 json.decode, { octets_escaped }, true, { octets_raw } },
206 { json.decode, { '"\\uF800"' }, true, { "\239\160\128" } }, 257 { "Decode single UTF-16 escape",
207 -- Test inverted surrogate pairs 258 json.decode, { [["\uF800"]] }, true, { "\239\160\128" } },
208 { json.decode, { '"\\uDB00\\uD800"' }, 259 { "Decode swapped surrogate pair",
260 json.decode, { [["\uDC00\uD800"]] },
209 false, { "Expected value but found invalid unicode escape code at character 2" } }, 261 false, { "Expected value but found invalid unicode escape code at character 2" } },
210 -- Test 2x high surrogate code units 262 { "Decode duplicate high surrogate",
211 { json.decode, { '"\\uDB00\\uDB00"' }, 263 json.decode, { [["\uDB00\uDB00"]] },
212 false, { "Expected value but found invalid unicode escape code at character 2" } }, 264 false, { "Expected value but found invalid unicode escape code at character 2" } },
213 -- Test invalid 2nd escape 265 { "Decode duplicate low surrogate",
214 { json.decode, { '"\\uDB00\\"' }, 266 json.decode, { [["\uDB00\uDB00"]] },
215 false, { "Expected value but found invalid unicode escape code at character 2" } }, 267 false, { "Expected value but found invalid unicode escape code at character 2" } },
216 { json.decode, { '"\\uDB00\\uD"' }, 268 { "Decode missing low surrogate",
269 json.decode, { [["\uDB00"]] },
217 false, { "Expected value but found invalid unicode escape code at character 2" } }, 270 false, { "Expected value but found invalid unicode escape code at character 2" } },
218 -- Test decoding of all UTF-16 escapes 271 { "Decode invalid low surrogate",
219 { json.decode, { utf16_escaped }, true, { utf8_raw } } 272 json.decode, { [["\uDB00\uD"]] },
220} 273 false, { "Expected value but found invalid unicode escape code at character 2" } },
274 { "Decode all UTF-16 escapes (including surrogate combinations)",
275 json.decode, { utf16_escaped }, true, { utf8_raw } },
221 276
222-- The standard Lua interpreter is ANSI C online doesn't support locales 277 -- Locale tests
223-- by default. Force a known problematic locale to test strtod()/sprintf(). 278 --
224local locale_tests = { 279 -- The standard Lua interpreter is ANSI C online doesn't support locales
225 function () 280 -- by default. Force a known problematic locale to test strtod()/sprintf().
281 { "Setting locale to cs_CZ (comma separator)", function ()
226 os.setlocale("cs_CZ") 282 os.setlocale("cs_CZ")
227 json.new() 283 json.new()
228 return "Setting locale to cs_CZ (comma separator)" 284 end },
229 end, 285 { "Encode number under comma locale",
230 { json.encode, { 1.5 }, true, { '1.5' } }, 286 json.encode, { 1.5 }, true, { '1.5' } },
231 { json.decode, { "[ 10, \"test\" ]" }, true, { { 10, "test" } } }, 287 { "Decode number in array under comma locale",
232 function () 288 json.decode, { '[ 10, "test" ]' }, true, { { 10, "test" } } },
289 { "Reverting locale to POSIX", function ()
233 os.setlocale("C") 290 os.setlocale("C")
234 json.new() 291 json.new()
235 return "Reverting locale to POSIX" 292 end },
236 end
237} 293}
238 294
239print(string.format("Testing Lua CJSON version %s\n", json.version)) 295print(string.format("Testing Lua CJSON version %s\n", json.version))
240 296
241util.run_test_group("decode simple value", decode_simple_tests) 297util.run_test_group(all_tests)
242util.run_test_group("encode simple value", encode_simple_tests)
243util.run_test_group("decode numeric", decode_numeric_tests)
244util.run_test_group("encode table", encode_table_tests)
245util.run_test_group("decode error", decode_error_tests)
246util.run_test_group("encode error", encode_error_tests)
247util.run_test_group("escape", escape_tests)
248util.run_test_group("locale", locale_tests)
249 298
250json.encode_invalid_numbers(true) 299json.encode_invalid_numbers(true)
251json.decode_invalid_numbers(true) 300json.decode_invalid_numbers(true)
252json.encode_max_depth(20) 301json.encode_max_depth(20)
253for i = 1, #arg do 302for i = 1, #arg do
254 util.run_test("decode cycle " .. arg[i], test_decode_cycle, { arg[i] }, 303 util.run_test("Decode cycle " .. arg[i], test_decode_cycle, { arg[i] },
255 true, { true }) 304 true, { true })
256end 305end
257 306