luajson 0.9.1 for luarocks
[luajson.git] / tests / regressionTest.lua
blob01fa01ecd61d77f25a9dde7793122fb075f04a3c
1 -- Additional path that may be required
2 require("json")
4 require("lfs")
6 local success = true
8 local function getFileData(fileName)
9 local f = io.open(fileName, 'rb')
10 if not f then return end
11 local data = f:read('*a')
12 f:close()
13 return data
14 end
16 local function putTempData(data)
17 local name = os.tmpname()
18 local f = assert(io.open(name, 'wb'))
19 f:write(data)
20 f:close()
21 return name
22 end
24 -- Ensure that the encoder/decoder can round-trip valid JSON
25 local function RoundTripTest(parseFunc, jsonData, luaData, fullRoundTrip)
26 local dataString = json.encode(luaData)
27 assert(dataString, "Couldn't encode the lua data")
28 local success, result = pcall(parseFunc, dataString)
29 if not success then
30 print("Could not parse the generated JSON of (", luaData)
31 print("GENERATED: [[" .. dataString .. "]]")
32 print("DATA STORED IN: ", putTempData(dataString))
33 return
34 end
35 if fullRoundTrip then
36 -- Ensure that whitespace is trimmed off ends
37 dataString = dataString:match("^[%s]*(.-)[%s]*$")
38 jsonData = jsonData:match("^[%s]*(.-)[%s]*$")
39 if dataString ~= jsonData then
40 print("Encoded values do not match")
41 print("ORIGINAL: << " .. jsonData .. " >>")
42 print("RE-ENCOD: << " .. dataString .. " >>")
43 return
44 end
45 end
46 return true
47 end
49 local function testFile(fileName, parseFunc, expectSuccess, fullRoundTrip)
50 local data = getFileData(fileName)
51 if not data then return end
52 io.write(".")
53 local succeed, result = pcall(parseFunc, data)
54 if expectSuccess ~= succeed then
55 print("Wrongly " .. (expectSuccess and "Failed" or "Succeeded") .. " on : " .. fileName .. "(" .. tostring(result) .. ")")
56 success = false
57 elseif succeed then
58 if not RoundTripTest(parseFunc, data, result, fullRoundTrip) then
59 print("FAILED TO ROUND TRIP: " .. fileName)
60 success = false
61 end
62 end
63 end
65 local function testDirectories(parseFunc, directories, ...)
66 if not directories then return end
67 for _,directory in ipairs(directories) do
68 if lfs.attributes(directory, 'mode') == 'directory' then
69 for f in lfs.dir(directory) do
70 testFile(directory .. "/" .. f, parseFunc, ...)
71 end
72 end
73 end
74 io.write("\n")
75 end
77 local function TestParser(parseFunc, successNames, failNames, roundTripNames)
78 testDirectories(parseFunc, successNames, true, false)
79 testDirectories(parseFunc, failNames, false, false)
80 testDirectories(parseFunc, roundTripNames, true, true)
81 end
82 print("Testing lax/fast mode:")
83 TestParser(function(data) return json.decode(data) end, {"test/pass","test/fail_strict"}, {"test/fail_all"},{"test/roundtrip","test/roundtrip_lax"})
85 print("Testing (mostly) strict mode:")
86 local strict = json.decode.util.merge({}, json.decode.strict, {
87 number = {
88 nan = false,
89 inf = true,
90 strict = true
93 TestParser(function(data) return json.decode(data, strict) end, {"test/pass"}, {"test/fail_strict","test/fail_all"}, {"test/roundtrip"})
95 if not success then
96 os.exit(1)
97 end