Module:Template test case: Difference between revisions

allow suppression of headers and of individual templates, and track any "heading" arguments in Category:Test cases using heading parameters
(Add a "before" parameter, similar to "after" but placed before template output. Only exists in column mode (function TestCase:renderColumns). See Template:Test case/testcases#Columns format with custom headings)
(allow suppression of headers and of individual templates, and track any "heading" arguments in Category:Test cases using heading parameters)
Line 54:
getFullPage = true,
getName = true,
makeHeadingmakeHeader = true,
getOutput = true
}
Line 133:
end
 
function Template:makeHeadingmakeHeader()
return self.heading or self:makeBraceLink()
end
Line 180:
-- numbered, whereas general options are not.
local generalOptions, templateOptions = {}, {}
for k, v in pairs(options) do
do
local prefix, num
local optionNum = {} -- a unique key for option numbers inside templateOptions
if type(k) == 'string' then
local rawTemplateOptions = {}
prefix, num = k:match('^(.-)([1-9][0-9]*)$')
for k, v in pairs(options) do
local prefix, num
if type(k) == 'string' then
prefix, num = k:match('^(.-)([1-9][0-9]*)$')
end
if prefix then
num = tonumber(num)
rawTemplateOptions[num] = rawTemplateOptions[num] or {}
rawTemplateOptions[num][prefix] = v
rawTemplateOptions[num][optionNum] = num -- record for use in error messages
else
generalOptions[k] = v
end
end
if prefix then
num = tonumber(num)
templateOptions[num] = templateOptions[num] or {}
templateOptions[num][prefix] = v
else
generalOptions[k] = v
end
end
 
-- Set up first two templategeneral options tables, so that if only the
generalOptions.showcode = yesno(generalOptions.showcode)
-- "template3" is specified it isn't made the first template when the
generalOptions.showheader = yesno(generalOptions.showheader) ~= false
-- the table options array is compressed.
generalOptions.collapsible = yesno(generalOptions.collapsible)
rawTemplateOptions[1] = rawTemplateOptions[1] or {}
obj.options = generalOptions
rawTemplateOptions[2] = rawTemplateOptions[2] or {}
 
-- Preprocess template args
-- Allow the "template" option to override the "template1" option for
for num, t in pairs(templateOptions) do
-- backwards compatibility with [[Module:Testcase table]].
if t.showtemplate ~= nil then
rawTemplateOptions[1].template = generalOptions.template
t.showtemplate = yesno(t.showtemplate)
or rawTemplateOptions[1].template
end
end
 
-- AddSet defaultup first two template options tables, so that if only the
-- "template3" is specified it isn't made the first template when the
if rawTemplateOptions[1].template and not rawTemplateOptions[2].template then
-- the table options array is compressed.
rawTemplateOptions[2].template = rawTemplateOptions[1].template ..
templateOptions[1] = templateOptions[1] or {}
'/' .. obj.cfg.sandboxSubpage
templateOptions[2] = templateOptions[2] or {}
 
-- Allow the "template" option to override the "template1" option for
-- backwards compatibility with [[Module:Testcase table]].
if generalOptions.template then
templateOptions[1].template = generalOptions.template
end
 
-- Add default template options
if templateOptions[1].template and not templateOptions[2].template then
templateOptions[2].template = templateOptions[1].template ..
'/' .. obj.cfg.sandboxSubpage
end
if not templateOptions[1].template then
templateOptions[1].title = mw.title.getCurrentTitle().basePageTitle
end
if not templateOptions[2].template then
templateOptions[2].title = templateOptions[1].title:subPageTitle(
obj.cfg.sandboxSubpage
)
end
 
-- Remove template options for any templates where the showtemplate
-- argument is false. This prevents any output for that template.
for num, t in pairs(templateOptions) do
if t.showtemplate == false then
templateOptions[num] = nil
end
end
if not rawTemplateOptions[1].template then
 
rawTemplateOptions[1].title = mw.title.getCurrentTitle().basePageTitle
-- Check for missing template names.
end
for num, t in pairs(templateOptions) do
if not rawTemplateOptions[2].template then
if not t.template and not t.title then
rawTemplateOptions[2].title = rawTemplateOptions[1].title:subPageTitle(
error(obj:message(
obj.cfg.sandboxSubpage
'missing-template-option-error',
)
num, num
), 2)
end
end
 
