Modulo:Hatnote/pangsubok

Manipud iti Wikipedia, ti nawaya nga ensiklopedia
Darsen a mapan iti pagdaliasatan Darsen a mapan agbiruk
local mHatnote = require('Modulo:Hatnote') -- the module to be tested
local ScribuntoUnit = require('Modulo:ScribuntoUnit')
local suite = ScribuntoUnit:new()

function suite:assertError(func, ...)
	local success, result = pcall(func, ...)
	self:assertFalse(success)
end

function suite:assertNotEquals(expected, actual)
	self:assertTrue(expected ~= actual)
end

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

-------------------------------------------------------------------------------
-- findNamespaceId tests
-------------------------------------------------------------------------------

function suite:testFindNamespaceIdInputErrors()
    self:assertError(mHatnote.findNamespaceId, 9)
    self:assertError(mHatnote.findNamespaceId)
    self:assertError(mHatnote.findNamespaceId, 'Maysa a panid', 9)
end

function suite:testFindNamespaceIdNamespaces()
	self:assertEquals(0, mHatnote.findNamespaceId('Foo'))
	self:assertEquals(2, mHatnote.findNamespaceId('Agar-aramat:Kaspagarigan'))
	self:assertEquals(14, mHatnote.findNamespaceId('Kategoria:Kaspagarigan'))
end

function suite:testFindNamespaceIdColonRemoval()
	self:assertEquals(14, mHatnote.findNamespaceId(':Kategoria:Kaspagarigan'))
end

function suite:testFindNamespaceIdSkipColonRemoval()
	self:assertNotEquals(14, mHatnote.findNamespaceId(':Kategoria:Kaspagarigan', false))
end

-------------------------------------------------------------------------------
-- formatPages tests
-------------------------------------------------------------------------------

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

-------------------------------------------------------------------------------
-- formatPageTables tests
-------------------------------------------------------------------------------

function suite:testFormatPageTables()
	self:assertDeepEquals(
		{'[[:Foo|foo]]', '[[:Bar|bar]]'},
		mHatnote.formatPageTables({'Foo', 'foo'}, {'Bar', 'bar'})
	)
end

-------------------------------------------------------------------------------
-- makeWikitextError tests
-------------------------------------------------------------------------------

function suite:testMakeWikitextError()
	self:assertEquals(
		'<strong class="error">Biddut: Foo.</strong>[[Kategoria:Dagiti plantilia ti hatnote nga addaan kadagiti biddut]]',
		mHatnote.makeWikitextError('Foo', nil, nil, mw.title.new('Kaspagarigan'))
	)
end

function suite:testMakeWikitextErrorHelpLink()
	self:assertEquals(
		'<strong class="error">Biddut: Foo ([[Bar|tulong]]).</strong>[[Kategoria:Dagiti plantilia ti hatnote nga addaan kadagiti biddut]]',
		mHatnote.makeWikitextError('Foo', 'Bar', nil, mw.title.new('Kaspagarigan'))
	)
end

function suite:testMakeWikitextErrorManualCategorySuppression()
	self:assertEquals(
		'<strong class="error">Biddut: Foo.</strong>',
		mHatnote.makeWikitextError('Foo', nil, false, mw.title.new('Kaspagarigan'))
	)
end

function suite:testMakeWikitextErrorTalkPageCategorySuppression()
	self:assertEquals(
		'<strong class="error">Biddut: Foo.</strong>',
		mHatnote.makeWikitextError('Foo', nil, nil, mw.title.new('Tungtungan:Kaspagarigan'))
	)
end

-------------------------------------------------------------------------------
-- formatLink tests
-------------------------------------------------------------------------------

function suite:testFormatLink()
	self:assertEquals('[[:Foo]]', mHatnote._formatLink('Foo'))
end

function suite:testFormatLinkColonHandling()
	self:assertEquals('[[:Kategoria:Foo]]', mHatnote._formatLink(':Kategoria:Foo'))
end

function suite:testFormatLinkSectionLinking()
	self:assertEquals('[[:Foo#Bar|Foo §&nbsp;Bar]]', mHatnote._formatLink('Foo#Bar'))
end

function suite:testFormatLinkPipeHandling()
	self:assertEquals('[[:Foo|Bar]]', mHatnote._formatLink('Foo|Bar'))
end

function suite:testFormatLinkDisplay()
	self:assertEquals('[[:Foo|Bar]]', mHatnote._formatLink('Foo', 'Bar'))
end

function suite:testFormatLinkEntryPoint()
	self:assertParentFrameCallEquals('[[:Foo]]', mHatnote.formatLink, {'Foo'})
	self:assertParentFrameCallEquals('[[:Foo|Bar]]', mHatnote.formatLink, {'Foo', 'Bar'})
end

-------------------------------------------------------------------------------
-- hatnote tests
-------------------------------------------------------------------------------

function suite:testHatnoteInputErrors()
    self:assertError(mHatnote._hatnote, 9)
    self:assertError(mHatnote._hatnote)
    self:assertError(mHatnote._hatnote, 'Maysa a panid', 9)
end

function suite:testHatnote()
	self:assertEquals('<div role="note" class="hatnote">Foo</div>', mHatnote._hatnote('Foo'))
end

function suite:testHatnoteSelfref()
	self:assertEquals(
		'<div role="note" class="hatnote selfref">Foo</div>',
		mHatnote._hatnote('Foo', {selfref = true})
	)
end

function suite:testHatnoteExtraClasses()
	self:assertEquals(
		'<div role="note" class="hatnote extraclass">Foo</div>',
		mHatnote._hatnote('Foo', {extraclasses = 'extraclass'})
	)
end

function suite:testHatnoteEntryPoint()
	self:assertParentFrameCallEquals(
		'<div role="note" class="hatnote">Foo</div>',
		mHatnote.hatnote,
		{'Foo'}
	)
end

function suite:testHatnoteEntryPointSelfref()
	self:assertParentFrameCallEquals(
		'<div role="note" class="hatnote selfref">Foo</div>',
		mHatnote.hatnote,
		{'Foo', selfref = 'yes'}
	)
end

function suite:testHatnoteEntryPointExtraClasses()
	self:assertParentFrameCallEquals(
		'<div role="note" class="hatnote extraclass">Foo</div>',
		mHatnote.hatnote,
		{'Foo', extraclasses = 'extraclass'}
	)
end

return suite