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