Module:Template test case: Difference between revisions

    (make a start on the exports)
    (add nowiki exports and finish the NowikiInvocation class)
    Line 4: Line 4:
    local libraryUtil = require('libraryUtil')
    local libraryUtil = require('libraryUtil')
    local checkType = libraryUtil.checkType
    local checkType = libraryUtil.checkType
    local TEMPLATE_NAME_MAGIC_WORD = '<TEMPLATE_NAME>'
    local TEMPLATE_NAME_MAGIC_WORD_ESCAPED = TEMPLATE_NAME_MAGIC_WORD:gsub('%p', '%%%0')


    -------------------------------------------------------------------------------
    -------------------------------------------------------------------------------
    Line 73: Line 76:
    function Invocation:getOptions()
    function Invocation:getOptions()
    return self._options
    return self._options
    end
    function Invocation:setTemplateOptions(t)
    self._templateOptions = t
    end
    function Invocation:getTemplateOptions()
    return self._templateOptions
    end
    end


    Line 95: Line 90:
    setmetatable(obj, NowikiInvocation)
    setmetatable(obj, NowikiInvocation)


    obj.invocation = mw.text.unstrip(args.invocation)
    args.invocation = nil
    local options = {}
    local options = {}
    for k, v in pairs(args) do
    options[k] = v
    end
    obj.options = options


    return obj
    return obj
    Line 101: Line 102:


    function NowikiInvocation:getInvocation(template)
    function NowikiInvocation:getInvocation(template)
    template = template:gsub('%%', '%%%%') -- Escape a % with %%
    local invocation, count = self.invocation:gsub(
    TEMPLATE_NAME_MAGIC_WORD_ESCAPED,
    template
    )
    if count < 1 then
    error(string.format(
    "the template invocation must include '%s' in place " ..
    "of the template name",
    TEMPLATE_NAME_MAGIC_WORD
    ))
    end
    return invocation
    end
    end


    function NowikiInvocation:getOutput(template)
    function NowikiInvocation:getOutput(template)
    local invocation = self:getInvocation(template)
    return mw.getCurrentFrame():preprocess(invocation)
    end
    end


    Line 187: Line 203:
    -- Exports
    -- Exports
    -------------------------------------------------------------------------------
    -------------------------------------------------------------------------------
    -- Table-based exports


    local function getTableArgs(frame, wrappers)
    local function getTableArgs(frame, wrappers)
    Line 226: Line 244:
    return p._table(args)
    return p._table(args)
    end
    end
    -- Nowiki-based exports
    function p._nowiki(args)
    local invocation = NowikiInvocation.new(args)
    return invocation
    end
    function p.nowiki(frame)
    local args = require('Module:Arguments').getArgs(frame, {
    wrappers = 'Template:Test case'
    })
    return p._nowiki(args)
    end
    -- Exports for testing


    function p._exportClasses()
    function p._exportClasses()