Annotate

tests/test.lua @ 4678:9613673f916a

mod_http: Fix specifying method in app route keys
author Matthew Wild <mwild1@gmail.com>
date Tue, 24 Apr 2012 16:02:30 +0100
parent 4420:4314eeeed394
child 5604:6df0ec991f2e
child 6594:fa6eb6fb4a80
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1972
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1972
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
4 --
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 681
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 681
diff changeset
6 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
7 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
8
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 509
diff changeset
9
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
10
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
11 function run_all_tests()
4339
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
12 package.loaded["net.connlisteners"] = { get = function () return {} end };
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
13 dotest "util.jid"
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
14 dotest "util.multitable"
4420
4314eeeed394 util.rfc3484: New util implementing RFC3484 sorting
Florian Zeitz <florob@babelmonkeys.de>
parents: 4339
diff changeset
15 dotest "util.rfc3484"
4339
63304d323983 tests/test.lua, tests/test_net_http.lua: Tests for net.http's url and form encoding/decoding functions
Matthew Wild <mwild1@gmail.com>
parents: 3540
diff changeset
16 dotest "net.http"
1961
3652ef68c361 tests: Add tests for new modulemanager load_modules_for_host code
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
17 dotest "core.modulemanager"
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
18 dotest "core.stanza_router"
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
19 dotest "core.s2smanager"
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
20 dotest "core.configmanager"
681
686b73503ce8 Add test for previous commit
Matthew Wild <mwild1@gmail.com>
parents: 615
diff changeset
21 dotest "util.stanza"
3406
748246005893 tests: Added tests for util.sasl.scram.Hi().
Waqas Hussain <waqas20@gmail.com>
parents: 2925
diff changeset
22 dotest "util.sasl.scram"
2248
37344b18b551 tests/test.lua: Changes to environment handling of tests, and replace module() with dummy function that doesn't alter the current environment
Matthew Wild <mwild1@gmail.com>
parents: 2246
diff changeset
23
509
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
24 dosingletest("test_sasl.lua", "latin1toutf8");
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
25 end
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 local verbosity = tonumber(arg[1]) or 2;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
808
ce39abe0259a Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents: 787
diff changeset
29 if os.getenv("WINDIR") then
ce39abe0259a Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents: 787
diff changeset
30 package.path = package.path..";..\\?.lua";
ce39abe0259a Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents: 787
diff changeset
31 package.cpath = package.cpath..";..\\?.dll";
ce39abe0259a Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents: 787
diff changeset
32 else
ce39abe0259a Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents: 787
diff changeset
33 package.path = package.path..";../?.lua";
ce39abe0259a Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents: 787
diff changeset
34 package.cpath = package.cpath..";../?.so";
ce39abe0259a Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents: 787
diff changeset
35 end
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
36
1972
26d4b99ba211 tests: More environment magic to help get stuff working in a sandbox
Matthew Wild <mwild1@gmail.com>
parents: 1963
diff changeset
37 local _realG = _G;
26d4b99ba211 tests: More environment magic to help get stuff working in a sandbox
Matthew Wild <mwild1@gmail.com>
parents: 1963
diff changeset
38
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
39 require "util.import"
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
40
1972
26d4b99ba211 tests: More environment magic to help get stuff working in a sandbox
Matthew Wild <mwild1@gmail.com>
parents: 1963
diff changeset
41 local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end };
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
42 function testlib_new_env(t)
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
43 return setmetatable(t or {}, env_mt);
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
44 end
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
45
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
46 function assert_equal(a, b, message, level)
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 if not (a == b) then
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
48 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
49 elseif verbosity >= 4 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
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 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53
569
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
54 function assert_table(a, message, level)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
55 assert_equal(type(a), "table", message, (level or 1) + 1);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
56 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
57 function assert_function(a, message, level)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
58 assert_equal(type(a), "function", message, (level or 1) + 1);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
59 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
60 function assert_string(a, message, level)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
61 assert_equal(type(a), "string", message, (level or 1) + 1);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
62 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
63 function assert_boolean(a, message)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
64 assert_equal(type(a), "boolean", message);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
65 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
66 function assert_is(a, message)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
67 assert_equal(not not a, true, message);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
68 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
69 function assert_is_not(a, message)
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
70 assert_equal(not not a, false, message);
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
71 end
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
72
5216efe6088b Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents: 519
diff changeset
73
509
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
74 function dosingletest(testname, fname)
1972
26d4b99ba211 tests: More environment magic to help get stuff working in a sandbox
Matthew Wild <mwild1@gmail.com>
parents: 1963
diff changeset
75 local tests = setmetatable({}, { __index = _realG });
509
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
76 tests.__unit = testname;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
77 tests.__test = fname;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
78 local chunk, err = loadfile(testname);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
79 if not chunk then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
80 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
81 return;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
82 end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
83
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
84 setfenv(chunk, tests);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
85 local success, err = pcall(chunk);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
86 if not success then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
87 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
88 return;
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
89 end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
90
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
91 if type(tests[fname]) ~= "function" then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
92 error(testname.." has no test '"..fname.."'", 0);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
93 end
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
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
96 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
97 debug.sethook(line_hook, "l")
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
98 local success, ret = pcall(tests[fname]);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
99 debug.sethook();
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
100 if not success then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
101 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]");
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
102 print(" Location: "..ret:gsub(":%s*\n", "\n"));
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
103 line_info(fname, false, report_file);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
104 elseif verbosity >= 2 then
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
105 print("TEST SUCCEEDED: ", testname, fname);
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
106 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
107 else
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
108 line_info(name, success, report_file);
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 end
32899c8a6fe5 Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents: 470
diff changeset
111
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 function dotest(unitname)
2248
37344b18b551 tests/test.lua: Changes to environment handling of tests, and replace module() with dummy function that doesn't alter the current environment
Matthew Wild <mwild1@gmail.com>
parents: 2246
diff changeset
113 local _fakeG = setmetatable({}, {__index = _realG});
37344b18b551 tests/test.lua: Changes to environment handling of tests, and replace module() with dummy function that doesn't alter the current environment
Matthew Wild <mwild1@gmail.com>
parents: 2246
diff changeset
114 _fakeG._G = _fakeG;
37344b18b551 tests/test.lua: Changes to environment handling of tests, and replace module() with dummy function that doesn't alter the current environment
Matthew Wild <mwild1@gmail.com>
parents: 2246
diff changeset
115 local tests = setmetatable({}, { __index = _fakeG });
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 tests.__unit = unitname;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 if not chunk then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 print("WARNING: ", "Failed to load tests for "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 setfenv(chunk, tests);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 local success, err = pcall(chunk);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 if not success then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 end
1972
26d4b99ba211 tests: More environment magic to help get stuff working in a sandbox
Matthew Wild <mwild1@gmail.com>
parents: 1963
diff changeset
129 if tests.env then setmetatable(tests.env, { __index = _realG }); end
2248
37344b18b551 tests/test.lua: Changes to environment handling of tests, and replace module() with dummy function that doesn't alter the current environment
Matthew Wild <mwild1@gmail.com>
parents: 2246
diff changeset
130 local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _fakeG }, { __index = tests.env or _fakeG }) });
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
131 local fn = "../"..unitname:gsub("%.", "/")..".lua";
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
132 local chunk, err = loadfile(fn);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 if not chunk then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 print("WARNING: ", "Failed to load module: "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 end
2248
37344b18b551 tests/test.lua: Changes to environment handling of tests, and replace module() with dummy function that doesn't alter the current environment
Matthew Wild <mwild1@gmail.com>
parents: 2246
diff changeset
137
37344b18b551 tests/test.lua: Changes to environment handling of tests, and replace module() with dummy function that doesn't alter the current environment
Matthew Wild <mwild1@gmail.com>
parents: 2246
diff changeset
138 local oldmodule, old_M = _fakeG.module, _fakeG._M;
37344b18b551 tests/test.lua: Changes to environment handling of tests, and replace module() with dummy function that doesn't alter the current environment
Matthew Wild <mwild1@gmail.com>
parents: 2246
diff changeset
139 _fakeG.module = function () _M = _G end
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 setfenv(chunk, unit);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 local success, err = pcall(chunk);
2248
37344b18b551 tests/test.lua: Changes to environment handling of tests, and replace module() with dummy function that doesn't alter the current environment
Matthew Wild <mwild1@gmail.com>
parents: 2246
diff changeset
142 _fakeG.module, _fakeG._M = oldmodule, old_M;
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 if not success then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 print("WARNING: ", "Failed to initialise module: "..unitname, err);
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 return;
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 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
149 local test = rawget(tests, name);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 if type(f) ~= "function" then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 if verbosity >= 3 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152 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
153 end
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
154 elseif type(test) ~= "function" then
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 if verbosity >= 1 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 print("WARNING: ", unitname.."."..name.." has no test!");
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 else
2246
eb047fe305aa tests/test.lua: Print the current test being run if verbosity sufficient
Matthew Wild <mwild1@gmail.com>
parents: 1972
diff changeset
159 if verbosity >= 4 then
eb047fe305aa tests/test.lua: Print the current test being run if verbosity sufficient
Matthew Wild <mwild1@gmail.com>
parents: 1972
diff changeset
160 print("INFO: ", "Testing "..unitname.."."..name);
eb047fe305aa tests/test.lua: Print the current test being run if verbosity sufficient
Matthew Wild <mwild1@gmail.com>
parents: 1972
diff changeset
161 end
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
162 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
163 debug.sethook(line_hook, "l")
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
164 local success, ret = pcall(test, f, unit);
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
165 debug.sethook();
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 if not success then
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
167 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]");
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
168 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
169 line_info(name, false, report_file);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 elseif verbosity >= 2 then
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 print("TEST SUCCEEDED: ", unitname, name);
370
9ade55e059ea Update test.lua with a work-in-progress
Matthew Wild <mwild1@gmail.com>
parents: 361
diff changeset
172 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
173 else
370
9ade55e059ea Update test.lua with a work-in-progress
Matthew Wild <mwild1@gmail.com>
parents: 361
diff changeset
174 line_info(name, success, report_file);
28
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 end
4a238233f278 Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
180 function runtest(f, msg)
301
fcb7e63630ae Warn when subtest function does not exist
Matthew Wild <mwild1@gmail.com>
parents: 271
diff changeset
181 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
182 local success, ret = pcall(f);
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
183 if success and verbosity >= 2 then
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
184 print("SUBTEST PASSED: "..(msg or "(no description)"));
787
c4a4d5604549 Small fix to change verbosity level of subtests
Matthew Wild <mwild1@gmail.com>
parents: 760
diff changeset
185 elseif (not success) and verbosity >= 0 then
271
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
186 print("SUBTEST FAILED: "..(msg or "(no description)"));
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
187 error(ret, 0);
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
188 end
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
189 end
396edd2f9d2e Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents: 28
diff changeset
190
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
191 function new_line_coverage_monitor(file)
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
192 local lines_hit, funcs_hit = {}, {};
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
193 local total_lines, covered_lines = 0, 0;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
194
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
195 for line in io.lines(file) do
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
196 total_lines = total_lines + 1;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
197 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
198
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
199 return function (event, line) -- Line hook
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
200 if not lines_hit[line] then
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
201 local info = debug.getinfo(2, "fSL")
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
202 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
203 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
204 funcs_hit[info.func] = true;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
205 for line in pairs(info.activelines) do
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
206 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
207 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
208 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
209 if lines_hit[line] == false then
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
210 --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
211 lines_hit[line] = true;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
212 covered_lines = covered_lines + 1;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
213 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
214 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
215 end,
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
216 function (test_name, success) -- Get info
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
217 local fn = file:gsub("^%W*", "");
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
218 local total_active_lines = 0;
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
219 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
220 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
221 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
222 if coverage_file then
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3406
diff changeset
223 if active == false then coverage_file:write(fn, "|", line, "|", name or "", "|miss\n");
361
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
224 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
225 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
226 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
227 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
228 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
229 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
230 end
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
231
a2d83b04d769 Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents: 337
diff changeset
232 run_all_tests()