2026-11-30 02:42:51 +08:00
|
|
|
local ____exports = __module.env
|
2026-11-29 03:35:49 +08:00
|
|
|
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
|
2026-11-29 15:05:35 +08:00
|
|
|
function ____exports.FormatReport(summary)
|
|
|
|
|
local s = summary or ____exports.GetSummary()
|
|
|
|
|
local lines = {}
|
2026-11-29 03:35:49 +08:00
|
|
|
local lastSuite = ""
|
|
|
|
|
do
|
|
|
|
|
local i = 0
|
2026-11-29 15:05:35 +08:00
|
|
|
while i < #s.results do
|
|
|
|
|
local r = s.results[i + 1]
|
2026-11-29 03:35:49 +08:00
|
|
|
if r.suite ~= lastSuite then
|
|
|
|
|
lastSuite = r.suite
|
2026-11-29 15:05:35 +08:00
|
|
|
lines[#lines + 1] = ""
|
|
|
|
|
lines[#lines + 1] = string.format("[%s]", r.suite)
|
2026-11-29 03:35:49 +08:00
|
|
|
end
|
|
|
|
|
if r.passed then
|
2026-11-29 15:05:35 +08:00
|
|
|
lines[#lines + 1] = string.format(" PASS %s", r.name)
|
2026-11-29 03:35:49 +08:00
|
|
|
else
|
2026-11-29 15:05:35 +08:00
|
|
|
lines[#lines + 1] = string.format(" FAIL %s\n -> %s", r.name, r.errorMessage or "unknown error")
|
2026-11-29 03:35:49 +08:00
|
|
|
end
|
|
|
|
|
i = i + 1
|
|
|
|
|
end
|
|
|
|
|
end
|
2026-11-29 15:05:35 +08:00
|
|
|
lines[#lines + 1] = ""
|
|
|
|
|
lines[#lines + 1] = "========================================"
|
|
|
|
|
lines[#lines + 1] = string.format("Total: %d | Passed: %d | Failed: %d", s.total, s.passed, s.failed)
|
|
|
|
|
lines[#lines + 1] = "========================================"
|
|
|
|
|
return table.concat(lines, "\n")
|
|
|
|
|
end
|
|
|
|
|
function ____exports.PrintReport()
|
|
|
|
|
local summary = ____exports.GetSummary()
|
|
|
|
|
print(____exports.FormatReport(summary))
|
2026-11-29 03:35:49 +08:00
|
|
|
return summary
|
|
|
|
|
end
|
2026-11-30 02:42:51 +08:00
|
|
|
function __module.UpdateImports()
|
|
|
|
|
end
|