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