19aa838952c629c550c13c21efbe12374bb0aa58
[prosody.git] / tests / test.lua
1
2 function run_all_tests()
3         dotest "util.jid"
4         dotest "core.stanza_router"
5         dotest "core.s2smanager"
6         dotest "core.configmanager"
7 end
8
9 local verbosity = tonumber(arg[1]) or 2;
10
11 package.path = package.path..";../?.lua";
12 package.cpath = package.cpath..";../?.so";
13
14 require "util.import"
15
16 local env_mt = { __index = function (t,k) return rawget(_G, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
17 function testlib_new_env(t)
18         return setmetatable(t or {}, env_mt);
19 end
20
21 function assert_equal(a, b, message)
22         if not (a == b) then
23                 error("\n   assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n   Message: "..message) or ""), 2);
24         elseif verbosity >= 4 then
25                 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
26         end
27 end
28
29 function dotest(unitname)
30         local tests = setmetatable({}, { __index = _G });
31         tests.__unit = unitname;
32         local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
33         if not chunk then
34                 print("WARNING: ", "Failed to load tests for "..unitname, err);
35                 return;
36         end
37
38         setfenv(chunk, tests);
39         local success, err = pcall(chunk);
40         if not success then
41                 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
42                 return;
43         end
44         
45         local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) });
46
47         local fn = "../"..unitname:gsub("%.", "/")..".lua";
48         local chunk, err = loadfile(fn);
49         if not chunk then
50                 print("WARNING: ", "Failed to load module: "..unitname, err);
51                 return;
52         end
53
54         setfenv(chunk, unit);
55         local success, err = pcall(chunk);
56         if not success then
57                 print("WARNING: ", "Failed to initialise module: "..unitname, err);
58                 return;
59         end
60         
61         for name, f in pairs(unit) do
62                 local test = rawget(tests, name);
63                 if type(f) ~= "function" then
64                         if verbosity >= 3 then
65                                 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
66                         end
67                 elseif type(test) ~= "function" then
68                         if verbosity >= 1 then
69                                 print("WARNING: ", unitname.."."..name.." has no test!");
70                         end
71                 else
72                         local line_hook, line_info = new_line_coverage_monitor(fn);
73                         debug.sethook(line_hook, "l")
74                         local success, ret = pcall(test, f, unit);
75                         debug.sethook();
76                         if not success then
77                                 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
78                                 print("   Location: "..ret:gsub(":%s*\n", "\n"));
79                                 line_info(name, false, report_file);
80                         elseif verbosity >= 2 then
81                                 print("TEST SUCCEEDED: ", unitname, name);
82                                 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
83                         else
84                                 line_info(name, success, report_file);
85                         end
86                 end
87         end
88 end
89
90 function runtest(f, msg)
91         if not f then print("SUBTEST NOT FOUND: "..(msg or "(no description)")); return; end
92         local success, ret = pcall(f);
93         if success and verbosity >= 2 then
94                 print("SUBTEST PASSED: "..(msg or "(no description)"));
95         elseif (not success) and verbosity >= 1 then
96                 print("SUBTEST FAILED: "..(msg or "(no description)"));
97                 error(ret, 0);
98         end
99 end
100
101 function new_line_coverage_monitor(file)
102         local lines_hit, funcs_hit = {}, {};
103         local total_lines, covered_lines = 0, 0;
104         
105         for line in io.lines(file) do
106                 total_lines = total_lines + 1;
107         end
108         
109         return function (event, line) -- Line hook
110                         if not lines_hit[line] then
111                                 local info = debug.getinfo(2, "fSL")
112                                 if not info.source:find(file) then return; end
113                                 if not funcs_hit[info.func] and info.activelines then
114                                         funcs_hit[info.func] = true;
115                                         for line in pairs(info.activelines) do
116                                                 lines_hit[line] = false; -- Marks it as hittable, but not hit yet
117                                         end
118                                 end
119                                 if lines_hit[line] == false then
120                                         --print("New line hit: "..line.." in "..debug.getinfo(2, "S").source);
121                                         lines_hit[line] = true;
122                                         covered_lines = covered_lines + 1;
123                                 end
124                         end
125                 end,
126                 function (test_name, success) -- Get info
127                         local fn = file:gsub("^%W*", "");
128                         local total_active_lines = 0;
129                         local coverage_file = io.open("reports/coverage_"..fn:gsub("%W+", "_")..".report", "a+");
130                         for line, active in pairs(lines_hit) do
131                                 if active ~= nil then total_active_lines = total_active_lines + 1; end
132                                 if coverage_file then
133                                         if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n"); 
134                                         else coverage_file:write(fn, "|", line, "|", name or "", "|", tostring(success), "\n"); end
135                                 end
136                         end
137                         if coverage_file then coverage_file:close(); end
138                         return covered_lines, total_active_lines, lines_hit;
139                 end
140 end
141
142 run_all_tests()