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.
9 local tests_passed = true;
11 function run_all_tests()
12 package.loaded["net.connlisteners"] = { get = function () return {} end };
14 dotest "util.multitable"
17 dotest "core.stanza_router"
18 dotest "core.s2smanager"
19 dotest "core.configmanager"
22 dotest "util.sasl.scram"
25 dosingletest("test_sasl.lua", "latin1toutf8");
26 dosingletest("test_utf8.lua", "valid");
29 local verbosity = tonumber(arg[1]) or 2;
31 if os.getenv("WINDIR") then
32 package.path = package.path..";..\\?.lua";
33 package.cpath = package.cpath..";..\\?.dll";
35 package.path = package.path..";../?.lua";
36 package.cpath = package.cpath..";../?.so";
43 local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
44 function testlib_new_env(t)
45 return setmetatable(t or {}, env_mt);
48 function assert_equal(a, b, message, level)
50 error("\n assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n Message: "..message) or ""), (level or 1) + 1);
51 elseif verbosity >= 4 then
52 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
56 function assert_table(a, message, level)
57 assert_equal(type(a), "table", message, (level or 1) + 1);
59 function assert_function(a, message, level)
60 assert_equal(type(a), "function", message, (level or 1) + 1);
62 function assert_string(a, message, level)
63 assert_equal(type(a), "string", message, (level or 1) + 1);
65 function assert_boolean(a, message)
66 assert_equal(type(a), "boolean", message);
68 function assert_is(a, message)
69 assert_equal(not not a, true, message);
71 function assert_is_not(a, message)
72 assert_equal(not not a, false, message);
76 function dosingletest(testname, fname)
77 local tests = setmetatable({}, { __index = _realG });
78 tests.__unit = testname;
80 local chunk, err = loadfile(testname);
82 print("WARNING: ", "Failed to load tests for "..testname, err);
86 setfenv(chunk, tests);
87 local success, err = pcall(chunk);
89 print("WARNING: ", "Failed to initialise tests for "..testname, err);
93 if type(tests[fname]) ~= "function" then
94 error(testname.." has no test '"..fname.."'", 0);
98 local line_hook, line_info = new_line_coverage_monitor(testname);
99 debug.sethook(line_hook, "l")
100 local success, ret = pcall(tests[fname]);
103 tests_passed = false;
104 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
105 print(" Location: "..ret:gsub(":%s*\n", "\n"));
106 line_info(fname, false, report_file);
107 elseif verbosity >= 2 then
108 print("TEST SUCCEEDED: ", testname, fname);
109 print(string.format("TEST COVERED %d/%d lines", line_info(fname, true, report_file)));
111 line_info(name, success, report_file);
115 function dotest(unitname)
116 local _fakeG = setmetatable({}, {__index = _realG});
118 local tests = setmetatable({}, { __index = _fakeG });
119 tests.__unit = unitname;
120 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
122 print("WARNING: ", "Failed to load tests for "..unitname, err);
126 setfenv(chunk, tests);
127 local success, err = pcall(chunk);
129 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
132 if tests.env then setmetatable(tests.env, { __index = _realG }); end
133 local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _fakeG }, { __index = tests.env or _fakeG }) });
134 local fn = "../"..unitname:gsub("%.", "/")..".lua";
135 local chunk, err = loadfile(fn);
137 print("WARNING: ", "Failed to load module: "..unitname, err);
141 local oldmodule, old_M = _fakeG.module, _fakeG._M;
142 _fakeG.module = function () _M = unit end
143 setfenv(chunk, unit);
144 local success, ret = pcall(chunk);
145 _fakeG.module, _fakeG._M = oldmodule, old_M;
147 print("WARNING: ", "Failed to initialise module: "..unitname, err);
151 if type(ret) == "table" then
152 for k,v in pairs(ret) do
157 for name, f in pairs(unit) do
158 local test = rawget(tests, name);
159 if type(f) ~= "function" then
160 if verbosity >= 3 then
161 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
163 elseif type(test) ~= "function" then
164 if verbosity >= 1 then
165 print("WARNING: ", unitname.."."..name.." has no test!");
168 if verbosity >= 4 then
169 print("INFO: ", "Testing "..unitname.."."..name);
171 local line_hook, line_info = new_line_coverage_monitor(fn);
172 debug.sethook(line_hook, "l")
173 local success, ret = pcall(test, f, unit);
176 tests_passed = false;
177 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
178 print(" Location: "..ret:gsub(":%s*\n", "\n"));
179 line_info(name, false, report_file);
180 elseif verbosity >= 2 then
181 print("TEST SUCCEEDED: ", unitname, name);
182 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
184 line_info(name, success, report_file);
190 function runtest(f, msg)
191 if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
192 local success, ret = pcall(f);
193 if success and verbosity >= 2 then
194 print("SUBTEST PASSED: "..(msg or "(no description)"));
195 elseif (not success) and verbosity >= 0 then
196 tests_passed = false;
197 print("SUBTEST FAILED: "..(msg or "(no description)"));
202 function new_line_coverage_monitor(file)
203 local lines_hit, funcs_hit = {}, {};
204 local total_lines, covered_lines = 0, 0;
206 for line in io.lines(file) do
207 total_lines = total_lines + 1;
210 return function (event, line) -- Line hook
211 if not lines_hit[line] then
212 local info = debug.getinfo(2, "fSL")
213 if not info.source:find(file) then return; end
214 if not funcs_hit[info.func] and info.activelines then
215 funcs_hit[info.func] = true;
216 for line in pairs(info.activelines) do
217 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
220 if lines_hit[line] == false then
221 --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
222 lines_hit[line] = true;
223 covered_lines = covered_lines + 1;
227 function (test_name, success) -- Get info
228 local fn = file:gsub("^%W*", "");
229 local total_active_lines = 0;
230 local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
231 for line, active in pairs(lines_hit) do
232 if active ~= nil then total_active_lines = total_active_lines + 1; end
233 if coverage_file then
234 if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
235 else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
238 if coverage_file then coverage_file:close(); end
239 return covered_lines, total_active_lines, lines_hit;
245 os.exit(tests_passed and 0 or 1);