util.pubsub: Remove access checking from remove_all_subscriptions (temporarily?)
[prosody.git] / tests / test.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
11 function run_all_tests()
12         package.loaded["net.connlisteners"] = { get = function () return {} end };
13         dotest "util.jid"
14         dotest "util.multitable"
15         dotest "net.http"
16         dotest "core.modulemanager"
17         dotest "core.stanza_router"
18         dotest "core.s2smanager"
19         dotest "core.configmanager"
20         dotest "util.stanza"
21         dotest "util.sasl.scram"
22         
23         dosingletest("test_sasl.lua", "latin1toutf8");
24 end
25
26 local verbosity = tonumber(arg[1]) or 2;
27
28 if os.getenv("WINDIR") then
29         package.path = package.path..";..\\?.lua";
30         package.cpath = package.cpath..";..\\?.dll";
31 else
32         package.path = package.path..";../?.lua";
33         package.cpath = package.cpath..";../?.so";
34 end
35
36 local _realG = _G;
37
38 require "util.import"
39
40 local env_mt = { __index = function (t,k) return rawget(_realG, 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 = _realG });
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 _fakeG = setmetatable({}, {__index = _realG});
113         _fakeG._G = _fakeG;
114         local tests = setmetatable({}, { __index = _fakeG });
115         tests.__unit = unitname;
116         local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
117         if not chunk then
118                 print("WARNING: ", "Failed to load tests for "..unitname, err);
119                 return;
120         end
121
122         setfenv(chunk, tests);
123         local success, err = pcall(chunk);
124         if not success then
125                 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
126                 return;
127         end
128         if tests.env then setmetatable(tests.env, { __index = _realG }); end
129         local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _fakeG }, { __index = tests.env or _fakeG }) });
130         local fn = "../"..unitname:gsub("%.", "/")..".lua";
131         local chunk, err = loadfile(fn);
132         if not chunk then
133                 print("WARNING: ", "Failed to load module: "..unitname, err);
134                 return;
135         end
136         
137         local oldmodule, old_M = _fakeG.module, _fakeG._M;
138         _fakeG.module = function () _M = _G end
139         setfenv(chunk, unit);
140         local success, err = pcall(chunk);
141         _fakeG.module, _fakeG._M = oldmodule, old_M;
142         if not success then
143                 print("WARNING: ", "Failed to initialise module: "..unitname, err);
144                 return;
145         end
146         
147         for name, f in pairs(unit) do
148                 local test = rawget(tests, name);
149                 if type(f) ~= "function" then
150                         if verbosity >= 3 then
151                                 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
152                         end
153                 elseif type(test) ~= "function" then
154                         if verbosity >= 1 then
155                                 print("WARNING: ", unitname.."."..name.." has no test!");
156                         end
157                 else
158                         if verbosity >= 4 then
159                                 print("INFO: ", "Testing "..unitname.."."..name);
160                         end
161                         local line_hook, line_info = new_line_coverage_monitor(fn);
162                         debug.sethook(line_hook, "l")
163                         local success, ret = pcall(test, f, unit);
164                         debug.sethook();
165                         if not success then
166                                 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
167                                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
168                                 line_info(name, false, report_file);
169                         elseif verbosity >= 2 then
170                                 print("TEST SUCCEEDED: ", unitname, name);
171                                 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
172                         else
173                                 line_info(name, success, report_file);
174                         end
175                 end
176         end
177 end
178
179 function runtest(f, msg)
180         if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
181         local success, ret = pcall(f);
182         if success and verbosity >= 2 then
183                 print("SUBTEST PASSED: "..(msg or "(no description)"));
184         elseif (not success) and verbosity >= 0 then
185                 print("SUBTEST FAILED: "..(msg or "(no description)"));
186                 error(ret, 0);
187         end
188 end
189
190 function new_line_coverage_monitor(file)
191         local lines_hit, funcs_hit = {}, {};
192         local total_lines, covered_lines = 0, 0;
193         
194         for line in io.lines(file) do
195                 total_lines = total_lines + 1;
196         end
197         
198         return function (event, line) -- Line hook
199                         if not lines_hit[line] then
200                                 local info = debug.getinfo(2, "fSL")
201                                 if not info.source:find(file) then return; end
202                                 if not funcs_hit[info.func] and info.activelines then
203                                         funcs_hit[info.func] = true;
204                                         for line in pairs(info.activelines) do
205                                                 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
206                                         end
207                                 end
208                                 if lines_hit[line] == false then
209                                         --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
210                                         lines_hit[line] = true;
211                                         covered_lines = covered_lines + 1;
212                                 end
213                         end
214                 end,
215                 function (test_name, success) -- Get info
216                         local fn = file:gsub("^%W*", "");
217                         local total_active_lines = 0;
218                         local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
219                         for line, active in pairs(lines_hit) do
220                                 if active ~= nil then total_active_lines = total_active_lines + 1; end
221                                 if coverage_file then
222                                         if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
223                                         else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
224                                 end
225                         end
226                         if coverage_file then coverage_file:close(); end
227                         return covered_lines, total_active_lines, lines_hit;
228                 end
229 end
230
231 run_all_tests()