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