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 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
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                 tests_passed = false;
103                 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
104                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
105                 line_info(fname, false, report_file);
106         elseif verbosity >= 2 then
107                 print("TEST SUCCEEDED: ", testname, fname);
108                 print(string.format("TEST COVERED %d/%d lines", line_info(fname, true, report_file)));
109         else
110                 line_info(name, success, report_file);
111         end
112 end
113
114 function dotest(unitname)
115         local _fakeG = setmetatable({}, {__index = _realG});
116         _fakeG._G = _fakeG;
117         local tests = setmetatable({}, { __index = _fakeG });
118         tests.__unit = unitname;
119         local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
120         if not chunk then
121                 print("WARNING: ", "Failed to load tests for "..unitname, err);
122                 return;
123         end
124
125         setfenv(chunk, tests);
126         local success, err = pcall(chunk);
127         if not success then
128                 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
129                 return;
130         end
131         if tests.env then setmetatable(tests.env, { __index = _realG }); end
132         local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _fakeG }, { __index = tests.env or _fakeG }) });
133         local fn = "../"..unitname:gsub("%.", "/")..".lua";
134         local chunk, err = loadfile(fn);
135         if not chunk then
136                 print("WARNING: ", "Failed to load module: "..unitname, err);
137                 return;
138         end
139
140         local oldmodule, old_M = _fakeG.module, _fakeG._M;
141         _fakeG.module = function () _M = unit end
142         setfenv(chunk, unit);
143         local success, ret = pcall(chunk);
144         _fakeG.module, _fakeG._M = oldmodule, old_M;
145         if not success then
146                 print("WARNING: ", "Failed to initialise module: "..unitname, err);
147                 return;
148         end
149
150         if type(ret) == "table" then
151                 for k,v in pairs(ret) do
152                         unit[k] = v;
153                 end
154         end
155
156         for name, f in pairs(unit) do
157                 local test = rawget(tests, name);
158                 if type(f) ~= "function" then
159                         if verbosity >= 3 then
160                                 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
161                         end
162                 elseif type(test) ~= "function" then
163                         if verbosity >= 1 then
164                                 print("WARNING: ", unitname.."."..name.." has no test!");
165                         end
166                 else
167                         if verbosity >= 4 then
168                                 print("INFO: ", "Testing "..unitname.."."..name);
169                         end
170                         local line_hook, line_info = new_line_coverage_monitor(fn);
171                         debug.sethook(line_hook, "l")
172                         local success, ret = pcall(test, f, unit);
173                         debug.sethook();
174                         if not success then
175                                 tests_passed = false;
176                                 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
177                                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
178                                 line_info(name, false, report_file);
179                         elseif verbosity >= 2 then
180                                 print("TEST SUCCEEDED: ", unitname, name);
181                                 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
182                         else
183                                 line_info(name, success, report_file);
184                         end
185                 end
186         end
187 end
188
189 function runtest(f, msg)
190         if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
191         local success, ret = pcall(f);
192         if success and verbosity >= 2 then
193                 print("SUBTEST PASSED: "..(msg or "(no description)"));
194         elseif (not success) and verbosity >= 0 then
195                 tests_passed = false;
196                 print("SUBTEST FAILED: "..(msg or "(no description)"));
197                 error(ret, 0);
198         end
199 end
200
201 function new_line_coverage_monitor(file)
202         local lines_hit, funcs_hit = {}, {};
203         local total_lines, covered_lines = 0, 0;
204
205         for line in io.lines(file) do
206                 total_lines = total_lines + 1;
207         end
208
209         return function (event, line) -- Line hook
210                         if not lines_hit[line] then
211                                 local info = debug.getinfo(2, "fSL")
212                                 if not info.source:find(file) then return; end
213                                 if not funcs_hit[info.func] and info.activelines then
214                                         funcs_hit[info.func] = true;
215                                         for line in pairs(info.activelines) do
216                                                 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
217                                         end
218                                 end
219                                 if lines_hit[line] == false then
220                                         --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
221                                         lines_hit[line] = true;
222                                         covered_lines = covered_lines + 1;
223                                 end
224                         end
225                 end,
226                 function (test_name, success) -- Get info
227                         local fn = file:gsub("^%W*", "");
228                         local total_active_lines = 0;
229                         local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
230                         for line, active in pairs(lines_hit) do
231                                 if active ~= nil then total_active_lines = total_active_lines + 1; end
232                                 if coverage_file then
233                                         if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
234                                         else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
235                                 end
236                         end
237                         if coverage_file then coverage_file:close(); end
238                         return covered_lines, total_active_lines, lines_hit;
239                 end
240 end
241
242 run_all_tests()
243
244 os.exit(tests_passed and 0 or 1);