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")
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')
18 local function putTempData(data
)
19 local name
= os
.tmpname()
20 local f
= assert(io
.open(name
, 'wb'))
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
)
30 assert(not success
, "Round trip encoding test result not as expected")
33 assert(success
, "Couldn't encode the lua data..." .. tostring(dataString
))
35 local success
, result
= pcall(parseFunc
, dataString
)
37 print("Could not parse the generated JSON of (", luaData
)
38 print("GENERATED: [[" .. dataString
.. "]]")
39 print("DATA STORED IN: ", putTempData(dataString
))
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
.. " >>")
56 local function testFile(fileName
, parseFunc
, encodeFunc
, expectSuccess
, fullRoundTrip
, failRoundTrip
)
57 local data
= getFileData(fileName
)
58 if not data
then return end
60 local succeed
, result
= pcall(parseFunc
, data
)
61 if expectSuccess
~= succeed
then
62 print("Wrongly " .. (expectSuccess
and "Failed" or "Succeeded") .. " on : " .. fileName
.. "(" .. tostring(result
) .. ")")
65 if not RoundTripTest(parseFunc
, encodeFunc
, data
, result
, fullRoundTrip
, failRoundTrip
) then
66 print("FAILED TO ROUND TRIP: " .. fileName
)
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
, ...)
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)
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
, {
100 local strict_encode
= json
.util
.merge({}, json
.encode
.strict
, {
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)