1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
local json = require("json")
local lunit = require("lunit")
-- Test module for handling the simple decoding that behaves more like expected
if not module then
_ENV = lunit.module("lunit-simple-decode", 'seeall')
else
module("lunit-simple-decode", lunit.testcase, package.seeall)
end
function test_decode_simple_undefined()
assert_nil(json.decode('undefined', json.decode.simple))
end
function test_decode_default_undefined()
assert_equal(json.util.undefined, json.decode('undefined'))
end
function test_decode_simple_null()
assert_nil(json.decode('null', json.decode.simple))
end
function test_decode_default_null()
assert_equal(json.util.null, json.decode('null'))
end
function test_decode_array_simple_with_only_null()
local result = assert(json.decode('[null]', json.decode.simple))
assert_nil(result[1])
assert_equal(1, result.n)
end
function test_decode_array_default_with_only_null()
local result = assert(json.decode('[null]'))
assert_equal(json.util.null, result[1])
assert_equal(1, #result)
end
function test_decode_array_simple_with_null()
local result = assert(json.decode('[1, null, 3]', json.decode.simple))
assert_equal(1, result[1])
assert_nil(result[2])
assert_equal(3, result[3])
assert_equal(3, result.n)
end
function test_decode_array_default_with_null()
local result = assert(json.decode('[1, null, 3]'))
assert_equal(1, result[1])
assert_equal(json.util.null, result[2])
assert_equal(3, result[3])
assert_equal(3, #result)
end
function test_decode_small_array_simple_with_trailing_null()
local result = assert(json.decode('[1, null]', json.decode.simple))
assert_equal(1, result[1])
assert_nil(result[2])
assert_equal(2, result.n)
end
function test_decode_small_array_default_with_trailing_null()
local result = assert(json.decode('[1, null]'))
assert_equal(1, result[1])
assert_equal(json.util.null, result[2])
assert_equal(2, #result)
end
function test_decode_small_array_simple_with_trailing_null()
local result = assert(json.decode('[1, null]', json.decode.simple))
assert_equal(1, result[1])
assert_nil(result[2])
assert_equal(2, result.n)
end
function test_decode_small_array_default_with_trailing_null()
local result = assert(json.decode('[1, null]'))
assert_equal(1, result[1])
assert_equal(json.util.null, result[2])
assert_equal(2, #result)
end
function test_decode_array_simple_with_trailing_null()
local result = assert(json.decode('[1, null, 3, null]', json.decode.simple))
assert_equal(1, result[1])
assert_nil(result[2])
assert_equal(3, result[3])
assert_nil(result[4])
assert_equal(4, result.n)
end
function test_decode_array_default_with_trailing_null()
local result = assert(json.decode('[1, null, 3, null]'))
assert_equal(1, result[1])
assert_equal(json.util.null, result[2])
assert_equal(3, result[3])
assert_equal(json.util.null, result[4])
assert_equal(4, #result)
end
function test_decode_object_simple_with_null()
local result = assert(json.decode('{x: null}', json.decode.simple))
assert_nil(result.x)
assert_nil(next(result))
end
function test_decode_object_default_with_null()
local result = assert(json.decode('{x: null}'))
assert_equal(json.util.null, result.x)
assert_not_nil(next(result))
end
function test_decode_object_with_stringized_numeric_keys_default()
local result = assert(json.decode('{"1": "one"}'))
assert_equal("one", result["1"])
assert_equal(nil, result[1])
end
function test_decode_object_with_stringized_numeric_keys_force_numeric()
local result = assert(
json.decode(
'{"1": "one"}',
{ object = { setObjectKey = assert(json.decode.util.setObjectKeyForceNumber) } }
)
)
assert_equal(nil, result["1"])
assert_equal("one", result[1])
end
|