Jump to content

Module:Roman/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Andy M. Wang (talk | contribs) at 07:34, 23 April 2016 (I get it). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- Unit tests for [[Module:Roman/sandbox]]. Click talk page to run tests.

local moduleName = 'Roman/sandbox' -- assigning this to a variable as it is later 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(expected, invokeResult)
end

function suite:assertInvokeEqual(funcName, testTable, convertNumber)
	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

function suite:test_preliminary()
	local args = {
		{'N', {'0'}},
		{'N', {'0', 'toobig'}},
	}
	self:assertInvokeEqualMany('main', args)
end

return suite