1 local json
= require("json")
2 local lunit
= require("lunit")
4 -- Test module for handling the decoding with 'empties' allowed
6 _ENV
= lunit
.module("lunit-empties-decode", 'seeall')
8 module("lunit-empties-decode", lunit
.testcase
, package
.seeall
)
13 allowEmptyElement
= true
16 allowEmptyElement
= true,
20 allowEmptyElement
= true,
23 local options_notrailing
= {
25 allowEmptyElement
= true,
29 allowEmptyElement
= true,
30 allowUndefined
= true,
34 allowEmptyElement
= true,
38 local options_simple_null
= {
40 allowEmptyElement
= true
43 allowEmptyElement
= true,
47 allowEmptyElement
= true,
55 function test_decode_array_with_only_null()
56 local result
= assert(json
.decode('[null]', options_simple_null
))
58 assert_equal(1, result
.n
)
59 local result
= assert(json
.decode('[null]', options
))
60 assert_equal(json
.util
.null
, result
[1])
61 assert_equal(1, #result
)
64 function test_decode_array_with_empties()
65 local result
= assert(json
.decode('[,]', options_simple_null
))
67 assert_equal(1, result
.n
)
68 local result
= assert(json
.decode('[,]', options
))
69 assert_equal(json
.util
.undefined
, result
[1])
70 assert_equal(1, #result
)
72 local result
= assert(json
.decode('[,]', options_notrailing
))
73 assert_equal(json
.util
.undefined
, result
[1])
74 assert_equal(json
.util
.undefined
, result
[2])
75 assert_equal(2, #result
)
78 function test_decode_array_with_null()
79 local result
= assert(json
.decode('[1, null, 3]', options_simple_null
))
80 assert_equal(1, result
[1])
82 assert_equal(3, result
[3])
83 assert_equal(3, result
.n
)
84 local result
= assert(json
.decode('[1, null, 3]', options
))
85 assert_equal(1, result
[1])
86 assert_equal(json
.util
.null
, result
[2])
87 assert_equal(3, result
[3])
89 function test_decode_array_with_empty()
90 local result
= assert(json
.decode('[1,, 3]', options_simple_null
))
91 assert_equal(1, result
[1])
93 assert_equal(3, result
[3])
94 assert_equal(3, result
.n
)
95 local result
= assert(json
.decode('[1,, 3]', options
))
96 assert_equal(1, result
[1])
97 assert_equal(json
.util
.undefined
, result
[2])
98 assert_equal(3, result
[3])
101 function test_decode_small_array_with_trailing_null()
102 local result
= assert(json
.decode('[1, null]', options_simple_null
))
103 assert_equal(1, result
[1])
104 assert_nil(result
[2])
105 assert_equal(2, result
.n
)
106 local result
= assert(json
.decode('[1, ]', options_simple_null
))
107 assert_equal(1, result
[1])
108 assert_equal(1, #result
)
109 local result
= assert(json
.decode('[1, ]', options
))
110 assert_equal(1, result
[1])
111 assert_equal(1, #result
)
112 local result
= assert(json
.decode('[1, ]', options_notrailing
))
113 assert_equal(1, result
[1])
114 assert_equal(json
.util
.undefined
, result
[2])
115 assert_equal(2, #result
)
118 function test_decode_array_with_trailing_null()
119 local result
= assert(json
.decode('[1, null, 3, null]', options_simple_null
))
120 assert_equal(1, result
[1])
121 assert_nil(result
[2])
122 assert_equal(3, result
[3])
123 assert_nil(result
[4])
124 assert_equal(4, result
.n
)
125 local result
= assert(json
.decode('[1, null, 3, null]', options
))
126 assert_equal(1, result
[1])
127 assert_equal(json
.util
.null
, result
[2])
128 assert_equal(3, result
[3])
129 assert_equal(json
.util
.null
, result
[4])
130 assert_equal(4, #result
)
131 local result
= assert(json
.decode('[1, , 3, ]', options
))
132 assert_equal(1, result
[1])
133 assert_equal(json
.util
.undefined
, result
[2])
134 assert_equal(3, result
[3])
135 assert_equal(3, #result
)
136 local result
= assert(json
.decode('[1, , 3, ]', options_notrailing
))
137 assert_equal(1, result
[1])
138 assert_equal(json
.util
.undefined
, result
[2])
139 assert_equal(3, result
[3])
140 assert_equal(json
.util
.undefined
, result
[4])
141 assert_equal(4, #result
)
144 function test_decode_object_with_null()
145 local result
= assert(json
.decode('{x: null}', options_simple_null
))
147 assert_nil(next(result
))
149 local result
= assert(json
.decode('{x: null}', options
))
150 assert_equal(json
.util
.null
, result
.x
)
152 local result
= assert(json
.decode('{x: }', options_simple_null
))
154 assert_nil(next(result
))
156 local result
= assert(json
.decode('{x: }', options
))
157 assert_equal(json
.util
.undefined
, result
.x
)
159 -- Handle the trailing comma case
160 local result
= assert(json
.decode('{x: ,}', options_simple_null
))
162 assert_nil(next(result
))
164 local result
= assert(json
.decode('{x: ,}', options
))
165 assert_equal(json
.util
.undefined
, result
.x
)
167 -- NOTE: Trailing comma must be allowed explicitly in this case
168 assert_error(function()
169 json
.decode('{x: ,}', options_notrailing
)
172 -- Standard setup doesn't allow empties
173 assert_error(function()
177 function test_decode_bigger_object_with_null()
178 local result
= assert(json
.decode('{y: 1, x: null}', options_simple_null
))
179 assert_equal(1, result
.y
)
182 local result
= assert(json
.decode('{y: 1, x: null}', options
))
183 assert_equal(1, result
.y
)
184 assert_equal(json
.util
.null
, result
.x
)
186 local result
= assert(json
.decode('{y: 1, x: }', options_simple_null
))
187 assert_equal(1, result
.y
)
189 local result
= assert(json
.decode('{x: , y: 1}', options_simple_null
))
190 assert_equal(1, result
.y
)
193 local result
= assert(json
.decode('{y: 1, x: }', options
))
194 assert_equal(1, result
.y
)
195 assert_equal(json
.util
.undefined
, result
.x
)
197 local result
= assert(json
.decode('{x: , y: 1}', options
))
198 assert_equal(1, result
.y
)
199 assert_equal(json
.util
.undefined
, result
.x
)
201 -- Handle the trailing comma case
202 local result
= assert(json
.decode('{y: 1, x: , }', options_simple_null
))
203 assert_equal(1, result
.y
)
205 local result
= assert(json
.decode('{x: , y: 1, }', options_simple_null
))
206 assert_equal(1, result
.y
)
209 local result
= assert(json
.decode('{y: 1, x: ,}', options
))
210 assert_equal(1, result
.y
)
211 assert_equal(json
.util
.undefined
, result
.x
)
213 local result
= assert(json
.decode('{x: , y: 1, }', options
))
214 assert_equal(1, result
.y
)
215 assert_equal(json
.util
.undefined
, result
.x
)
217 -- NOTE: Trailing comma must be allowed explicitly in this case as there is no such thing as an "empty" key:value pair
218 assert_error(function()
219 json
.decode('{y: 1, x: ,}', options_notrailing
)
221 assert_error(function()
222 json
.decode('{x: , y: 1, }', options_notrailing
)
226 function test_decode_call_with_empties()
227 local result
= assert(json
.decode('call(,)', options_simple_null
))
228 result
= result
.parameters
229 assert_nil(result
[1])
230 assert_equal(1, result
.n
)
231 local result
= assert(json
.decode('call(,)', options
))
232 result
= result
.parameters
233 assert_equal(json
.util
.undefined
, result
[1])
234 assert_equal(1, #result
)
236 local result
= assert(json
.decode('call(,)', options_notrailing
))
237 result
= result
.parameters
238 assert_equal(json
.util
.undefined
, result
[1])
239 assert_equal(json
.util
.undefined
, result
[2])
240 assert_equal(2, #result
)
245 function test_call_with_empties_and_trailing()
246 local result
= assert(json
.decode('call(1, null, 3, null)', options_simple_null
))
247 result
= result
.parameters
248 assert_equal(1, result
[1])
249 assert_nil(result
[2])
250 assert_equal(3, result
[3])
251 assert_nil(result
[4])
252 assert_equal(4, result
.n
)
253 local result
= assert(json
.decode('call(1, null, 3, null)', options
))
254 result
= result
.parameters
255 assert_equal(1, result
[1])
256 assert_equal(json
.util
.null
, result
[2])
257 assert_equal(3, result
[3])
258 assert_equal(json
.util
.null
, result
[4])
259 assert_equal(4, #result
)
260 local result
= assert(json
.decode('call(1, , 3, )', options
))
261 result
= result
.parameters
262 assert_equal(1, result
[1])
263 assert_equal(json
.util
.undefined
, result
[2])
264 assert_equal(3, result
[3])
265 assert_equal(3, #result
)
266 local result
= assert(json
.decode('call(1, , 3, )', options_notrailing
))
267 result
= result
.parameters
268 assert_equal(1, result
[1])
269 assert_equal(json
.util
.undefined
, result
[2])
270 assert_equal(3, result
[3])
271 assert_equal(json
.util
.undefined
, result
[4])
272 assert_equal(4, #result
)