Modulo:ScribuntoUnit: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Update from master using Synchronizer #synchronizer
Update from master using #Synchronizer
 
Riga 101:
function ScribuntoUnit:markTestSkipped()
DebugHelper.raise({ScribuntoUnit = true, skipped = true}, 3)
end
 
-------------------------------------------------------------------------------
-- Unconditionally fail a test
-- @param message optional description of the test
--
function ScribuntoUnit:fail(message)
DebugHelper.raise({ScribuntoUnit = true, text = "Test failed", message = message}, 2)
end
 
Riga 189 ⟶ 197:
--
function ScribuntoUnit:assertEquals(expected, actual, message)
 
if type(expected) == 'number' and type(actual) == 'number' then
self:assertWithinDelta(expected, actual, 1e-8, message)
 
elseif expected ~= actual then
DebugHelper.raise({
Riga 202 ⟶ 208:
}, 2)
end
end
 
-------------------------------------------------------------------------------
-- Checks that 'actual'an isinput withindoes 'delta'not ofhave the 'expected' value.
-- @param message optional description of the test
-- @example assertEqualsassertNotEquals(1/35, 9/3add(2,2), "9/32+2 should not be 1/35", 0.000001)
--
function ScribuntoUnit:assertNotEquals(expected, actual, message)
if type(expected) == 'number' and type(actual) == 'number' then
self:assertNotWithinDelta(expected, actual, 1e-8, message)
elseif expected == actual then
DebugHelper.raise({
ScribuntoUnit = true,
text = string.format("Failed to assert that %s does not equal expected %s", tostring(actual), tostring(expected)),
actual = actual,
expected = expected,
message = message,
end}, 2)
end
end
 
-------------------------------------------------------------------------------
-- Validates that both the expected and actual values are numbers
-- Checks that 'actual' is within 'delta' of 'expected'.
-- @param message optional description of the test
--
-- @example assertEquals(1/3, 9/3, "9/3 should be 1/3", 0.000001)
local function ScribuntoUnit:assertWithinDeltavalidateNumbers(expected, actual, delta, message)
if type(expected) ~= "number" then
DebugHelper.raise({
Riga 217 ⟶ 241:
expected = expected,
message = message,
}, 23)
end
if type(actual) ~= "number" then
Riga 226 ⟶ 250:
expected = expected,
message = message,
}, 23)
end
end
 
-------------------------------------------------------------------------------
-- Checks that 'actual' is within 'delta' of 'expected'.
-- @param message optional description of the test
-- @example assertWithinDelta(1/3, 3/9, 0.000001, "3/9 should be 1/3")
function ScribuntoUnit:assertWithinDelta(expected, actual, delta, message)
validateNumbers(expected, actual, message)
local diff = expected - actual
if diff < 0 then diff = - diff end -- instead of importing math.abs
Riga 234 ⟶ 266:
ScribuntoUnit = true,
text = string.format("Failed to assert that %f is within %f of expected %f", actual, delta, expected),
actual = actual,
expected = expected,
message = message,
}, 2)
end
end
 
-------------------------------------------------------------------------------
-- Checks that 'actual' is not within 'delta' of 'expected'.
-- @param message optional description of the test
-- @example assertNotWithinDelta(1/3, 2/3, 0.000001, "1/3 should not be 2/3")
function ScribuntoUnit:assertNotWithinDelta(expected, actual, delta, message)
validateNumbers(expected, actual, message)
local diff = expected - actual
if diff < 0 then diff = - diff end -- instead of importing math.abs
if diff <= delta then
DebugHelper.raise({
ScribuntoUnit = true,
text = string.format("Failed to assert that %f is not within %f of expected %f", actual, delta, expected),
actual = actual,
expected = expected,
Riga 404 ⟶ 455:
function ScribuntoUnit:new(o)
o = o or {}
setmetatable(o, {__index._tests = self{})
setmetatable(o, {
__index = self,
__newindex = function (t, k, v)
if type(k) == "string" and k:find('^test') and type(v) == "function" then
-- Store test functions in the order they were defined
table.insert(nameso._tests, {name = k, test = v})
else
rawset(t, k, v)
end
end
})
o.run = function(frame) return self:run(o, frame) end
return o
Riga 439 ⟶ 501:
else
self.failureCount = self.failureCount + 1
local message = details.source or ""
if details.message then
message = message .. details.message .. "\n"
Riga 453 ⟶ 515:
function ScribuntoUnit:runSuite(suite, frame)
self:init(frame)
for i, nametestDetails in ipairs(namessuite._tests) do
local names = {}
self:runTest(suite, testDetails.name, functestDetails.test)
for name in pairs(suite) do
if name:find('^test') then
table.insert(names, name)
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 {
Riga 549 ⟶ 603:
name = name .. ' / ' .. result.testname
end
text = text .. mw.text.nowiki(name) .. '\n| ' .. mw.text.nowiki(tostring(result.expected)) .. '\n| ' .. mw.text.nowiki(tostring(result.actual)) .. '\n'
else
text = text .. mw.text.nowiki(result.name) .. '\n| ' .. ' colspan="2" | ' .. mw.text.nowiki(result.message) .. '\n'
end
else
text = text .. '| ' .. successIcon .. '\n| ' .. mw.text.nowiki(result.name) .. '\n|\n|\n'
end
end