diff options
| author | Mark Pulford <mark@kyne.com.au> | 2011-05-10 19:52:19 +0930 |
|---|---|---|
| committer | Mark Pulford <mark@kyne.com.au> | 2011-05-10 19:52:19 +0930 |
| commit | 2323b5d0839412f276415f661e3b058b55f5bd4c (patch) | |
| tree | 6183a361c7778dbb2b9640555748429e6e6a9d3e /tests/test.lua | |
| parent | 88fba505797c8c828c3737395b7351da0c9bd8fe (diff) | |
| download | lua-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.
Diffstat (limited to '')
| -rwxr-xr-x | tests/test.lua | 31 |
1 files changed, 30 insertions, 1 deletions
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) |
