tests/test.lua

Thu, 20 Nov 2008 21:02:49 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 20 Nov 2008 21:02:49 +0000
changeset 361
a2d83b04d769
parent 337
4a1dd1c2c219
child 370
9ade55e059ea
permissions
-rw-r--r--

Update unit testing to output coverage reports

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

mercurial