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"
24 dosingletest("test_sasl.lua", "latin1toutf8");
25 dosingletest("test_utf8.lua", "valid");
28 local verbosity = tonumber(arg[1]) or 2;
30 if os.getenv("WINDIR") then
31 package.path = package.path..";..\\?.lua";
32 package.cpath = package.cpath..";..\\?.dll";
34 package.path = package.path..";../?.lua";
35 package.cpath = package.cpath..";../?.so";
42 local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
43 function testlib_new_env(t)
44 return setmetatable(t or {}, env_mt);
47 function assert_equal(a, b, message, level)
49 error("\n assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n Message: "..message) or ""), (level or 1) + 1);
50 elseif verbosity >= 4 then
51 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
55 function assert_table(a, message, level)
56 assert_equal(type(a), "table", message, (level or 1) + 1);
58 function assert_function(a, message, level)
59 assert_equal(type(a), "function", message, (level or 1) + 1);
61 function assert_string(a, message, level)
62 assert_equal(type(a), "string", message, (level or 1) + 1);
64 function assert_boolean(a, message)
65 assert_equal(type(a), "boolean", message);
67 function assert_is(a, message)
68 assert_equal(not not a, true, message);
70 function assert_is_not(a, message)
71 assert_equal(not not a, false, message);
75 function dosingletest(testname, fname)
76 local tests = setmetatable({}, { __index = _realG });
77 tests.__unit = testname;
79 local chunk, err = loadfile(testname);
81 print("WARNING: ", "Failed to load tests for "..testname, err);
85 setfenv(chunk, tests);
86 local success, err = pcall(chunk);
88 print("WARNING: ", "Failed to initialise tests for "..testname, err);
92 if type(tests[fname]) ~= "function" then
93 error(testname.." has no test '"..fname.."'", 0);
97 local line_hook, line_info = new_line_coverage_monitor(testname);
98 debug.sethook(line_hook, "l")
99 local success, ret = pcall(tests[fname]);
102 tests_passed = false;
103 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
104 print(" Location: "..ret:gsub(":%s*\n", "\n"));
105 line_info(fname, false, report_file);
106 elseif verbosity >= 2 then
107 print("TEST SUCCEEDED: ", testname, fname);
108 print(string.format("TEST COVERED %d/%d lines", line_info(fname, true, report_file)));
110 line_info(name, success, report_file);
114 function dotest(unitname)
115 local _fakeG = setmetatable({}, {__index = _realG});
117 local tests = setmetatable({}, { __index = _fakeG });
118 tests.__unit = unitname;
119 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
121 print("WARNING: ", "Failed to load tests for "..unitname, err);
125 setfenv(chunk, tests);
126 local success, err = pcall(chunk);
128 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
131 if tests.env then setmetatable(tests.env, { __index = _realG }); end
132 local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _fakeG }, { __index = tests.env or _fakeG }) });
133 local fn = "../"..unitname:gsub("%.", "/")..".lua";
134 local chunk, err = loadfile(fn);
136 print("WARNING: ", "Failed to load module: "..unitname, err);
140 local oldmodule, old_M = _fakeG.module, _fakeG._M;
141 _fakeG.module = function () _M = unit end
142 setfenv(chunk, unit);
143 local success, ret = pcall(chunk);
144 _fakeG.module, _fakeG._M = oldmodule, old_M;
146 print("WARNING: ", "Failed to initialise module: "..unitname, err);
150 if type(ret) == "table" then
151 for k,v in pairs(ret) do
156 for name, f in pairs(unit) do
157 local test = rawget(tests, name);
158 if type(f) ~= "function" then
159 if verbosity >= 3 then
160 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
162 elseif type(test) ~= "function" then
163 if verbosity >= 1 then
164 print("WARNING: ", unitname.."."..name.." has no test!");
167 if verbosity >= 4 then
168 print("INFO: ", "Testing "..unitname.."."..name);
170 local line_hook, line_info = new_line_coverage_monitor(fn);
171 debug.sethook(line_hook, "l")
172 local success, ret = pcall(test, f, unit);
175 tests_passed = false;
176 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
177 print(" Location: "..ret:gsub(":%s*\n", "\n"));
178 line_info(name, false, report_file);
179 elseif verbosity >= 2 then
180 print("TEST SUCCEEDED: ", unitname, name);
181 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
183 line_info(name, success, report_file);
189 function runtest(f, msg)
190 if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
191 local success, ret = pcall(f);
192 if success and verbosity >= 2 then
193 print("SUBTEST PASSED: "..(msg or "(no description)"));
194 elseif (not success) and verbosity >= 0 then
195 tests_passed = false;
196 print("SUBTEST FAILED: "..(msg or "(no description)"));
201 function new_line_coverage_monitor(file)
202 local lines_hit, funcs_hit = {}, {};
203 local total_lines, covered_lines = 0, 0;
205 for line in io.lines(file) do
206 total_lines = total_lines + 1;
209 return function (event, line) -- Line hook
210 if not lines_hit[line] then
211 local info = debug.getinfo(2, "fSL")
212 if not info.source:find(file) then return; end
213 if not funcs_hit[info.func] and info.activelines then
214 funcs_hit[info.func] = true;
215 for line in pairs(info.activelines) do
216 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
219 if lines_hit[line] == false then
220 --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
221 lines_hit[line] = true;
222 covered_lines = covered_lines + 1;
226 function (test_name, success) -- Get info
227 local fn = file:gsub("^%W*", "");
228 local total_active_lines = 0;
229 local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
230 for line, active in pairs(lines_hit) do
231 if active ~= nil then total_active_lines = total_active_lines + 1; end
232 if coverage_file then
233 if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
234 else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
237 if coverage_file then coverage_file:close(); end
238 return covered_lines, total_active_lines, lines_hit;
244 os.exit(tests_passed and 0 or 1);