Module:Template test case: Difference between revisions

    (fix error message for missing template parameters and remove dependency on Module:TableTools)
    (add the option defaults before we compress the table to fix the case where the first numbered argument is higher than 2)
    Line 155: Line 155:
    obj.cfg = cfg
    obj.cfg = cfg


    -- Separate numbered and named options.
    -- Separate general options from template options. Template options are
    -- numbered, whereas general options are not.
    local generalOptions, templateOptions = {}, {}
    local generalOptions, templateOptions = {}, {}
    do
    do
    -- Separate numbered and named options
    local optionNum = {} -- a unique key for option numbers inside templateOptions
    local optionNum = {} -- a unique key for option numbers inside templateOptions
    local rawTemplateOptions = {}
    local rawTemplateOptions = {}
    Line 175: Line 175:
    end
    end
    end
    end
    -- Add default template options
    rawTemplateOptions[1] = rawTemplateOptions[1] or {}
    rawTemplateOptions[2] = rawTemplateOptions[2] or {}
    if rawTemplateOptions[1].template and not rawTemplateOptions[2].template then
    rawTemplateOptions[2].template = rawTemplateOptions[1].template ..
    '/' .. obj.cfg.sandboxSubpage
    end
    if not rawTemplateOptions[1].template then
    rawTemplateOptions[1].title = mw.title.getCurrentTitle().basePageTitle
    end
    if not rawTemplateOptions[2].template then
    rawTemplateOptions[2].title = rawTemplateOptions[1].title:subPageTitle(
    obj.cfg.sandboxSubpage
    )
    end
    -- Remove gaps in the numbered options
    -- Remove gaps in the numbered options
    local nums = {}
    local nums = {}
    Line 184: Line 201:
    templateOptions[i] = rawTemplateOptions[num]
    templateOptions[i] = rawTemplateOptions[num]
    end
    end
    -- Check that there are no missing template options.
    -- Check that there are no missing template options.
    for i = 3, #templateOptions do -- Defaults are added later for 1 and 2
    for i = 3, #templateOptions do -- Defaults have already been added for 1 and 2.
    local t = templateOptions[i]
    local t = templateOptions[i]
    if not t.template then
    if not t.template then
    Line 201: Line 219:
    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[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
    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


    -- Make the template objects
    -- Make the template objects