2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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()
13 dotest "util.multitable"
14 dotest "core.modulemanager"
15 dotest "core.stanza_router"
16 dotest "core.s2smanager"
17 dotest "core.configmanager"
20 dosingletest("test_sasl.lua", "latin1toutf8");
23 local verbosity = tonumber(arg[1]) or 2;
25 if os.getenv("WINDIR") then
26 package.path = package.path..";..\\?.lua";
27 package.cpath = package.cpath..";..\\?.dll";
29 package.path = package.path..";../?.lua";
30 package.cpath = package.cpath..";../?.so";
37 local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
38 function testlib_new_env(t)
39 return setmetatable(t or {}, env_mt);
42 function assert_equal(a, b, message, level)
44 error("\n assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n Message: "..message) or ""), (level or 1) + 1);
45 elseif verbosity >= 4 then
46 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
50 function assert_table(a, message, level)
51 assert_equal(type(a), "table", message, (level or 1) + 1);
53 function assert_function(a, message, level)
54 assert_equal(type(a), "function", message, (level or 1) + 1);
56 function assert_string(a, message, level)
57 assert_equal(type(a), "string", message, (level or 1) + 1);
59 function assert_boolean(a, message)
60 assert_equal(type(a), "boolean", message);
62 function assert_is(a, message)
63 assert_equal(not not a, true, message);
65 function assert_is_not(a, message)
66 assert_equal(not not a, false, message);
70 function dosingletest(testname, fname)
71 local tests = setmetatable({}, { __index = _realG });
72 tests.__unit = testname;
74 local chunk, err = loadfile(testname);
76 print("WARNING: ", "Failed to load tests for "..testname, err);
80 setfenv(chunk, tests);
81 local success, err = pcall(chunk);
83 print("WARNING: ", "Failed to initialise tests for "..testname, err);
87 if type(tests[fname]) ~= "function" then
88 error(testname.." has no test '"..fname.."'", 0);
92 local line_hook, line_info = new_line_coverage_monitor(testname);
93 debug.sethook(line_hook, "l")
94 local success, ret = pcall(tests[fname]);
97 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
98 print(" Location: "..ret:gsub(":%s*\n", "\n"));
99 line_info(fname, false, report_file);
100 elseif verbosity >= 2 then
101 print("TEST SUCCEEDED: ", testname, fname);
102 print(string.format("TEST COVERED %d/%d lines", line_info(fname, true, report_file)));
104 line_info(name, success, report_file);
108 function dotest(unitname)
109 local tests = setmetatable({}, { __index = _realG });
110 tests.__unit = unitname;
111 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
113 print("WARNING: ", "Failed to load tests for "..unitname, err);
117 setfenv(chunk, tests);
118 local success, err = pcall(chunk);
120 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
124 if tests.env then setmetatable(tests.env, { __index = _realG }); end
125 local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _G }, { __index = tests.env or _G }) });
126 unit._G = unit; _realG._G = unit;
127 local fn = "../"..unitname:gsub("%.", "/")..".lua";
128 local chunk, err = loadfile(fn);
130 print("WARNING: ", "Failed to load module: "..unitname, err);
134 setfenv(chunk, unit);
135 local success, err = pcall(chunk);
137 print("WARNING: ", "Failed to initialise module: "..unitname, err);
141 for name, f in pairs(unit) do
142 local test = rawget(tests, name);
143 if type(f) ~= "function" then
144 if verbosity >= 3 then
145 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
147 elseif type(test) ~= "function" then
148 if verbosity >= 1 then
149 print("WARNING: ", unitname.."."..name.." has no test!");
152 local line_hook, line_info = new_line_coverage_monitor(fn);
153 debug.sethook(line_hook, "l")
154 local success, ret = pcall(test, f, unit);
157 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
158 print(" Location: "..ret:gsub(":%s*\n", "\n"));
159 line_info(name, false, report_file);
160 elseif verbosity >= 2 then
161 print("TEST SUCCEEDED: ", unitname, name);
162 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
164 line_info(name, success, report_file);
170 function runtest(f, msg)
171 if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
172 local success, ret = pcall(f);
173 if success and verbosity >= 2 then
174 print("SUBTEST PASSED: "..(msg or "(no description)"));
175 elseif (not success) and verbosity >= 0 then
176 print("SUBTEST FAILED: "..(msg or "(no description)"));
181 function new_line_coverage_monitor(file)
182 local lines_hit, funcs_hit = {}, {};
183 local total_lines, covered_lines = 0, 0;
185 for line in io.lines(file) do
186 total_lines = total_lines + 1;
189 return function (event, line) -- Line hook
190 if not lines_hit[line] then
191 local info = debug.getinfo(2, "fSL")
192 if not info.source:find(file) then return; end
193 if not funcs_hit[info.func] and info.activelines then
194 funcs_hit[info.func] = true;
195 for line in pairs(info.activelines) do
196 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
199 if lines_hit[line] == false then
200 --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
201 lines_hit[line] = true;
202 covered_lines = covered_lines + 1;
206 function (test_name, success) -- Get info
207 local fn = file:gsub("^%W*", "");
208 local total_active_lines = 0;
209 local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
210 for line, active in pairs(lines_hit) do
211 if active ~= nil then total_active_lines = total_active_lines + 1; end
212 if coverage_file then
213 if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
214 else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
217 if coverage_file then coverage_file:close(); end
218 return covered_lines, total_active_lines, lines_hit;