2 function run_all_tests()
4 dotest "core.stanza_router"
5 dotest "core.s2smanager"
6 dotest "core.configmanager"
9 local verbosity = tonumber(arg[1]) or 2;
11 package.path = package.path..";../?.lua";
15 local env_mt = { __index = function (t,k) return rawget(_G, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
16 function testlib_new_env(t)
17 return setmetatable(t or {}, env_mt);
20 function assert_equal(a, b, message)
22 error("\n assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n Message: "..message) or ""), 2);
23 elseif verbosity >= 4 then
24 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
28 function dotest(unitname)
29 local tests = setmetatable({}, { __index = _G });
30 tests.__unit = unitname;
31 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
33 print("WARNING: ", "Failed to load tests for "..unitname, err);
37 setfenv(chunk, tests);
38 local success, err = pcall(chunk);
40 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
44 local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) });
46 local fn = "../"..unitname:gsub("%.", "/")..".lua";
47 local chunk, err = loadfile(fn);
49 print("WARNING: ", "Failed to load module: "..unitname, err);
54 local success, err = pcall(chunk);
56 print("WARNING: ", "Failed to initialise module: "..unitname, err);
60 for name, f in pairs(unit) do
61 local test = rawget(tests, name);
62 if type(f) ~= "function" then
63 if verbosity >= 3 then
64 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
66 elseif type(test) ~= "function" then
67 if verbosity >= 1 then
68 print("WARNING: ", unitname.."."..name.." has no test!");
71 local line_hook, line_info = new_line_coverage_monitor(fn);
72 debug.sethook(line_hook, "l")
73 local success, ret = pcall(test, f, unit);
76 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
77 print(" Location: "..ret:gsub(":%s*\n", "\n"));
78 line_info(name, false, report_file);
79 elseif verbosity >= 2 then
80 print("TEST SUCCEEDED: ", unitname, name);
81 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
83 line_info(name, success, report_file);
89 function runtest(f, msg)
90 if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
91 local success, ret = pcall(f);
92 if success and verbosity >= 2 then
93 print("SUBTEST PASSED: "..(msg or "(no description)"));
94 elseif (not success) and verbosity >= 1 then
95 print("SUBTEST FAILED: "..(msg or "(no description)"));
100 function new_line_coverage_monitor(file)
101 local lines_hit, funcs_hit = {}, {};
102 local total_lines, covered_lines = 0, 0;
104 for line in io.lines(file) do
105 total_lines = total_lines + 1;
108 return function (event, line) -- Line hook
109 if not lines_hit[line] then
110 local info = debug.getinfo(2, "fSL")
111 if not info.source:find(file) then return; end
112 if not funcs_hit[info.func] and info.activelines then
113 funcs_hit[info.func] = true;
114 for line in pairs(info.activelines) do
115 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
118 if lines_hit[line] == false then
119 --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
120 lines_hit[line] = true;
121 covered_lines = covered_lines + 1;
125 function (test_name, success) -- Get info
126 local fn = file:gsub("^%W*", "");
127 local total_active_lines = 0;
128 local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
129 for line, active in pairs(lines_hit) do
130 if active ~= nil then total_active_lines = total_active_lines + 1; end
131 if coverage_file then
132 if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
133 else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
136 if coverage_file then coverage_file:close(); end
137 return covered_lines, total_active_lines, lines_hit;