Jump to content

Module:Italic title/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gonnym (talk | contribs) at 22:55, 10 June 2021 (added namespace prefix test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local mItalicTitle = require('Module:Italic title/sandbox')
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 = {
	{
		func = mItalicTitle._main,
		description = 'test _main: Italicize all - single-word titles',
		page = "Example",
		args = {},
		expectedDisplayTitleArgs = {"<i>Example</i>"},
	},
	{
		func = mItalicTitle._main,
		description = 'test _main: Italicize all - multi-word titles',
		page = "Star Wars",
		args = {},
		expectedDisplayTitleArgs = {"<i>Star Wars</i>"},
	},
	{
		func = mItalicTitle._main,
		description = 'test _main: Only specified string argument italicized (string=)',
		page = "List of Ally McBeal episodes",
		args = {string = "Ally McBeal"},
		expectedDisplayTitleArgs = {"List of <i>Ally McBeal</i> episodes"},
	},
	{
		func = mItalicTitle._main,
		description = 'test _main: Only specified string argument italicized - in disambiguation (string= and all=)',
		page = "Peter (Fringe episode)",
		args = {string = "Fringe", all = "yes"},
		expectedDisplayTitleArgs = {"Peter (<i>Fringe</i> episode)"},
	},
	{
		func = mItalicTitle._main,
		description = 'test _main: Title and dismabiguation italicized (all=)',
		page = "Title (dismabiguation)",
		args = {all = "yes"},
		expectedDisplayTitleArgs = {"<i>Title (dismabiguation)</i>"},
	},
	{
		func = mItalicTitle._dabonly,
		description = 'test _dabonly: Italicize disambiguation',
		page = "The End (Lost)",
		args = {},
		expectedDisplayTitleArgs = {"The End (<i>Lost</i>)"},
	},
	{
		func = mItalicTitle._dabonly,
		description = 'test _dabonly: Italicize disambiguation with parentheses',
		page = "The Ghost Talks (Randall and Hopkirk (Deceased))",
		args = {},
		expectedDisplayTitleArgs = {"The Ghost Talks (<i>Randall and Hopkirk (Deceased)</i>)"},
	},
	{
		func = mItalicTitle._dabonly,
		description = 'test _dabonly: Italicize disambiguation without parentheses before',
		page = "Title (not disambiguation) (disambiguation)",
		args = {},
		expectedDisplayTitleArgs = {"Title (not disambiguation) (<i>disambiguation</i>)"},
	},
	{
		func = mItalicTitle._dabonly,
		description = 'test _dabonly: Only specified string argument italicized - in disambiguation (string=)',
		page = "Hell on Wheels (Hell on Wheels episode)",
		args = {string = "Hell on Wheels"},
		expectedDisplayTitleArgs = {"Hell on Wheels (<i>Hell on Wheels</i> episode)"},
	},
}

for _, testData in ipairs(displayTitleTestData) do
	-- Comment out when testing non-namespace prefixed titles.
	local namespace = "Wikipedia:"
	testData.page = namespace .. testData.page
	testData.expectedDisplayTitleArgs[1] = namespace .. testData.expectedDisplayTitleArgs[1]

	suite[testData.description] = function (self)
		patchCurrentTitle(
			testData.page,
			function ()
				patchDisplayTitle(
					function (callParserFunctionSpy, frame)
						testData.func(testData.args)
						callParserFunctionSpy:assertCallMatches{
							atIndex = 1,
							arguments = {
								frame,
								'DISPLAYTITLE',
								unpack(testData.expectedDisplayTitleArgs),
							}
						}
					end
				)
			end
		)
	end
end

suite["test _main: when the string argument does not match the title, a tracking category is added"] = function (self)
	patchCurrentTitle(
		"Example",
		function ()
			self:assertStringContains(
				mItalicTitle._main{string = "foo"},
				"[[Category:Pages using italic title with no matching string]]",
				true
			)
		end
	)
end	

return suite