Editing Module:Template test case
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
-- | -- This module provides several methods to generate test cases. | ||
-- Load required modules | -- Load required modules | ||
Line 54: | Line 33: | ||
getFullPage = true, | getFullPage = true, | ||
getName = true, | getName = true, | ||
makeHeading = true, | |||
getOutput = true | getOutput = true | ||
} | } | ||
Line 94: | Line 73: | ||
function Template:getFullPage() | function Template:getFullPage() | ||
if | if self.template then | ||
local strippedTemplate, hasColon = self.template:gsub('^:', '', 1) | local strippedTemplate, hasColon = self.template:gsub('^:', '', 1) | ||
hasColon = hasColon > 0 | hasColon = hasColon > 0 | ||
Line 110: | Line 85: | ||
return mw.site.namespaces[10].name .. ':' .. strippedTemplate | return mw.site.namespaces[10].name .. ':' .. strippedTemplate | ||
end | end | ||
else | |||
return self.title.prefixedText | |||
end | end | ||
end | end | ||
Line 135: | Line 112: | ||
end | end | ||
function Template: | function Template:makeHeading() | ||
return self.heading or self:makeBraceLink() | return self.heading or self:makeBraceLink() | ||
end | end | ||
function Template:getInvocation(format) | function Template:getInvocation(format) | ||
local invocation = self._invocation:getInvocation | local invocation = self._invocation:getInvocation(self:getName()) | ||
if format == 'code' then | if format == 'code' then | ||
invocation = '<code>' .. mw.text.nowiki(invocation) .. '</code>' | invocation = '<code>' .. mw.text.nowiki(invocation) .. '</code>' | ||
elseif format == 'plain' then | elseif format == 'plain' then | ||
invocation = mw.text.nowiki(invocation) | invocation = mw.text.nowiki(invocation) | ||
Line 160: | Line 132: | ||
function Template:getOutput() | function Template:getOutput() | ||
return self._invocation:getOutput(self:getName()) | |||
end | end | ||
Line 181: | Line 148: | ||
columns = 'renderColumns', | columns = 'renderColumns', | ||
rows = 'renderRows', | rows = 'renderRows', | ||
default = 'renderDefault' | default = 'renderDefault' | ||
} | } | ||
Line 191: | Line 155: | ||
obj.cfg = cfg | obj.cfg = cfg | ||
-- Separate | -- Separate numbered and named options. | ||
local generalOptions, templateOptions = {}, {} | local generalOptions, templateOptions = {}, {} | ||
for k, v in pairs(options) do | do | ||
-- Separate numbered and named options | |||
local optionNum = {} -- a unique key for option numbers inside templateOptions | |||
local rawTemplateOptions = {} | |||
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 | end | ||
-- Remove gaps in the numbered options | |||
num | local nums = {} | ||
templateOptions[ | for num in pairs(rawTemplateOptions) do | ||
templateOptions[num | nums[#nums + 1] = num | ||
end | |||
table.sort(nums) | |||
for i, num in ipairs(nums) do | |||
templateOptions[i] = rawTemplateOptions[num] | |||
end | |||
-- Check that there are no missing template options. | |||
for i = 3, #templateOptions do -- Defaults are added later 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 | ||
end | end | ||
Line 210: | Line 199: | ||
-- Set general options | -- Set general options | ||
generalOptions.showcode = yesno(generalOptions.showcode) | generalOptions.showcode = yesno(generalOptions.showcode) | ||
generalOptions.collapsible = yesno(generalOptions.collapsible) | generalOptions.collapsible = yesno(generalOptions.collapsible) | ||
obj.options = generalOptions | obj.options = generalOptions | ||
-- | -- Add default template options | ||
templateOptions[1] = templateOptions[1] or {} | templateOptions[1] = templateOptions[1] or {} | ||
templateOptions[2] = templateOptions[2] or {} | templateOptions[2] = templateOptions[2] or {} | ||
if templateOptions[1].template and not templateOptions[2].template then | if templateOptions[1].template and not templateOptions[2].template then | ||
templateOptions[2].template = templateOptions[1].template .. | templateOptions[2].template = templateOptions[1].template .. | ||
Line 249: | Line 217: | ||
) | ) | ||
end | end | ||
-- Make the template objects | -- Make the template objects | ||
obj.templates = {} | obj.templates = {} | ||
for i, | for i, t in ipairs(templateOptions) do | ||
table.insert(obj.templates, Template.new(invocationObj, | table.insert(obj.templates, Template.new(invocationObj, t)) | ||
end | end | ||
Line 327: | Line 244: | ||
local out = obj:getOutput() | local out = obj:getOutput() | ||
-- Remove the random parts from strip markers. | -- Remove the random parts from strip markers. | ||
out = out:gsub('( | out = out:gsub('(%cUNIQ).-(QINU%c)', '%1%2') | ||
return out | return out | ||
end | end | ||
Line 341: | Line 258: | ||
function TestCase:makeCollapsible(s) | function TestCase:makeCollapsible(s) | ||
local isEqual = self:templateOutputIsEqual() | local isEqual = self:templateOutputIsEqual() | ||
local root = mw.html.create('table') | local root = mw.html.create('table') | ||
root | root | ||
:addClass('collapsible') | |||
:addClass(isEqual and 'collapsed' or nil) | |||
:addClass(' | |||
:css('background-color', 'transparent') | :css('background-color', 'transparent') | ||
:css('width', '100%') | :css('width', '100%') | ||
Line 387: | Line 269: | ||
:tag('th') | :tag('th') | ||
:css('background-color', isEqual and 'lightgreen' or 'yellow') | :css('background-color', isEqual and 'lightgreen' or 'yellow') | ||
:wikitext(title) | :wikitext(self.options.title or self.templates[1]:makeHeading()) | ||
:done() | :done() | ||
:done() | :done() | ||
:tag('tr') | :tag('tr') | ||
:tag('td') | :tag('td') | ||
:wikitext(s) | :wikitext(s) | ||
return tostring(root) | return tostring(root) | ||
end | end | ||
Line 408: | Line 287: | ||
local tableroot = root:tag('table') | 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')) | |||
-- Headings | |||
local headingRow = 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. | |||
headingRow: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 | |||
headingRow | |||
:tag('th') | |||
:css('width', width) | |||
:wikitext(obj:makeHeading()) | |||
end | end | ||
Line 450: | Line 323: | ||
-- Template output | -- Template output | ||
for i, obj in ipairs(self.templates) do | for i, obj in ipairs(self.templates) do | ||
dataRow:tag('td') | |||
:newline() | |||
:wikitext(self:getTemplateOutput(obj)) | |||
:wikitext(self.options.after) | |||
end | end | ||
Line 499: | Line 352: | ||
for _, obj in ipairs(self.templates) do | for _, obj in ipairs(self.templates) do | ||
-- Build the row HTML | |||
tableroot | |||
-- | :tag('tr') | ||
:tag('td') | |||
:css('text-align', 'center') | :css('text-align', 'center') | ||
:css('font-weight', 'bold') | :css('font-weight', 'bold') | ||
:wikitext(obj: | :wikitext(obj:makeHeading()) | ||
:done() | |||
:done() | |||
:tag('tr') | |||
:tag('td') | |||
:newline() | |||
:wikitext(self:getTemplateOutput(obj)) | |||
: | |||
end | end | ||
return tostring(root) | return tostring(root) | ||
Line 646: | Line 377: | ||
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:makeBraceLink() | |||
ret[#ret + 1] = self:getTemplateOutput(obj) | |||
end | end | ||
return table.concat(ret, '\n\n') | return table.concat(ret, '\n\n') | ||
Line 666: | Line 389: | ||
if self.options.collapsible then | if self.options.collapsible then | ||
ret = self:makeCollapsible(ret) | ret = self:makeCollapsible(ret) | ||
end | end | ||
return ret | return ret | ||
Line 696: | Line 416: | ||
end | end | ||
function NowikiInvocation:getInvocation( | function NowikiInvocation:getInvocation(template) | ||
template = template:gsub('%%', '%%%%') -- Escape "%" with "%%" | |||
local invocation, count = self.invocation:gsub( | local invocation, count = self.invocation:gsub( | ||
self.cfg.templateNameMagicWordPattern, | self.cfg.templateNameMagicWordPattern, | ||
template | template | ||
) | ) | ||
if | if count < 1 then | ||
error(self:message( | error(self:message( | ||
'nowiki-magic-word-error', | 'nowiki-magic-word-error', | ||
Line 711: | Line 431: | ||
end | end | ||
function NowikiInvocation:getOutput( | function NowikiInvocation:getOutput(template) | ||
local invocation = self:getInvocation( | local invocation = self:getInvocation(template) | ||
return mw.getCurrentFrame():preprocess(invocation) | return mw.getCurrentFrame():preprocess(invocation) | ||
end | end | ||
Line 732: | Line 452: | ||
end | end | ||
function TableInvocation:getInvocation( | function TableInvocation:getInvocation(template) | ||
if self.code then | if self.code then | ||
local nowikiObj = NowikiInvocation | local nowikiObj = NowikiInvocation(self.code, self.cfg) | ||
return nowikiObj:getInvocation( | return nowikiObj:getInvocation(template) | ||
else | else | ||
return require('Module:Template invocation').invocation( | return require('Module:Template invocation').invocation( | ||
template, | |||
self.invokeArgs | self.invokeArgs | ||
) | ) | ||
Line 744: | Line 464: | ||
end | end | ||
function TableInvocation:getOutput( | function TableInvocation:getOutput(template) | ||
return mw.getCurrentFrame():expandTemplate{ | return mw.getCurrentFrame():expandTemplate{ | ||
title = | title = template, | ||
args = self.invokeArgs | args = self.invokeArgs | ||
} | } | ||
Line 758: | Line 472: | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
-- | -- Exports | ||
------------------------------------------------------------------------------- | ------------------------------------------------------------------------------- | ||
local | local p = {} | ||
function | function p.table(args, cfg) | ||
cfg = cfg or mw.loadData(DATA_MODULE) | cfg = cfg or mw.loadData(DATA_MODULE) | ||
Line 795: | Line 506: | ||
end | end | ||
function | function p.nowiki(args, cfg) | ||
cfg = cfg or mw.loadData(DATA_MODULE) | cfg = cfg or mw.loadData(DATA_MODULE) | ||
local invocationObj = NowikiInvocation.new(args.code, cfg) | |||
local invocationObj = NowikiInvocation.new(code, cfg) | |||
args.code = nil | args.code = nil | ||
-- Assume we want to see the code as we already passed it in. | -- Assume we want to see the code as we already passed it in. | ||
args.showcode = args.showcode or true | args.showcode = args.showcode or true | ||
Line 807: | Line 516: | ||
return tostring(testCaseObj) | return tostring(testCaseObj) | ||
end | end | ||
function p.main(frame, cfg) | function p.main(frame, cfg) | ||
Line 847: | Line 550: | ||
end | end | ||
return | return p[func](args, cfg) | ||
end | end | ||