File: regressionTest.lua

package info (click to toggle)
lua-json 1.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 688 kB
  • sloc: makefile: 71; php: 3
file content (114 lines) | stat: -rw-r--r-- 3,625 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
-- Additional path that may be required
local json = require("json")
local io = require("io")
local os = require("os")

local lfs = require("lfs")

local success = true

local function getFileData(fileName)
	local f = io.open(fileName, 'rb')
	if not f then return end
	local data = f:read('*a')
	f:close()
	return data
end

local function putTempData(data)
	local name = os.tmpname()
	local f = assert(io.open(name, 'wb'))
	f:write(data)
	f:close()
	return name
end

-- Ensure that the encoder/decoder can round-trip valid JSON
local function RoundTripTest(parseFunc, encodeFunc, jsonData, luaData, fullRoundTrip, failRoundTrip)
	local success, dataString = pcall(encodeFunc, luaData)
	if failRoundTrip then
		assert(not success, "Round trip encoding test result not as expected")
		return true
	else
		assert(success, "Couldn't encode the lua data..." .. tostring(dataString))
	end
	local success, result = pcall(parseFunc, dataString)
	if not success then
		print("Could not parse the generated JSON of (", luaData)
		print("GENERATED: [[" .. dataString .. "]]")
		print("DATA STORED IN: ", putTempData(dataString))
		return
	end
	if fullRoundTrip then
		-- Ensure that whitespace is trimmed off ends
		dataString = dataString:match("^[%s]*(.-)[%s]*$")
		jsonData = jsonData:match("^[%s]*(.-)[%s]*$")
		if dataString ~= jsonData then
			print("Encoded values do not match")
			print("ORIGINAL: << " .. jsonData .. " >>")
			print("RE-ENCOD: << " .. dataString .. " >>")
			return
		end
	end
	return true
end

local function testFile(fileName, parseFunc, encodeFunc, expectSuccess, fullRoundTrip, failRoundTrip)
	local data = getFileData(fileName)
	if not data then return end
	io.write(".")
	local succeed, result = pcall(parseFunc, data)
	if expectSuccess ~= succeed then
		print("Wrongly " .. (expectSuccess and "Failed" or "Succeeded") .. " on : " .. fileName .. "(" .. tostring(result) .. ")")
		success = false
	elseif succeed then
		if not RoundTripTest(parseFunc, encodeFunc, data, result, fullRoundTrip, failRoundTrip) then
			print("FAILED TO ROUND TRIP: " .. fileName)
			success = false
		end
	end
end

local function testDirectories(parseFunc, encodeFunc, directories, ...)
	if not directories then return end
	for _,directory in ipairs(directories) do
		if lfs.attributes(directory, 'mode') == 'directory' then
			for f in lfs.dir(directory) do
				testFile(directory .. "/" .. f, parseFunc, encodeFunc, ...)
			end
		end
	end
	io.write("\n")
end

local function TestParser(parseFunc, encodeFunc, successNames, failNames, roundTripNames)
	testDirectories(parseFunc, encodeFunc, successNames, true, false)
	testDirectories(parseFunc, encodeFunc, failNames, false, false)
	testDirectories(parseFunc, encodeFunc, roundTripNames, true, true)
end
print("Testing lax/fast mode:")
TestParser(json.decode.getDecoder(), json.encode.getEncoder(), {"test/pass","test/fail_strict"}, {"test/fail_all"},{"test/roundtrip","test/roundtrip_lax"})

print("Testing (mostly) strict mode:")
local strict = json.util.merge({}, json.decode.strict, {
	number = {
		nan = false,
		inf = true,
		strict = true
	}
})
local strict_encode = json.util.merge({}, json.encode.strict, {
	number = {
		nan = false,
		inf = true,
		strict = true
	}
})
TestParser(json.decode.getDecoder(strict), json.encode.getEncoder(strict_encode), {"test/pass"}, {"test/fail_strict","test/fail_all"}, {"test/roundtrip"})

print("Testing (mostly) strict encoder with non-strict decodings")
testDirectories(json.decode.getDecoder(), json.encode.getEncoder(json.encode.strict), {"test/fail_strict_encode"}, true, true, true)

if not success then
	os.exit(1)
end