Modulo:ScribuntoUnit: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Moroboshi (discussione | contributi)
Nuova pagina: ------------------------------------------------------------------------------- -- Unit tests for Scribunto. -----------------------------------------------------------...
 
Update from master using Synchronizer #synchronizer
 
(Una versione intermedia di un altro utente non mostrate)
Riga 2:
-- Unit tests for Scribunto.
-------------------------------------------------------------------------------
require('strict')
 
local DebugHelper = {}
local ScribuntoUnit = {}
 
-- The cfg table contains all localisable strings and configuration, to make it
-- easier to port this module to another wiki.
local cfg = mw.loadData('Module:ScribuntoUnit/config')
 
-------------------------------------------------------------------------------
-- Concatenates keys and values, ideal for displaying a template or parser function argument table.
-- @param keySeparator glue between key and value (defaults to " = ")
-- @param separator glue between different key-value pairs (defaults to ", ")
Line 81 ⟶ 87:
level = (level or 1) + 1
details.trace = debug.traceback('', level)
details.source = mwstring.text.splitmatch(details.trace, '^%s*stack traceback:%s*(%S*: )')[5]
-- this would be more robust but does not work
-- local match = string.match(details.trace, '^%s*stack traceback:%s*(%S*): ')
-- details.source = match and match[1] or ''
 
-- setmetatable(details, {
-- __tostring: function() return details.text end
-- })
 
error(details, level)
end
Line 294 ⟶ 298:
expected = processed2,
expectedRaw = text2,
message = message,
}, 2)
end
end
 
-------------------------------------------------------------------------------
-- Checks that a parser function gives the expected output.
-- @param message optional description of the test
-- @example assertParserFunctionEquals("Hello world", "msg:concat", {"Hello", " world"})
function ScribuntoUnit:assertParserFunctionEquals(expected, pfname, args, message)
local frame = self.frame
local actual = frame:callParserFunction{ name = pfname, args = args}
if expected ~= actual then
DebugHelper.raise({
ScribuntoUnit = true,
text = string.format("Failed to assert that %s with args %s equals expected %s after preprocessing",
DebugHelper.concatWithKeys(args), pfname, expected),
actual = actual,
actualRaw = pfname,
expected = expected,
message = message,
}, 2)
Line 302 ⟶ 326:
-- Checks that a template gives the expected output.
-- @param message optional description of the test
-- @example assertTemplateEquals("Hello world", "concat", {"Hello", " world"})
function ScribuntoUnit:assertTemplateEquals(expected, template, args, message)
local frame = self.frame
local actual = frame:expandTemplate({ title = template, args) = args}
if expected ~= actual then
DebugHelper.raise({
Line 317 ⟶ 341:
}, 2)
end
end
 
-------------------------------------------------------------------------------
-- Checks whether a function throws an error
-- @param fn the function to test
-- @param expectedMessage optional the expected error message
-- @param message optional description of the test
function ScribuntoUnit:assertThrows(fn, expectedMessage, message)
local succeeded, actualMessage = pcall(fn)
if succeeded then
DebugHelper.raise({
ScribuntoUnit = true,
text = 'Expected exception but none was thrown',
message = message,
}, 2)
end
-- For strings, strip the line number added to the error message
actualMessage = type(actualMessage) == 'string'
and string.match(actualMessage, 'Module:[^:]*:[0-9]*: (.*)')
or actualMessage
local messagesMatch = DebugHelper.deepCompare(expectedMessage, actualMessage)
if expectedMessage and not messagesMatch then
DebugHelper.raise({
ScribuntoUnit = true,
expected = expectedMessage,
actual = actualMessage,
text = string.format('Expected exception with message %s, but got message %s',
tostring(expectedMessage), tostring(actualMessage)
),
message = message
}, 2)
end
end
 
-------------------------------------------------------------------------------
-- Checks whether a function doesn't throw an error
-- @param fn the function to test
-- @param message optional description of the test
function ScribuntoUnit:assertDoesNotThrow(fn, message)
local succeeded, actualMessage = pcall(fn)
if succeeded then
return
end
-- For strings, strip the line number added to the error message
actualMessage = type(actualMessage) == 'string'
and string.match(actualMessage, 'Module:[^:]*:[0-9]*: (.*)')
or actualMessage
DebugHelper.raise({
ScribuntoUnit = true,
actual = actualMessage,
text = string.format('Expected no exception, but got exception with message %s',
tostring(actualMessage)
),
message = message
}, 2)
end
 
Line 334 ⟶ 413:
--
function ScribuntoUnit:init(frame)
self.frame = frame or mw.getCurrentFrame()
self.successCount = 0
self.failureCount = 0
Line 365 ⟶ 444:
end
message = message .. details.text
table.insert(self.results, {name = name, error = true, message = message, expected = details.expected, actual = details.actual, testname = details.message})
end
end
Line 400 ⟶ 479:
function ScribuntoUnit:run(suite, frame)
local testData = self:runSuite(suite, frame)
if frame and frame.args then
return self:displayResults(testData, frame.args.displayMode or 'table')
else
Line 437 ⟶ 516:
end
end
 
-- TODO l10n
 
function ScribuntoUnit:displayResultsAsShort(testData)
local text = string.format('success: %d, error: %d, skipped: %d'cfg.shortResultsFormat, testData.successCount, testData.failureCount, testData.skipCount)
if testData.failureCount > 0 then
text = '<span class="error">' .. text .. '</span>'
Line 449 ⟶ 526:
 
function ScribuntoUnit:displayResultsAsTable(testData)
local successIcon, failIcon = self.frame:preprocess('{{tick}}'cfg.successIndicator), self.frame:preprocess('{{cross}}'cfg.failureIndicator)
local text = ''
if testData.failureCount > 0 then
local msg = mw.message.newRawMessage(cfg.failureSummary, testData.failureCount):plain()
local msg = "'''$1 {{PLURAL:$1|test|tests}} failed'''."
msg = mw.message.newRawMessage(msg, 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'
local msg = "'''All tests passed'''."
text = text .. successIcon .. ' ' .. msg .. '\n'
end
text = text .. '{| class="wikitable scribunto-test-table"\n'
text = text .. '!\n! Name' .. cfg.nameString .. '\n! Expected' .. cfg.expectedString .. '\n! Actual' .. cfg.actualString .. '\n'
for _, result in ipairs(testData.results) do
text = text .. '|-\n'
if result.error then
text = text .. '| ' .. failIcon .. '\n| ' .. result.name .. '\n| '
if (result.expected and result.actual) then
local name = result.name
text = text .. mw.text.nowiki(tostring(result.expected)) .. '\n| ' .. mw.text.nowiki(tostring(result.actual)) .. '\n'
if result.testname then
name = name .. ' / ' .. result.testname
end
text = text .. name .. '\n| ' .. mw.text.nowiki(tostring(result.expected)) .. '\n| ' .. mw.text.nowiki(tostring(result.actual)) .. '\n'
else
text = text .. result.name .. '\n| ' .. ' colspan="2" | ' .. mw.text.nowiki(result.message) .. '\n'
end
else