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