diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/common.lua | 18 | ||||
| -rwxr-xr-x | tests/test.lua | 31 |
2 files changed, 43 insertions, 6 deletions
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) | |||
| 228 | end | 228 | end |
| 229 | 229 | ||
| 230 | function run_test_group(testgroup, tests) | 230 | function 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 | ||
| 135 | local 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 | |||
| 160 | local json_nested = string.rep("[", 100000) .. string.rep("]", 100000) | ||
| 161 | |||
| 135 | local decode_error_tests = { | 162 | local 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 | ||
| 158 | local escape_tests = { | 187 | local 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 | ||
| 189 | run_test_group("encode table", encode_table_tests) | 217 | run_test_group("encode table", encode_table_tests) |
| 190 | run_test_group("decode error", decode_error_tests) | 218 | run_test_group("decode error", decode_error_tests) |
| 219 | run_test_group("encode error", encode_error_tests) | ||
| 191 | run_test_group("escape", escape_tests) | 220 | run_test_group("escape", escape_tests) |
| 192 | 221 | ||
| 193 | cjson.encode_max_depth(20) | 222 | cjson.encode_max_depth(20) |
