-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.ttslua
124 lines (96 loc) · 3.56 KB
/
tests.ttslua
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
115
116
117
118
119
120
121
122
123
124
-- ================================================================================
-- Test suite.
-- ================================================================================
-- The module.
local Test = { }
-- ================================================================================
-- Privates
-- ----------------------------------------
-- Private constants
local test_modules = {
{ "Base64", require("kintastic/tests/Base64") },
{ "ColorUtils", require("kintastic/tests/ColorUtils") },
{ "Crc32", require("kintastic/tests/Crc32") },
{ "Encode", require("kintastic/tests/Encode") },
{ "Set", require("kintastic/tests/Set") },
{ "Wrap", require("kintastic/tests/Wrap") },
}
local test_modules_by_name = { }
for i, _ in ipairs(test_modules) do
local name, test_module = table.unpack(_)
test_modules_by_name[name] = test_module
end
-- ----------------------------------------
-- Private function info_msg
-- Private function error_msg
local function strong_msg(msg) printToAll(msg, Color.Blue) end
local function good_msg(msg) printToAll(msg, Color.Green) end
local function info_msg(msg) printToAll(msg, Color.White) end
local function error_msg(msg) printToAll(msg, Color.Red) end
-- ----------------------------------------
-- Private function run_test
local function run_test(state, name, f, ...)
local success, rv = pcall(f, state, ...)
if not success then
error_msg(name .. ": failed (Exception)")
error_msg(rv)
return false
elseif not rv then
printToAll(name .. ": failed", Color.Red)
return false
else
info_msg(name .. ": ok")
return true
end
end
-- ----------------------------------------
-- Private function run_test_module
local function run_test_module(name, test_module)
strong_msg("== " .. name .. " ==")
if not test_module then
error_msg("No test module named \"" .. name .. "\"")
return 1, 0
end
local num_tests = #test_module
local num_passed = 0
local state = { }
for i, test in ipairs(test_module) do
if run_test(state, table.unpack(test)) then
num_passed = num_passed + 1
end
end
return num_tests, num_passed
end
-- ================================================================================
-- Public interface
-- ----------------------------------------
-- Public function Test.run_tests
function Test.run_tests(selected_tests)
local num_tests = 0
local num_passed = 0
if selected_tests then
strong_msg("[[ Running test suite (selected modules) ]]")
for i, name in ipairs(selected_tests) do
local _num_tests, _num_passed = run_test_module(name, test_modules_by_name[name])
num_tests = num_tests + _num_tests
num_passed = num_passed + _num_passed
end
else
strong_msg("[[ Running test suite (complete) ]]")
for i, _ in ipairs(test_modules) do
local _num_tests, _num_passed = run_test_module(table.unpack(_))
num_tests = num_tests + _num_tests
num_passed = num_passed + _num_passed
end
end
strong_msg("[[ Summary ]]")
info_msg(num_tests .. " tests were executed.")
if num_passed == num_tests then
good_msg("All tests passed.")
else
info_msg(num_passed .. " tests passed.")
error_msg( ( num_tests - num_passed ) .. " tests failed.")
end
end
-- ================================================================================
return Test