1 local json
= require("json")
2 local lunit
= require("lunit")
4 -- Test module for handling the simple decoding that behaves more like expected
5 module("lunit-simple-decode", lunit
.testcase
, package
.seeall
)
7 function test_decode_simple_undefined()
8 assert_nil(json
.decode('undefined', json
.decode
.simple
))
10 function test_decode_default_undefined()
11 assert_equal(json
.util
.undefined
, json
.decode('undefined'))
14 function test_decode_simple_null()
15 assert_nil(json
.decode('null', json
.decode
.simple
))
18 function test_decode_default_null()
19 assert_equal(json
.util
.null
, json
.decode('null'))
22 function test_decode_array_simple_with_only_null()
23 local result
= assert(json
.decode('[null]', json
.decode
.simple
))
25 assert_equal(1, result
.n
)
28 function test_decode_array_default_with_only_null()
29 local result
= assert(json
.decode('[null]'))
30 assert_equal(json
.util
.null
, result
[1])
31 assert_equal(1, #result
)
34 function test_decode_array_simple_with_null()
35 local result
= assert(json
.decode('[1, null, 3]', json
.decode
.simple
))
36 assert_equal(1, result
[1])
38 assert_equal(3, result
[3])
39 assert_equal(3, result
.n
)
42 function test_decode_array_default_with_null()
43 local result
= assert(json
.decode('[1, null, 3]'))
44 assert_equal(1, result
[1])
45 assert_equal(json
.util
.null
, result
[2])
46 assert_equal(3, result
[3])
47 assert_equal(3, #result
)
50 function test_decode_small_array_simple_with_trailing_null()
51 local result
= assert(json
.decode('[1, null]', json
.decode
.simple
))
52 assert_equal(1, result
[1])
54 assert_equal(2, result
.n
)
57 function test_decode_small_array_default_with_trailing_null()
58 local result
= assert(json
.decode('[1, null]'))
59 assert_equal(1, result
[1])
60 assert_equal(json
.util
.null
, result
[2])
61 assert_equal(2, #result
)
65 function test_decode_small_array_simple_with_trailing_null()
66 local result
= assert(json
.decode('[1, null]', json
.decode
.simple
))
67 assert_equal(1, result
[1])
69 assert_equal(2, result
.n
)
72 function test_decode_small_array_default_with_trailing_null()
73 local result
= assert(json
.decode('[1, null]'))
74 assert_equal(1, result
[1])
75 assert_equal(json
.util
.null
, result
[2])
76 assert_equal(2, #result
)
80 function test_decode_array_simple_with_trailing_null()
81 local result
= assert(json
.decode('[1, null, 3, null]', json
.decode
.simple
))
82 assert_equal(1, result
[1])
84 assert_equal(3, result
[3])
86 assert_equal(4, result
.n
)
89 function test_decode_array_default_with_trailing_null()
90 local result
= assert(json
.decode('[1, null, 3, null]'))
91 assert_equal(1, result
[1])
92 assert_equal(json
.util
.null
, result
[2])
93 assert_equal(3, result
[3])
94 assert_equal(json
.util
.null
, result
[4])
95 assert_equal(4, #result
)
99 function test_decode_object_simple_with_null()
100 local result
= assert(json
.decode('{x: null}', json
.decode
.simple
))
102 assert_nil(next(result
))
105 function test_decode_object_default_with_null()
106 local result
= assert(json
.decode('{x: null}'))
107 assert_equal(json
.util
.null
, result
.x
)
108 assert_not_nil(next(result
))
111 function test_decode_object_with_stringized_numeric_keys_default()
112 local result
= assert(json
.decode('{"1": "one"}'))
113 assert_equal("one", result
["1"])
114 assert_equal(nil, result
[1])
117 function test_decode_object_with_stringized_numeric_keys_force_numeric()
118 local result
= assert(
121 { object
= { setObjectKey
= assert(json
.decode
.util
.setObjectKeyForceNumber
) } }
124 assert_equal(nil, result
["1"])
125 assert_equal("one", result
[1])