Merge pull request #49 from jokajak/bugfix/lpeg_version_check
[luajson.git] / tests / lunit-simple-decode.lua
blob86acace47a564a153340fef3c752a64b28df4c4f
1 local json = require("json")
2 local lunit = require("lunit")
4 -- Test module for handling the simple decoding that behaves more like expected
5 if not module then
6 _ENV = lunit.module("lunit-simple-decode", 'seeall')
7 else
8 module("lunit-simple-decode", lunit.testcase, package.seeall)
9 end
11 function test_decode_simple_undefined()
12 assert_nil(json.decode('undefined', json.decode.simple))
13 end
14 function test_decode_default_undefined()
15 assert_equal(json.util.undefined, json.decode('undefined'))
16 end
18 function test_decode_simple_null()
19 assert_nil(json.decode('null', json.decode.simple))
20 end
22 function test_decode_default_null()
23 assert_equal(json.util.null, json.decode('null'))
24 end
26 function test_decode_array_simple_with_only_null()
27 local result = assert(json.decode('[null]', json.decode.simple))
28 assert_nil(result[1])
29 assert_equal(1, result.n)
30 end
32 function test_decode_array_default_with_only_null()
33 local result = assert(json.decode('[null]'))
34 assert_equal(json.util.null, result[1])
35 assert_equal(1, #result)
36 end
38 function test_decode_array_simple_with_null()
39 local result = assert(json.decode('[1, null, 3]', json.decode.simple))
40 assert_equal(1, result[1])
41 assert_nil(result[2])
42 assert_equal(3, result[3])
43 assert_equal(3, result.n)
44 end
46 function test_decode_array_default_with_null()
47 local result = assert(json.decode('[1, null, 3]'))
48 assert_equal(1, result[1])
49 assert_equal(json.util.null, result[2])
50 assert_equal(3, result[3])
51 assert_equal(3, #result)
52 end
54 function test_decode_small_array_simple_with_trailing_null()
55 local result = assert(json.decode('[1, null]', json.decode.simple))
56 assert_equal(1, result[1])
57 assert_nil(result[2])
58 assert_equal(2, result.n)
59 end
61 function test_decode_small_array_default_with_trailing_null()
62 local result = assert(json.decode('[1, null]'))
63 assert_equal(1, result[1])
64 assert_equal(json.util.null, result[2])
65 assert_equal(2, #result)
66 end
69 function test_decode_small_array_simple_with_trailing_null()
70 local result = assert(json.decode('[1, null]', json.decode.simple))
71 assert_equal(1, result[1])
72 assert_nil(result[2])
73 assert_equal(2, result.n)
74 end
76 function test_decode_small_array_default_with_trailing_null()
77 local result = assert(json.decode('[1, null]'))
78 assert_equal(1, result[1])
79 assert_equal(json.util.null, result[2])
80 assert_equal(2, #result)
81 end
84 function test_decode_array_simple_with_trailing_null()
85 local result = assert(json.decode('[1, null, 3, null]', json.decode.simple))
86 assert_equal(1, result[1])
87 assert_nil(result[2])
88 assert_equal(3, result[3])
89 assert_nil(result[4])
90 assert_equal(4, result.n)
91 end
93 function test_decode_array_default_with_trailing_null()
94 local result = assert(json.decode('[1, null, 3, null]'))
95 assert_equal(1, result[1])
96 assert_equal(json.util.null, result[2])
97 assert_equal(3, result[3])
98 assert_equal(json.util.null, result[4])
99 assert_equal(4, #result)
103 function test_decode_object_simple_with_null()
104 local result = assert(json.decode('{x: null}', json.decode.simple))
105 assert_nil(result.x)
106 assert_nil(next(result))
109 function test_decode_object_default_with_null()
110 local result = assert(json.decode('{x: null}'))
111 assert_equal(json.util.null, result.x)
112 assert_not_nil(next(result))
115 function test_decode_object_with_stringized_numeric_keys_default()
116 local result = assert(json.decode('{"1": "one"}'))
117 assert_equal("one", result["1"])
118 assert_equal(nil, result[1])
121 function test_decode_object_with_stringized_numeric_keys_force_numeric()
122 local result = assert(
123 json.decode(
124 '{"1": "one"}',
125 { object = { setObjectKey = assert(json.decode.util.setObjectKeyForceNumber) } }
128 assert_equal(nil, result["1"])
129 assert_equal("one", result[1])