120 lines
3.7 KiB
Lua
120 lines
3.7 KiB
Lua
local ____exports = __module.env
|
|||
|
|
function ____exports.Assert(condition, message)
|
||
|
|
if not condition then
|
||
|
|
error(message or "Assertion failed", 2)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
function ____exports.AssertEqual(actual, expected, label)
|
||
|
|
if actual ~= expected then
|
||
|
|
local prefix = label ~= nil and label .. ": " or ""
|
||
|
|
error(
|
||
|
|
string.format(
|
||
|
|
"%sexpected %s, got %s",
|
||
|
|
prefix,
|
||
|
|
tostring(expected),
|
||
|
|
tostring(actual)
|
||
|
|
),
|
||
|
|
2
|
||
|
|
)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
function ____exports.AssertNotEqual(actual, unexpected, label)
|
||
|
|
if actual == unexpected then
|
||
|
|
local prefix = label ~= nil and label .. ": " or ""
|
||
|
|
error(
|
||
|
|
string.format(
|
||
|
|
"%sexpected value different from %s",
|
||
|
|
prefix,
|
||
|
|
tostring(unexpected)
|
||
|
|
),
|
||
|
|
2
|
||
|
|
)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
function ____exports.AssertNear(actual, expected, epsilon, label)
|
||
|
|
if math.abs(actual - expected) > epsilon then
|
||
|
|
local prefix = label ~= nil and label .. ": " or ""
|
||
|
|
error(
|
||
|
|
string.format(
|
||
|
|
"%sexpected %s (+/- %s), got %s",
|
||
|
|
prefix,
|
||
|
|
tostring(expected),
|
||
|
|
tostring(epsilon),
|
||
|
|
tostring(actual)
|
||
|
|
),
|
||
|
|
2
|
||
|
|
)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
function ____exports.AssertArrayEqual(actual, expected, label)
|
||
|
|
local prefix = label ~= nil and label .. ": " or ""
|
||
|
|
if #actual ~= #expected then
|
||
|
|
error(
|
||
|
|
string.format("%sarray length mismatch: expected %d, got %d", prefix, #expected, #actual),
|
||
|
|
2
|
||
|
|
)
|
||
|
|
end
|
||
|
|
do
|
||
|
|
local i = 0
|
||
|
|
while i < #expected do
|
||
|
|
if actual[i + 1] ~= expected[i + 1] then
|
||
|
|
error(
|
||
|
|
string.format(
|
||
|
|
"%sarray[%d] mismatch: expected %s, got %s",
|
||
|
|
prefix,
|
||
|
|
i,
|
||
|
|
tostring(expected[i + 1]),
|
||
|
|
tostring(actual[i + 1])
|
||
|
|
),
|
||
|
|
2
|
||
|
|
)
|
||
|
|
end
|
||
|
|
i = i + 1
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
function ____exports.RunSuites(suites)
|
||
|
|
local summary = {total = 0, passed = 0, failed = 0, failures = {}}
|
||
|
|
do
|
||
|
|
local s = 0
|
||
|
|
while s < #suites do
|
||
|
|
local suite = suites[s + 1]
|
||
|
|
do
|
||
|
|
local c = 0
|
||
|
|
while c < #suite.cases do
|
||
|
|
local testCase = suite.cases[c + 1]
|
||
|
|
summary.total = summary.total + 1
|
||
|
|
local ok, err = xpcall(testCase.fn, debug.traceback)
|
||
|
|
if ok then
|
||
|
|
summary.passed = summary.passed + 1
|
||
|
|
else
|
||
|
|
summary.failed = summary.failed + 1
|
||
|
|
summary.failures[#summary.failures + 1] = {
|
||
|
|
suite = suite.name,
|
||
|
|
name = testCase.name,
|
||
|
|
error = tostring(err)
|
||
|
|
}
|
||
|
|
end
|
||
|
|
c = c + 1
|
||
|
|
end
|
||
|
|
end
|
||
|
|
s = s + 1
|
||
|
|
end
|
||
|
|
end
|
||
|
|
return summary
|
||
|
|
end
|
||
|
|
function ____exports.FormatReport(summary)
|
||
|
|
local lines = {}
|
||
|
|
lines[#lines + 1] = string.format("=== Test Summary: %d/%d passed (%d failed) ===", summary.passed, summary.total, summary.failed)
|
||
|
|
do
|
||
|
|
local i = 0
|
||
|
|
while i < #summary.failures do
|
||
|
|
local f = summary.failures[i + 1]
|
||
|
|
lines[#lines + 1] = string.format("[FAIL] %s :: %s\n%s", f.suite, f.name, f.error)
|
||
|
|
i = i + 1
|
||
|
|
end
|
||
|
|
end
|
||
|
|
return table.concat(lines, "\n")
|
||
|
|
end
|
||
|
|
function __module.UpdateImports()
|
||
|
|
end
|