Difference between revisions of "Module:Redirect hatnote/testcases"
Jump to navigation
Jump to search
test>Mr. Stradivarius (update one test to reflect how the template currently works) |
m (26 revisions imported) |
||
| (19 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | local mRedirectHatnote = require('Module:Redirect hatnote') | + | local mRedirectHatnote = require('Module:Redirect hatnote/sandbox') |
| − | local | + | local _redirect = mRedirectHatnote._redirect |
local ScribuntoUnit = require('Module:ScribuntoUnit') | local ScribuntoUnit = require('Module:ScribuntoUnit') | ||
local suite = ScribuntoUnit:new() | local suite = ScribuntoUnit:new() | ||
| Line 8: | Line 8: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| − | function suite.runRedirect(args) | + | function suite.runRedirect(args, childArgs) |
-- Runs the "redirect" function with an args table passed from a | -- Runs the "redirect" function with an args table passed from a | ||
-- parent frame. | -- parent frame. | ||
local frame = mw.getCurrentFrame() | local frame = mw.getCurrentFrame() | ||
local parent = frame:newChild{args = args} | local parent = frame:newChild{args = args} | ||
| − | local child = parent:newChild{} | + | local child = parent:newChild{args = childArgs} |
return mRedirectHatnote.redirect(child) | return mRedirectHatnote.redirect(child) | ||
end | end | ||
| Line 22: | Line 22: | ||
function suite:testBlank() | function suite:testBlank() | ||
| − | local result = suite.runRedirect{} | + | local result = suite.runRedirect({},{}) |
self:assertStringContains('no redirect specified', result, true) | self:assertStringContains('no redirect specified', result, true) | ||
self:assertStringContains('Template:Redirect#Errors', result, true) | self:assertStringContains('Template:Redirect#Errors', result, true) | ||
| Line 28: | Line 28: | ||
function suite:testRedirectOnly() | function suite:testRedirectOnly() | ||
| − | local result = suite.runRedirect{'REDIRECT'} | + | local result = suite.runRedirect({'REDIRECT'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For other uses, see [[REDIRECT (disambiguation)]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For other uses, see [[:REDIRECT (disambiguation)]].', result, true) |
end | end | ||
function suite:testUse1() | function suite:testUse1() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[REDIRECT (disambiguation)]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:REDIRECT (disambiguation)]].', result, true) |
end | end | ||
function suite:testPage1() | function suite:testPage1() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]].', result, true) |
end | end | ||
function suite:testUse2() | function suite:testUse2() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[ | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:REDIRECT (disambiguation)]].', result, true) |
end | end | ||
function suite:testPage2() | function suite:testPage2() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:PAGE2]].', result, true) |
end | end | ||
function suite:testUse3() | function suite:testUse3() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]]. For USE3, see [[ | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:PAGE2]]. For USE3, see [[:REDIRECT (disambiguation)]].', result, true) |
end | end | ||
function suite:testPage3() | function suite:testPage3() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]]. For USE3, see [[PAGE3]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:PAGE2]]. For USE3, see [[:PAGE3]].', result, true) |
end | end | ||
function suite:testUse4() | function suite:testUse4() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3', 'USE4'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3', 'USE4'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]]. For USE3, see [[PAGE3]]. For USE4, see [[ | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:PAGE2]]. For USE3, see [[:PAGE3]]. For USE4, see [[:REDIRECT (disambiguation)]].', result, true) |
end | end | ||
function suite:testPage4() | function suite:testPage4() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3', 'USE4', 'PAGE4'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3', 'USE4', 'PAGE4'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]]. For USE3, see [[PAGE3]]. For USE4, see [[PAGE4]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:PAGE2]]. For USE3, see [[:PAGE3]]. For USE4, see [[:PAGE4]].', result, true) |
end | end | ||
function suite:testPipeInput() | function suite:testPipeInput() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1 (disambiguation)|PAGE1', 'USE2', 'PAGE2 (disambiguation)|PAGE2', 'USE3', 'PAGE3 (disambiguation)|PAGE3', 'USE4', 'PAGE4 (disambiguation)|PAGE4'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1 (disambiguation)|PAGE1', 'USE2', 'PAGE2 (disambiguation)|PAGE2', 'USE3', 'PAGE3 (disambiguation)|PAGE3', 'USE4', 'PAGE4 (disambiguation)|PAGE4'},{1}) |
| − | 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) | + | 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 | end | ||
| Line 82: | Line 82: | ||
function suite:testPage1AndPage2() | function suite:testPage1AndPage2() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]] and [[PAGE2]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]] and [[:PAGE2]].', result, true) |
end | end | ||
function suite:testPage2AndPage3() | function suite:testPage2AndPage3() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'and', 'PAGE3'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'and', 'PAGE3'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]] and [[PAGE3]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:PAGE2]] and [[:PAGE3]].', result, true) |
end | end | ||
function suite:testPage1AndPage2Page3AndPage4() | function suite:testPage1AndPage2Page3AndPage4() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'USE2', 'PAGE3', 'and', 'PAGE4'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'USE2', 'PAGE3', 'and', 'PAGE4'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]] and [[PAGE2]]. For USE2, see [[PAGE3]] and [[PAGE4]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]] and [[:PAGE2]]. For USE2, see [[:PAGE3]] and [[:PAGE4]].', result, true) |
end | end | ||
function suite:testTwoAnds() | function suite:testTwoAnds() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'and', 'PAGE3'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'and', 'PAGE3'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]], [[PAGE2]] and [[PAGE3]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]], [[:PAGE2]], and [[:PAGE3]].', result, true) |
end | end | ||
function suite:testThreeAnds() | function suite:testThreeAnds() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'and', 'PAGE3', 'and', 'PAGE4'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'and', 'PAGE3', 'and', 'PAGE4'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]], [[PAGE2]], [[PAGE3]] and [[PAGE4]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]], [[:PAGE2]], [[:PAGE3]], and [[:PAGE4]].', result, true) |
end | end | ||
function suite:testTwoAndsUse2() | function suite:testTwoAndsUse2() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'and', 'PAGE3', 'USE2', 'PAGE4'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'and', 'PAGE3', 'USE2', 'PAGE4'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]], [[PAGE2]] and [[PAGE3]]. For USE2, see [[PAGE4]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]], [[:PAGE2]], and [[:PAGE3]]. For USE2, see [[:PAGE4]].', result, true) |
end | end | ||
| Line 116: | Line 116: | ||
function suite:testUse1Skipped() | function suite:testUse1Skipped() | ||
| − | local result = suite.runRedirect{'REDIRECT', '', 'PAGE1'} | + | local result = suite.runRedirect({'REDIRECT', '', 'PAGE1'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For other uses, see [[PAGE1]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For other uses, see [[:PAGE1]].', result, true) |
end | end | ||
function suite:testPage1Skipped() | function suite:testPage1Skipped() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', '', 'USE2'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', '', 'USE2'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[REDIRECT (disambiguation)]]. For USE2, see [[ | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:REDIRECT (disambiguation)]]. For USE2, see [[:REDIRECT (disambiguation)]].', result, true) |
end | end | ||
function suite:testUse2Skipped() | function suite:testUse2Skipped() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', '', 'PAGE2'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', '', 'PAGE2'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]].', result, true) |
end | end | ||
function suite:testPage2Skipped() | function suite:testPage2Skipped() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', '', 'USE3'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', '', 'USE3'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[ | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:REDIRECT (disambiguation)]]. For USE3, see [[:REDIRECT (disambiguation)]].', result, true) |
end | end | ||
function suite:testUse3Skipped() | function suite:testUse3Skipped() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', '', 'PAGE3'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', '', 'PAGE3'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[PAGE2]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:PAGE2]].', result, true) |
end | end | ||
function suite:testMiddleUseSkipped() | function suite:testMiddleUseSkipped() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', '', 'PAGE2', 'USE3', 'PAGE3'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', '', 'PAGE2', 'USE3', 'PAGE3'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]].', result, true) |
end | end | ||
function suite:testMiddlePageSkipped() | function suite:testMiddlePageSkipped() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', 'USE2', '', 'USE3', 'PAGE3'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', '', 'USE3', 'PAGE3'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For USE2, see [[ | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:REDIRECT (disambiguation)]]. For USE3, see [[:PAGE3]].', result, true) |
end | end | ||
| Line 155: | Line 155: | ||
function suite:testUse2Skipped() | function suite:testUse2Skipped() | ||
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', '', 'PAGE2'} | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', '', 'PAGE2'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]].', result, true) |
end | end | ||
| − | function suite: | + | function suite:testOtherUsesIsNotFinal() |
| − | local result = suite.runRedirect{'REDIRECT', 'USE1', 'PAGE1', ' | + | local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'other uses', 'PAGE2', 'USE3', 'PAGE3'},{1}) |
| − | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[PAGE1]]. For other uses, see [[PAGE2]].', result, true) | + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For other uses, see [[:PAGE2]]. For USE3, see [[:PAGE3]].', result, true) |
| + | end | ||
| + | |||
| + | -------------------------------------------------------------------------------- | ||
| + | -- Test options | ||
| + | -------------------------------------------------------------------------------- | ||
| + | |||
| + | function suite:testSelfRef() | ||
| + | local result = suite.runRedirect({'REDIRECT', selfref = 'yes'},{1}) | ||
| + | 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({'REDIRECT'}) | ||
| + | self:assertStringContains('"REDIRECT" redirects here. For other uses, see [[:REDIRECT (disambiguation)]].', result, true) | ||
| + | end | ||
| + | |||
| + | function suite:testLuaPage1() | ||
| + | local result = _redirect( | ||
| + | {'REDIRECT', 'USE1', 'PAGE1'}, | ||
| + | 1 | ||
| + | ) | ||
| + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]].', result, true) | ||
| + | end | ||
| + | |||
| + | function suite:testLuaPage3() | ||
| + | local result = _redirect( | ||
| + | {'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3'}, | ||
| + | 1 | ||
| + | ) | ||
| + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:PAGE2]]. For USE3, see [[:PAGE3]].', result, true) | ||
| + | end | ||
| + | |||
| + | function suite:testLuaMultiplePages() | ||
| + | local result = _redirect( | ||
| + | {'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'and', 'PAGE3', 'USE2', 'PAGE4', 'and', 'PAGE5'}, | ||
| + | 1 | ||
| + | ) | ||
| + | self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]], [[:PAGE2]], and [[:PAGE3]]. For USE2, see [[:PAGE4]] and [[:PAGE5]].', result, true) | ||
| + | end | ||
| + | |||
| + | function suite:testLuaSelfref() | ||
| + | local result = _redirect({'REDIRECT'}, 1, {selfref = true}) | ||
| + | self:assertStringContains('class=".-selfref.-"', result, false) | ||
end | end | ||
| − | function suite: | + | -------------------------------------------------------------------------------- |
| − | local | + | -- Test tracking category |
| − | self: | + | -------------------------------------------------------------------------------- |
| + | |||
| + | 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 title | ||
| + | end | ||
| + | |||
| + | local d = {} | ||
| + | |||
| + | -- Categories | ||
| + | d.missingCat = 'Missing redirects' | ||
| + | d.invalidCat = 'Articles with redirect hatnotes needing review' | ||
| + | |||
| + | function suite:assertContainsTrackingCategory(category, result) | ||
| + | category = string.format('[[Category:%s]]', 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({'Example'}, nil, nil, currentTitle)) | ||
| + | end | ||
| + | |||
| + | function suite:testTrackingCategoryExceptions() | ||
| + | local currentTitle = suite.makeFakeTitleObject('Example') | ||
| + | for _, s in ipairs{'REDIRECT', 'REDIRECT1', 'REDIRECT1678', 'TERM'} do | ||
| + | self:assertNotContainsTrackingCategory(_redirect({s}, nil, nil, currentTitle)) | ||
| + | end | ||
| + | end | ||
| + | |||
| + | function suite:testTrackingCategoryInvalidTitle() | ||
| + | local currentTitle = suite.makeFakeTitleObject('Example') | ||
| + | self:assertContainsTrackingCategory(d.missingCat, _redirect({'Exam<>ple 2'}, nil, nil, currentTitle)) | ||
| + | end | ||
| + | |||
| + | function suite:testTrackingCategoryNonexistent() | ||
| + | local currentTitle = suite.makeFakeTitleObject('Example') | ||
| + | local redirectTitle = suite.makeFakeTitleObject('Example 2', {exists = false, isRedirect = false}) | ||
| + | self:assertContainsTrackingCategory(d.missingCat, _redirect({redirectTitle.prefixedText}, nil, nil, currentTitle, redirectTitle)) | ||
| + | end | ||
| + | |||
| + | function suite:testTrackingCategoryNotRedirect() | ||
| + | local currentTitle = suite.makeFakeTitleObject('Example') | ||
| + | local redirectTitle = suite.makeFakeTitleObject('Example 2', {exists = true, isRedirect = false}) | ||
| + | self:assertContainsTrackingCategory(d.invalidCat, _redirect({redirectTitle.prefixedText}, nil, nil, currentTitle, redirectTitle)) | ||
| + | end | ||
| + | |||
| + | function suite:testTrackingCategoryWrongTarget() | ||
| + | local currentTitle = suite.makeFakeTitleObject('Example') | ||
| + | local redirectTitle = suite.makeFakeTitleObject('Example 2', {exists = true, isRedirect = true, getContent = function () | ||
| + | return '#REDIRECT [[Example 3]]' -- This is to fool Module:Redirect. | ||
| + | end}) | ||
| + | local targetTitle = suite.makeFakeTitleObject('Example 3') | ||
| + | self:assertContainsTrackingCategory(d.invalidCat, _redirect({redirectTitle.prefixedText}, nil, nil, currentTitle, redirectTitle, targetTitle)) | ||
| + | end | ||
| + | |||
| + | function suite:testTrackingCategoryCorrectTarget() | ||
| + | local currentTitle = suite.makeFakeTitleObject('Example') | ||
| + | local redirectTitle = suite.makeFakeTitleObject('Example 2', {exists = true, isRedirect = true, getContent = function () | ||
| + | return '#REDIRECT [[Example]]' -- This is to fool Module:Redirect. | ||
| + | end}) | ||
| + | local targetTitle = suite.makeFakeTitleObject('Example') | ||
| + | self:assertNotContainsTrackingCategory(_redirect({redirectTitle.prefixedText}, nil, nil, currentTitle, redirectTitle, targetTitle)) | ||
end | end | ||
return suite | return suite | ||
Latest revision as of 11:38, 7 September 2020
Documentation for this module may be created at Module:Redirect hatnote/testcases/doc
local mRedirectHatnote = require('Module:Redirect hatnote/sandbox')
local _redirect = mRedirectHatnote._redirect
local 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{args = args}
local child = parent:newChild{args = childArgs}
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'},{1})
self:assertStringContains('"REDIRECT" redirects here. For other uses, see [[:REDIRECT (disambiguation)]].', result, true)
end
function suite:testUse1()
local result = suite.runRedirect({'REDIRECT', 'USE1'},{1})
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:REDIRECT (disambiguation)]].', result, true)
end
function suite:testPage1()
local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1'},{1})
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]].', result, true)
end
function suite:testUse2()
local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2'},{1})
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:REDIRECT (disambiguation)]].', result, true)
end
function suite:testPage2()
local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2'},{1})
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'},{1})
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:PAGE2]]. For USE3, see [[:REDIRECT (disambiguation)]].', result, true)
end
function suite:testPage3()
local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3'},{1})
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'},{1})
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:PAGE2]]. For USE3, see [[:PAGE3]]. For USE4, see [[:REDIRECT (disambiguation)]].', result, true)
end
function suite:testPage4()
local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3', 'USE4', 'PAGE4'},{1})
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'},{1})
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'},{1})
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'},{1})
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'},{1})
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'},{1})
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'},{1})
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'},{1})
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'},{1})
self:assertStringContains('"REDIRECT" redirects here. For other uses, see [[:PAGE1]].', result, true)
end
function suite:testPage1Skipped()
local result = suite.runRedirect({'REDIRECT', 'USE1', '', 'USE2'},{1})
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:REDIRECT (disambiguation)]]. For USE2, see [[:REDIRECT (disambiguation)]].', result, true)
end
function suite:testUse2Skipped()
local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', '', 'PAGE2'},{1})
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]].', result, true)
end
function suite:testPage2Skipped()
local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', '', 'USE3'},{1})
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:REDIRECT (disambiguation)]]. For USE3, see [[:REDIRECT (disambiguation)]].', result, true)
end
function suite:testUse3Skipped()
local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', '', 'PAGE3'},{1})
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'},{1})
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'},{1})
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:REDIRECT (disambiguation)]]. For USE3, see [[:PAGE3]].', result, true)
end
--------------------------------------------------------------------------------
-- Test other uses
--------------------------------------------------------------------------------
function suite:testUse2Skipped()
local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', '', 'PAGE2'},{1})
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]].', result, true)
end
function suite:testOtherUsesIsNotFinal()
local result = suite.runRedirect({'REDIRECT', 'USE1', 'PAGE1', 'other uses', 'PAGE2', 'USE3', 'PAGE3'},{1})
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For other uses, see [[:PAGE2]]. For USE3, see [[:PAGE3]].', result, true)
end
--------------------------------------------------------------------------------
-- Test options
--------------------------------------------------------------------------------
function suite:testSelfRef()
local result = suite.runRedirect({'REDIRECT', selfref = 'yes'},{1})
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({'REDIRECT'})
self:assertStringContains('"REDIRECT" redirects here. For other uses, see [[:REDIRECT (disambiguation)]].', result, true)
end
function suite:testLuaPage1()
local result = _redirect(
{'REDIRECT', 'USE1', 'PAGE1'},
1
)
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]].', result, true)
end
function suite:testLuaPage3()
local result = _redirect(
{'REDIRECT', 'USE1', 'PAGE1', 'USE2', 'PAGE2', 'USE3', 'PAGE3'},
1
)
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]]. For USE2, see [[:PAGE2]]. For USE3, see [[:PAGE3]].', result, true)
end
function suite:testLuaMultiplePages()
local result = _redirect(
{'REDIRECT', 'USE1', 'PAGE1', 'and', 'PAGE2', 'and', 'PAGE3', 'USE2', 'PAGE4', 'and', 'PAGE5'},
1
)
self:assertStringContains('"REDIRECT" redirects here. For USE1, see [[:PAGE1]], [[:PAGE2]], and [[:PAGE3]]. For USE2, see [[:PAGE4]] and [[:PAGE5]].', result, true)
end
function suite:testLuaSelfref()
local result = _redirect({'REDIRECT'}, 1, {selfref = true})
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 title
end
local d = {}
-- Categories
d.missingCat = 'Missing redirects'
d.invalidCat = 'Articles with redirect hatnotes needing review'
function suite:assertContainsTrackingCategory(category, result)
category = string.format('[[Category:%s]]', 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({'Example'}, nil, nil, currentTitle))
end
function suite:testTrackingCategoryExceptions()
local currentTitle = suite.makeFakeTitleObject('Example')
for _, s in ipairs{'REDIRECT', 'REDIRECT1', 'REDIRECT1678', 'TERM'} do
self:assertNotContainsTrackingCategory(_redirect({s}, nil, nil, currentTitle))
end
end
function suite:testTrackingCategoryInvalidTitle()
local currentTitle = suite.makeFakeTitleObject('Example')
self:assertContainsTrackingCategory(d.missingCat, _redirect({'Exam<>ple 2'}, nil, nil, currentTitle))
end
function suite:testTrackingCategoryNonexistent()
local currentTitle = suite.makeFakeTitleObject('Example')
local redirectTitle = suite.makeFakeTitleObject('Example 2', {exists = false, isRedirect = false})
self:assertContainsTrackingCategory(d.missingCat, _redirect({redirectTitle.prefixedText}, nil, nil, currentTitle, redirectTitle))
end
function suite:testTrackingCategoryNotRedirect()
local currentTitle = suite.makeFakeTitleObject('Example')
local redirectTitle = suite.makeFakeTitleObject('Example 2', {exists = true, isRedirect = false})
self:assertContainsTrackingCategory(d.invalidCat, _redirect({redirectTitle.prefixedText}, nil, nil, currentTitle, redirectTitle))
end
function suite:testTrackingCategoryWrongTarget()
local currentTitle = suite.makeFakeTitleObject('Example')
local redirectTitle = suite.makeFakeTitleObject('Example 2', {exists = true, isRedirect = true, getContent = function ()
return '#REDIRECT [[Example 3]]' -- This is to fool Module:Redirect.
end})
local targetTitle = suite.makeFakeTitleObject('Example 3')
self:assertContainsTrackingCategory(d.invalidCat, _redirect({redirectTitle.prefixedText}, nil, nil, currentTitle, redirectTitle, targetTitle))
end
function suite:testTrackingCategoryCorrectTarget()
local currentTitle = suite.makeFakeTitleObject('Example')
local redirectTitle = suite.makeFakeTitleObject('Example 2', {exists = true, isRedirect = true, getContent = function ()
return '#REDIRECT [[Example]]' -- This is to fool Module:Redirect.
end})
local targetTitle = suite.makeFakeTitleObject('Example')
self:assertNotContainsTrackingCategory(_redirect({redirectTitle.prefixedText}, nil, nil, currentTitle, redirectTitle, targetTitle))
end
return suite