1 local json
= require("json")
2 local lunit
= require("lunit")
4 -- Test module for handling the simple decoding that behaves more like expected
6 _ENV
= lunit
.module("lunit-simple-decode", 'seeall')
8 module("lunit-simple-decode", lunit
.testcase
, package
.seeall
)
11 function test_decode_simple_undefined()
12 assert_nil(json
.decode('undefined', json
.decode
.simple
))
14 function test_decode_default_undefined()
15 assert_equal(json
.util
.undefined
, json
.decode('undefined'))
18 function test_decode_simple_null()
19 assert_nil(json
.decode('null', json
.decode
.simple
))
22 function test_decode_default_null()
23 assert_equal(json
.util
.null
, json
.decode('null'))
26 function test_decode_array_simple_with_only_null()
27 local result
= assert(json
.decode('[null]', json
.decode
.simple
))
29 assert_equal(1, result
.n
)
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
)
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])
42 assert_equal(3, result
[3])
43 assert_equal(3, result
.n
)
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
)
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])
58 assert_equal(2, result
.n
)
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
)
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])
73 assert_equal(2, result
.n
)
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
)
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])
88 assert_equal(3, result
[3])
90 assert_equal(4, result
.n
)
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
))
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(
125 { object
= { setObjectKey
= assert(json
.decode
.util
.setObjectKeyForceNumber
) } }
128 assert_equal(nil, result
["1"])
129 assert_equal("one", result
[1])