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
|
local print = print
local output = {}
function _G.print(...)
local args = {...}
for i, v in ipairs(args) do
args[i] = tostring(v)
end
table.insert(output, table.concat(args, " "))
end
local function test_pcall(fn)
return xpcall(fn, function(msg)
return debug.traceback(msg), msg
end)
end
function love.errorhandler(msg)
print(msg)
love.event.quit()
end
global "assert_error"
function assert_error(fn, ...)
local ok, traceback, err = test_pcall(fn, ...)
if ok then
error("expected error to occur", 2)
end
end
local function print_output(name, traceback, output)
local function log(msg)
if msg:sub(#msg, #msg) ~= '\n' then
msg = msg .. '\n'
end
for l in msg:gmatch '(.-)\n' do
print("["..name.."] "..l)
end
end
for _, msg in ipairs(output) do
log(msg)
end
log(traceback)
end
local test_results = {}
local passed = 0
local failed = 0
local function test(name, fn)
if not test_results[fn] then
local ok, traceback, err = test_pcall(fn)
if not ok then
failed = failed + 1
test_results[fn] = {ok, output, err, traceback}
print_output(name, traceback, output)
else
passed = passed + 1
test_results[fn] = {ok, output}
end
end
output = {}
return unpack(test_results[fn])
end
for _, mod_name in ipairs({...}) do
local mod = require("tests."..mod_name)
for name, fn in pairs(mod) do
test(mod_name.."."..name, fn)
end
end
if failed > 0 then
print(failed.." tests FAILED.")
end
print(passed.." tests passed.")
love.event.quit()
|