Module:Hatnote/testcases

local mHatnote = require('Module:Hatnote') -- the module to be tested
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit.new()

--------------------------------------------------------------------------------
-- Sandbox run
--------------------------------------------------------------------------------

function suite.runSandbox(...)
	mHatnote = require('Module:Hatnote/sandbox')
	return suite.run(...)
end

--------------------------------------------------------------------------------
-- 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), nil, 1)
end

function suite:assertParentFrameCallContains(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:assertStringContains(expected, func(child), false, nil, 1)
end

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

function suite:testFindNamespaceIdInputErrors()
    self:assertThrows(mHatnote.findNamespaceId, "bad argument #1 to 'findNamespaceId' (string expected, got number)", nil, 9)
    self:assertThrows(mHatnote.findNamespaceId, "bad argument #1 to 'findNamespaceId' (string expected, got nil)", nil)
    self:assertThrows(mHatnote.findNamespaceId, "bad argument #2 to 'findNamespaceId' (boolean expected, got number)", nil, 'A page', 9)
    self:assertThrows(mHatnote.findNamespaceId, "bad argument #2 to 'findNamespaceId' (boolean expected, got string)", nil, 'A page', "ds")
end

function suite:testFindNamespaceIdNamespaces()
	self:assertDoesNotThrow(mHatnote.findNamespaceId, nil, 'Foo')
	self:assertDoesNotThrow(mHatnote.findNamespaceId, nil, 'User:Example')
	self:assertDoesNotThrow(mHatnote.findNamespaceId, nil, 'Category:Example')
    self:assertEquals(0, mHatnote.findNamespaceId('Foo'))
	self:assertEquals(2, mHatnote.findNamespaceId('User:Example'))
	self:assertEquals(14, mHatnote.findNamespaceId('Category:Example'))
end

function suite:testFindNamespaceIdColonRemoval()
	self:assertEquals(14, mHatnote.findNamespaceId(':Category:Example'))
end

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

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

function suite:testMakeWikitextError()
	self:assertEquals(
		'<strong class="error">Error: Foo.</strong>[[Category:Hatnote templates with errors]]',
		mHatnote.makeWikitextError('Foo', nil, nil, mw.title.new('Example'))
	)
end

function suite:testMakeWikitextErrorHelpLink()
	self:assertEquals(
		'<strong class="error">Error: Foo ([[Bar|help]]).</strong>[[Category:Hatnote templates with errors]]',
		mHatnote.makeWikitextError('Foo', 'Bar', nil, mw.title.new('Example'))
	)
end

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

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

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

function suite:testHatnoteInputErrors()
	self:assertThrows(mHatnote._hatnote, "bad argument #1 to '_hatnote' (string expected, got number)", nil, 9)
	self:assertThrows(mHatnote._hatnote, "bad argument #1 to '_hatnote' (string expected, got table)", nil, {})
	self:assertThrows(mHatnote._hatnote, "bad argument #1 to '_hatnote' (string expected, got nil)", nil)
	self:assertThrows(mHatnote._hatnote, "bad argument #2 to '_hatnote' (table expected, got number)", nil, 'A page', 9)
	self:assertThrows(mHatnote._hatnote, "bad argument #2 to '_hatnote' (table expected, got string)", nil, 'A page', "A string")
end

function suite:testHatnote()
	self:assertDoesNotThrow(mHatnote._hatnote, nil, 'Foo')
	self:assertStringContains(
		'<div role="note" class="hatnote navigation%-not%-searchable">Foo</div>',
		mHatnote._hatnote('Foo')
	)
end

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

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

function suite:testHatnoteEntryPoint()
	self:assertParentFrameCallContains(
		'<div role="note" class="hatnote navigation%-not%-searchable">Foo</div>',
		mHatnote.hatnote,
		{'Foo'}
	)
end

function suite:testHatnoteEntryPointSelfref()
	self:assertParentFrameCallContains(
		'<div role="note" class="hatnote navigation%-not%-searchable selfref">Foo</div>',
		mHatnote.hatnote,
		{'Foo', selfref = 'yes'}
	)
end

function suite:testHatnoteEntryPointExtraClasses()
	self:assertParentFrameCallContains(
		'<div role="note" class="hatnote navigation%-not%-searchable extraclass">Foo</div>',
		mHatnote.hatnote,
		{'Foo', extraclasses = 'extraclass'}
	)
end

return suite

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.