aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2008-09-27 19:18:05 +0100
committerMatthew Wild <mwild1@gmail.com>2008-09-27 19:18:05 +0100
commiteabe25e8a4de6a2238559330909401e5be31998d (patch)
tree51f8847a829719e97badd60394fdcc0669ed69ba /tests
parent58d1a65408ee32d590071c2ced7260b5592b5d3c (diff)
downloadprosody-eabe25e8a4de6a2238559330909401e5be31998d.tar.gz
prosody-eabe25e8a4de6a2238559330909401e5be31998d.zip
Adding initial unit testing scripts
Diffstat (limited to 'tests')
-rw-r--r--tests/test.lua64
-rw-r--r--tests/test_util_jid.lua13
2 files changed, 77 insertions, 0 deletions
diff --git a/tests/test.lua b/tests/test.lua
new file mode 100644
index 00000000..108dd9a4
--- /dev/null
+++ b/tests/test.lua
@@ -0,0 +1,64 @@
+
+local verbosity = tonumber(arg[1]) or 2;
+
+function assert_equal(a, b)
+ if not (a == b) then
+ error(getfenv(2).__unit.."assert_equal failed: "..tostring(a).." ~= "..tostring(b), 2);
+ elseif verbosity >= 4 then
+ print("assert_equal succeeded: "..tostring(a).." == "..tostring(b));
+ end
+end
+
+function dotest(unitname)
+ local tests = setmetatable({}, { __index = _G });
+ tests.__unit = unitname;
+ local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua");
+ if not chunk then
+ print("WARNING: ", "Failed to load tests for "..unitname, err);
+ return;
+ end
+
+ setfenv(chunk, tests);
+ local success, err = pcall(chunk);
+ if not success then
+ print("WARNING: ", "Failed to initialise tests for "..unitname, err);
+ return;
+ end
+
+ local unit = setmetatable({}, { __index = setmetatable({ module = function () end }, { __index = _G }) });
+
+ local chunk, err = loadfile("../"..unitname:gsub("%.", "/")..".lua");
+ if not chunk then
+ print("WARNING: ", "Failed to load module: "..unitname, err);
+ return;
+ end
+
+ setfenv(chunk, unit);
+ local success, err = pcall(chunk);
+ if not success then
+ print("WARNING: ", "Failed to initialise module: "..unitname, err);
+ return;
+ end
+
+ for name, f in pairs(unit) do
+ if type(f) ~= "function" then
+ if verbosity >= 3 then
+ print("INFO: ", "Skipping "..unitname.."."..name.." because it is not a function");
+ end
+ elseif type(tests[name]) ~= "function" then
+ if verbosity >= 1 then
+ print("WARNING: ", unitname.."."..name.." has no test!");
+ end
+ else
+ local success, ret = pcall(tests[name], f, unit);
+ if not success then
+ print("TEST FAILED: ", unitname, name, ret);
+ elseif verbosity >= 2 then
+ print("TEST SUCCEEDED: ", unitname, name);
+ end
+ end
+ end
+end
+
+dotest "util.jid"
+
diff --git a/tests/test_util_jid.lua b/tests/test_util_jid.lua
new file mode 100644
index 00000000..3be0bfa1
--- /dev/null
+++ b/tests/test_util_jid.lua
@@ -0,0 +1,13 @@
+
+function split(split)
+ function test(jid, node, server, resource)
+ local rnode, rserver, rresource = split(jid);
+ assert_equal(node, rnode, "split("..jid..") failed");
+ assert_equal(server, rserver, "split("..jid..") failed");
+ assert_equal(resource, rresource, "split("..jid..") failed");
+ end
+ test("node@server", "node", "server", nil );
+ test("node@server/resource", "node", "server", "resource" );
+ test("server", nil, "server", nil );
+ test("server/resource", nil, "server", "resource" );
+end