1 -- Additional path that may be required
2 package
.path
= package
.path
.. ';?/init.lua;../src/?.lua;../src/?/init.lua'
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')
17 local function putTempData(data
)
18 local name
= os
.tmpname()
19 local f
= assert(io
.open(name
, 'wb'))
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
)
31 print("Could not parse the generated JSON of (", luaData
)
32 print("GENERATED: [[" .. dataString
.. "]]")
33 print("DATA STORED IN: ", putTempData(dataString
))
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
.. " >>")
50 local function testFile(fileName
, parseFunc
, expectSuccess
, fullRoundTrip
)
51 local data
= getFileData(fileName
)
52 if not data
then return end
54 local succeed
, result
= pcall(parseFunc
, data
)
55 if expectSuccess
~= succeed
then
56 print("Wrongly " .. (expectSuccess
and "Failed" or "Succeeded") .. " on : " .. fileName
.. "(" .. tostring(result
) .. ")")
59 if not RoundTripTest(parseFunc
, data
, result
, fullRoundTrip
) then
60 print("FAILED TO ROUND TRIP: " .. fileName
)
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
, ...)
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)
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"})