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: | Line 54: | ||
getFullPage = true, | getFullPage = true, | ||
getName = true, | getName = true, | ||
makeHeader = true, | |||
getOutput = true | getOutput = true | ||
} | } | ||
Line 133: | Line 133: | ||
end | end | ||
function Template: | function Template:makeHeader() | ||
return self.heading or self:makeBraceLink() | return self.heading or self:makeBraceLink() | ||
end | end | ||
Line 180: | Line 180: | ||
-- numbered, whereas general options are not. | -- numbered, whereas general options are not. | ||
local generalOptions, templateOptions = {}, {} | 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 | |||
-- Set general options | |||
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 | ||
end | |||
-- Set up first two template options tables, so that if only the | |||
-- "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 | ||
end | |||
-- Check for missing template names. | |||
if not | 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 | ||
end | |||
-- Compress templateOptions table so we can iterate over it with ipairs. | |||
templateOptions = (function (t) | |||
local nums = {} | local nums = {} | ||
for num in pairs( | for num in pairs(t) do | ||
nums[#nums + 1] = num | nums[#nums + 1] = num | ||
end | end | ||
table.sort(nums) | table.sort(nums) | ||
local ret = {} | |||
for i, num in ipairs(nums) do | for i, num in ipairs(nums) do | ||
ret[i] = t[num] | |||
end | end | ||
end | return ret | ||
end)(templateOptions) | |||
-- Make the template objects | -- Make the template objects | ||
Line 255: | Line 269: | ||
for i, t in ipairs(templateOptions) do | for i, t in ipairs(templateOptions) do | ||
table.insert(obj.templates, Template.new(invocationObj, t)) | 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 | end | ||
Line 302: | Line 326: | ||
:tag('th') | :tag('th') | ||
:css('background-color', isEqual and 'lightgreen' or 'yellow') | :css('background-color', isEqual and 'lightgreen' or 'yellow') | ||
:wikitext(self.options.title or self.templates[1]: | :wikitext(self.options.title or self.templates[1]:makeHeader()) | ||
:done() | :done() | ||
:done() | :done() | ||
Line 322: | Line 346: | ||
local tableroot = root:tag('table') | 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 | end | ||
Line 389: | Line 417: | ||
for _, obj in ipairs(self.templates) do | for _, obj in ipairs(self.templates) do | ||
-- Build the row HTML | -- Build the row HTML | ||
if self.options.showheader then | |||
:tag('tr') | tableroot:tag('tr'):tag('td') | ||
:css('text-align', 'center') | |||
:css('font-weight', 'bold') | |||
:wikitext(obj:makeHeader()) | |||
end | |||
tableroot:tag('tr'):tag('td') | |||
:newline() | |||
:wikitext(self:getTemplateOutput(obj)) | |||
end | end | ||
Line 431: | Line 456: | ||
for i, obj in ipairs(self.templates) do | for i, obj in ipairs(self.templates) do | ||
ret[#ret + 1] = '<div style="clear: both;"></div>' | ret[#ret + 1] = '<div style="clear: both;"></div>' | ||
ret[#ret + 1] = obj: | if self.options.showheader then | ||
ret[#ret + 1] = obj:makeHeader() | |||
end | |||
ret[#ret + 1] = self:getTemplateOutput(obj) | ret[#ret + 1] = self:getTemplateOutput(obj) | ||
end | end | ||
Line 443: | Line 470: | ||
if self.options.collapsible then | if self.options.collapsible then | ||
ret = self:makeCollapsible(ret) | ret = self:makeCollapsible(ret) | ||
end | |||
for cat in pairs(self.categories) do | |||
ret = ret .. string.format('[[Category:%s]]', cat) | |||
end | end | ||
return ret | return ret |