2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
11 function run_all_tests()
12 package.loaded["net.connlisteners"] = { get = function () return {} end };
14 dotest "util.multitable"
17 dotest "core.modulemanager"
18 dotest "core.stanza_router"
19 dotest "core.s2smanager"
20 dotest "core.configmanager"
22 dotest "util.sasl.scram"
24 dosingletest("test_sasl.lua", "latin1toutf8");
27 local verbosity = tonumber(arg[1]) or 2;
29 if os.getenv("WINDIR") then
30 package.path = package.path..";..\\?.lua";
31 package.cpath = package.cpath..";..\\?.dll";
33 package.path = package.path..";../?.lua";
34 package.cpath = package.cpath..";../?.so";
41 local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
42 function testlib_new_env(t)
43 return setmetatable(t or {}, env_mt);
46 function assert_equal(a, b, message, level)
48 error("\n assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n Message: "..message) or ""), (level or 1) + 1);
49 elseif verbosity >= 4 then
50 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
54 function assert_table(a, message, level)
55 assert_equal(type(a), "table", message, (level or 1) + 1);
57 function assert_function(a, message, level)
58 assert_equal(type(a), "function", message, (level or 1) + 1);
60 function assert_string(a, message, level)
61 assert_equal(type(a), "string", message, (level or 1) + 1);
63 function assert_boolean(a, message)
64 assert_equal(type(a), "boolean", message);
66 function assert_is(a, message)
67 assert_equal(not not a, true, message);
69 function assert_is_not(a, message)
70 assert_equal(not not a, false, message);
74 function dosingletest(testname, fname)
75 local tests = setmetatable({}, { __index = _realG });
76 tests.__unit = testname;
78 local chunk, err = loadfile(testname);
80 print("WARNING: ", "Failed to load tests for "..testname, err);
84 setfenv(chunk, tests);
85 local success, err = pcall(chunk);
87 print("WARNING: ", "Failed to initialise tests for "..testname, err);
91 if type(tests[fname]) ~= "function" then
92 error(testname.." has no test '"..fname.."'", 0);
96 local line_hook, line_info = new_line_coverage_monitor(testname);
97 debug.sethook(line_hook, "l")
98 local success, ret = pcall(tests[fname]);
101 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
102 print(" Location: "..ret:gsub(":%s*\n", "\n"));
103 line_info(fname, false, report_file);
104 elseif verbosity >= 2 then
105 print("TEST SUCCEEDED: ", testname, fname);
106 print(string.format("TEST COVERED %d/%d lines", line_info(fname, true, report_file)));
108 line_info(name, success, report_file);
112 function dotest(unitname)
113 local _fakeG = setmetatable({}, {__index = _realG});
115 local tests = setmetatable({}, { __index = _fakeG });
116 tests.__unit = unitname;
117 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
119 print("WARNING: ", "Failed to load tests for "..unitname, err);
123 setfenv(chunk, tests);
124 local success, err = pcall(chunk);
126 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
129 if tests.env then setmetatable(tests.env, { __index = _realG }); end
130 local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _fakeG }, { __index = tests.env or _fakeG }) });
131 local fn = "../"..unitname:gsub("%.", "/")..".lua";
132 local chunk, err = loadfile(fn);
134 print("WARNING: ", "Failed to load module: "..unitname, err);
138 local oldmodule, old_M = _fakeG.module, _fakeG._M;
139 _fakeG.module = function () _M = _G end
140 setfenv(chunk, unit);
141 local success, err = pcall(chunk);
142 _fakeG.module, _fakeG._M = oldmodule, old_M;
144 print("WARNING: ", "Failed to initialise module: "..unitname, err);
148 for name, f in pairs(unit) do
149 local test = rawget(tests, name);
150 if type(f) ~= "function" then
151 if verbosity >= 3 then
152 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
154 elseif type(test) ~= "function" then
155 if verbosity >= 1 then
156 print("WARNING: ", unitname.."."..name.." has no test!");
159 if verbosity >= 4 then
160 print("INFO: ", "Testing "..unitname.."."..name);
162 local line_hook, line_info = new_line_coverage_monitor(fn);
163 debug.sethook(line_hook, "l")
164 local success, ret = pcall(test, f, unit);
167 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
168 print(" Location: "..ret:gsub(":%s*\n", "\n"));
169 line_info(name, false, report_file);
170 elseif verbosity >= 2 then
171 print("TEST SUCCEEDED: ", unitname, name);
172 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
174 line_info(name, success, report_file);
180 function runtest(f, msg)
181 if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
182 local success, ret = pcall(f);
183 if success and verbosity >= 2 then
184 print("SUBTEST PASSED: "..(msg or "(no description)"));
185 elseif (not success) and verbosity >= 0 then
186 print("SUBTEST FAILED: "..(msg or "(no description)"));
191 function new_line_coverage_monitor(file)
192 local lines_hit, funcs_hit = {}, {};
193 local total_lines, covered_lines = 0, 0;
195 for line in io.lines(file) do
196 total_lines = total_lines + 1;
199 return function (event, line) -- Line hook
200 if not lines_hit[line] then
201 local info = debug.getinfo(2, "fSL")
202 if not info.source:find(file) then return; end
203 if not funcs_hit[info.func] and info.activelines then
204 funcs_hit[info.func] = true;
205 for line in pairs(info.activelines) do
206 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
209 if lines_hit[line] == false then
210 --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
211 lines_hit[line] = true;
212 covered_lines = covered_lines + 1;
216 function (test_name, success) -- Get info
217 local fn = file:gsub("^%W*", "");
218 local total_active_lines = 0;
219 local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
220 for line, active in pairs(lines_hit) do
221 if active ~= nil then total_active_lines = total_active_lines + 1; end
222 if coverage_file then
223 if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
224 else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
227 if coverage_file then coverage_file:close(); end
228 return covered_lines, total_active_lines, lines_hit;