diff options
Diffstat (limited to 'tests/test.lua')
-rwxr-xr-x | tests/test.lua | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/tests/test.lua b/tests/test.lua new file mode 100755 index 0000000..9424e8f --- /dev/null +++ b/tests/test.lua | |||
@@ -0,0 +1,132 @@ | |||
1 | #!/usr/bin/env lua | ||
2 | |||
3 | require "common" | ||
4 | local json = require "cjson" | ||
5 | |||
6 | local cjson_test_non_default = true | ||
7 | |||
8 | function run_tests(tests, func) | ||
9 | for k, v in ipairs(tests) do | ||
10 | local success, msg = pcall(func, v) | ||
11 | if not success then | ||
12 | print("Error: " .. msg) | ||
13 | end | ||
14 | print() | ||
15 | end | ||
16 | end | ||
17 | |||
18 | local simple_value_tests = { | ||
19 | [[ "test string" ]], | ||
20 | [[ -5e3 ]], | ||
21 | [[ null ]], | ||
22 | [[ true ]], | ||
23 | [[ false ]], | ||
24 | [[ { "1": "one", "3": "three" } ]] | ||
25 | } | ||
26 | |||
27 | local numeric_tests = { | ||
28 | "[ 0.0, -1, 0.3e-3, 1023.2 ]", | ||
29 | "[ 00123 ]", | ||
30 | "[ 05.2 ]", | ||
31 | "[ 0e10 ]", | ||
32 | "[ 0x6 ]", | ||
33 | "[ +Inf ]", | ||
34 | "[ Inf ]", | ||
35 | "[ -Inf ]", | ||
36 | "[ +Infinity ]", | ||
37 | "[ Infinity ]", | ||
38 | "[ -Infinity ]", | ||
39 | "[ +NaN ]", | ||
40 | "[ NaN ]", | ||
41 | "[ -NaN ]", | ||
42 | "[ Infrared ]", | ||
43 | "[ Noodle ]" | ||
44 | } | ||
45 | |||
46 | local object_tests = { | ||
47 | { [5] = "sparse test" }, | ||
48 | { [6] = "sparse test" }, | ||
49 | {{{{{{{{{{{{{{{{{{{{{ "nested" }}}}}}}}}}}}}}}}}}}}} | ||
50 | } | ||
51 | |||
52 | local decode_error_tests = { | ||
53 | '{ "unexpected eof": ', | ||
54 | '{ "extra data": true }, false', | ||
55 | [[ { "bad escape \q code" } ]], | ||
56 | [[ { "bad unicode \u0f6 escape" } ]], | ||
57 | [[ [ "bad barewood", test ] ]], | ||
58 | "[ -+12 ]", | ||
59 | "-v", | ||
60 | "[ 0.4eg10 ]", | ||
61 | } | ||
62 | |||
63 | local simple_encode_tests = { | ||
64 | json.null, true, false, { }, 10, "hello" | ||
65 | } | ||
66 | |||
67 | |||
68 | local function gen_ascii(max) | ||
69 | local chars = {} | ||
70 | for i = 0, max do | ||
71 | chars[i] = string.char(i) | ||
72 | end | ||
73 | return table.concat(chars) | ||
74 | end | ||
75 | |||
76 | local function decode_encode(text) | ||
77 | print("==JSON=> " .. text) | ||
78 | local obj_data = json.decode(text) | ||
79 | dump_value(obj_data) | ||
80 | local obj_json = json.encode(obj_data) | ||
81 | print(obj_json) | ||
82 | end | ||
83 | local function encode_decode(obj) | ||
84 | print("==OBJ==> ") | ||
85 | dump_value(obj) | ||
86 | local obj_json = json.encode(obj) | ||
87 | print(obj_json) | ||
88 | local obj_data = json.decode(obj_json) | ||
89 | dump_value(obj_data) | ||
90 | end | ||
91 | |||
92 | run_tests(simple_value_tests, decode_encode) | ||
93 | run_tests(decode_error_tests, decode_encode) | ||
94 | |||
95 | run_tests(numeric_tests, decode_encode) | ||
96 | |||
97 | if cjson_test_non_default then | ||
98 | print("=== Disabling strict numbers ===") | ||
99 | json.strict_numbers(false) | ||
100 | run_tests(numeric_tests, decode_encode) | ||
101 | json.strict_numbers(true) | ||
102 | end | ||
103 | |||
104 | run_tests(object_tests, encode_decode) | ||
105 | print ("Encode tests..") | ||
106 | |||
107 | run_tests(simple_encode_tests, encode_decode) | ||
108 | |||
109 | if cjson_test_non_default then | ||
110 | print("=== Setting max_depth to 21, sparse_ratio to 5 ===") | ||
111 | json.max_depth(21) | ||
112 | json.sparse_ratio(5) | ||
113 | run_tests(object_tests, encode_decode) | ||
114 | end | ||
115 | |||
116 | local ascii1 = gen_ascii(255) | ||
117 | print("Unprintable escapes:") | ||
118 | print(json.encode(ascii1)) | ||
119 | print("===") | ||
120 | local ascii2 = json.decode(json.encode(ascii)) | ||
121 | |||
122 | print("8bit clean encode/decode: " .. tostring(ascii1 ~= ascii2)) | ||
123 | |||
124 | for i = 1, #arg do | ||
125 | print(arg[i] .. " enc->dec..") | ||
126 | local obj1 = json.decode(file_load(arg[i])) | ||
127 | local obj2 = json.decode(json.encode(obj1)) | ||
128 | -- obj_compare(obj_json1, obj_json2) | ||
129 | print(".. unimplemented") | ||
130 | end | ||
131 | |||
132 | -- vi:ai et sw=4 ts=4: | ||