-- Compress templateOptions table so we can iterate over it with ipairs.
-- Remove gaps in the numbered options
templateOptions = (function (t)
local nums = {}
for num in pairs(rawTemplateOptionst) do
nums[#nums + 1] = num
end
table.sort(nums)
local ret = {}
for i, num in ipairs(nums) do
templateOptionsret[i] = rawTemplateOptionst[num]
end
return ret
 
end)(templateOptions)
-- Check that there are no missing template options.
for i = 3, #templateOptions do -- Defaults have already been added for 1 and 2.
local t = templateOptions[i]
if not t.template then
local num = t[optionNum]
error(obj:message(
'missing-template-option-error',
num, num
), 2)
end
end
end
 
-- Set general options
generalOptions.showcode = yesno(generalOptions.showcode)
generalOptions.collapsible = yesno(generalOptions.collapsible)
obj.options = generalOptions
 
-- Make the template objects
Line 255 ⟶ 269:
for i, t in ipairs(templateOptions) do
table.insert(obj.templates, Template.new(invocationObj, t))
end
 
-- Add tracking categories. At the moment we are only tracking templates
-- that use any "heading" parameters.
obj.categories = {}
for k, v in pairs(options) do
if type(k) == 'string' and k:find('heading') then
categories['Test cases using heading parameters'] = true
break
end
end
 
Line 302 ⟶ 326:
:tag('th')
:css('background-color', isEqual and 'lightgreen' or 'yellow')
:wikitext(self.options.title or self.templates[1]:makeHeadingmakeHeader())
:done()
:done()
Line 322 ⟶ 346:
 
local tableroot = root:tag('table')
tableroot
:addClass(self.options.class)
:cssText(self.options.style)
:tag('caption')
:wikitext(self.options.caption or self:message('columns-header'))
 
if self.options.showheader then
-- Headings
-- Caption
local headingRow = tableroot:tag('tr')
tableroot
if self.options.rowheader then
:addClass(self.options.class)
-- rowheader is correct here. We need to add another th cell if
:cssText(self.options.style)
-- rowheader is set further down, even if heading0 is missing.
:tag('caption')
headingRow:tag('th'):wikitext(self.options.heading0)
:wikitext(self.options.caption or self:message('columns-header'))
end
 
local width
-- Headers
if #self.templates > 0 then
local headerRow = tableroot:tag('tr')
width = tostring(math.floor(100 / #self.templates)) .. '%'
if self.options.rowheader then
else
-- rowheader is correct here. We need to add another th cell if
width = '100%'
-- rowheader is set further down, even if heading0 is missing.
end
headerRow:tag('th'):wikitext(self.options.heading0)
for i, obj in ipairs(self.templates) do
end
headingRow
local width
:tag('th')
if #self.templates > 0 then
:css('width', width)
width = tostring(math.floor(100 / #self.templates)) .. '%'
:wikitext(obj:makeHeading())
else
width = '100%'
end
for i, obj in ipairs(self.templates) do
headerRow
:tag('th')
:css('width', width)
:wikitext(obj:makeHeader())
end
end
 
Line 389 ⟶ 417:
for _, obj in ipairs(self.templates) do
-- Build the row HTML
if self.options.showheader then
tableroot
tableroot:tag('tr'):tag('td')
:tagcss('tdtext-align', 'center')
:css('textfont-alignweight', 'centerbold')
:wikitext(obj:makeHeader())
:css('font-weight', 'bold')
end
:wikitext(obj:makeHeading())
tableroot:tag('tr'):tag('td')
:done()
:donenewline()
:wikitext(self:getTemplateOutput(obj))
:tag('tr')
:tag('td')
:newline()
:wikitext(self:getTemplateOutput(obj))
end
 
Line 431 ⟶ 456:
for i, obj in ipairs(self.templates) do
ret[#ret + 1] = '<div style="clear: both;"></div>'
if self.options.showheader then
ret[#ret + 1] = obj:makeHeading()
ret[#ret + 1] = obj:makeHeader()
end
ret[#ret + 1] = self:getTemplateOutput(obj)
end
Line 443 ⟶ 470:
if self.options.collapsible then
ret = self:makeCollapsible(ret)
end
for cat in pairs(self.categories) do
ret = ret .. string.format('[[Category:%s]]', cat)
end
return ret