1 local json
= require("json")
2 local lunit
= require("lunit")
3 local testutil
= require("testutil")
4 -- DECODE NOT 'local' due to requirement for testutil to access it
5 decode
= json
.decode
.getDecoder(false)
7 module("lunit-tests", lunit
.testcase
, package
.seeall
)
10 _G
["decode"] = json
.decode
.getDecoder(false)
13 function test_array_empty()
14 local ret
= assert_table(decode("[]"))
19 function test_array_trailComma_nostrict()
20 local ret
= assert_table(decode("[true,]"))
21 assert_equal(true, ret
[1])
22 assert_nil(next(ret
, 1))
26 function test_array_innerComma()
27 assert_error(function()
28 decode("[true,,true]")
32 function test_preprocess()
33 assert_equal('"Hello"', json
.encode(1, {preProcess
= function() return "Hello" end}))
34 assert_equal('-1', json
.encode(1, {preProcess
= function(x
) return -x
end}))
35 assert_equal('-Infinity', json
.encode(1/0, {preProcess
= function(x
) return -x
end}))
38 local strictDecoder
= json
.decode
.getDecoder(true)
40 local function buildStrictDecoder(f
)
41 return testutil
.buildPatchedDecoder(f
, strictDecoder
)
43 local function buildFailedStrictDecoder(f
)
44 return testutil
.buildFailedPatchedDecoder(f
, strictDecoder
)
46 -- SETUP CHECKS FOR SEQUENCE OF DECODERS
47 for k
, v
in pairs(_M
) do
48 if k
:match("^test_") and not k
:match("_gen$") then
49 if k
:match("_nostrict") then
50 _M
[k
.. "_strict_gen"] = buildFailedStrictDecoder(v
)
52 _M
[k
.. "_strict_gen"] = buildStrictDecoder(v
)