Software /
code /
prosody
Annotate
tests/test.lua @ 7371:d5ba0dec0c95
MUC: Cache public rooms and their names to speed up disco#items
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 18 Apr 2016 19:19:33 +0200 |
parent | 7241:5e7797822f19 |
child | 7437:ff74188488ab |
child | 7493:f67b7509ee28 |
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 |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5604
diff
changeset
|
4 -- |
758 | 5 -- This project is MIT/X11 licensed. Please see the |
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 |
6824
85578994d376
tests: Return status code depending on whether tests passed
Matthew Wild <mwild1@gmail.com>
parents:
6602
diff
changeset
|
9 local tests_passed = true; |
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" |
5604
6df0ec991f2e
tests: Some much-needed cleanup...
Matthew Wild <mwild1@gmail.com>
parents:
4420
diff
changeset
|
15 dotest "util.rfc6724" |
6df0ec991f2e
tests: Some much-needed cleanup...
Matthew Wild <mwild1@gmail.com>
parents:
4420
diff
changeset
|
16 dotest "util.http" |
361
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
17 dotest "core.stanza_router" |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
18 dotest "core.s2smanager" |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
19 dotest "core.configmanager" |
5604
6df0ec991f2e
tests: Some much-needed cleanup...
Matthew Wild <mwild1@gmail.com>
parents:
4420
diff
changeset
|
20 dotest "util.ip" |
7233 | 21 dotest "util.json" |
681
686b73503ce8
Add test for previous commit
Matthew Wild <mwild1@gmail.com>
parents:
615
diff
changeset
|
22 dotest "util.stanza" |
3406
748246005893
tests: Added tests for util.sasl.scram.Hi().
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
23 dotest "util.sasl.scram" |
6946
31fb9eb9edce
tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
6824
diff
changeset
|
24 dotest "util.cache" |
7024
8ce592e376ff
tests: Add small test for util.throttle
Kim Alvefur <zash@zash.se>
parents:
6946
diff
changeset
|
25 dotest "util.throttle" |
7073
31fa6770019c
tests: Add test for util.uuid (checks that the output format is correct)
Kim Alvefur <zash@zash.se>
parents:
7072
diff
changeset
|
26 dotest "util.uuid" |
7172
32b74ad54432
tests: Add minimal test for util.random that checks that it returns the number of bytes requested
Kim Alvefur <zash@zash.se>
parents:
7108
diff
changeset
|
27 dotest "util.random" |
7241
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
7233
diff
changeset
|
28 dotest "util.xml" |
5e7797822f19
tests: Add basic tests for util.xml and util.xmppstream
Matthew Wild <mwild1@gmail.com>
parents:
7233
diff
changeset
|
29 dotest "util.xmppstream" |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5604
diff
changeset
|
30 |
509
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
31 dosingletest("test_sasl.lua", "latin1toutf8"); |
6594
fa6eb6fb4a80
tests: Add UTF-8 validity tests
Matthew Wild <mwild1@gmail.com>
parents:
4420
diff
changeset
|
32 dosingletest("test_utf8.lua", "valid"); |
361
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
33 end |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 local verbosity = tonumber(arg[1]) or 2; |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
808
ce39abe0259a
Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents:
787
diff
changeset
|
37 if os.getenv("WINDIR") then |
ce39abe0259a
Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents:
787
diff
changeset
|
38 package.path = package.path..";..\\?.lua"; |
ce39abe0259a
Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents:
787
diff
changeset
|
39 package.cpath = package.cpath..";..\\?.dll"; |
ce39abe0259a
Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents:
787
diff
changeset
|
40 else |
ce39abe0259a
Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents:
787
diff
changeset
|
41 package.path = package.path..";../?.lua"; |
ce39abe0259a
Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents:
787
diff
changeset
|
42 package.cpath = package.cpath..";../?.so"; |
ce39abe0259a
Fixed tests/test.lua to work on Windows
Waqas Hussain <waqas20@gmail.com>
parents:
787
diff
changeset
|
43 end |
271
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
44 |
1972
26d4b99ba211
tests: More environment magic to help get stuff working in a sandbox
Matthew Wild <mwild1@gmail.com>
parents:
1963
diff
changeset
|
45 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
|
46 |
271
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
47 require "util.import" |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
48 |
7108
bb8a38c68191
tests: Use util.envload to load chunks (fixes #608)
Kim Alvefur <zash@zash.se>
parents:
7073
diff
changeset
|
49 local envloadfile = require "util.envload".envloadfile; |
bb8a38c68191
tests: Use util.envload to load chunks (fixes #608)
Kim Alvefur <zash@zash.se>
parents:
7073
diff
changeset
|
50 |
1972
26d4b99ba211
tests: More environment magic to help get stuff working in a sandbox
Matthew Wild <mwild1@gmail.com>
parents:
1963
diff
changeset
|
51 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
|
52 function testlib_new_env(t) |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
53 return setmetatable(t or {}, env_mt); |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
54 end |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
55 |
569
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
56 function assert_equal(a, b, message, level) |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 if not (a == b) then |
569
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
58 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
|
59 elseif verbosity >= 4 then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b)); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 |
569
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
64 function assert_table(a, message, level) |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
65 assert_equal(type(a), "table", message, (level or 1) + 1); |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
66 end |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
67 function assert_function(a, message, level) |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
68 assert_equal(type(a), "function", message, (level or 1) + 1); |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
69 end |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
70 function assert_string(a, message, level) |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
71 assert_equal(type(a), "string", message, (level or 1) + 1); |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
72 end |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
73 function assert_boolean(a, message) |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
74 assert_equal(type(a), "boolean", message); |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
75 end |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
76 function assert_is(a, message) |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
77 assert_equal(not not a, true, message); |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
78 end |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
79 function assert_is_not(a, message) |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
80 assert_equal(not not a, false, message); |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
81 end |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
82 |
5216efe6088b
Add hostmanager, and eventmanager
Matthew Wild <mwild1@gmail.com>
parents:
519
diff
changeset
|
83 |
509
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
84 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
|
85 local tests = setmetatable({}, { __index = _realG }); |
509
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
86 tests.__unit = testname; |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
87 tests.__test = fname; |
7108
bb8a38c68191
tests: Use util.envload to load chunks (fixes #608)
Kim Alvefur <zash@zash.se>
parents:
7073
diff
changeset
|
88 local chunk, err = envloadfile(testname, tests); |
509
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
89 if not chunk then |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
90 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
|
91 return; |
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 local success, err = pcall(chunk); |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
95 if not success then |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
96 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
|
97 return; |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
98 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5604
diff
changeset
|
99 |
509
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
100 if type(tests[fname]) ~= "function" then |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
101 error(testname.." has no test '"..fname.."'", 0); |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
102 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5604
diff
changeset
|
103 |
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5604
diff
changeset
|
104 |
509
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
105 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
|
106 debug.sethook(line_hook, "l") |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
107 local success, ret = pcall(tests[fname]); |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
108 debug.sethook(); |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
109 if not success then |
6824
85578994d376
tests: Return status code depending on whether tests passed
Matthew Wild <mwild1@gmail.com>
parents:
6602
diff
changeset
|
110 tests_passed = false; |
509
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
111 print("TEST FAILED! Unit: ["..testname.."] Function: ["..fname.."]"); |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
112 print(" Location: "..ret:gsub(":%s*\n", "\n")); |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
113 line_info(fname, false, report_file); |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
114 elseif verbosity >= 2 then |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
115 print("TEST SUCCEEDED: ", testname, fname); |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
116 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
|
117 else |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
118 line_info(name, success, report_file); |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
119 end |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
120 end |
32899c8a6fe5
Add test for latin1toutf8 (which passes)
Matthew Wild <mwild1@gmail.com>
parents:
470
diff
changeset
|
121 |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 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
|
123 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
|
124 _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
|
125 local tests = setmetatable({}, { __index = _fakeG }); |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 tests.__unit = unitname; |
7108
bb8a38c68191
tests: Use util.envload to load chunks (fixes #608)
Kim Alvefur <zash@zash.se>
parents:
7073
diff
changeset
|
127 local chunk, err = envloadfile("test_"..unitname:gsub("%.", "_")..".lua", tests); |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 if not chunk then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 print("WARNING: ", "Failed to load tests for "..unitname, err); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 return; |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 local success, err = pcall(chunk); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 if not success then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 print("WARNING: ", "Failed to initialise tests for "..unitname, err); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 return; |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 end |
1972
26d4b99ba211
tests: More environment magic to help get stuff working in a sandbox
Matthew Wild <mwild1@gmail.com>
parents:
1963
diff
changeset
|
138 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
|
139 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
|
140 local fn = "../"..unitname:gsub("%.", "/")..".lua"; |
7108
bb8a38c68191
tests: Use util.envload to load chunks (fixes #608)
Kim Alvefur <zash@zash.se>
parents:
7073
diff
changeset
|
141 local chunk, err = envloadfile(fn, unit); |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 if not chunk then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 print("WARNING: ", "Failed to load module: "..unitname, err); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 return; |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5604
diff
changeset
|
146 |
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
|
147 local oldmodule, old_M = _fakeG.module, _fakeG._M; |
7051
ecfa474ff570
tests/test.lua: Fix fake module() function to prevent _M from being _G (test.lua's environment), which caused modules to break the sandbox when they set _M.*
Matthew Wild <mwild1@gmail.com>
parents:
6594
diff
changeset
|
148 _fakeG.module = function () |
ecfa474ff570
tests/test.lua: Fix fake module() function to prevent _M from being _G (test.lua's environment), which caused modules to break the sandbox when they set _M.*
Matthew Wild <mwild1@gmail.com>
parents:
6594
diff
changeset
|
149 setmetatable(unit, nil); |
ecfa474ff570
tests/test.lua: Fix fake module() function to prevent _M from being _G (test.lua's environment), which caused modules to break the sandbox when they set _M.*
Matthew Wild <mwild1@gmail.com>
parents:
6594
diff
changeset
|
150 unit._M = unit; |
ecfa474ff570
tests/test.lua: Fix fake module() function to prevent _M from being _G (test.lua's environment), which caused modules to break the sandbox when they set _M.*
Matthew Wild <mwild1@gmail.com>
parents:
6594
diff
changeset
|
151 end |
7072
3121054e665f
tests: Correct mistake from latst merge, use the same variable name in all 3 places where it matters
Kim Alvefur <zash@zash.se>
parents:
7062
diff
changeset
|
152 local success, ret = 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
|
153 _fakeG.module, _fakeG._M = oldmodule, old_M; |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 if not success then |
7072
3121054e665f
tests: Correct mistake from latst merge, use the same variable name in all 3 places where it matters
Kim Alvefur <zash@zash.se>
parents:
7062
diff
changeset
|
155 print("WARNING: ", "Failed to initialise module: "..unitname, ret); |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 return; |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5604
diff
changeset
|
158 |
5604
6df0ec991f2e
tests: Some much-needed cleanup...
Matthew Wild <mwild1@gmail.com>
parents:
4420
diff
changeset
|
159 if type(ret) == "table" then |
6df0ec991f2e
tests: Some much-needed cleanup...
Matthew Wild <mwild1@gmail.com>
parents:
4420
diff
changeset
|
160 for k,v in pairs(ret) do |
6df0ec991f2e
tests: Some much-needed cleanup...
Matthew Wild <mwild1@gmail.com>
parents:
4420
diff
changeset
|
161 unit[k] = v; |
6df0ec991f2e
tests: Some much-needed cleanup...
Matthew Wild <mwild1@gmail.com>
parents:
4420
diff
changeset
|
162 end |
6df0ec991f2e
tests: Some much-needed cleanup...
Matthew Wild <mwild1@gmail.com>
parents:
4420
diff
changeset
|
163 end |
6df0ec991f2e
tests: Some much-needed cleanup...
Matthew Wild <mwild1@gmail.com>
parents:
4420
diff
changeset
|
164 |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 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
|
166 local test = rawget(tests, name); |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 if type(f) ~= "function" then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 if verbosity >= 3 then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 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
|
170 end |
361
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
171 elseif type(test) ~= "function" then |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 if verbosity >= 1 then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 print("WARNING: ", unitname.."."..name.." has no test!"); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 else |
2246
eb047fe305aa
tests/test.lua: Print the current test being run if verbosity sufficient
Matthew Wild <mwild1@gmail.com>
parents:
1972
diff
changeset
|
176 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
|
177 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
|
178 end |
361
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
179 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
|
180 debug.sethook(line_hook, "l") |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
181 local success, ret = pcall(test, f, unit); |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
182 debug.sethook(); |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 if not success then |
6824
85578994d376
tests: Return status code depending on whether tests passed
Matthew Wild <mwild1@gmail.com>
parents:
6602
diff
changeset
|
184 tests_passed = false; |
271
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
185 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]"); |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
186 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
|
187 line_info(name, false, report_file); |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 elseif verbosity >= 2 then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 print("TEST SUCCEEDED: ", unitname, name); |
370
9ade55e059ea
Update test.lua with a work-in-progress
Matthew Wild <mwild1@gmail.com>
parents:
361
diff
changeset
|
190 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
|
191 else |
370
9ade55e059ea
Update test.lua with a work-in-progress
Matthew Wild <mwild1@gmail.com>
parents:
361
diff
changeset
|
192 line_info(name, success, report_file); |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 |
271
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
198 function runtest(f, msg) |
301
fcb7e63630ae
Warn when subtest function does not exist
Matthew Wild <mwild1@gmail.com>
parents:
271
diff
changeset
|
199 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
|
200 local success, ret = pcall(f); |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
201 if success and verbosity >= 2 then |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
202 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
|
203 elseif (not success) and verbosity >= 0 then |
6824
85578994d376
tests: Return status code depending on whether tests passed
Matthew Wild <mwild1@gmail.com>
parents:
6602
diff
changeset
|
204 tests_passed = false; |
271
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
205 print("SUBTEST FAILED: "..(msg or "(no description)")); |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
206 error(ret, 0); |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
207 end |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
208 end |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
209 |
361
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
210 function new_line_coverage_monitor(file) |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
211 local lines_hit, funcs_hit = {}, {}; |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
212 local total_lines, covered_lines = 0, 0; |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5604
diff
changeset
|
213 |
361
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
214 for line in io.lines(file) do |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
215 total_lines = total_lines + 1; |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
216 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5604
diff
changeset
|
217 |
361
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
218 return function (event, line) -- Line hook |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
219 if not lines_hit[line] then |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
220 local info = debug.getinfo(2, "fSL") |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
221 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
|
222 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
|
223 funcs_hit[info.func] = true; |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
224 for line in pairs(info.activelines) do |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
225 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
|
226 end |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
227 end |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
228 if lines_hit[line] == false then |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
229 --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
|
230 lines_hit[line] = true; |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
231 covered_lines = covered_lines + 1; |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
232 end |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
233 end |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
234 end, |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
235 function (test_name, success) -- Get info |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
236 local fn = file:gsub("^%W*", ""); |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
237 local total_active_lines = 0; |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
238 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
|
239 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
|
240 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
|
241 if coverage_file then |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3406
diff
changeset
|
242 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
|
243 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
|
244 end |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
245 end |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
246 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
|
247 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
|
248 end |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
249 end |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
250 |
a2d83b04d769
Update unit testing to output coverage reports
Matthew Wild <mwild1@gmail.com>
parents:
337
diff
changeset
|
251 run_all_tests() |
6824
85578994d376
tests: Return status code depending on whether tests passed
Matthew Wild <mwild1@gmail.com>
parents:
6602
diff
changeset
|
252 |
85578994d376
tests: Return status code depending on whether tests passed
Matthew Wild <mwild1@gmail.com>
parents:
6602
diff
changeset
|
253 os.exit(tests_passed and 0 or 1); |