cc-libs/apis/libtest.lua
2026-06-08 05:10:31 +02:00

115 lines
2.2 KiB
Lua

local _VERSION = "1.4.0"
local function createLibTest(args)
local api = {}
local tests = {}
local pretty = false
local verbose = false
local reportPath = nil
local printMarker = true
local i = 1
while i <= #(args or {}) do
local arg = args[i]
if arg == "--verbose" then
pretty = true
verbose = true
elseif arg == "--pretty" then
pretty = true
elseif arg == "--report" then
reportPath = args[i + 1]
i = i + 1
elseif arg == "--no-marker" then
printMarker = false
end
i = i + 1
end
local function fail(message)
error(message, 2)
end
local function writeReport(line)
if not reportPath then
return
end
local file = fs.open(reportPath, "a")
if file then
file.writeLine(line)
file.close()
end
end
function api.log(message)
if verbose then
writeReport("LOG " .. tostring(message))
end
end
function api.test(name, fn)
assert(type(name) == "string", "bad argument #1 (string expected)")
assert(type(fn) == "function", "bad argument #2 (function expected)")
tests[#tests + 1] = { name = name, fn = fn }
end
function api.assertEquals(actual, expected, message)
if actual ~= expected then
fail(
(message or "assertEquals failed")
.. ": expected "
.. tostring(expected)
.. ", got "
.. tostring(actual)
)
end
end
function api.assertTrue(value, message)
if not value then
fail(message or "assertTrue failed")
end
end
function api.assertErrors(fn, expected)
assert(type(fn) == "function", "bad argument #1 (function expected)")
local ok, err = pcall(fn)
if ok then
fail("expected error")
end
if expected and not string.find(tostring(err), expected, 1, true) then
fail("expected error containing " .. expected .. ", got " .. tostring(err))
end
end
function api.run()
for _, t in ipairs(tests) do
api.log("RUN " .. t.name)
local ok, err = pcall(t.fn)
if not ok then
writeReport("KO " .. t.name .. ": " .. tostring(err))
if not pretty then
print("FAIL " .. t.name .. ": " .. tostring(err))
end
error(err, 0)
end
writeReport("OK " .. t.name)
end
if printMarker then
print("__TRAPOS_TEST_OK__")
end
return true
end
function api.version()
return _VERSION
end
return api
end
return createLibTest