stanza_router: That'll teach me to not commit at this time of night. Or not.
[prosody.git] / tests / test.lua
1 -- Prosody IM v0.2
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21
22 function run_all_tests()
23         dotest "util.jid"
24         dotest "util.multitable"
25         dotest "core.stanza_router"
26         dotest "core.s2smanager"
27         dotest "core.configmanager"
28         dotest "util.stanza"
29                 
30         dosingletest("test_sasl.lua", "latin1toutf8");
31 end
32
33 local verbosity = tonumber(arg[1]) or 2;
34
35 package.path = package.path..";../?.lua";
36 package.cpath = package.cpath..";../?.so";
37
38 require "util.import"
39
40 local env_mt = { __index = function (t,k) return rawget(_G, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
41 function testlib_new_env(t)
42         return setmetatable(t or {}, env_mt);
43 end
44
45 function assert_equal(a, b, message, level)
46         if not (a == b) then
47                 error("\n   assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n   Message: "..message) or ""), (level or 1) + 1);
48         elseif verbosity >= 4 then
49                 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
50         end
51 end
52
53 function assert_table(a, message, level)
54         assert_equal(type(a), "table", message, (level or 1) + 1);
55 end
56 function assert_function(a, message, level)
57         assert_equal(type(a), "function", message, (level or 1) + 1);
58 end
59 function assert_string(a, message, level)
60         assert_equal(type(a), "string", message, (level or 1) + 1);
61 end
62 function assert_boolean(a, message)
63         assert_equal(type(a), "boolean", message);
64 end
65 function assert_is(a, message)
66         assert_equal(not not a, true, message);
67 end
68 function assert_is_not(a, message)
69         assert_equal(not not a, false, message);
70 end
71
72
73 function dosingletest(testname, fname)
74         local tests = setmetatable({}, { __index = _G });
75         tests.__unit = testname;
76         tests.__test = fname;
77         local chunk, err = loadfile(testname);
78         if not chunk then
79                 print("WARNING: ", "Failed to load tests for "..testname, err);
80                 return;
81         end
82
83         setfenv(chunk, tests);
84         local success, err = pcall(chunk);
85         if not success then
86                 print("WARNING: ", "Failed to initialise tests for "..testname, err);
87                 return;
88         end
89         
90         if type(tests[fname]) ~= "function" then
91                 error(testname.." has no test '"..fname.."'", 0);
92         end
93         
94         
95         local line_hook, line_info = new_line_coverage_monitor(testname);
96         debug.sethook(line_hook, "l")
97         local success, ret = pcall(tests[fname]);
98         debug.sethook();
99         if not success then
100                 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
101                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
102                 line_info(fname, false, report_file);
103         elseif verbosity >= 2 then
104                 print("TEST SUCCEEDED: ", testname, fname);
105                 print(string.format("TEST COVERED %d/%d lines", line_info(fname, true, report_file)));
106         else
107                 line_info(name, success, report_file);
108         end
109 end
110
111 function dotest(unitname)
112         local tests = setmetatable({}, { __index = _G });
113         tests.__unit = unitname;
114         local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
115         if not chunk then
116                 print("WARNING: ", "Failed to load tests for "..unitname, err);
117                 return;
118         end
119
120         setfenv(chunk, tests);
121         local success, err = pcall(chunk);
122         if not success then
123                 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
124                 return;
125         end
126         
127         local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) });
128
129         local fn = "../"..unitname:gsub("%.", "/")..".lua";
130         local chunk, err = loadfile(fn);
131         if not chunk then
132                 print("WARNING: ", "Failed to load module: "..unitname, err);
133                 return;
134         end
135
136         setfenv(chunk, unit);
137         local success, err = pcall(chunk);
138         if not success then
139                 print("WARNING: ", "Failed to initialise module: "..unitname, err);
140                 return;
141         end
142         
143         for name, f in pairs(unit) do
144                 local test = rawget(tests, name);
145                 if type(f) ~= "function" then
146                         if verbosity >= 3 then
147                                 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
148                         end
149                 elseif type(test) ~= "function" then
150                         if verbosity >= 1 then
151                                 print("WARNING: ", unitname.."."..name.." has no test!");
152                         end
153                 else
154                         local line_hook, line_info = new_line_coverage_monitor(fn);
155                         debug.sethook(line_hook, "l")
156                         local success, ret = pcall(test, f, unit);
157                         debug.sethook();
158                         if not success then
159                                 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
160                                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
161                                 line_info(name, false, report_file);
162                         elseif verbosity >= 2 then
163                                 print("TEST SUCCEEDED: ", unitname, name);
164                                 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
165                         else
166                                 line_info(name, success, report_file);
167                         end
168                 end
169         end
170 end
171
172 function runtest(f, msg)
173         if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
174         local success, ret = pcall(f);
175         if success and verbosity >= 2 then
176                 print("SUBTEST PASSED: "..(msg or "(no description)"));
177         elseif (not success) and verbosity >= 1 then
178                 print("SUBTEST FAILED: "..(msg or "(no description)"));
179                 error(ret, 0);
180         end
181 end
182
183 function new_line_coverage_monitor(file)
184         local lines_hit, funcs_hit = {}, {};
185         local total_lines, covered_lines = 0, 0;
186         
187         for line in io.lines(file) do
188                 total_lines = total_lines + 1;
189         end
190         
191         return function (event, line) -- Line hook
192                         if not lines_hit[line] then
193                                 local info = debug.getinfo(2, "fSL")
194                                 if not info.source:find(file) then return; end
195                                 if not funcs_hit[info.func] and info.activelines then
196                                         funcs_hit[info.func] = true;
197                                         for line in pairs(info.activelines) do
198                                                 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
199                                         end
200                                 end
201                                 if lines_hit[line] == false then
202                                         --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
203                                         lines_hit[line] = true;
204                                         covered_lines = covered_lines + 1;
205                                 end
206                         end
207                 end,
208                 function (test_name, success) -- Get info
209                         local fn = file:gsub("^%W*", "");
210                         local total_active_lines = 0;
211                         local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
212                         for line, active in pairs(lines_hit) do
213                                 if active ~= nil then total_active_lines = total_active_lines + 1; end
214                                 if coverage_file then
215                                         if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n"); 
216                                         else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
217                                 end
218                         end
219                         if coverage_file then coverage_file:close(); end
220                         return covered_lines, total_active_lines, lines_hit;
221                 end
222 end
223
224 run_all_tests()