tangled
alpha
login
or
join now
algmyr.se
/
vclib.nvim
0
fork
atom
Common library code for other vc*.nvim projects.
0
fork
atom
overview
issues
pulls
pipelines
Add test utils.
algmyr.se
8 months ago
d3b46e71
705fd85a
+79
1 changed file
expand all
collapse all
unified
split
lua
vclib
testing.lua
+79
lua/vclib/testing.lua
···
1
1
+
local M = {}
2
2
+
3
3
+
local function _run_test_suite(suite_name, test_suite)
4
4
+
local suite_failed = 0
5
5
+
local suite_total = 0
6
6
+
local test_cases = test_suite.test_cases
7
7
+
local test_function = test_suite.test
8
8
+
for case_name, case in pairs(test_cases) do
9
9
+
local full_test_name = string.format("%s__%s", suite_name, case_name)
10
10
+
11
11
+
local status, err = pcall(function()
12
12
+
test_function(case)
13
13
+
end)
14
14
+
suite_total = suite_total + 1
15
15
+
if not status then
16
16
+
suite_failed = suite_failed + 1
17
17
+
print(string.format("Test %s failed: %s", full_test_name, err))
18
18
+
end
19
19
+
end
20
20
+
21
21
+
if suite_failed == 0 then
22
22
+
print(
23
23
+
string.format(
24
24
+
"All tests in %s passed (%d tests)",
25
25
+
suite_name,
26
26
+
suite_total
27
27
+
)
28
28
+
)
29
29
+
else
30
30
+
print(
31
31
+
string.format(
32
32
+
"%d/%d tests failed in %s",
33
33
+
suite_failed,
34
34
+
suite_total,
35
35
+
suite_name
36
36
+
)
37
37
+
)
38
38
+
end
39
39
+
return suite_failed, suite_total
40
40
+
end
41
41
+
42
42
+
function M.run_tests(test_modules)
43
43
+
local failed = 0
44
44
+
local total = 0
45
45
+
for _, test_module_name in ipairs(test_modules) do
46
46
+
local test_module = require(test_module_name)
47
47
+
print(string.format("=== Running tests in %s ===", test_module_name))
48
48
+
for suite_name, test_suite in pairs(test_module) do
49
49
+
local suite_failed, suite_total = _run_test_suite(suite_name, test_suite)
50
50
+
failed = failed + suite_failed
51
51
+
total = total + suite_total
52
52
+
end
53
53
+
end
54
54
+
print "--------------------------------"
55
55
+
if failed == 0 then
56
56
+
print(string.format("All tests passed (%d tests)", total))
57
57
+
else
58
58
+
print(string.format("%d/%d tests failed", failed, total))
59
59
+
end
60
60
+
end
61
61
+
62
62
+
function M.assert_list_eq(actual, expected)
63
63
+
assert(
64
64
+
#actual == #expected,
65
65
+
string.format("Lists have different lengths: %d vs %d", #actual, #expected)
66
66
+
)
67
67
+
local diff = ""
68
68
+
for i = 1, #expected do
69
69
+
if actual[i] ~= expected[i] then
70
70
+
diff = diff
71
71
+
.. string.format("\n[%d]: %s ~= %s", i, actual[i], expected[i])
72
72
+
end
73
73
+
end
74
74
+
if diff ~= "" then
75
75
+
error("Lists differ:" .. diff)
76
76
+
end
77
77
+
end
78
78
+
79
79
+
return M