Anonymous user
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,
getOutput = true
}
Line 133:
end
function Template:
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
local prefix, num
if type(k) == 'string' then
prefix, num = k:match('^(.-)([1-9][0-9]*)$')
end
if prefix then
num = tonumber(num)
templateOptions[num] = templateOptions[num] or {}
templateOptions[num][prefix] = v
else
generalOptions[k] = v
end
end
generalOptions.showcode = yesno(generalOptions.showcode)
generalOptions.showheader = yesno(generalOptions.showheader) ~= false
generalOptions.collapsible = yesno(generalOptions.collapsible)
obj.options = generalOptions
-- Preprocess template args
for num, t in pairs(templateOptions) do
if t.showtemplate ~= nil then
t.showtemplate = yesno(t.showtemplate)
end
end
-- "template3" is specified it isn't made the first template when the
-- the table options array is compressed.
templateOptions[1] = templateOptions[1] or {}
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
-- Check for missing template names.
for num, t in pairs(templateOptions) do
if not t.template and not t.title then
error(obj:message(
'missing-template-option-error',
num, num
), 2)
end
end
-- Compress templateOptions table so we can iterate over it with ipairs.
templateOptions = (function (t)
local nums = {}
for num in pairs(
nums[#nums + 1] = num
end
table.sort(nums)
local ret = {}
for i, num in ipairs(nums) do
end
return ret
end)(templateOptions)
-- 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]:
:done()
:done()
Line 322 ⟶ 346:
local tableroot = root:tag('table')
if self.options.showheader then
-- Caption
tableroot
:addClass(self.options.class)
:cssText(self.options.style)
:tag('caption')
:wikitext(self.options.caption or self:message('columns-header'))
-- Headers
local headerRow = tableroot:tag('tr')
if self.options.rowheader then
-- rowheader is correct here. We need to add another th cell if
-- rowheader is set further down, even if heading0 is missing.
headerRow:tag('th'):wikitext(self.options.heading0)
end
local width
if #self.templates > 0 then
width = tostring(math.floor(100 / #self.templates)) .. '%'
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:tag('tr'):tag('td')
:
:wikitext(obj:makeHeader())
end
tableroot:tag('tr'):tag('td')
: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: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
|