Module:Template test case: Difference between revisions

    (comment tweak and add a todo item)
    (if the first template is specified but the second is not, make the second the sandbox of the first rather than the sandbox of the base page; simplify the title-getting code in the process)
    Line 15: Line 15:
    Template.__index = Template
    Template.__index = Template


    function Template.new(invocationObj, options, titleCallback)
    function Template.new(invocationObj, options)
    local obj = setmetatable({}, Template)
    local obj = setmetatable({}, Template)


    Line 24: Line 24:
    obj.invocation = invocationObj
    obj.invocation = invocationObj


    -- Validate template
    -- Validate input
    if not obj.template then
    if not obj.template and not obj.title then
    if titleCallback then
    error('no template or title specified', 2)
    obj.title = titleCallback()
    else
    error('no template or title callback specified', 2)
    end
    end
    end


    Line 127: Line 123:
    local templateOptions = mTableTools.numData(options, true)
    local templateOptions = mTableTools.numData(options, true)
    obj.options = templateOptions.other or {}
    obj.options = templateOptions.other or {}
    -- 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 .. '/sandbox'
    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('sandbox')
    end


    -- Make the template objects
    -- Make the template objects
    obj.templates = {}
    obj.templates = {}
    local function templateTitleCallback()
    for i, t in ipairs(templateOptions) do
    return mw.title.getCurrentTitle().basePageTitle
    table.insert(obj.templates, Template.new(invocationObj, t))
    end
    obj.templates[1] = Template.new(
    invocationObj,
    templateOptions[1],
    templateTitleCallback
    )
    -- @TODO: Use the sandbox of the first template if it is specified,
    -- rather than the sandbox of the base page.
    obj.templates[2] = Template.new(
    invocationObj,
    templateOptions[2],
    function ()
    return templateTitleCallback():subPageTitle('sandbox')
    end
    )
    for i = 3, #templateOptions do
    table.insert(obj.templates, Template.new(
    invocationObj,
    templateOptions[i]
    ))
    end
    end