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