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