Module:User:Mr. Stradivarius/gambiarra explained

--

local function TERMINAL_HANDLER(e, test, msg) if e

'pass' then mw.log("�[32m✔�[0m "..test..': '..msg) elseif e == 'fail' then mw.log("�[31m✘�[0m "..test..': '..msg) elseif e == 'except' then mw.log("�[31m✘�[0m "..test..': '..msg) end end local function deepeq(a, b) -- Different types: false if type(a) ~= type(b) then return false end -- Functions if type(a) == 'function' then return string.dump(a) == string.dump(b) end -- Primitives and equal pointers if a == b then return true end -- Only equal tables could have passed previous tests if type(a) ~= 'table' then return false end -- Compare tables field by field for k,v in pairs(a) do if b[k]

nil or not deepeq(v, b[k]) then return false end end for k,v in pairs(b) do if a[k]

nil or not deepeq(v, a[k]) then return false end end return trueend

-- Compatibility for Lua 5.1 and Lua 5.2local function args(...) return end

local function spy(f) local s = setmetatable(s,) return send

return function(handler, env)

local pendingtests =

local function runpending if pendingtests[1] ~= nil then pendingtests[1](runpending) end end

local function test(name, f, async) local testfn = function(next)

local prev =

local restore = function env.ok = prev.ok env.spy = prev.spy env.eq = prev.eq env.gambiarrahandler('end', name) table.remove(pendingtests, 1) if next then next end end

local handler = env.gambiarrahandler

env.eq = deepeq env.spy = spy env.ok = function(cond, msg) if cond then handler('pass', name, msg) else handler('fail', name, msg) end end

handler('begin', name); local ok, err = pcall(f, restore) if not ok then handler('except', name, err) end

if not async then handler('end', name); env.ok = prev.ok; env.spy = prev.spy; env.eq = prev.eq; end end

if not async then testfn else table.insert(pendingtests, testfn) if #pendingtests

1 then runpending end end end

env = env or _G env.gambiarrahandler = handler or TERMINAL_HANDLER

env.test = testend