Software /
code /
prosody
Annotate
tests/test.lua @ 322:d370fea67264
Fix for broadcasting presence of available resources to newly approved contact
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Mon, 17 Nov 2008 10:24:47 +0500 |
parent | 301:fcb7e63630ae |
child | 337:4a1dd1c2c219 |
rev | line source |
---|---|
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local verbosity = tonumber(arg[1]) or 2; |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
271
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
4 package.path = package.path..";../?.lua"; |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
5 |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
6 require "util.import" |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
7 |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
8 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
|
9 function testlib_new_env(t) |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
10 return setmetatable(t or {}, env_mt); |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
11 end |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
12 |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
13 function assert_equal(a, b, message) |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 if not (a == b) then |
271
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
15 error("\n assert_equal failed: "..tostring(a).." ~= "..tostring(b)..(message and ("\n Message: "..message) or ""), 2); |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 elseif verbosity >= 4 then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b)); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 function dotest(unitname) |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 local tests = setmetatable({}, { __index = _G }); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 tests.__unit = unitname; |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua"); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 if not chunk then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 print("WARNING: ", "Failed to load tests for "..unitname, err); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 return; |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 setfenv(chunk, tests); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 local success, err = pcall(chunk); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 if not success then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 print("WARNING: ", "Failed to initialise tests for "..unitname, err); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 return; |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) }); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 local chunk, err = loadfile("../"..unitname:gsub("%.", "/")..".lua"); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 if not chunk then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 print("WARNING: ", "Failed to load module: "..unitname, err); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 return; |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 setfenv(chunk, unit); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 local success, err = pcall(chunk); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 if not success then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 print("WARNING: ", "Failed to initialise module: "..unitname, err); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 return; |
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 |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 for name, f in pairs(unit) do |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 if type(f) ~= "function" then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 if verbosity >= 3 then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 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
|
56 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 elseif type(tests[name]) ~= "function" then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 if verbosity >= 1 then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 print("WARNING: ", unitname.."."..name.." has no test!"); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 else |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 local success, ret = pcall(tests[name], f, unit); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 if not success then |
271
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
64 print("TEST FAILED! Unit: ["..unitname.."] Function: ["..name.."]"); |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
65 print(" Location: "..ret:gsub(":%s*\n", "\n")); |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 elseif verbosity >= 2 then |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 print("TEST SUCCEEDED: ", unitname, name); |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 end |
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 |
271
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
73 function runtest(f, msg) |
301
fcb7e63630ae
Warn when subtest function does not exist
Matthew Wild <mwild1@gmail.com>
parents:
271
diff
changeset
|
74 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
|
75 local success, ret = pcall(f); |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
76 if success and verbosity >= 2 then |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
77 print("SUBTEST PASSED: "..(msg or "(no description)")); |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
78 elseif (not success) and verbosity >= 1 then |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
79 print("SUBTEST FAILED: "..(msg or "(no description)")); |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
80 error(ret, 0); |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
81 end |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
82 end |
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
83 |
28
4a238233f278
Adding initial unit testing scripts
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 dotest "util.jid" |
271
396edd2f9d2e
Some fixes for our test runner
Matthew Wild <mwild1@gmail.com>
parents:
28
diff
changeset
|
85 dotest "core.stanza_router" |