tests/test.lua

Fri, 09 Jan 2009 17:36:28 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 09 Jan 2009 17:36:28 +0000
changeset 681
686b73503ce8
parent 615
4ae3e81513f3
child 758
b1885732e979
permissions
-rw-r--r--

Add test for previous commit

615
4ae3e81513f3 0.1 -> 0.2
Matthew Wild <mwild1@gmail.com>
parents: 569
diff changeset
1 -- Prosody IM v0.2
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
2 -- Copyright (C) 2008 Matthew Wild
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
3 -- Copyright (C) 2008 Waqas Hussain
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
4 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
5 -- This program is free software; you can redistribute it and/or
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
6 -- modify it under the terms of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
7 -- as published by the Free Software Foundation; either version 2
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
8 -- of the License, or (at your option) any later version.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
9 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
10 -- This program is distributed in the hope that it will be useful,
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
13 -- GNU General Public License for more details.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
14 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
15 -- You should have received a copy of the GNU General Public License
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
16 -- along with this program; if not, write to the Free Software
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
18 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
19
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
20
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
21
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
22 function run_all_tests()
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
23 dotest "util.jid"
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
24 dotest "util.multitable"
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
25 dotest "core.stanza_router"
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
26 dotest "core.s2smanager"
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
27 dotest "core.configmanager"
681
686b73503ce8 Add test for previous commit
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
28 dotest "util.stanza"
686b73503ce8 Add test for previous commit
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
29
509
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
30 dosingletest("test_sasl.lua", "latin1toutf8");
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
31 end
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local verbosity = tonumber(arg[1]) or 2;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
35 package.path = package.path..";../?.lua";
470
2f9d42fdeffa Also look for binary modules in the parent directory when running tests
Matthew Wild <mwild1@gmail.com>
parents: 370
diff changeset
36 package.cpath = package.cpath..";../?.so";
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
37
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
38 require "util.import"
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
39
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
40 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
41 function testlib_new_env(t)
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
42 return setmetatable(t or {}, env_mt);
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
43 end
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
44
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
45 function assert_equal(a, b, message, level)
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if not (a == b) then
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
47 error("\n assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n Message: "..message) or ""), (level or 1) + 1);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 elseif verbosity >= 4 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
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
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
53 function assert_table(a, message, level)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
54 assert_equal(type(a), "table", message, (level or 1) + 1);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
55 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
56 function assert_function(a, message, level)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
57 assert_equal(type(a), "function", message, (level or 1) + 1);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
58 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
59 function assert_string(a, message, level)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
60 assert_equal(type(a), "string", message, (level or 1) + 1);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
61 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
62 function assert_boolean(a, message)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
63 assert_equal(type(a), "boolean", message);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
64 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
65 function assert_is(a, message)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
66 assert_equal(not not a, true, message);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
67 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
68 function assert_is_not(a, message)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
69 assert_equal(not not a, false, message);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
70 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
71
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
72
509
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
73 function dosingletest(testname, fname)
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
74 local tests = setmetatable({}, { __index = _G });
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
75 tests.__unit = testname;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
76 tests.__test = fname;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
77 local chunk, err = loadfile(testname);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
78 if not chunk then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
79 print("WARNING: ", "Failed to load tests for "..testname, err);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
80 return;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
81 end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
82
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
83 setfenv(chunk, tests);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
84 local success, err = pcall(chunk);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
85 if not success then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
86 print("WARNING: ", "Failed to initialise tests for "..testname, err);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
87 return;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
88 end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
89
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
90 if type(tests[fname]) ~= "function" then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
91 error(testname.." has no test '"..fname.."'", 0);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
92 end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
93
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
94
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
95 local line_hook, line_info = new_line_coverage_monitor(testname);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
96 debug.sethook(line_hook, "l")
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
97 local success, ret = pcall(tests[fname]);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
98 debug.sethook();
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
99 if not success then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
100 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
101 print(" Location: "..ret:gsub(":%s*\n", "\n"));
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
102 line_info(fname, false, report_file);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
103 elseif verbosity >= 2 then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
104 print("TEST SUCCEEDED: ", testname, fname);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
105 print(string.format("TEST COVERED %d/%d lines", line_info(fname, true, report_file)));
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
106 else
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
107 line_info(name, success, report_file);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
108 end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
109 end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
110
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 function dotest(unitname)
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 local tests = setmetatable({}, { __index = _G });
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 tests.__unit = unitname;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 if not chunk then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 print("WARNING: ", "Failed to load tests for "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 setfenv(chunk, tests);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 local success, err = pcall(chunk);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 if not success then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) });
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
129 local fn = "../"..unitname:gsub("%.", "/")..".lua";
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
130 local chunk, err = loadfile(fn);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 if not chunk then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 print("WARNING: ", "Failed to load module: "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 setfenv(chunk, unit);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 local success, err = pcall(chunk);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 if not success then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 print("WARNING: ", "Failed to initialise module: "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 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
144 local test = rawget(tests, name);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 if type(f) ~= "function" then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 if verbosity >= 3 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 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
148 end
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
149 elseif type(test) ~= "function" then
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 if verbosity >= 1 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 print("WARNING: ", unitname.."."..name.." has no test!");
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 else
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
154 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
155 debug.sethook(line_hook, "l")
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
156 local success, ret = pcall(test, f, unit);
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
157 debug.sethook();
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 if not success then
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
159 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
160 print(" Location: "..ret:gsub(":%s*\n", "\n"));
370
9ade55e059ea Update test.lua with a work-in-progress
Matthew Wild <mwild1@gmail.com>
parents: 361
diff changeset
161 line_info(name, false, report_file);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 elseif verbosity >= 2 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 print("TEST SUCCEEDED: ", unitname, name);
370
9ade55e059ea Update test.lua with a work-in-progress
Matthew Wild <mwild1@gmail.com>
parents: 361
diff changeset
164 print(string.format("TEST COVERED %d/%d lines", line_info(name, true, report_file)));
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
165 else
370
9ade55e059ea Update test.lua with a work-in-progress
Matthew Wild <mwild1@gmail.com>
parents: 361
diff changeset
166 line_info(name, success, report_file);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
172 function runtest(f, msg)
301
fcb7e63630ae Warn when subtest function does not exist
Matthew Wild <mwild1@gmail.com>
parents: 271
diff changeset
173 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
174 local success, ret = pcall(f);
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
175 if success and verbosity >= 2 then
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
176 print("SUBTEST PASSED: "..(msg or "(no description)"));
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
177 elseif (not success) and verbosity >= 1 then
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
178 print("SUBTEST FAILED: "..(msg or "(no description)"));
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
179 error(ret, 0);
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
180 end
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
181 end
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
182
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
183 function new_line_coverage_monitor(file)
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
184 local lines_hit, funcs_hit = {}, {};
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
185 local total_lines, covered_lines = 0, 0;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
186
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
187 for line in io.lines(file) do
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
188 total_lines = total_lines + 1;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
189 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
190
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
191 return function (event, line) -- Line hook
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
192 if not lines_hit[line] then
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
193 local info = debug.getinfo(2, "fSL")
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
194 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
195 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
196 funcs_hit[info.func] = true;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
197 for line in pairs(info.activelines) do
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
198 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
199 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
200 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
201 if lines_hit[line] == false then
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
202 --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
203 lines_hit[line] = true;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
204 covered_lines = covered_lines + 1;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
205 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
206 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
207 end,
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
208 function (test_name, success) -- Get info
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
209 local fn = file:gsub("^%W*", "");
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
210 local total_active_lines = 0;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
211 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
212 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
213 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
214 if coverage_file then
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
215 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
216 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
217 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
218 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
219 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
220 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
221 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
222 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
223
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
224 run_all_tests()

mercurial