Module:Category main article/testcases
මෙම Lua module පිටු බොහෝ ගණනක භාවිතා වන නිසා සිදුකරන වෙනස් කිරීම් බොහෝ ස්ථානවලට බලපානු ඇත. ඔබ සිදුකිරීමට අදහස් කරන වෙනස්කම් මෙම මොඩියුලයට අදාළ /sandbox හෝ /testcases උපපිටු. එම වෙනස්කම් සිදුකිරීමට ප්රථම අදාළ සාකච්ඡා පිටුවේ ඒ පිළිබඳව සංවාදයක් ගොඩනැගීමට කාරුණික වන්න.
Transclusion count updated automatically (ප්රලේඛනය වෙතට යොමුවන්න). |
මෙම මොඩියුලයට අවශ්ය අනෙකුත් මොඩියුල මත: |
This module produces hatnote saying "The main article for this category is x." It implements the {{Category main article}} template.
Use from wikitext
සංස්කරණයThis module should usually be used via the {{Category main article}} template. However, it can also be used from #invoke with the syntax {{#invoke:Category main article|catMain|parameters}}
. Please see the {{Category main article}} template documentation for available parameters.
Use from other Lua modules
සංස්කරණයLoad the module:
local mCatMain = require('Module:Category main article')
You can then use the _catMain function like this:
mCatMain._catMain(options, ...)
options is an optional table that can be used to configure the function's output. There are two available options, "article" and "selfref".
- article - if this is set to false, "no", "n", "false", or 0, the module outputs "The main page" rather than "The main article". Use the code .XML Example
{article = false}
- selfref - this is used when the output is a self-reference to Wikipedia. To set this option, use . (See the {{selfref}} template for more details on self-references.)XML Example
{selfref = true}
The remaining arguments are page names to be turned into link(s) following the text "The main article for this category is". If no page names are specified, the current page name (minus the namespace name) is used for the first link.
- Example 1
mCatMain._catMain(nil, 'Foo')
Produces:
<div class="hatnote relarticle mainarticle">The main article for this [[Help:Categories|category]] is '''[[Foo]]'''.</div>
Displays as:
- Example 2
mCatMain._catMain(nil, 'Foo', 'Bar', 'Baz')
Produces:
<div class="hatnote relarticle mainarticle">The main articles for this [[Help:Categories|category]] are '''[[Foo]]''', '''[[Bar]]''' and '''[[Baz]]'''.</div>
Displays as:
- Example 3
mCatMain._catMain({article = false}, 'Foo')
Produces:
<div class="hatnote relarticle mainarticle">The main page for this [[Help:Categories|category]] is '''[[Foo]]'''.</div>
Displays as:
Technical details
සංස්කරණයThis module uses Module:Hatnote to format the hatnote text.
local mCatMain = require('Module:Cat main/sandbox') -- the module to be tested
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit:new()
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
local function patchCurrentTitle(newTitle, func)
local oldGetCurrentTitle = mw.title.getCurrentTitle
mw.title.getCurrentTitle = function ()
return mw.title.new("Category:Example")
end
func()
mw.title.getCurrentTitle = oldGetCurrentTitle
end
--------------------------------------------------------------------------------
-- Custom assert methods
--------------------------------------------------------------------------------
function suite:assertHasClass(expectedClass, result)
result = mw.text.killMarkers(result) -- remove TemplateStyles marker
local classes = result:match('^<div[^>]*class="([^"]*)"')
classes = mw.text.split(classes, ' ')
local hasClass = false
for _, actualClass in ipairs(classes) do
if actualClass == expectedClass then
hasClass = true
break
end
end
self:assertTrue(
hasClass,
string.format(
'Class "%s" %s in result "%s"',
expectedClass,
hasClass and "found" or "not found",
result
)
)
end
--------------------------------------------------------------------------------
-- Tests
--------------------------------------------------------------------------------
function suite:testWholeOutput()
self:assertEquals(
[=[<div role="note" class="hatnote navigation-not-searchable">The main article for this [[Help:Categories|category]] is '''[[:Foo]]'''.</div>]=],
mw.text.killMarkers(mCatMain._catMain(nil, 'Foo'))
)
end
function suite:testOneArticle()
self:assertStringContains(
"The main article for this [[Help:Categories|category]] is '''[[:Foo]]'''.",
mCatMain._catMain(nil, 'Foo'),
true
)
end
function suite:testTwoArticles()
self:assertStringContains(
"The main articles for this [[Help:Categories|category]] are '''[[:Foo]]''' and '''[[:Bar]]'''.",
mCatMain._catMain(nil, 'Foo', 'Bar'),
true
)
end
function suite:testThreeArticles()
self:assertStringContains(
"The main articles for this [[Help:Categories|category]] are '''[[:Foo]]''', '''[[:Bar]]''' and '''[[:Baz]]'''.",
mCatMain._catMain(nil, 'Foo', 'Bar', 'Baz'),
true
)
end
function suite:testNonArticle()
self:assertStringContains(
"The main page for this [[Help:Categories|category]] is '''[[:Foo]]'''.",
mCatMain._catMain({article = false}, 'Foo'),
true
)
end
function suite:testSelfReference()
self:assertHasClass("selfref", mCatMain._catMain({selfref = true}, 'Foo'))
end
function suite:testNoArticles()
patchCurrentTitle(
"Category:Example",
function ()
self:assertStringContains(
"The main article for this [[Help:Categories|category]] is '''[[:Example]]'''.",
mCatMain._catMain(),
true
)
end
)
end
return suite