aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-05-10 19:52:19 +0930
committerMark Pulford <mark@kyne.com.au>2011-05-10 19:52:19 +0930
commit2323b5d0839412f276415f661e3b058b55f5bd4c (patch)
tree6183a361c7778dbb2b9640555748429e6e6a9d3e
parent88fba505797c8c828c3737395b7351da0c9bd8fe (diff)
downloadlua-cjson-2323b5d0839412f276415f661e3b058b55f5bd4c.tar.gz
lua-cjson-2323b5d0839412f276415f661e3b058b55f5bd4c.tar.bz2
lua-cjson-2323b5d0839412f276415f661e3b058b55f5bd4c.zip
Add extra encoding/nesting tests
- Use pcall() to call test config functions. - Test encoding with refuse_invalid_numbers() options. - Test encoding invalid types.
-rw-r--r--TODO6
-rw-r--r--tests/common.lua18
-rwxr-xr-xtests/test.lua31
3 files changed, 45 insertions, 10 deletions
diff --git a/TODO b/TODO
index 46ecb91..a17bc1b 100644
--- a/TODO
+++ b/TODO
@@ -1,6 +1,4 @@
1- Option to configure encoding escapes. 1- Option to configure encoding escapes.
2 2
3- Make encode/decode routines thread safe (within the same lua_State) 3- Make encode/decode routines OS thread safe (within the same lua_State)
4 - Optionally create an object. Clone function. 4 - Optionally create an object for settings. Clone function.
5
6- run_test_group "config" function calls should be protected by pcall?
diff --git a/tests/common.lua b/tests/common.lua
index 63d37c0..d712c83 100644
--- a/tests/common.lua
+++ b/tests/common.lua
@@ -40,7 +40,7 @@ function serialise_table(value, indent, depth)
40 end 40 end
41 depth = depth + 1 41 depth = depth + 1
42 if depth > 50 then 42 if depth > 50 then
43 return "ERROR: Too many nested tables" 43 return "Cannot serialise any further: too many nested tables"
44 end 44 end
45 45
46 local max = is_array(value) 46 local max = is_array(value)
@@ -228,14 +228,22 @@ function run_test(testname, func, input, should_work, output)
228end 228end
229 229
230function run_test_group(testgroup, tests) 230function run_test_group(testgroup, tests)
231 function run_config(configname, func)
232 local success, msg = pcall(func)
233 print(string.format("==> Config %s: %s", configname, msg))
234 print()
235 end
236
237 function test_id(group, id)
238 return string.format("%s [%d]", group, id)
239 end
240
231 for k, v in ipairs(tests) do 241 for k, v in ipairs(tests) do
232 if type(v) == "function" then 242 if type(v) == "function" then
233 -- Useful for changing configuration during a batch 243 -- Useful for changing configuration during a batch
234 msg = v() 244 run_config(test_id(testgroup, k), v)
235 print(string.format("==> Config %s [%d]: %s", testgroup, k, msg))
236 print()
237 elseif type(v) == "table" then 245 elseif type(v) == "table" then
238 run_test(testgroup .. " [" .. k .. "]", unpack(v)) 246 run_test(test_id(testgroup, k), unpack(v))
239 else 247 else
240 error("Testgroup can only contain functions and tables") 248 error("Testgroup can only contain functions and tables")
241 end 249 end
diff --git a/tests/test.lua b/tests/test.lua
index 10d8989..b1395b0 100755
--- a/tests/test.lua
+++ b/tests/test.lua
@@ -132,6 +132,33 @@ local encode_table_tests = {
132 false, { "Cannot serialise, excessive nesting (6)" } } 132 false, { "Cannot serialise, excessive nesting (6)" } }
133} 133}
134 134
135local encode_error_tests = {
136 { json.encode, { { [false] = "wrong" } },
137 false, { "Cannot serialise boolean: table key must be a number or string" } },
138 { json.encode, { function () end },
139 false, { "Cannot serialise function: type not supported" } },
140 function ()
141 json.refuse_invalid_numbers("encode")
142 return 'Setting refuse_invalid_numbers("encode")'
143 end,
144 { json.encode, { NaN },
145 false, { "Cannot serialise number: must not be NaN or Inf" } },
146 { json.encode, { Inf },
147 false, { "Cannot serialise number: must not be NaN or Inf" } },
148 function ()
149 json.refuse_invalid_numbers(false)
150 return 'Setting refuse_invalid_numbers(false)'
151 end,
152 { json.encode, { NaN }, true, { "-nan" } },
153 { json.encode, { Inf }, true, { "inf" } },
154 function ()
155 json.refuse_invalid_numbers("encode")
156 return 'Setting refuse_invalid_numbers("encode")'
157 end,
158}
159
160local json_nested = string.rep("[", 100000) .. string.rep("]", 100000)
161
135local decode_error_tests = { 162local decode_error_tests = {
136 { json.decode, { '\0"\0"' }, 163 { json.decode, { '\0"\0"' },
137 false, { "JSON parser does not support UTF-16 or UTF-32" } }, 164 false, { "JSON parser does not support UTF-16 or UTF-32" } },
@@ -153,6 +180,8 @@ local decode_error_tests = {
153 false, { "Expected value but found invalid number at character 1" } }, 180 false, { "Expected value but found invalid number at character 1" } },
154 { json.decode, { '[ 0.4eg10 ]' }, 181 { json.decode, { '[ 0.4eg10 ]' },
155 false, { "Expected comma or array end but found invalid token at character 6" } }, 182 false, { "Expected comma or array end but found invalid token at character 6" } },
183 { json.decode, { json_nested },
184 false, { "stack overflow (too many nested data structures)" } }
156} 185}
157 186
158local escape_tests = { 187local escape_tests = {
@@ -184,10 +213,10 @@ run_test_group("decode numeric", decode_numeric_tests)
184-- - Sparse array exception.. 213-- - Sparse array exception..
185-- - .. 214-- - ..
186-- cjson.encode_sparse_array(true, 2, 3) 215-- cjson.encode_sparse_array(true, 2, 3)
187-- run_test_group("encode error", encode_error_tests)
188 216
189run_test_group("encode table", encode_table_tests) 217run_test_group("encode table", encode_table_tests)
190run_test_group("decode error", decode_error_tests) 218run_test_group("decode error", decode_error_tests)
219run_test_group("encode error", encode_error_tests)
191run_test_group("escape", escape_tests) 220run_test_group("escape", escape_tests)
192 221
193cjson.encode_max_depth(20) 222cjson.encode_max_depth(20)