From English Wikipedia @ Freddythechick
-- Unit tests for [[Module:Sandbox/Andy M. Wang/Sandbox]]. Click talk page to run tests.
local moduleName = 'Sandbox/Andy M. Wang/Sandbox' -- used to generate an #invoke statement
local mainFuncName = 'main'
local mm = require('Module:' .. moduleName)
local suite = require('Module:UnitTests')
function suite.buildInvocation(funcName, args)
args = args or {}
local argsClone = mw.clone(args)
-- Build a module invocation equivalent to the args table. Taken from [[Module:Unsubst]].
-- Numbered args first.
local ret = '{{#invoke:' .. moduleName .. '|' .. funcName
for k, v in ipairs(argsClone) do
v = tostring(v)
if string.find(v, '=', 1, true) then
-- likely something like 1=foo=bar, we need to do it as a named arg
break
end
ret = ret .. '|' .. v
argsClone[k] = nil
end
for k, v in pairs(argsClone) do
k = tostring(k)
v = tostring(v)
ret = ret .. '|' .. k .. '=' .. v
end
return ret .. '}}'
end
function suite:getInvokeResult(funcName, args, convertNumber) -- Unless convertNumber is false, the number is converted to a number, if possible, on re-entry to Lua.
args = args or {}
local invocation = self.buildInvocation(funcName, args)
local result = self.frame:preprocess(invocation)
if convertNumber ~= false and tonumber(result) then
return tonumber(result)
else
return result
end
end
function suite:assertInvokeEquals(expected, funcName, args, convertNumber)
args = args or {}
local invokeResult = self:getInvokeResult(funcName, args, convertNumber)
self:preprocess_equals(invokeResult, expected)
end
function suite:assertInvokeEqual(funcName, testTable, convertNumber)
testTable = testTable or {}
local expected = testTable[1]
local args = testTable[2] or {}
self:assertInvokeEquals(expected, funcName, args, convertNumber)
end
function suite:assertInvokeEqualMany(funcName, testTables, convertNumber)
for i, testTable in ipairs(testTables) do
self:assertInvokeEqual(funcName, testTable, convertNumber)
end
end
----------------------------------------
---- Tests ----
----------------------------------------
return suite