Module:Template test case: Difference between revisions

fix error message for missing template parameters and remove dependency on Module:TableTools
m (fix variable name)
(fix error message for missing template parameters and remove dependency on Module:TableTools)
Line 3:
-- Load required modules
local yesno = require('Module:Yesno')
local mTableTools = require('Module:TableTools')
 
-- Set constants
Line 156 ⟶ 155:
obj.cfg = cfg
 
-- ValidateSeparate numbered and named options.
local generalOptions, templateOptions = {}, {}
do
-- Separate numbered and named options
local highestNum = 0
local optionNum = {} -- a unique key for option numbers inside templateOptions
for k in pairs(options) do
local rawTemplateOptions = {}
for k, v in pairs(options) do
local prefix, num
if type(k) == 'string' then
localprefix, num = k:match('^(.-)([1-9][0-9]*)$')
end
if prefix then
num = tonumber(num)
rawTemplateOptions[num] = rawTemplateOptions[num] or {}
if num and num > highestNum then
rawTemplateOptions[num][prefix] = v
highestNum = num
rawTemplateOptions[num][optionNum] = num -- record for use in error messages
end
else
generalOptions[k] = v
end
end
-- Remove gaps in the numbered options
for i = 3, highestNum do
local highestNumnums = 0{}
if not options['template' .. i] then
for num in pairs(rawTemplateOptions) do
nums[#nums + 1] = num
end
table.sort(nums)
for i, =num 3,in highestNumipairs(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
obj.options local t = templateOptions.other or {}[i]
if not options['t.template' .. i] then
local num = t[optionNum]
error(obj:message(
'missing-template-option-error',
inum, inum
), 2)
end
Line 178 ⟶ 197:
end
 
-- SeparateSet general options from options for specific templates
obj.optionsgeneralOptions.showcode = yesno(obj.optionsgeneralOptions.showcode)
local templateOptions = mTableTools.numData(options, true)
obj.optionsgeneralOptions.collapsible = yesno(obj.optionsgeneralOptions.collapsible)
obj.options = templateOptions.other or {}
obj.options = generalOptions
 
-- Normalize boolean options
obj.options.showcode = yesno(obj.options.showcode)
obj.options.collapsible = yesno(obj.options.collapsible)
 
-- Add default template options
Line 190 ⟶ 206:
templateOptions[2] = templateOptions[2] or {}
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