73 lines
1.6 KiB
Lua
73 lines
1.6 KiB
Lua
local _VERSION = '1.1.0';
|
|
|
|
local function createLibTest(args)
|
|
local api = {};
|
|
local tests = {};
|
|
local verbose = false;
|
|
|
|
for _, arg in ipairs(args or {}) do
|
|
if arg == '--verbose' then
|
|
verbose = true;
|
|
end
|
|
end
|
|
|
|
local function fail(message)
|
|
error(message, 2);
|
|
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
|
|
if verbose then
|
|
print('RUN ' .. t.name);
|
|
end
|
|
local ok, err = pcall(t.fn);
|
|
if not ok then
|
|
print('FAIL ' .. t.name .. ': ' .. tostring(err));
|
|
os.shutdown();
|
|
end
|
|
end
|
|
|
|
print('__TRAPOS_TEST_OK__');
|
|
os.shutdown();
|
|
end
|
|
|
|
function api.version()
|
|
return _VERSION;
|
|
end
|
|
|
|
return api;
|
|
end
|
|
|
|
return createLibTest;
|