base: updates Travis-CI build structure to use sample
[luajson.git] / tests / lunit-tests.lua
blob30e82df9a24d5c59701ce1ab62984d7d33364dbb
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 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}))
36 end
38 local strictDecoder = json.decode.getDecoder(true)
40 local function buildStrictDecoder(f)
41 return testutil.buildPatchedDecoder(f, strictDecoder)
42 end
43 local function buildFailedStrictDecoder(f)
44 return testutil.buildFailedPatchedDecoder(f, strictDecoder)
45 end
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)
51 else
52 _M[k .. "_strict_gen"] = buildStrictDecoder(v)
53 end
54 end
55 end