Module:Template test case: Difference between revisions

    m (fix variable name)
    (fix error message for missing template parameters and remove dependency on Module:TableTools)
    Line 3: Line 3:
    -- Load required modules
    -- Load required modules
    local yesno = require('Module:Yesno')
    local yesno = require('Module:Yesno')
    local mTableTools = require('Module:TableTools')


    -- Set constants
    -- Set constants
    Line 156: Line 155:
    obj.cfg = cfg
    obj.cfg = cfg


    -- Validate options
    -- Separate numbered and named options.
    local generalOptions, templateOptions = {}, {}
    do
    do
    local highestNum = 0
    -- Separate numbered and named options
    for k in pairs(options) do
    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
    if type(k) == 'string' then
    local num = k:match('([1-9][0-9]*)$')
    prefix, num = k:match('^(.-)([1-9][0-9]*)$')
    end
    if prefix then
    num = tonumber(num)
    num = tonumber(num)
    if num and num > highestNum then
    rawTemplateOptions[num] = rawTemplateOptions[num] or {}
    highestNum = num
    rawTemplateOptions[num][prefix] = v
    end
    rawTemplateOptions[num][optionNum] = num -- record for use in error messages
    else
    generalOptions[k] = v
    end
    end
    end
    end
    for i = 3, highestNum do
    -- Remove gaps in the numbered options
    if not options['template' .. i] then
    local nums = {}
    for num in pairs(rawTemplateOptions) do
    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(
    error(obj:message(
    'missing-template-option-error',
    'missing-template-option-error',
    i, i
    num, num
    ), 2)
    ), 2)
    end
    end
    Line 178: Line 197:
    end
    end


    -- Separate general options from options for specific templates
    -- Set general options
    local templateOptions = mTableTools.numData(options, true)
    generalOptions.showcode = yesno(generalOptions.showcode)
    obj.options = templateOptions.other or {}
    generalOptions.collapsible = yesno(generalOptions.collapsible)
     
    obj.options = generalOptions
    -- Normalize boolean options
    obj.options.showcode = yesno(obj.options.showcode)
    obj.options.collapsible = yesno(obj.options.collapsible)


    -- Add default template options
    -- Add default template options
    Line 190: Line 206:
    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 ..
    .. '/' .. obj.cfg.sandboxSubpage
    '/' .. obj.cfg.sandboxSubpage
    end
    end
    if not templateOptions[1].template then
    if not templateOptions[1].template then