Fix jid.split test function
[prosody.git] / tests / test.lua
1
2 local verbosity = tonumber(arg[1]) or 2;
3
4 function assert_equal(a, b)
5         if not (a == b) then
6                 error(getfenv(2).__unit.."assert_equal failed: "..tostring(a).." ~= "..tostring(b), 2);
7         elseif verbosity >= 4 then
8                 print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
9         end
10 end
11
12 function dotest(unitname)
13         local tests = setmetatable({}, { __index = _G });
14         tests.__unit = unitname;
15         local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
16         if not chunk then
17                 print("WARNING: ", "Failed to load tests for "..unitname, err);
18                 return;
19         end
20
21         setfenv(chunk, tests);
22         local success, err = pcall(chunk);
23         if not success then
24                 print("WARNING: ", "Failed to initialise tests for "..unitname, err);
25                 return;
26         end
27         
28         local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) });
29
30         local chunk, err = loadfile("../"..unitname:gsub("%.", "/")..".lua");
31         if not chunk then
32                 print("WARNING: ", "Failed to load module: "..unitname, err);
33                 return;
34         end
35
36         setfenv(chunk, unit);
37         local success, err = pcall(chunk);
38         if not success then
39                 print("WARNING: ", "Failed to initialise module: "..unitname, err);
40                 return;
41         end
42         
43         for name, f in pairs(unit) do
44                 if type(f) ~= "function" then
45                         if verbosity >= 3 then
46                                 print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
47                         end
48                 elseif type(tests[name]) ~= "function" then
49                         if verbosity >= 1 then
50                                 print("WARNING: ", unitname.."."..name.." has no test!");
51                         end
52                 else
53                         local success, ret = pcall(tests[name], f, unit);
54                         if not success then
55                                 print("TEST FAILED: ", unitname, name, ret);
56                         elseif verbosity >= 2 then
57                                 print("TEST SUCCEEDED: ", unitname, name);
58                         end
59                 end
60         end
61 end
62
63 dotest "util.jid"
64