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_null()
23 local result
= assert(json
.decode('[1, null, 3]', json
.decode
.simple
))
24 assert_equal(1, result
[1])
26 assert_equal(3, result
[3])
27 assert_equal(3, result
.n
)
30 function test_decode_array_default_with_null()
31 local result
= assert(json
.decode('[1, null, 3]'))
32 assert_equal(1, result
[1])
33 assert_equal(json
.util
.null
, result
[2])
34 assert_equal(3, result
[3])
35 assert_equal(3, #result
)
38 function test_decode_object_simple_with_null()
39 local result
= assert(json
.decode('{x: null}', json
.decode
.simple
))
41 assert_nil(next(result
))
44 function test_decode_object_default_with_null()
45 local result
= assert(json
.decode('{x: null}'))
46 assert_equal(json
.util
.null
, result
.x
)
47 assert_not_nil(next(result
))
50 function test_decode_object_with_stringized_numeric_keys_default()
51 local result
= assert(json
.decode('{"1": "one"}'))
52 assert_equal("one", result
["1"])
53 assert_equal(nil, result
[1])
56 function test_decode_object_with_stringized_numeric_keys_force_numeric()
57 local result
= assert(
60 { object
= { setObjectKey
= assert(json
.decode
.util
.setObjectKeyForceNumber
) } }
63 assert_equal(nil, result
["1"])
64 assert_equal("one", result
[1])