Jump to content

Module:Convert/tester

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Johnuniq (talk | contribs) at 09:45, 24 September 2013 (another attempt to escape stuff in table cell while keeping piped link). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- Test the output from a template that invokes [[Module:Convert]].
-- The result is compared with fixed text.
-- The expected text must be in a single line, but can include "\n" (two characters)
-- to indicate that a newline is expected.
-- Tests are run by setting p.tests then executing run_tests.
-- Adapted from [[Module:ConvertTestcase]].

local function collection()
    -- Return a table to hold lines of text.
    return {
        n = 0,
        add = function (self, s)
            self.n = self.n + 1
            self[self.n] = s
        end,
        join = function (self, sep)
            return table.concat(self, sep or '\n')
        end,
    }
end

local function strip(text)
    -- Return text with no leading/trailing whitespace.
    return text:match("^%s*(.-)%s*$")
end

local function status_box(stats, expected, actual)
    local label, bgcolor, align
    if expected == '' then
        stats.ignored = stats.ignored + 1
        return actual, ''
    elseif expected == actual then
        stats.pass = stats.pass + 1
        actual = ''
        align = 'center'
        bgcolor = 'green'
        label = 'Pass'
    else
        stats.fail = stats.fail + 1
        align = 'center'
        bgcolor = 'red'
        label = 'Fail'
    end
    return actual, 'style="text-align:' .. align .. ';color:white;background:' .. bgcolor .. ';" | ' .. label
end

local function status_text(stats)
    local bgcolor, ignored_text, msg
    if stats.fail == 0 then
        if stats.pass == 0 then
            bgcolor = 'salmon'
            msg = 'No tests performed'
        else
            bgcolor = 'green'
            msg = string.format('All %d tests passed', stats.pass)
        end
    else
        bgcolor = 'darkred'
        msg = string.format('%d test%s failed', stats.fail, stats.fail == 1 and '' or 's')
    end
    if stats.ignored == 0 then
        ignored_text = ''
    else
        bgcolor = 'salmon'
        ignored_text = string.format(', %d test%s ignored because expected text is blank', stats.ignored, stats.ignored == 1 and '' or 's')
    end
    return '<span style="font-size:120%;color:white;background-color:' .. bgcolor .. ';">' .. msg .. ignored_text .. '.</span>'
end

local function run_template(frame, template)
    local title, argstr = template:match('^{{%s*(.-)%s*|(.*)}}$')
    if title == nil or title == '' or argstr == '' then
        return '(invalid template)'
    end
    local args = {}
    for item in string.gmatch(argstr .. '|', '(.-)|') do
        local p = item:find('=', 1, true)
        if p then
            args[item:sub(1, p-1)] = item:sub(p+1)
        else
            table.insert(args, item)
        end
    end
    return frame:expandTemplate({ title = title, args = args })
end

local p = {}

function p.run_tests(frame)
    if type(p.tests) ~= 'string' then
        return 'Error: Must first set p.tests to a string with tests'
    end
    local function collapse_multiline(text)
        return text:gsub('\n', '\\n')
    end
    local function safe_cell(text, multiline)
        -- For testing {{convert}}, want wikitext like '[[kilogram|kg]]' to be
        -- unchanged so the link works and so the rendered text is short.
        text = text:gsub('(%[%[[^%[%]]-)|(.-%]%])', '%1\0%2')  -- replace pipe in piped link with a zero byte
        text = text:gsub('{', '&#123;'):gsub('|', '&#124;'):gsub('%z', '|')  -- restore pipe in piped link
        if multiline then
            text = text:gsub('\\n', '<br />')
        end
        return text
    end
    local stats = { pass = 0, fail = 0, ignored = 0 }
    local result = collection()
    result:add('{| class="wikitable"')
    result:add('! Template !! Expected !! Actual, if different !! Status')
    for template, expected in p.tests:gmatch('({{.-}})(.-)\n') do
        expected = strip(expected)
        local actual = collapse_multiline(run_template(frame, template))
        local actual, sbox = status_box(stats, expected, actual)
        result:add('|-')
        result:add('| ' .. safe_cell(template))
        result:add('| ' .. safe_cell(expected, true))
        result:add('| ' .. safe_cell(actual, true))
        result:add('| ' .. sbox)
    end
    result:add('|}')
    return status_text(stats) .. '\n\n' .. result:join()
end

return p