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