Automated merge with http://waqas.ath.cx/
[prosody.git] / tests / test.lua
1 -- Prosody IM v0.1
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 "core.stanza_router"
25         dotest "core.s2smanager"
26         dotest "core.configmanager"
27         
28         dosingletest("test_sasl.lua", "latin1toutf8");
29 end
30
31 local verbosity = tonumber(arg[1]) or 2;
32
33 package.path = package.path..";../?.lua";
34 package.cpath = package.cpath..";../?.so";
35
36 require "util.import"
37
38 local env_mt = { __index = function (t,k) return rawget(_G, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
39 function testlib_new_env(t)
40         return setmetatable(t or {}, env_mt);
41 end
42
43 function assert_equal(a, b, message)
44         if not (a == b) then
45                 error("\n   assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n   Message: "..message) or ""), 2);
46         elseif verbosity >= 4 then
47                 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
48         end
49 end
50
51 function dosingletest(testname, fname)
52         local tests = setmetatable({}, { __index = _G });
53         tests.__unit = testname;
54         tests.__test = fname;
55         local chunk, err = loadfile(testname);
56         if not chunk then
57                 print("WARNING: ", "Failed to load tests for "..testname, err);
58                 return;
59         end
60
61         setfenv(chunk, tests);
62         local success, err = pcall(chunk);
63         if not success then
64                 print("WARNING: ", "Failed to initialise tests for "..testname, err);
65                 return;
66         end
67         
68         if type(tests[fname]) ~= "function" then
69                 error(testname.." has no test '"..fname.."'", 0);
70         end
71         
72         
73         local line_hook, line_info = new_line_coverage_monitor(testname);
74         debug.sethook(line_hook, "l")
75         local success, ret = pcall(tests[fname]);
76         debug.sethook();
77         if not success then
78                 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
79                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
80                 line_info(fname, false, report_file);
81         elseif verbosity >= 2 then
82                 print("TEST SUCCEEDED: ", testname, fname);
83                 print(string.format("TEST COVERED %d/%d lines", line_info(fname, true, report_file)));
84         else
85                 line_info(name, success, report_file);
86         end
87 end
88
89 function dotest(unitname)
90         local tests = setmetatable({}, { __index = _G });
91         tests.__unit = unitname;
92         local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
93         if not chunk then
94                 print("WARNING: ", "Failed to load tests for "..unitname, err);
95                 return;
96         end
97
98         setfenv(chunk, tests);
99         local success, err = pcall(chunk);
100         if not success then
101                 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
102                 return;
103         end
104         
105         local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) });
106
107         local fn = "../"..unitname:gsub("%.", "/")..".lua";
108         local chunk, err = loadfile(fn);
109         if not chunk then
110                 print("WARNING: ", "Failed to load module: "..unitname, err);
111                 return;
112         end
113
114         setfenv(chunk, unit);
115         local success, err = pcall(chunk);
116         if not success then
117                 print("WARNING: ", "Failed to initialise module: "..unitname, err);
118                 return;
119         end
120         
121         for name, f in pairs(unit) do
122                 local test = rawget(tests, name);
123                 if type(f) ~= "function" then
124                         if verbosity >= 3 then
125                                 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
126                         end
127                 elseif type(test) ~= "function" then
128                         if verbosity >= 1 then
129                                 print("WARNING: ", unitname.."."..name.." has no test!");
130                         end
131                 else
132                         local line_hook, line_info = new_line_coverage_monitor(fn);
133                         debug.sethook(line_hook, "l")
134                         local success, ret = pcall(test, f, unit);
135                         debug.sethook();
136                         if not success then
137                                 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
138                                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
139                                 line_info(name, false, report_file);
140                         elseif verbosity >= 2 then
141                                 print("TEST SUCCEEDED: ", unitname, name);
142                                 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
143                         else
144                                 line_info(name, success, report_file);
145                         end
146                 end
147         end
148 end
149
150 function runtest(f, msg)
151         if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
152         local success, ret = pcall(f);
153         if success and verbosity >= 2 then
154                 print("SUBTEST PASSED: "..(msg or "(no description)"));
155         elseif (not success) and verbosity >= 1 then
156                 print("SUBTEST FAILED: "..(msg or "(no description)"));
157                 error(ret, 0);
158         end
159 end
160
161 function new_line_coverage_monitor(file)
162         local lines_hit, funcs_hit = {}, {};
163         local total_lines, covered_lines = 0, 0;
164         
165         for line in io.lines(file) do
166                 total_lines = total_lines + 1;
167         end
168         
169         return function (event, line) -- Line hook
170                         if not lines_hit[line] then
171                                 local info = debug.getinfo(2, "fSL")
172                                 if not info.source:find(file) then return; end
173                                 if not funcs_hit[info.func] and info.activelines then
174                                         funcs_hit[info.func] = true;
175                                         for line in pairs(info.activelines) do
176                                                 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
177                                         end
178                                 end
179                                 if lines_hit[line] == false then
180                                         --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
181                                         lines_hit[line] = true;
182                                         covered_lines = covered_lines + 1;
183                                 end
184                         end
185                 end,
186                 function (test_name, success) -- Get info
187                         local fn = file:gsub("^%W*", "");
188                         local total_active_lines = 0;
189                         local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
190                         for line, active in pairs(lines_hit) do
191                                 if active ~= nil then total_active_lines = total_active_lines + 1; end
192                                 if coverage_file then
193                                         if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n"); 
194                                         else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
195                                 end
196                         end
197                         if coverage_file then coverage_file:close(); end
198                         return covered_lines, total_active_lines, lines_hit;
199                 end
200 end
201
202 run_all_tests()