Module:Redirect/testcases explained

-- Unit tests for . Click talk page to run tests.

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

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

function suite:makeFrameWithArgs(args) local frame = mw.getCurrentFrame return frame:newChildend

---------------------------------------------------------------------------------- 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 endend

-- Shortcut method for typical pages used in testsfunction suite:mockAFCShortcut local content = [=[ #REDIRECT [[Wikipedia:Articles for creation]]

]=] local pageOptions = self:mockNewTitleend

function suite:mockMothersDayPage self:mockNewTitleend

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

function suite:testMain self:mockAFCShortcut self:assertEquals('Wikipedia:Articles for creation', mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testMainWithPadding self:mockAFCShortcut self:assertEquals('Wikipedia:Articles for creation', mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testMainWithBlankArgument self:mockAFCShortcut self:assertEquals(, mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testMainWithInputBrackets self:mockAFCShortcut self:assertEquals('Wikipedia:Articles for creation', mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testMainWithOutputBrackets self:mockAFCShortcut self:assertEquals('', mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testMainWithInputAndOutputBrackets self:mockAFCShortcut self:assertEquals('', mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testMainWithInputBracketsAndPadding self:mockAFCShortcut self:assertEquals('Wikipedia:Articles for creation', mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

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

function suite:testMainWithRealValidRedirect self:assertEquals('Blah', mRedirect.main(self:makeFrameWithArgs) )end

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

function suite:testContentAmpersandCanonicalisation self:mockNewTitle self:assertEquals('Module talk:Redirect', mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testContentPercentCanonicalisation self:mockNewTitle self:assertEquals('Module talk:Redirect', mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testContentAmpersandInsidePercentCanonicalisation self:mockNewTitle self:assertEquals('Module talk:Redirect', mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testContentPercentInsideAmpersandCanonicalisation self:mockNewTitle self:assertEquals('Module talk:Redirect/sandbox', mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testTitleCanonicalisation self:mockMothersDayPage self:assertEquals("Mother's Day", mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testTitleInitialCapitalCanonicalisation self:mockMothersDayPage self:assertEquals("Mother's Day", mRedirect.main(self:makeFrameWithArgs) ) self:resetTitleMockend

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

function suite:testIsRedirect self:mockAFCShortcut self:assertEquals('yes', mRedirect.isRedirect(self:makeFrameWithArgs) ) self:resetTitleMockend

function suite:testIsRedirect self:mockMothersDayPage self:assertEquals(, mRedirect.isRedirect(self:makeFrameWithArgs) ) self:resetTitleMockend

return suite