Module:Format link/testcases

-- Unit tests for Module:Format link local mFormatLink = require('Module:Format link/sandbox') local scribuntoUnit = require('Module:ScribuntoUnit') local suite = scribuntoUnit:new

-- Testing helper functions

function suite:assertParentFrameCallEquals(expected, func, args) args = args or {} local current = mw.getCurrentFrame local parent = current:newChild{title = 'Parent', args = args} local child = parent:newChild{title = 'Child'} self:assertEquals(expected, func(child)) end

--- -- formatLink tests ---

function suite:testFormatLink self:assertEquals('Foo', mFormatLink._formatLink{link = 'Foo'}) end

function suite:testFormatLinkColonHandling self:assertEquals(		'Category:Foo',		mFormatLink._formatLink{link = ':Category:Foo'}	) end

function suite:testFormatLinkSectionLinking self:assertEquals(		'Foo §&#x26;nbsp;Bar',		mFormatLink._formatLink{link = 'Foo#Bar'}	) end

function suite:testFormatLinkPipeHandling self:assertEquals(		'Bar',		mFormatLink._formatLink{link = 'Foo|Bar'}	) end

function suite:testFormatLinkDisplay self:assertEquals(		'Bar',		mFormatLink._formatLink{link = 'Foo', display = 'Bar'}	) end

function suite:testFormatLinkDisplayOverwritesManualPiping self:assertEquals(		'Baz',		mFormatLink._formatLink{link = 'Foo|Bar', display = 'Baz'}	) end

function suite:testFormatLinkPageItalicization self:assertEquals(		"&#x3c;i&#x3e;Foo&#x3c;/i&#x3e;",		mFormatLink._formatLink{link = 'Foo', italicizePage = true}	) end

function suite:testFormatLinkPageItalicizationWithSection self:assertEquals(		"&#x3c;i&#x3e;Foo&#x3c;/i&#x3e; §&#x26;nbsp;Bar",		mFormatLink._formatLink{link = 'Foo#Bar', italicizePage = true}	) end

function suite:testFormatLinkSectionItalicization self:assertEquals(		"Foo §&#x26;nbsp;&#x3c;i&#x3e;Bar&#x3c;/i&#x3e;",		mFormatLink._formatLink{link = 'Foo#Bar', italicizeSection = true}	) end

function suite:testFormatLinkPageItalicizationIsOverwrittenByDisplay self:assertEquals(		"Baz",		mFormatLink._formatLink{			link = 'Foo#Bar',			display = 'Baz',			italicizePage = true,		}	) end

function suite:testFormatLinkSectionItalicizationIsOverwrittenByDisplay self:assertEquals(		"Baz",		mFormatLink._formatLink{			link = 'Foo#Bar',			display = 'Baz',			italicizeSection = true,		}	) end

function suite:testFormatLinkItalicizationIsOverwrittenByManualPiping self:assertEquals(		"Baz",		mFormatLink._formatLink{			link = 'Foo#Bar|Baz',			italicizePage = true,			italicizeSection = true,		}	) end

function suite:testFormatLinkWithSectionOnlyLink self:assertEquals(		"§&#x26;nbsp;Section",		mFormatLink._formatLink{			link = '#Section',		}	) end

function suite:testFormatLinkWithSectionOnlyLinkAndItalicizedSection self:assertEquals(		"§&#x26;nbsp;&#x3c;i&#x3e;Section&#x3c;/i&#x3e;",		mFormatLink._formatLink{			link = '#Section',			italicizeSection = true,		}	) end

function suite:testFormatLinkWithSectionOnlyLinkAndItalicizedPage self:assertEquals(		"§&#x26;nbsp;Section",		mFormatLink._formatLink{			link = '#Section',			italicizePage=true,		}	) end

function suite:testFormatLinkEntryPoint self:assertParentFrameCallEquals('Foo', mFormatLink.formatLink, {'Foo'}) self:assertParentFrameCallEquals(		'Bar',		mFormatLink.formatLink, {'Foo', 'Bar'}	) self:assertParentFrameCallEquals(		"&#x3c;i&#x3e;Foo&#x3c;/i&#x3e; §&#x26;nbsp;&#x3c;i&#x3e;Bar&#x3c;/i&#x3e;",		mFormatLink.formatLink,		{'Foo#Bar', italicizepage="yes", italicizesection="yes"}	) self:assertParentFrameCallEquals(		"Foo §&#x26;nbsp;Bar",		mFormatLink.formatLink,		{'Foo#Bar', italicizepage="no", italicizesection="no"}	) end

function suite:testFormatLinkNonexistentPageCategorization self:assertEquals(		'Nonexistent page',		mFormatLink._formatLink{			link = 'Nonexistent page', --*should* be nonexistent; is salted			categorizeMissing = 'Test'		}	) end

function suite:testFormatLinkTarget self:assertEquals(		'Foo §&#x26;nbsp;Bar',		mFormatLink._formatLink{			link = "Foo#Bar",			target = 'Baz'		}	) end

function suite:testFormatLinkTargetPiping self:assertEquals(		'Boop',		mFormatLink._formatLink{			link = "Foo#Bar|Boop",			target = 'Baz'		}	) end

--- -- formatPages tests ---

function suite:testFormatPages self:assertDeepEquals(		{'Foo', 'Bar'},		mFormatLink.formatPages({}, {'Foo', 'Bar'})	) end

return suite