diff options
Diffstat (limited to 'test/luatest.lua')
-rw-r--r-- | test/luatest.lua | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/test/luatest.lua b/test/luatest.lua index cd191ed..5ea7ba8 100644 --- a/test/luatest.lua +++ b/test/luatest.lua @@ -1,9 +1,7 @@ -- See Copyright Notice in the file LICENSE -local P = {} - -- arrays: deep comparison -function P.eq (t1, t2, lut) +local function 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,20 +14,20 @@ function P.eq (t1, t2, lut) lut[t1][t2], lut[t2][t1] = true, true for k,v in ipairs (t1) do - if not P.eq (t2[k], v, lut) then return false end -- recursion + if not eq (t2[k], v, lut) then return false end -- recursion end return true end -- a "nil GUID", to be used instead of nils in datasets -P.NT = "b5f74fe5-46f4-483a-8321-e58ba2fa0e17" +local NT = "b5f74fe5-46f4-483a-8321-e58ba2fa0e17" -- 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 P.NT or v + t[i] = (v == nil) and NT or v end return t end @@ -39,7 +37,7 @@ local function unpackNT (t) local len = #t local function unpack_from (i) local v = t[i] - if v == P.NT then v = nil end + if v == NT then v = nil end if i == len then return v end return v, unpack_from (i+1) end @@ -47,31 +45,29 @@ local function unpackNT (t) end -- print results (deep into arrays) -function P.print_results (val, indent, lut) +local function 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 == P.NT then - print (indent .. "nil") - elseif lut[val] then - print (indent .. str) + if lut[val] then + io.write (indent, str, "\n") else lut[val] = true - print (indent .. str) + io.write (indent, str, "\n") for i,v in ipairs (val) do - P.print_results (v, " " .. indent, lut) -- recursion + print_results (v, " " .. indent, lut) -- recursion end end else - print (indent .. str) + io.write (indent, val == NT and "nil" or str, "\n") end end -- returns: -- 1) true, if success; false, if failure -- 2) test results table or error_message -function P.test_function (test, func) +local function test_function (test, func) local res local t = packNT (pcall (func, unpackNT (test[1]))) if t[1] then @@ -81,7 +77,7 @@ function P.test_function (test, func) res = t[2] --> error_message end local how = (type (res) == type (test[2])) and - (type (res) == "string" or P.eq (res, test[2])) -- allow error messages to differ + (type (res) == "string" or eq (res, test[2])) -- allow error messages to differ return how, res end @@ -89,7 +85,7 @@ end -- 1) true, if success; false, if failure -- 2) test results table or error_message -- 3) test results table or error_message -function P.test_method (test, constructor, name) +local function test_method (test, constructor, name) local res1, res2 local ok, r = pcall (constructor, unpackNT (test[1])) if ok then @@ -103,17 +99,17 @@ function P.test_method (test, constructor, name) else res1, res2 = 1, r --> 1, error_message end - return P.eq (res1, test[3]), res1, res2 + return eq (res1, test[3]), res1, res2 end -- returns: a list of failed tests -function P.test_set (set, lib) +local function 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 = P.test_function (test, func) + local ok, res = test_function (test, func) if not ok then table.insert (list, {i=i, res}) end @@ -121,7 +117,7 @@ function P.test_set (set, lib) elseif type (set.Method) == "string" then for i,test in ipairs (set) do - local ok, res1, res2 = P.test_method (test, lib.new, set.Method) + local ok, res1, res2 = test_method (test, lib.new, set.Method) if not ok then table.insert (list, {i=i, res1, res2}) end @@ -134,4 +130,11 @@ function P.test_set (set, lib) return list end -return P +return { + eq = eq, + NT = NT, + print_results = print_results, + test_function = test_function, + test_method = test_method, + test_set = test_set, +} |