diff options
author | Mark Pulford <mark@kyne.com.au> | 2012-01-16 22:42:58 +1030 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2012-03-04 18:54:35 +1030 |
commit | 4500ab5520d0000bfeadfd550c13813ddf5eee60 (patch) | |
tree | 56d1b3a4cfbbd97d059f492733d655fc7da99126 /tests | |
parent | c9a4e121f18a1c36b3b0065aa7eed7097f12356c (diff) | |
download | lua-cjson-4500ab5520d0000bfeadfd550c13813ddf5eee60.tar.gz lua-cjson-4500ab5520d0000bfeadfd550c13813ddf5eee60.tar.bz2 lua-cjson-4500ab5520d0000bfeadfd550c13813ddf5eee60.zip |
Test config API errors and setting configuration
- Update comments
- Use enumerated return values
- Test various configuration API errors
- Test resetting configuration to default
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/test.lua | 94 |
1 files changed, 66 insertions, 28 deletions
diff --git a/tests/test.lua b/tests/test.lua index a411b1e..f432b22 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -82,12 +82,13 @@ local NaN = math.huge * 0; | |||
82 | 82 | ||
83 | local testdata = load_testdata() | 83 | local testdata = load_testdata() |
84 | 84 | ||
85 | local all_tests = { | 85 | local cjson_tests = { |
86 | -- Test API variables | ||
86 | { "Check module name, version", | 87 | { "Check module name, version", |
87 | function () return json._NAME, json._VERSION, json.version end, { }, | 88 | function () return json._NAME, json._VERSION, json.version end, { }, |
88 | true, { "cjson", "1.0devel", "1.0devel" } }, | 89 | true, { "cjson", "1.0devel", "1.0devel" } }, |
89 | 90 | ||
90 | -- Simple decode tests | 91 | -- Test decoding simple types |
91 | { "Decode string", | 92 | { "Decode string", |
92 | json.decode, { '"test string"' }, true, { "test string" } }, | 93 | json.decode, { '"test string"' }, true, { "test string" } }, |
93 | { "Decode numbers", | 94 | { "Decode numbers", |
@@ -102,11 +103,14 @@ local all_tests = { | |||
102 | { "Decode object with numeric keys", | 103 | { "Decode object with numeric keys", |
103 | json.decode, { '{ "1": "one", "3": "three" }' }, | 104 | json.decode, { '{ "1": "one", "3": "three" }' }, |
104 | true, { { ["1"] = "one", ["3"] = "three" } } }, | 105 | true, { { ["1"] = "one", ["3"] = "three" } } }, |
106 | { "Decode object with string keys", | ||
107 | json.decode, { '{ "a": "a", "b": "b" }' }, | ||
108 | true, { { a = "a", b = "b" } } }, | ||
105 | { "Decode array", | 109 | { "Decode array", |
106 | json.decode, { '[ "one", null, "three" ]' }, | 110 | json.decode, { '[ "one", null, "three" ]' }, |
107 | true, { { "one", json.null, "three" } } }, | 111 | true, { { "one", json.null, "three" } } }, |
108 | 112 | ||
109 | -- Decode error tests | 113 | -- Test decoding errors |
110 | { "Decode UTF-16BE", | 114 | { "Decode UTF-16BE", |
111 | json.decode, { '\0"\0"' }, | 115 | json.decode, { '\0"\0"' }, |
112 | false, { "JSON parser does not support UTF-16 or UTF-32" } }, | 116 | false, { "JSON parser does not support UTF-16 or UTF-32" } }, |
@@ -144,7 +148,7 @@ local all_tests = { | |||
144 | json.decode, { '[ 0.4eg10 ]' }, | 148 | json.decode, { '[ 0.4eg10 ]' }, |
145 | false, { "Expected comma or array end but found invalid token at character 6" } }, | 149 | false, { "Expected comma or array end but found invalid token at character 6" } }, |
146 | 150 | ||
147 | -- Test nested tables / arrays / objects | 151 | -- Test decoding nested arrays / objects |
148 | { "Set decode_max_depth(5)", | 152 | { "Set decode_max_depth(5)", |
149 | json.decode_max_depth, { 5 }, true, { 5 } }, | 153 | json.decode_max_depth, { 5 }, true, { 5 } }, |
150 | { "Decode array at nested limit", | 154 | { "Decode array at nested limit", |
@@ -162,6 +166,7 @@ local all_tests = { | |||
162 | { "Set decode_max_depth(1000)", | 166 | { "Set decode_max_depth(1000)", |
163 | json.decode_max_depth, { 1000 }, true, { 1000 } }, | 167 | json.decode_max_depth, { 1000 }, true, { 1000 } }, |
164 | 168 | ||
169 | -- Test encoding nested tables | ||
165 | { "Set encode_max_depth(5)", | 170 | { "Set encode_max_depth(5)", |
166 | json.encode_max_depth, { 5 }, true, { 5 } }, | 171 | json.encode_max_depth, { 5 }, true, { 5 } }, |
167 | { "Encode nested table as array at nested limit", | 172 | { "Encode nested table as array at nested limit", |
@@ -181,7 +186,7 @@ local all_tests = { | |||
181 | { "Set encode_max_depth(1000)", | 186 | { "Set encode_max_depth(1000)", |
182 | json.encode_max_depth, { 1000 }, true, { 1000 } }, | 187 | json.encode_max_depth, { 1000 }, true, { 1000 } }, |
183 | 188 | ||
184 | -- Simple encode tests | 189 | -- Test encoding simple types |
185 | { "Encode null", | 190 | { "Encode null", |
186 | json.encode, { json.null }, true, { 'null' } }, | 191 | json.encode, { json.null }, true, { 'null' } }, |
187 | { "Encode true", | 192 | { "Encode true", |
@@ -194,17 +199,9 @@ local all_tests = { | |||
194 | json.encode, { 10 }, true, { '10' } }, | 199 | json.encode, { 10 }, true, { '10' } }, |
195 | { "Encode string", | 200 | { "Encode string", |
196 | json.encode, { "hello" }, true, { '"hello"' } }, | 201 | json.encode, { "hello" }, true, { '"hello"' } }, |
197 | 202 | { "Encode Lua function", | |
198 | { "Set encode_keep_buffer(false)", | 203 | json.encode, { function () end }, |
199 | json.encode_keep_buffer, { false }, true, { "off" } }, | 204 | false, { "Cannot serialise function: type not supported" } }, |
200 | { "Set encode_number_precision(3)", | ||
201 | json.encode_number_precision, { 3 }, true, { 3 } }, | ||
202 | { "Encode number with precision 3", | ||
203 | json.encode, { 1/3 }, true, { "0.333" } }, | ||
204 | { "Set encode_number_precision(14)", | ||
205 | json.encode_number_precision, { 14 }, true, { 14 } }, | ||
206 | { "Set encode_keep_buffer(true)", | ||
207 | json.encode_keep_buffer, { true }, true, { "on" } }, | ||
208 | 205 | ||
209 | -- Test decoding invalid numbers | 206 | -- Test decoding invalid numbers |
210 | { "Set decode_invalid_numbers(true)", | 207 | { "Set decode_invalid_numbers(true)", |
@@ -265,9 +262,9 @@ local all_tests = { | |||
265 | { 'Set encode_invalid_numbers("off")', | 262 | { 'Set encode_invalid_numbers("off")', |
266 | json.encode_invalid_numbers, { "off" }, true, { "off" } }, | 263 | json.encode_invalid_numbers, { "off" }, true, { "off" } }, |
267 | 264 | ||
268 | -- Table encode tests | 265 | -- Test encoding tables |
269 | { "Set encode_sparse_array(true, 2, 3)", | 266 | { "Set encode_sparse_array(true, 2, 3)", |
270 | json.encode_sparse_array, { true, 2, 3 }, true, { true, 2, 3 } }, | 267 | json.encode_sparse_array, { true, 2, 3 }, true, { "on", 2, 3 } }, |
271 | { "Encode sparse table as array #1", | 268 | { "Encode sparse table as array #1", |
272 | json.encode, { { [3] = "sparse test" } }, | 269 | json.encode, { { [3] = "sparse test" } }, |
273 | true, { '[null,null,"sparse test"]' } }, | 270 | true, { '[null,null,"sparse test"]' } }, |
@@ -277,20 +274,16 @@ local all_tests = { | |||
277 | { "Encode sparse array as object", | 274 | { "Encode sparse array as object", |
278 | json.encode, { { [1] = "one", [5] = "sparse test" } }, | 275 | json.encode, { { [1] = "one", [5] = "sparse test" } }, |
279 | true, { '{"1":"one","5":"sparse test"}' } }, | 276 | true, { '{"1":"one","5":"sparse test"}' } }, |
280 | |||
281 | { "Encode table with numeric string key as object", | 277 | { "Encode table with numeric string key as object", |
282 | json.encode, { { ["2"] = "numeric string key test" } }, | 278 | json.encode, { { ["2"] = "numeric string key test" } }, |
283 | true, { '{"2":"numeric string key test"}' } }, | 279 | true, { '{"2":"numeric string key test"}' } }, |
284 | 280 | { "Set encode_sparse_array(false)", | |
285 | -- Encode error tests | 281 | json.encode_sparse_array, { false }, true, { "off", 2, 3 } }, |
286 | { "Encode table with incompatible key", | 282 | { "Encode table with incompatible key", |
287 | json.encode, { { [false] = "wrong" } }, | 283 | json.encode, { { [false] = "wrong" } }, |
288 | false, { "Cannot serialise boolean: table key must be a number or string" } }, | 284 | false, { "Cannot serialise boolean: table key must be a number or string" } }, |
289 | { "Encode Lua function", | ||
290 | json.encode, { function () end }, | ||
291 | false, { "Cannot serialise function: type not supported" } }, | ||
292 | 285 | ||
293 | -- Escaping tests | 286 | -- Test escaping |
294 | { "Encode all octets (8-bit clean)", | 287 | { "Encode all octets (8-bit clean)", |
295 | json.encode, { testdata.octets_raw }, true, { testdata.octets_escaped } }, | 288 | json.encode, { testdata.octets_raw }, true, { testdata.octets_escaped } }, |
296 | { "Decode all escaped octets", | 289 | { "Decode all escaped octets", |
@@ -315,7 +308,7 @@ local all_tests = { | |||
315 | json.decode, { [["\uDB00\uD"]] }, | 308 | json.decode, { [["\uDB00\uD"]] }, |
316 | false, { "Expected value but found invalid unicode escape code at character 2" } }, | 309 | false, { "Expected value but found invalid unicode escape code at character 2" } }, |
317 | 310 | ||
318 | -- Locale tests | 311 | -- Test locale support |
319 | -- | 312 | -- |
320 | -- The standard Lua interpreter is ANSI C online doesn't support locales | 313 | -- The standard Lua interpreter is ANSI C online doesn't support locales |
321 | -- by default. Force a known problematic locale to test strtod()/sprintf(). | 314 | -- by default. Force a known problematic locale to test strtod()/sprintf(). |
@@ -331,11 +324,56 @@ local all_tests = { | |||
331 | os.setlocale("C") | 324 | os.setlocale("C") |
332 | json.new() | 325 | json.new() |
333 | end }, | 326 | end }, |
327 | |||
328 | -- Test encode_keep_buffer() and enable_number_precision() | ||
329 | { "Set encode_keep_buffer(false)", | ||
330 | json.encode_keep_buffer, { false }, true, { "off" } }, | ||
331 | { "Set encode_number_precision(3)", | ||
332 | json.encode_number_precision, { 3 }, true, { 3 } }, | ||
333 | { "Encode number with precision 3", | ||
334 | json.encode, { 1/3 }, true, { "0.333" } }, | ||
335 | { "Set encode_number_precision(14)", | ||
336 | json.encode_number_precision, { 14 }, true, { 14 } }, | ||
337 | { "Set encode_keep_buffer(true)", | ||
338 | json.encode_keep_buffer, { true }, true, { "on" } }, | ||
339 | |||
340 | -- Test config API errors | ||
341 | -- Function is listed as '?' due to pcall | ||
342 | { "Set encode_number_precision(0)", | ||
343 | json.encode_number_precision, { 0 }, | ||
344 | false, { "bad argument #1 to '?' (expected integer between 1 and 14)" } }, | ||
345 | { "Set encode_number_precision(\"five\")", | ||
346 | json.encode_number_precision, { "five" }, | ||
347 | false, { "bad argument #1 to '?' (number expected, got string)" } }, | ||
348 | { "Set encode_keep_buffer(nil, true)", | ||
349 | json.encode_keep_buffer, { nil, true }, | ||
350 | false, { "bad argument #2 to '?' (found too many arguments)" } }, | ||
351 | { "Set encode_max_depth(\"wrong\")", | ||
352 | json.encode_max_depth, { "wrong" }, | ||
353 | false, { "bad argument #1 to '?' (number expected, got string)" } }, | ||
354 | { "Set decode_max_depth(0)", | ||
355 | json.decode_max_depth, { "0" }, | ||
356 | false, { "bad argument #1 to '?' (expected integer between 1 and 2147483647)" } }, | ||
357 | { "Set encode_invalid_numbers(-2)", | ||
358 | json.encode_invalid_numbers, { -2 }, | ||
359 | false, { "bad argument #1 to '?' (invalid option '-2')" } }, | ||
360 | { "Set decode_invalid_numbers(true, false)", | ||
361 | json.decode_invalid_numbers, { true, false }, | ||
362 | false, { "bad argument #2 to '?' (found too many arguments)" } }, | ||
363 | { "Set encode_sparse_array(\"not quite on\")", | ||
364 | json.encode_sparse_array, { "not quite on" }, | ||
365 | false, { "bad argument #1 to '?' (invalid option 'not quite on')" } }, | ||
366 | |||
367 | { "Reset Lua CJSON configuration", function () json = json.new() end }, | ||
368 | -- Wrap in a function to ensure the table returned by json.new() is used | ||
369 | { "Check encode_sparse_array()", | ||
370 | function (...) return json.encode_sparse_array(...) end, { }, | ||
371 | true, { "off", 2, 10 } }, | ||
334 | } | 372 | } |
335 | 373 | ||
336 | print(string.format("Testing Lua CJSON version %s\n", json.version)) | 374 | print(string.format("Testing Lua CJSON version %s\n", json._VERSION)) |
337 | 375 | ||
338 | util.run_test_group(all_tests) | 376 | util.run_test_group(cjson_tests) |
339 | 377 | ||
340 | for _, filename in ipairs(arg) do | 378 | for _, filename in ipairs(arg) do |
341 | util.run_test("Decode cycle " .. filename, test_decode_cycle, { filename }, | 379 | util.run_test("Decode cycle " .. filename, test_decode_cycle, { filename }, |