Files
UnrealLuaDemo/Script/lua/Tests/Harness/TestRunner.lua
T

153 lines
4.2 KiB
Lua

local ____exports = {}
local currentSuite = "Default"
local results = {}
function ____exports.Reset()
currentSuite = "Default"
results = {}
end
function ____exports.Suite(name, body)
local prev = currentSuite
currentSuite = name
body()
currentSuite = prev
end
function ____exports.Test(name, fn)
local ok, err = pcall(fn)
if ok then
results[#results + 1] = {suite = currentSuite, name = name, passed = true}
else
results[#results + 1] = {
suite = currentSuite,
name = name,
passed = false,
errorMessage = tostring(err)
}
end
end
function ____exports.AssertTrue(cond, msg)
if not cond then
error(msg or "Expected condition to be truthy", 2)
end
end
function ____exports.AssertFalse(cond, msg)
if cond then
error(msg or "Expected condition to be falsy", 2)
end
end
function ____exports.AssertEqual(actual, expected, msg)
if actual ~= expected then
local prefix = msg and msg .. ": " or ""
error(
string.format(
"%sExpected %s, got %s",
prefix,
tostring(expected),
tostring(actual)
),
2
)
end
end
function ____exports.AssertNear(actual, expected, epsilon, msg)
if epsilon == nil then
epsilon = 0.0001
end
if math.abs(actual - expected) > epsilon then
local prefix = msg and msg .. ": " or ""
error(
string.format(
"%sExpected %.6f (±%.6f), got %.6f",
prefix,
expected,
epsilon,
actual
),
2
)
end
end
function ____exports.AssertNil(value, msg)
if value ~= nil then
local prefix = msg and msg .. ": " or ""
error(
string.format(
"%sExpected nil, got %s",
prefix,
tostring(value)
),
2
)
end
end
function ____exports.AssertNotNil(value, msg)
if value == nil then
error(msg or "Expected non-nil value", 2)
end
end
function ____exports.AssertArrayEqual(actual, expected, msg)
local prefix = msg and msg .. ": " 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 element [%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.GetSummary()
local passed = 0
local failed = 0
do
local i = 0
while i < #results do
if results[i + 1].passed then
passed = passed + 1
else
failed = failed + 1
end
i = i + 1
end
end
return {total = #results, passed = passed, failed = failed, results = results}
end
function ____exports.PrintReport()
local summary = ____exports.GetSummary()
local lastSuite = ""
do
local i = 0
while i < #summary.results do
local r = summary.results[i + 1]
if r.suite ~= lastSuite then
lastSuite = r.suite
print(string.format("\n[%s]", r.suite))
end
if r.passed then
print(string.format(" PASS %s", r.name))
else
print(string.format(" FAIL %s\n -> %s", r.name, r.errorMessage or "unknown error"))
end
i = i + 1
end
end
print(string.format("\n========================================\nTotal: %d | Passed: %d | Failed: %d\n========================================", summary.total, summary.passed, summary.failed))
return summary
end
return ____exports