Module:Redirect/testcases

From Stiles Wiki
< Module:Redirect
Revision as of 19:49, 25 September 2020 by imported>Jackmcbarn (actually test the right thing)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Redirect/testcases/doc

-- Unit tests for [[Module:Redirect]]. Click talk page to run tests.

local mRedirect = require('Module:Redirect') -- the module to be tested
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()

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

function suite:makeFrameWithArgs(args)
	local frame = mw.getCurrentFrame()
	return frame:newChild{title = 'Frame', args = args}
end

--------------------------------------------------------------------------------
-- Mock mw.title.new
--------------------------------------------------------------------------------

do
	local oldNewTitle = mw.title.new

	function suite:mockNewTitle(options)
		mw.title.new = function (page)
			local titleObj = oldNewTitle(page)
			if options[page] then
				if options[page].content then
					local content = options[page].content
					rawset(titleObj, 'getContent', function ()
						return content
					end)
					options[page].content = nil
				end
				for k, v in pairs(options[page]) do
					rawset(titleObj, k, v)
				end
			end
			return titleObj
		end
	end
	
	function suite:resetTitleMock()
		mw.title.new = oldNewTitle
	end
end

-- Shortcut method for typical pages used in tests
function suite:mockAFCShortcut()
	local content = [=[
#REDIRECT [[Stiles.casa Wiki:Articles for creation]]

{{R to Stiles.casa Wiki namespace}}
{{R from shortcut}}
]=]
	local pageOptions = {
		content = content,
		isRedirect = true,
	}
	self:mockNewTitle{
		['SW:AFC'] = pageOptions,
		['Stiles.casa Wiki:AFC'] = pageOptions,
	}
end

function suite:mockMothersDayPage()
	self:mockNewTitle{["Mother's Day"] = {
		content = 'Lorem ipsum',
		isRedirect = false,
	}}
end

--------------------------------------------------------------------------------
-- Test Main
--------------------------------------------------------------------------------

function suite:testMain()
	self:mockAFCShortcut()
	self:assertEquals(
		'Stiles.casa Wiki:Articles for creation',
		mRedirect.main(self:makeFrameWithArgs{'SW:AFC'})
	)
	self:resetTitleMock()
end

function suite:testMainWithPadding()
	self:mockAFCShortcut()
	self:assertEquals(
		'Stiles.casa Wiki:Articles for creation',
		mRedirect.main(self:makeFrameWithArgs{'  SW:AFC  '})
	)
	self:resetTitleMock()
end

function suite:testMainWithBlankArgument()
	self:mockAFCShortcut()
	self:assertEquals(
		'',
		mRedirect.main(self:makeFrameWithArgs{'  '})
	)
	self:resetTitleMock()
end

function suite:testMainWithInputBrackets()
	self:mockAFCShortcut()
	self:assertEquals(
		'Stiles.casa Wiki:Articles for creation',
		mRedirect.main(self:makeFrameWithArgs{'[[SW:AFC]]'})
	)
	self:resetTitleMock()
end

function suite:testMainWithOutputBrackets()
	self:mockAFCShortcut()
	self:assertEquals(
		'[[Stiles.casa Wiki:Articles for creation]]',
		mRedirect.main(self:makeFrameWithArgs{'SW:AFC', bracket = 'yes'})
	)
	self:resetTitleMock()
end

function suite:testMainWithInputAndOutputBrackets()
	self:mockAFCShortcut()
	self:assertEquals(
		'[[Stiles.casa Wiki:Articles for creation]]',
		mRedirect.main(self:makeFrameWithArgs{'[[SW:AFC]]', bracket = 'yes'})
	)
	self:resetTitleMock()
end

function suite:testMainWithInputBracketsAndPadding()
	self:mockAFCShortcut()
	self:assertEquals(
		'Stiles.casa Wiki:Articles for creation',
		mRedirect.main(self:makeFrameWithArgs{'  [[SW:AFC]]  '})
	)
	self:resetTitleMock()
end

function suite:testMainWithRealInvalidRedirect()
	self:assertEquals(
		'User:John Vandenberg/testcases/Redirect/1',
		mRedirect.main(self:makeFrameWithArgs{'User:John Vandenberg/testcases/Redirect/1'})
	)
end

function suite:testMainWithRealValidRedirect()
	self:assertEquals(
		'Blah',
		mRedirect.main(self:makeFrameWithArgs{'User:John Vandenberg/testcases/Redirect/2'})
	)
end

--------------------------------------------------------------------------------
-- Test Canonicalisation
--------------------------------------------------------------------------------

function suite:testContentAmpersandCanonicalisation()
	self:mockNewTitle{['Module talk:Redirect/sandbox'] = {
		content = '#REDIRECT [[Module talk&#58;Redirect]]',
		isRedirect = true,
	}}
	self:assertEquals(
		'Module talk:Redirect',
		mRedirect.main(self:makeFrameWithArgs{'Module talk:Redirect/sandbox'})
	)
	self:resetTitleMock()
end

function suite:testContentPercentCanonicalisation()
	self:mockNewTitle{['Module talk:Redirect/sandbox'] = {
		content = '#REDIRECT [[Module talk%3ARedirect]]',
		isRedirect = true,
	}}
	self:assertEquals(
		'Module talk:Redirect',
		mRedirect.main(self:makeFrameWithArgs{'Module talk:Redirect/sandbox'})
	)
	self:resetTitleMock()
end

function suite:testContentAmpersandInsidePercentCanonicalisation()
	self:mockNewTitle{['Module talk:Redirect/sandbox'] = {
		content = '#REDIRECT [[Module talk%26#58;Redirect]]',
		isRedirect = true,
	}}
	self:assertEquals(
		'Module talk:Redirect',
		mRedirect.main(self:makeFrameWithArgs{'Module talk:Redirect/sandbox'})
	)
	self:resetTitleMock()
end

function suite:testContentPercentInsideAmpersandCanonicalisation()
	self:mockNewTitle{['Module talk:Redirect/sandbox'] = {
		content = '#REDIRECT [[Module talk&#37;3ARedirect]]',
		isRedirect = true, -- not actually considered a redirect, but we want to exercise the code paths anyway
	}}
	self:assertEquals(
		'Module talk:Redirect/sandbox',
		mRedirect.main(self:makeFrameWithArgs{'Module talk:Redirect/sandbox'})
	)
	self:resetTitleMock()
end

function suite:testTitleCanonicalisation()
	self:mockMothersDayPage()
	self:assertEquals(
		"Mother's Day",
		mRedirect.main(self:makeFrameWithArgs{"Mother&#39;s Day"})
	)
	self:resetTitleMock()
end

function suite:testTitleInitialCapitalCanonicalisation()
	self:mockMothersDayPage()
	self:assertEquals(
		"Mother's Day",
		mRedirect.main(self:makeFrameWithArgs{"mother's Day"})
	)
	self:resetTitleMock()
end

--------------------------------------------------------------------------------
-- Test isRedirect
--------------------------------------------------------------------------------

function suite:testIsRedirect()
	self:mockAFCShortcut()
	self:assertEquals(
		'yes',
		mRedirect.isRedirect(self:makeFrameWithArgs{'SW:AFC'})
	)
	self:resetTitleMock()
end

function suite:testIsRedirect()
	self:mockMothersDayPage()
	self:assertEquals(
		'',
		mRedirect.isRedirect(self:makeFrameWithArgs{"Mother's Day"})
	)
	self:resetTitleMock()
end

return suite