base: updates rock creation tool to include tag version
[luajson.git] / tests / lunit-simple-decode.lua
blob376ac371e5429e29fd78d414aab755b96eb25159
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))
9 end
10 function test_decode_default_undefined()
11 assert_equal(json.util.undefined, json.decode('undefined'))
12 end
14 function test_decode_simple_null()
15 assert_nil(json.decode('null', json.decode.simple))
16 end
18 function test_decode_default_null()
19 assert_equal(json.util.null, json.decode('null'))
20 end
22 function test_decode_array_simple_with_only_null()
23 local result = assert(json.decode('[null]', json.decode.simple))
24 assert_nil(result[1])
25 assert_equal(1, result.n)
26 end
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)
32 end
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])
37 assert_nil(result[2])
38 assert_equal(3, result[3])
39 assert_equal(3, result.n)
40 end
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)
48 end
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])
53 assert_nil(result[2])
54 assert_equal(2, result.n)
55 end
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)
62 end
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])
68 assert_nil(result[2])
69 assert_equal(2, result.n)
70 end
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)
77 end
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])
83 assert_nil(result[2])
84 assert_equal(3, result[3])
85 assert_nil(result[4])
86 assert_equal(4, result.n)
87 end
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)
96 end
99 function test_decode_object_simple_with_null()
100 local result = assert(json.decode('{x: null}', json.decode.simple))
101 assert_nil(result.x)
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(
119 json.decode(
120 '{"1": "one"}',
121 { object = { setObjectKey = assert(json.decode.util.setObjectKeyForceNumber) } }
124 assert_equal(nil, result["1"])
125 assert_equal("one", result[1])