Module:ScribuntoUnit and Module:ScribuntoUnit/sandbox: Difference between pages

(Difference between pages)
Jump to navigation Jump to search
Page 1
Page 2
>Vinifera7
No edit summary
 
>Vinifera7
mNo edit summary
 
Line 7: Line 7:
-- The cfg table contains all localisable strings and configuration, to make it
-- The cfg table contains all localisable strings and configuration, to make it
-- easier to port this module to another wiki.
-- easier to port this module to another wiki.
local cfg = mw.loadData('Module:ScribuntoUnit/config')
local cfg = mw.loadData('Module:ScribuntoUnit/config/sandbox')


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Line 127: Line 127:
--  
--  
function ScribuntoUnit:assertStringContains(pattern, s, plain, message)
function ScribuntoUnit:assertStringContains(pattern, s, plain, message)
if type(pattern) ~= 'string' then
    if type(pattern) ~= 'string' then
DebugHelper.raise({
        DebugHelper.raise({
ScribuntoUnit = true,
            ScribuntoUnit = true,
text = mw.ustring.format("Pattern type error (expected string, got %s)", type(pattern)),
            text = mw.ustring.format("Pattern type error (expected string, got %s)", type(pattern)),
message = message
            message = message
}, 2)
        }, 2)
end
    end
if type(s) ~= 'string' then
    if type(s) ~= 'string' then
DebugHelper.raise({
        DebugHelper.raise({
ScribuntoUnit = true,
            ScribuntoUnit = true,
text = mw.ustring.format("String type error (expected string, got %s)", type(s)),
            text = mw.ustring.format("String type error (expected string, got %s)", type(s)),
message = message
            message = message
}, 2)
        }, 2)
end
    end
if not mw.ustring.find(s, pattern, nil, plain) then
    if not mw.ustring.find(s, pattern, nil, plain) then
DebugHelper.raise({
        DebugHelper.raise({
ScribuntoUnit = true,
            ScribuntoUnit = true,
text = mw.ustring.format('Failed to find %s "%s" in string "%s"', plain and "plain string" or "pattern", pattern, s),
            text = mw.ustring.format('Failed to find %s "%s" in string "%s"', plain and "plain string" or "pattern", pattern, s),
message = message
            message = message
}, 2)
        }, 2)
end
    end
end
end


Line 156: Line 156:
--  
--  
function ScribuntoUnit:assertNotStringContains(pattern, s, plain, message)
function ScribuntoUnit:assertNotStringContains(pattern, s, plain, message)
if type(pattern) ~= 'string' then
    if type(pattern) ~= 'string' then
DebugHelper.raise({
        DebugHelper.raise({
ScribuntoUnit = true,
            ScribuntoUnit = true,
text = mw.ustring.format("Pattern type error (expected string, got %s)", type(pattern)),
            text = mw.ustring.format("Pattern type error (expected string, got %s)", type(pattern)),
message = message
            message = message
}, 2)
        }, 2)
end
    end
if type(s) ~= 'string' then
    if type(s) ~= 'string' then
DebugHelper.raise({
        DebugHelper.raise({
ScribuntoUnit = true,
            ScribuntoUnit = true,
text = mw.ustring.format("String type error (expected string, got %s)", type(s)),
            text = mw.ustring.format("String type error (expected string, got %s)", type(s)),
message = message
            message = message
}, 2)
        }, 2)
end
    end
local i, j = mw.ustring.find(s, pattern, nil, plain)
    local i, j = mw.ustring.find(s, pattern, nil, plain)
if i then
    if i then
local match = mw.ustring.sub(s, i, j)
        local match = mw.ustring.sub(s, i, j)
DebugHelper.raise({
        DebugHelper.raise({
ScribuntoUnit = true,
            ScribuntoUnit = true,
text = mw.ustring.format('Found match "%s" for %s "%s"', match, plain and "plain string" or "pattern", pattern),
            text = mw.ustring.format('Found match "%s" for %s "%s"', match, plain and "plain string" or "pattern", pattern),
message = message
            message = message
}, 2)
        }, 2)
end
    end
end
end


Line 188: Line 188:
function ScribuntoUnit:assertEquals(expected, actual, message)
function ScribuntoUnit:assertEquals(expected, actual, message)


if type(expected) == 'number' and type(actual) == 'number' then
    if type(expected) == 'number' and type(actual) == 'number' then
         self:assertWithinDelta(expected, actual, 1e-8, message)
         self:assertWithinDelta(expected, actual, 1e-8, message)


elseif expected ~= actual then
    elseif expected ~= actual then
         DebugHelper.raise({
         DebugHelper.raise({
             ScribuntoUnit = true,  
             ScribuntoUnit = true,  
Line 335: Line 335:
         }, 2)
         }, 2)
     end
     end
