diff options
author | Shmuel Zeigerman <solomuz0@gmail.com> | 2011-10-05 15:08:13 +0200 |
---|---|---|
committer | Shmuel Zeigerman <solomuz0@gmail.com> | 2011-10-05 15:08:13 +0200 |
commit | bd8370a70eb3ed7e299416b160430df3de30f043 (patch) | |
tree | e8233ac3b165c86682d39adbc7be6fa9d568d8b3 /test | |
parent | d04e03283373e03902713da12042fde03da6ca3b (diff) | |
download | lrexlib-bd8370a70eb3ed7e299416b160430df3de30f043.tar.gz |
luatest.lua: 1) bugfix; 2) don't use `module' function;
Diffstat (limited to 'test')
-rw-r--r-- | test/luatest.lua | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/test/luatest.lua b/test/luatest.lua index ba5a494..cd191ed 100644 --- a/test/luatest.lua +++ b/test/luatest.lua @@ -1,9 +1,9 @@ -- See Copyright Notice in the file LICENSE -module (..., package.seeall) +local P = {} -- arrays: deep comparison -function eq (t1, t2, lut) +function P.eq (t1, t2, lut) if t1 == t2 then return true end if type(t1) ~= "table" or type(t2) ~= "table" or #t1 ~= #t2 then return false @@ -16,19 +16,20 @@ function eq (t1, t2, lut) lut[t1][t2], lut[t2][t1] = true, true for k,v in ipairs (t1) do - if not eq (t2[k], v, lut) then return false end -- recursion + if not P.eq (t2[k], v, lut) then return false end -- recursion end return true end -NT = {} -- a unique "nil table", to be used instead of nils in datasets +-- a "nil GUID", to be used instead of nils in datasets +P.NT = "b5f74fe5-46f4-483a-8321-e58ba2fa0e17" --- pack vararg in table, replacing nils with "NT" table +-- pack vararg in table, replacing nils with "NT" items local function packNT (...) local t = {} for i=1, select ("#", ...) do local v = select (i, ...) - t[i] = (v == nil) and NT or v + t[i] = (v == nil) and P.NT or v end return t end @@ -38,7 +39,7 @@ local function unpackNT (t) local len = #t local function unpack_from (i) local v = t[i] - if v == NT then v = nil end + if v == P.NT then v = nil end if i == len then return v end return v, unpack_from (i+1) end @@ -46,12 +47,12 @@ local function unpackNT (t) end -- print results (deep into arrays) -function print_results (val, indent, lut) +function P.print_results (val, indent, lut) indent = indent or "" lut = lut or {} -- look-up table local str = tostring (val) if type (val) == "table" then - if val == NT then + if val == P.NT then print (indent .. "nil") elseif lut[val] then print (indent .. str) @@ -59,7 +60,7 @@ function print_results (val, indent, lut) lut[val] = true print (indent .. str) for i,v in ipairs (val) do - print_results (v, " " .. indent, lut) -- recursion + P.print_results (v, " " .. indent, lut) -- recursion end end else @@ -70,7 +71,7 @@ end -- returns: -- 1) true, if success; false, if failure -- 2) test results table or error_message -function test_function (test, func) +function P.test_function (test, func) local res local t = packNT (pcall (func, unpackNT (test[1]))) if t[1] then @@ -80,7 +81,7 @@ function test_function (test, func) res = t[2] --> error_message end local how = (type (res) == type (test[2])) and - (type (res) == "string" or eq (res, test[2])) -- allow error messages to differ + (type (res) == "string" or P.eq (res, test[2])) -- allow error messages to differ return how, res end @@ -88,7 +89,7 @@ end -- 1) true, if success; false, if failure -- 2) test results table or error_message -- 3) test results table or error_message -function test_method (test, constructor, name) +function P.test_method (test, constructor, name) local res1, res2 local ok, r = pcall (constructor, unpackNT (test[1])) if ok then @@ -102,17 +103,17 @@ function test_method (test, constructor, name) else res1, res2 = 1, r --> 1, error_message end - return eq (res1, test[3]), res1, res2 + return P.eq (res1, test[3]), res1, res2 end -- returns: a list of failed tests -function test_set (set, lib) +function P.test_set (set, lib) local list = {} if type (set.Func) == "function" then local func = set.Func for i,test in ipairs (set) do - local ok, res = test_function (test, func) + local ok, res = P.test_function (test, func) if not ok then table.insert (list, {i=i, res}) end @@ -120,7 +121,7 @@ function test_set (set, lib) elseif type (set.Method) == "string" then for i,test in ipairs (set) do - local ok, res1, res2 = test_method (test, lib.new, set.Method) + local ok, res1, res2 = P.test_method (test, lib.new, set.Method) if not ok then table.insert (list, {i=i, res1, res2}) end @@ -133,3 +134,4 @@ function test_set (set, lib) return list end +return P |