-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtestlib.lua
80 lines (71 loc) · 1.97 KB
/
testlib.lua
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
local Test = {
last = nil,
failed = nil,
scope_info = nil,
}
function Test.fail(message)
error(message or 'fail')
return false
end
function Test.assert(pred, message)
if not pred then
return Test.fail(message)
end
return true
end
function Test.expect_fail(fn)
assert(type(fn) == 'function')
local ok, msg = pcall(fn)
return Test.assert(not ok, msg)
end
function Test.info(info)
Test.scope_info = Test.scope_info or {}
table.insert(Test.scope_info, tostring(info))
end
function Test.reset()
Test.scope_info = nil
end
function Test.run(tests)
local ok, failed = 0, 0
for k, v in pairs(tests) do
Test.scope_info = nil
if type(v) == 'function' then
Test.failed = nil
print("Running test " .. k)
local output = {}
local oldprint = _G.print
_G.print = function(str)
table.insert(output, str)
end
local status, err = pcall(v)
_G.print = oldprint
if status and not Test.failed then
ok = ok + 1
print("[OK ]")
else
failed = failed + 1
if type(err) == 'table' and err.desc then err = err.desc end
print("[FAILED] " .. (Test.failed or (tostring(err) or "")))
if Test.scope_info then
if type(Test.scope_info) == 'string' then
print("[ info] " .. Test.scope_info)
elseif type(Test.scope_info) == 'table' then
for _, line in ipairs(Test.scope_info) do
print("[ info] " .. line)
end
end
end
for _, line in ipairs(output) do
if type(line) == 'string' then
print("> " .. line)
end
end
end
end
end
print("Ran " .. (ok + failed) .. " tests (" .. ok .. " succeeded, " .. failed .. " failed)")
if failed > 0 then
os.exit(1)
end
end
return Test