Jump to content

Module:Redirect hatnote/testcases

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 02:55, 15 May 2014 (update one test to reflect how the template currently works). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local mRedirectHatnote = require('Module:Redirect hatnote')
local mHatnote = require('Module:Hatnote')
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()

--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------

function suite.runRedirect(args)
	-- Runs the "redirect" function with an args table passed from a
	-- parent frame.
	local frame = mw.getCurrentFrame()
	local parent = frame:newChild{args = args}
	local child = parent:newChild{}
	return mRedirectHatnote.redirect(child)
end

--------------------------------------------------------------------------------
-- Test #invoke function basic
--------------------------------------------------------------------------------

function suite:testBlank()
	local result = suite.runRedirect{}
	self:assertStringContains('no redirect specified', result, true)
	self:assertStringContains('Template:Redirect#Errors', result, true)
end

function suite:testRedirectOnly()
	local result = suite.runRedirect{'REDIRECT'}
	self:assertStringContains('"REDIRECT" redirects here. For other uses, see [[REDIRECT (disambiguation)]].', result, true)
end

function suite:testUse1()
	local result = suite.runRedirect{'REDIRECT', 'USE1'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[REDIRECT (disambiguation)]].', result, true)
end

function suite:testPage1()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]].', result, true)
end

function suite:testUse2()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[USE2 (disambiguation)]].', result, true)
end

function suite:testPage2()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]].', result, true)
end

function suite:testUse3()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]]. For USE3, see [[USE3 (disambiguation)]].', result, true)
end

function suite:testPage3()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]]. For USE3, see [[PAGE3]].', result, true)
end

function suite:testUse4()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3', 'USE4'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]]. For USE3, see [[PAGE3]]. For USE4, see [[USE4 (disambiguation)]].', result, true)
end

function suite:testPage4()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3', 'USE4', 'PAGE4'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]]. For USE3, see [[PAGE3]]. For USE4, see [[PAGE4]].', result, true)
end

function suite:testPipeInput()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1 (disambiguation)|PAGE1', 'USE2', 'PAGE2 (disambiguation)|PAGE2', 'USE3', 'PAGE3 (disambiguation)|PAGE3', 'USE4', 'PAGE4 (disambiguation)|PAGE4'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1 (disambiguation)|PAGE1]]. For USE2, see [[PAGE2 (disambiguation)|PAGE2]]. For USE3, see [[PAGE3 (disambiguation)|PAGE3]]. For USE4, see [[PAGE4 (disambiguation)|PAGE4]].', result, true)
end

--------------------------------------------------------------------------------
-- Test "and"
--------------------------------------------------------------------------------

function suite:testPage1AndPage2()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]] and [[PAGE2]].', result, true)
end

function suite:testPage2AndPage3()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'and', 'PAGE3'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]] and [[PAGE3]].', result, true)
end

function suite:testPage1AndPage2Page3AndPage4()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'USE2', 'PAGE3', 'and', 'PAGE4'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]] and [[PAGE2]]. For USE2, see [[PAGE3]] and [[PAGE4]].', result, true)
end

function suite:testTwoAnds()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'and', 'PAGE3'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]], [[PAGE2]] and [[PAGE3]].', result, true)
end

function suite:testThreeAnds()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'and', 'PAGE3', 'and', 'PAGE4'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]], [[PAGE2]], [[PAGE3]] and [[PAGE4]].', result, true)
end

function suite:testTwoAndsUse2()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'and', 'PAGE3', 'USE2', 'PAGE4'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]], [[PAGE2]] and [[PAGE3]]. For USE2, see [[PAGE4]].', result, true)
end

--------------------------------------------------------------------------------
-- Test skipping parameters
--------------------------------------------------------------------------------

function suite:testUse1Skipped()
	local result = suite.runRedirect{'REDIRECT', '', 'PAGE1'}
	self:assertStringContains('"REDIRECT" redirects here. For other uses, see [[PAGE1]].', result, true)
end

function suite:testPage1Skipped()
	local result = suite.runRedirect{'REDIRECT', 'USE1', '', 'USE2'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[REDIRECT (disambiguation)]]. For USE2, see [[USE2 (disambiguation)]].', result, true)
end

function suite:testUse2Skipped()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', '', 'PAGE2'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]].', result, true)
end

function suite:testPage2Skipped()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', '', 'USE3'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[USE2 (disambiguation)]]. For USE3, see [[USE3 (disambiguation)]].', result, true)
end

function suite:testUse3Skipped()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', '', 'PAGE3'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]].', result, true)
end

function suite:testMiddleUseSkipped()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', '', 'PAGE2', 'USE3', 'PAGE3'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]].', result, true)
end

function suite:testMiddlePageSkipped()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', '', 'USE3', 'PAGE3'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[USE2 (disambiguation)]]. For USE3, see [[PAGE3]].', result, true)
end

--------------------------------------------------------------------------------
-- Test other uses
--------------------------------------------------------------------------------

function suite:testUse2Skipped()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', '', 'PAGE2'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]].', result, true)
end

function suite:testUse2Is1()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', '1', 'PAGE2'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For other uses, see [[PAGE2]].', result, true)
end

function suite:testOtherUsesIsNotFinal()
	local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', '1', 'PAGE2', 'USE3', 'PAGE3'}
	self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For other uses, see [[PAGE2]]. For USE3, see [[PAGE3]].', result, true)
end

return suite