license: updates copyright year
[luajson.git] / tests / lunit-empties-decode.lua
blob61207ccbbab6254e2a5da2a13721a6ba0ad5ce79
1 local json = require("json")
2 local lunit = require("lunit")
4 -- Test module for handling the decoding with 'empties' allowed
5 if not module then
6 _ENV = lunit.module("lunit-empties-decode", 'seeall')
7 else
8 module("lunit-empties-decode", lunit.testcase, package.seeall)
9 end
11 local options = {
12 array = {
13 allowEmptyElement = true
15 calls = {
16 allowEmptyElement = true,
17 allowUndefined = true
19 object = {
20 allowEmptyElement = true,
23 local options_notrailing = {
24 array = {
25 allowEmptyElement = true,
26 trailingComma = false
28 calls = {
29 allowEmptyElement = true,
30 allowUndefined = true,
31 trailingComma = false
33 object = {
34 allowEmptyElement = true,
35 trailingComma = false
38 local options_simple_null = {
39 array = {
40 allowEmptyElement = true
42 calls = {
43 allowEmptyElement = true,
44 allowUndefined = true
46 object = {
47 allowEmptyElement = true,
49 others = {
50 null = false,
51 undefined = false
55 function test_decode_array_with_only_null()
56 local result = assert(json.decode('[null]', options_simple_null))
57 assert_nil(result[1])
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)
62 end
64 function test_decode_array_with_empties()
65 local result = assert(json.decode('[,]', options_simple_null))
66 assert_nil(result[1])
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)
76 end
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])
81 assert_nil(result[2])
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])
88 end
89 function test_decode_array_with_empty()
90 local result = assert(json.decode('[1,, 3]', options_simple_null))
91 assert_equal(1, result[1])
92 assert_nil(result[2])
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])
99 end
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))
146 assert_nil(result.x)
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))
153 assert_nil(result.x)
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))
161 assert_nil(result.x)
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)
170 end)
172 -- Standard setup doesn't allow empties
173 assert_error(function()
174 json.decode('{x: }')
175 end)
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)
180 assert_nil(result.x)
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)
188 assert_nil(result.x)
189 local result = assert(json.decode('{x: , y: 1}', options_simple_null))
190 assert_equal(1, result.y)
191 assert_nil(result.x)
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)
204 assert_nil(result.x)
205 local result = assert(json.decode('{x: , y: 1, }', options_simple_null))
206 assert_equal(1, result.y)
207 assert_nil(result.x)
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)
220 end)
221 assert_error(function()
222 json.decode('{x: , y: 1, }', options_notrailing)
223 end)
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)