luajson 0.9.1 for luarocks
[luajson.git] / tests / lunit-tests.lua
blobdca04bee459da5d2184d786f2dcb0966e63ac0de
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)
9 function setup()
10 _G["decode"] = json.decode.getDecoder(false)
11 end
13 function test_array_empty()
14 local ret = assert_table(decode("[]"))
15 assert_equal(0, #ret)
16 assert_nil(next(ret))
17 end
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))
23 assert_equal(1, #ret)
24 end
26 function test_array_innerComma()
27 assert_error(function()
28 decode("[true,,true]")
29 end)
30 end
32 local strictDecoder = json.decode.getDecoder(true)
34 local function buildStrictDecoder(f)
35 return testutil.buildPatchedDecoder(f, strictDecoder)
36 end
37 local function buildFailedStrictDecoder(f)
38 return testutil.buildFailedPatchedDecoder(f, strictDecoder)
39 end
40 -- SETUP CHECKS FOR SEQUENCE OF DECODERS
41 for k, v in pairs(_M) do
42 if k:match("^test_") and not k:match("_gen$") then
43 if k:match("_nostrict") then
44 _M[k .. "_strict_gen"] = buildFailedStrictDecoder(v)
45 else
46 _M[k .. "_strict_gen"] = buildStrictDecoder(v)
47 end
48 end
49 end