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)
9 _ENV
= lunit
.module("lunit-tests", 'seeall')
12 module("lunit-tests", lunit
.testcase
, package
.seeall
)
17 _G
["decode"] = json
.decode
.getDecoder(false)
20 function test_array_empty()
21 local ret
= assert_table(decode("[]"))
26 function test_array_trailComma_nostrict()
27 local ret
= assert_table(decode("[true,]"))
28 assert_equal(true, ret
[1])
29 assert_nil(next(ret
, 1))
33 function test_array_innerComma()
34 assert_error(function()
35 decode("[true,,true]")
39 function test_preprocess()
40 assert_equal('"Hello"', json
.encode(1, {preProcess
= function() return "Hello" end}))
41 assert_equal('-1', json
.encode(1, {preProcess
= function(x
) return -x
end}))
42 assert_equal('-Infinity', json
.encode(1/0, {preProcess
= function(x
) return -x
end}))
45 local strictDecoder
= json
.decode
.getDecoder(true)
47 local function buildStrictDecoder(f
)
48 return testutil
.buildPatchedDecoder(f
, strictDecoder
)
50 local function buildFailedStrictDecoder(f
)
51 return testutil
.buildFailedPatchedDecoder(f
, strictDecoder
)
53 -- SETUP CHECKS FOR SEQUENCE OF DECODERS
54 for k
, v
in pairs(TEST_ENV
) do
55 if k
:match("^test_") and not k
:match("_gen$") then
56 if k
:match("_nostrict") then
57 TEST_ENV
[k
.. "_strict_gen"] = buildFailedStrictDecoder(v
)
59 TEST_ENV
[k
.. "_strict_gen"] = buildStrictDecoder(v
)