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