Module:Tmpl/testcases

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

✓[OK] All tests passed.

Name Expected Actual
✓[OK] testRenderTmpl

Code

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

return p