Module:Tmpl/testcases

From mediawiki.org
Module documentation

Yes All tests passed.

Name Expected Actual
Yes testRenderTmpl
Yes test_noExplosiveExpansion

local p = require('Module:ScribuntoUnit'):new()
local m = require('Module:Tmpl')

local function callWithArgs(arg0, args)
	local templateArgs = {}
	if args then
		for k, v in pairs(args) do
			local n = tonumber(k) or 0
			if (n >= 1) then
				templateArgs[n] = v
			end
		end
		templateArgs[0] = arg0
	end
	local frame = (mw.getCurrentFrame()
		:newChild{ args = templateArgs }
		:newChild{ args = {} })
	return m.renderTmpl(frame)
end

function p:testRenderTmpl()
	self:assertEquals('test a b c', callWithArgs('test $1 $2 $3', { 'a', 'b', 'c' }))
	self:assertEquals('test a $3', callWithArgs('test $1 $3', { 'a', 'b' }))
	self:assertEquals('test $1 b', callWithArgs('test $1 $2', { [2] = 'b' }))
	self:assertEquals('test $1 $2', callWithArgs('test $1 $2', {}))
end

function p:test_noExplosiveExpansion()
	local args = {}
	for i = 1, 9 do
		args[i] = ('$%s $%s'):format(i + 1, i + 1)
	end
	self:assertEquals(
		'$2 $2 $2 $2',
		mw.text.trim(mw.text.decode(callWithArgs('$1 $1', args))),
		'"$1" → "$2 $2"'
	)
end

return p