Module:Template test case: Difference between revisions

    (pass the config through to NowikiInvocation from TableInvocation:getInvocation)
    (move Template.outputEquals to the TestCase class)
    Line 22: Line 22:
    getOutput = true
    getOutput = true
    }
    }
    function Template.outputEquals(...)
    -- This function accepts 2 or more template objects and compares their
    -- output. If all the outputs are equal, it returns true, and if any of them
    -- are different, it returns false.
    local n = select('#', ...)
    if n < 2 then
    error('Template.outputEquals requires at least two arguments', 2)
    end
    local function normaliseOutput(obj)
    local out = obj:getOutput()
    -- Remove the random parts from strip markers (see [[Help:Strip markers]])
    out = out:gsub('(%cUNIQ).-(QINU%c)', '%1%2')
    return out
    end
    local prevOutput = normaliseOutput(select(1, ...))
    for i = 2, n do
    local output = normaliseOutput(select(i, ...))
    if output ~= prevOutput then
    return false
    end
    prevOutput = output
    end
    return true
    end


    function Template.new(invocationObj, options)
    function Template.new(invocationObj, options)
    Line 216: Line 191:
    end
    end
    return output
    return output
    end
    function TestCase:templateOutputIsEqual()
    -- Returns a boolean showing whether all of the template outputs are equal.
    -- The random parts of strip markers (see [[Help:Strip markers]]) are
    -- removed before comparison. This means a strip marker can contain anything
    -- and still be treated as equal, but it solves the problem of otherwise
    -- identical wikitext not returning as exactly equal.
    local function normaliseOutput(obj)
    local out = obj:getOutput()
    -- Remove the random parts from strip markers.
    out = out:gsub('(%cUNIQ).-(QINU%c)', '%1%2')
    return out
    end
    local firstOutput = normaliseOutput(self.templates[1])
    for i = 2, #self.templates do
    local output = normaliseOutput(self.templates[i])
    if output ~= firstOutput then
    return false
    end
    end
    return true
    end
    end


    function TestCase:makeCollapsible(s)
    function TestCase:makeCollapsible(s)
    local isEqual = Template.outputEquals(unpack(self.templates))
    local isEqual = self:templateOutputIsEqual()
    local root = mw.html.create('table')
    local root = mw.html.create('table')
    root
    root