Module:ScribuntoUnit and Module:ScribuntoUnit/sandbox: Difference between pages
(Difference between pages)
>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 | |||
DebugHelper.raise({ | |||
ScribuntoUnit = true, | |||
text = mw.ustring.format("Pattern type error (expected string, got %s)", type(pattern)), | |||
message = message | |||
}, 2) | |||
end | |||
if type(s) ~= 'string' then | |||
DebugHelper.raise({ | |||
ScribuntoUnit = true, | |||
text = mw.ustring.format("String type error (expected string, got %s)", type(s)), | |||
message = message | |||
}, 2) | |||
end | |||
if not mw.ustring.find(s, pattern, nil, plain) then | |||
DebugHelper.raise({ | |||
ScribuntoUnit = true, | |||
text = mw.ustring.format('Failed to find %s "%s" in string "%s"', plain and "plain string" or "pattern", pattern, s), | |||
message = message | |||
}, 2) | |||
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 | |||
DebugHelper.raise({ | |||
ScribuntoUnit = true, | |||
text = mw.ustring.format("Pattern type error (expected string, got %s)", type(pattern)), | |||
message = message | |||
}, 2) | |||
end | |||
if type(s) ~= 'string' then | |||
DebugHelper.raise({ | |||
ScribuntoUnit = true, | |||
text = mw.ustring.format("String type error (expected string, got %s)", type(s)), | |||
message = message | |||
}, 2) | |||
end | |||
local i, j = mw.ustring.find(s, pattern, nil, plain) | |||
if i then | |||
local match = mw.ustring.sub(s, i, j) | |||
DebugHelper.raise({ | |||
ScribuntoUnit = true, | |||
text = mw.ustring.format('Found match "%s" for %s "%s"', match, plain and "plain string" or "pattern", pattern), | |||
message = message | |||
}, 2) | |||
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 | |||
self:assertWithinDelta(expected, actual, 1e-8, message) | self:assertWithinDelta(expected, actual, 1e-8, message) | ||
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 | |||
actualMessage = type(actualMessage) == 'string' | actualMessage = type(actualMessage) == 'string' | ||
and string.match(actualMessage, 'Module:[^:]*:[0-9]*: (.*)') | |||
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 = {} | |||
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) | |||
end | end | ||
end | end | ||
table.sort(names) -- Put tests in alphabetical order. | |||
for i, name in ipairs(names) do | |||
local func = suite[name] | |||
self:runTest(suite, name, func) | |||
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 | |||
local msg = mw.message.newRawMessage(cfg.failureSummary, testData.failureCount):plain() | |||
msg = self.frame:preprocess(msg) | |||
if cfg.failureCategory then | |||
msg = cfg.failureCategory .. msg | |||
end | |||
text = text .. failIcon .. ' ' .. msg .. '\n' | |||
else | |||
text = text .. successIcon .. ' ' .. cfg.successSummary .. '\n' | |||
end | |||
local tbl = mw.html.create('table') | |||
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 | ||
tr = tbl:tag('tr') | |||
if result.error then | if result.error then | ||
tr:tag('td') | |||
:wikitext(failIcon) | |||
if (result.expected and result.actual) then | if (result.expected and result.actual) then | ||
local name = result.name | |||
if result.testname then | |||
name = name .. ' / ' .. result.testname | |||
end | |||
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 | ||
tr:tag('td') | |||
:wikitext(result.name) | |||
tr:tag('td') | |||
:attr('colspan', 2) | |||
:wikitext(mw.text.nowiki(tostring(result.message))) | |||
end | end | ||
else | else | ||
tr:tag('td') | |||
:wikitext(successIcon) | |||
tr:tag('td') | |||
:wikitext(result.name) | |||
tr:tag('td') | |||
tr:tag('td') | |||
end | end | ||
end | end | ||
text = text .. | text = text .. tostring(tbl) | ||
return text | return text | ||
end | end | ||
return ScribuntoUnit | return ScribuntoUnit |