local mRedirectHatnote = require('Module:Redirect hatnote/sandbox')local _redirect = mRedirectHatnote._redirectlocal ScribuntoUnit = require('Module:ScribuntoUnit')local suite = ScribuntoUnit:new
---------------------------------------------------------------------------------- Helper functions--------------------------------------------------------------------------------
function suite.runRedirect(args, childArgs) -- Runs the "redirect" function with an args table passed from a -- parent frame. local frame = mw.getCurrentFrame local parent = frame:newChild local child = parent:newChild return mRedirectHatnote.redirect(child)end
---------------------------------------------------------------------------------- Test #invoke function basic--------------------------------------------------------------------------------
function suite:testBlank local result = suite.runRedirect self:assertStringContains('missing redirect parameter', result, true) self:assertStringContains('Template:Redirect#Errors', result, true)end
function suite:testRedirectOnly local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For other uses, see .', result, true)end
function suite:testUse1 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see .', result, true)end
function suite:testPage1 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see .', result, true)end
function suite:testUse2 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see .', result, true)end
function suite:testPage2 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see .', result, true)end
function suite:testUse3 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see . For USE3, see .', result, true)end
function suite:testPage3 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see . For USE3, see .', result, true)end
function suite:testUse4 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see . For USE3, see . For USE4, see .', result, true)end
function suite:testPage4 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see . For USE3, see . For USE4, see .', result, true)end
function suite:testPipeInput local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see PAGE1. For USE2, see PAGE2. For USE3, see PAGE3. For USE4, see PAGE4.', result, true)end
---------------------------------------------------------------------------------- Test "and"--------------------------------------------------------------------------------
function suite:testPage1AndPage2 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see and .', result, true)end
function suite:testPage2AndPage3 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see and .', result, true)end
function suite:testPage1AndPage2Page3AndPage4 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see and . For USE2, see and .', result, true)end
function suite:testTwoAnds local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see,, and .', result, true)end
function suite:testThreeAnds local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see,,, and .', result, true)end
function suite:testTwoAndsUse2 local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see,, and . For USE2, see .', result, true)end
---------------------------------------------------------------------------------- Test skipping parameters--------------------------------------------------------------------------------
function suite:testUse1Skipped local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For other uses, see .', result, true)end
function suite:testPage1Skipped local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see .', result, true)end
function suite:testUse2Skipped local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see .', result, true)end
function suite:testPage2Skipped local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see . For USE3, see .', result, true)end
function suite:testUse3Skipped local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see .', result, true)end
function suite:testMiddleUseSkipped local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see .', result, true)end
function suite:testMiddlePageSkipped local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see . For USE3, see .', result, true)end
---------------------------------------------------------------------------------- Test other uses--------------------------------------------------------------------------------
function suite:testUse2Skipped local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see .', result, true)end
function suite:testOtherUsesIsNotFinal local result = suite.runRedirect self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For other uses, see . For USE3, see .', result, true)end
---------------------------------------------------------------------------------- Test options--------------------------------------------------------------------------------
function suite:testSelfRef local result = suite.runRedirect self:assertStringContains('class=".-selfref.-"', result, false)end
---------------------------------------------------------------------------------- Test Lua interface--------------------------------------------------------------------------------
function suite:assertError(...) local success, result = pcall(_redirect, ...) self:assertFalse(success)end
function suite:testInvalidInput self:assertError self:assertError(9) self:assertError(true) self:assertError('REDIRECT', 'invalid') self:assertError('REDIRECT', 'invalid',) self:assertError('REDIRECT', nil, 'invalid') self:assertError('REDIRECT',, 'invalid')end
function suite:testLuaRedirectOnly local result = _redirect self:assertStringContains('"REDIRECT" redirects here. For other uses, see .', result, true)end
function suite:testLuaPage1 local result = _redirect(1 ) self:assertStringContains('"REDIRECT" redirects here. For USE1, see .', result, true)end
function suite:testLuaPage3 local result = _redirect(1 ) self:assertStringContains('"REDIRECT" redirects here. For USE1, see . For USE2, see . For USE3, see .', result, true)end
function suite:testLuaMultiplePages local result = _redirect(1 ) self:assertStringContains('"REDIRECT" redirects here. For USE1, see,, and . For USE2, see and .', result, true)end
function suite:testLuaSelfref local result = _redirect(1,) self:assertStringContains('class=".-selfref.-"', result, false)end
---------------------------------------------------------------------------------- Test tracking category--------------------------------------------------------------------------------
function suite.makeFakeTitleObject(page, vals) local title if page then title = mw.title.new(page) else title = mw.title.getCurrentTitle end for k, v in pairs(vals or) do rawset(title, k, v) end return titleend
local d =
-- Categoriesd.missingCat = 'Missing redirects'd.invalidCat = 'Articles with redirect hatnotes needing review'
function suite:assertContainsTrackingCategory(category, result) category = string.format('', category) self:assertStringContains(category, result, true)end
function suite:assertNotContainsTrackingCategory(result) self:assertNotStringContains('%[%[Category:[^%]]+%]%]', result)end
function suite:testTrackingCategoryNonMainspace local currentTitle = suite.makeFakeTitleObject('Wikipedia:Namespaces') suite:assertNotContainsTrackingCategory(_redirect(nil, nil, currentTitle))end
function suite:testTrackingCategoryExceptions local currentTitle = suite.makeFakeTitleObject('Example') for _, s in ipairs do self:assertNotContainsTrackingCategory(_redirect(nil, nil, currentTitle)) endend
function suite:testTrackingCategoryInvalidTitle local currentTitle = suite.makeFakeTitleObject('Example') self:assertContainsTrackingCategory(d.missingCat, _redirect(nil, nil, currentTitle))end
function suite:testTrackingCategoryNonexistent local currentTitle = suite.makeFakeTitleObject('Example') local redirectTitle = suite.makeFakeTitleObject('Example 2',) self:assertContainsTrackingCategory(d.missingCat, _redirect(
, nil, nil, currentTitle, redirectTitle))end
function suite:testTrackingCategoryNotRedirect local currentTitle = suite.makeFakeTitleObject('Example') local redirectTitle = suite.makeFakeTitleObject('Example 2',) self:assertContainsTrackingCategory(d.invalidCat, _redirect(
, nil, nil, currentTitle, redirectTitle))end
function suite:testTrackingCategoryRfDRedirect local currentTitle = suite.makeFakeTitleObject('Example') local redirectTitle = suite.makeFakeTitleObject('Example 2',) self:assertNotContainsTrackingCategory(d.invalidCat, _redirect(
, nil, nil, currentTitle, redirectTitle))end
function suite:testTrackingCategoryWrongTarget local currentTitle = suite.makeFakeTitleObject('Example') local redirectTitle = suite.makeFakeTitleObject('Example 2',) local targetTitle = suite.makeFakeTitleObject('Example 3') self:assertContainsTrackingCategory(d.invalidCat, _redirect(
, nil, nil, currentTitle, redirectTitle, targetTitle))end
function suite:testTrackingCategoryCorrectTarget local currentTitle = suite.makeFakeTitleObject('Example') local redirectTitle = suite.makeFakeTitleObject('Example 2',) local targetTitle = suite.makeFakeTitleObject('Example') self:assertNotContainsTrackingCategory(_redirect(
, nil, nil, currentTitle, redirectTitle, targetTitle))end
return suite