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";
12 package.cpath = package.cpath..";../?.so";
16 local env_mt = { __index = function (t,k) return rawget(_G, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
17 function testlib_new_env(t)
18 return setmetatable(t or {}, env_mt);
21 function assert_equal(a, b, message)
23 error("\n assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n Message: "..message) or ""), 2);
24 elseif verbosity >= 4 then
25 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
29 function dotest(unitname)
30 local tests = setmetatable({}, { __index = _G });
31 tests.__unit = unitname;
32 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
34 print("WARNING: ", "Failed to load tests for "..unitname, err);
38 setfenv(chunk, tests);
39 local success, err = pcall(chunk);
41 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
45 local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) });
47 local fn = "../"..unitname:gsub("%.", "/")..".lua";
48 local chunk, err = loadfile(fn);
50 print("WARNING: ", "Failed to load module: "..unitname, err);
55 local success, err = pcall(chunk);
57 print("WARNING: ", "Failed to initialise module: "..unitname, err);
61 for name, f in pairs(unit) do
62 local test = rawget(tests, name);
63 if type(f) ~= "function" then
64 if verbosity >= 3 then
65 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
67 elseif type(test) ~= "function" then
68 if verbosity >= 1 then
69 print("WARNING: ", unitname.."."..name.." has no test!");
72 local line_hook, line_info = new_line_coverage_monitor(fn);
73 debug.sethook(line_hook, "l")
74 local success, ret = pcall(test, f, unit);
77 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
78 print(" Location: "..ret:gsub(":%s*\n", "\n"));
79 line_info(name, false, report_file);
80 elseif verbosity >= 2 then
81 print("TEST SUCCEEDED: ", unitname, name);
82 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
84 line_info(name, success, report_file);
90 function runtest(f, msg)
91 if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
92 local success, ret = pcall(f);
93 if success and verbosity >= 2 then
94 print("SUBTEST PASSED: "..(msg or "(no description)"));
95 elseif (not success) and verbosity >= 1 then
96 print("SUBTEST FAILED: "..(msg or "(no description)"));
101 function new_line_coverage_monitor(file)
102 local lines_hit, funcs_hit = {}, {};
103 local total_lines, covered_lines = 0, 0;
105 for line in io.lines(file) do
106 total_lines = total_lines + 1;
109 return function (event, line) -- Line hook
110 if not lines_hit[line] then
111 local info = debug.getinfo(2, "fSL")
112 if not info.source:find(file) then return; end
113 if not funcs_hit[info.func] and info.activelines then
114 funcs_hit[info.func] = true;
115 for line in pairs(info.activelines) do
116 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
119 if lines_hit[line] == false then
120 --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
121 lines_hit[line] = true;
122 covered_lines = covered_lines + 1;
126 function (test_name, success) -- Get info
127 local fn = file:gsub("^%W*", "");
128 local total_active_lines = 0;
129 local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
130 for line, active in pairs(lines_hit) do
131 if active ~= nil then total_active_lines = total_active_lines + 1; end
132 if coverage_file then
133 if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
134 else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
137 if coverage_file then coverage_file:close(); end
138 return covered_lines, total_active_lines, lines_hit;