Jump to content

Module:Italic title/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 12:57, 29 May 2021 (create test cases page for Module:Italic title using Module:ScribuntoUnit and Module:Lua-mock). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
local mItalicTitle = require('Module:Italic title')
local ScribuntoUnit = require('Module:ScribuntoUnit')
local Spy = require('Module:Lua-mock/Spy')
local suite = ScribuntoUnit:new()


local function patchCurrentTitle(page, func)
	local oldGetCurrentTitle = mw.title.getCurrentTitle
	mw.title.getCurrentTitle = function ()
		return mw.title.new(page)
	end
	func()
	mw.title.getCurrentTitle = oldGetCurrentTitle
end

local function patchDisplayTitle(func)
	local oldGetCurrentFrame = mw.getCurrentFrame
	local frame = oldGetCurrentFrame()
	local oldCallParserFunction = frame.callParserFunction
	local callParserFunctionSpy = Spy(oldCallParserFunction)
	mw.getCurrentFrame = function ()
		return frame
	end
	frame.callParserFunction = callParserFunctionSpy
	func(callParserFunctionSpy, frame)
	frame.callParserFunction = oldCallParserFunction
	mw.getCurrentFrame = oldGetCurrentFrame
end

local displayTitleTestData = {
	{
		description = 'test _main: with single-word titles, the whole title is italicized',
		page = "Example",
		args = {},
		expectedDisplayTitleArgs = {"<i>Example</i>"},
	},
	{
		description = 'test _main: when the string argument is specified, only that string is italicized',
		page = "List of Ally McBeal episodes",
		args = {string = "Ally McBeal"},
		expectedDisplayTitleArgs = {"List of <i>Ally McBeal</i> episodes"},
	},
}

for _, testData in ipairs(displayTitleTestData) do
	suite[testData.description] = function (self)
		patchCurrentTitle(
			testData.page,
			function ()
				patchDisplayTitle(
					function (callParserFunctionSpy, frame)
						mItalicTitle._main(testData.args)
						callParserFunctionSpy:assertCallMatches{
							atIndex = 1,
							arguments = {
								frame,
								'DISPLAYTITLE',
								unpack(testData.expectedDisplayTitleArgs),
							}
						}
					end
				)
			end
		)
	end
end

return suite