-- For strings, strip the line number added to the error message
    -- For strings, strip the line number added to the error message
     actualMessage = type(actualMessage) == 'string'  
     actualMessage = type(actualMessage) == 'string'  
    and string.match(actualMessage, 'Module:[^:]*:[0-9]*: (.*)')
        and string.match(actualMessage, 'Module:[^:]*:[0-9]*: (.*)')
    or actualMessage
        or actualMessage
     local messagesMatch = DebugHelper.deepCompare(expectedMessage, actualMessage)
     local messagesMatch = DebugHelper.deepCompare(expectedMessage, actualMessage)
     if expectedMessage and not messagesMatch then
     if expectedMessage and not messagesMatch then
Line 408: Line 408:
function ScribuntoUnit:runSuite(suite, frame)
function ScribuntoUnit:runSuite(suite, frame)
     self:init(frame)
     self:init(frame)
local names = {}
    local names = {}
     for name in pairs(suite) do
     for name in pairs(suite) do
         if name:find('^test') then
         if name:find('^test') then
table.insert(names, name)
            table.insert(names, name)
         end
         end
     end
     end
table.sort(names) -- Put tests in alphabetical order.
    table.sort(names) -- Put tests in alphabetical order.
for i, name in ipairs(names) do
    for i, name in ipairs(names) do
local func = suite[name]
        local func = suite[name]
self:runTest(suite, name, func)
        self:runTest(suite, name, func)
end
    end
     return {
     return {
         successCount = self.successCount,
         successCount = self.successCount,
Line 483: Line 483:
     local successIcon, failIcon = self.frame:preprocess(cfg.successIndicator), self.frame:preprocess(cfg.failureIndicator)
     local successIcon, failIcon = self.frame:preprocess(cfg.successIndicator), self.frame:preprocess(cfg.failureIndicator)
     local text = ''
     local text = ''
if testData.failureCount > 0 then
    if testData.failureCount > 0 then
local msg = mw.message.newRawMessage(cfg.failureSummary, testData.failureCount):plain()
        local msg = mw.message.newRawMessage(cfg.failureSummary, testData.failureCount):plain()
msg = self.frame:preprocess(msg)
        msg = self.frame:preprocess(msg)
if cfg.failureCategory then
        if cfg.failureCategory then
msg = cfg.failureCategory .. msg
            msg = cfg.failureCategory .. msg
end
        end
text = text .. failIcon .. ' ' .. msg .. '\n'
        text = text .. failIcon .. ' ' .. msg .. '\n'
else
    else
text = text .. successIcon .. ' ' .. cfg.successSummary .. '\n'
        text = text .. successIcon .. ' ' .. cfg.successSummary .. '\n'
end
    end
     text = text .. '{| class="wikitable scribunto-test-table"\n'
     local tbl = mw.html.create('table')
     text = text .. '!\n! ' .. cfg.nameString .. '\n! ' .. cfg.expectedString .. '\n! ' .. cfg.actualString .. '\n'
    tbl:addClass(cfg.tableClass)
    local tr = tbl:tag('tr')
     local headers = {'', cfg.nameString, cfg.expectedString, cfg.actualString}
    for _, header in ipairs(headers) do
        tr:tag('th')
            :wikitext(header)
    end
     for _, result in ipairs(testData.results) do
     for _, result in ipairs(testData.results) do
         text = text .. '|-\n'
         tr = tbl:tag('tr')
         if result.error then
         if result.error then
             text = text .. '| ' .. failIcon .. '\n| '
             tr:tag('td')
                :wikitext(failIcon)
             if (result.expected and result.actual) then
             if (result.expected and result.actual) then
            local name = result.name
                local name = result.name
            if result.testname then
                if result.testname then
            name = name .. ' / ' .. result.testname
                    name = name .. ' / ' .. result.testname
            end
                end
                 text = text .. name .. '\n| ' .. mw.text.nowiki(tostring(result.expected)) .. '\n| ' .. mw.text.nowiki(tostring(result.actual)) .. '\n'
                 tr:tag('td')
                    :wikitext(name)
                tr:tag('td')
                    :wikitext(mw.text.nowiki(tostring(result.expected)))
                tr:tag('td')
                    :wikitext(mw.text.nowiki(tostring(result.actual)))
             else
             else
                 text = text .. result.name .. '\n| ' .. ' colspan="2" | ' .. mw.text.nowiki(result.message) .. '\n'
                 tr:tag('td')
                    :wikitext(result.name)
                tr:tag('td')
                    :attr('colspan', 2)
                    :wikitext(mw.text.nowiki(tostring(result.message)))
             end
             end
         else
         else
             text = text .. '| ' .. successIcon .. '\n| ' .. result.name .. '\n|\n|\n'
             tr:tag('td')
                :wikitext(successIcon)
            tr:tag('td')
                :wikitext(result.name)
            tr:tag('td')
            tr:tag('td')
         end
         end
     end
     end
     text = text .. '|}\n'
     text = text .. tostring(tbl)
     return text
     return text
end
end


return ScribuntoUnit
return ScribuntoUnit