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