Merge branch '1.1.x' into 1.2.x
[luajson.git] / tests / lunit-simple-decode.lua
blob73c4c45e478a7fb7334d238671321fd781d1d0e4
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_null()
23 local result = assert(json.decode('[1, null, 3]', json.decode.simple))
24 assert_equal(1, result[1])
25 assert_nil(result[2])
26 assert_equal(3, result[3])
27 assert_equal(3, result.n)
28 end
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)
36 end
38 function test_decode_object_simple_with_null()
39 local result = assert(json.decode('{x: null}', json.decode.simple))
40 assert_nil(result.x)
41 assert_nil(next(result))
42 end
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))
48 end
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])
54 end
56 function test_decode_object_with_stringized_numeric_keys_force_numeric()
57 local result = assert(
58 json.decode(
59 '{"1": "one"}',
60 { object = { setObjectKey = assert(json.decode.util.setObjectKeyForceNumber) } }
63 assert_equal(nil, result["1"])
64 assert_equal("one", result[1])
65 end