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