local function try(t, c, f)
local co = cocreate(t)
local s, e = true
while s and costatus(co) ~= "dead" do
s, e = coresume(co)
if not s then
c(e)
end
end
if f then
f()
end
end
tinytest = {
new = function(self, o)
o = o or {}
setmetatable(o, self)
self.__index = self
o.failures = {}
o.fail_is_error = o.fail_is_error or false
return o
end,
table_count = function(table)
local count = 0
for key, value in pairs(table) do
count = count + 1
end
return count
end,
run = function(self, tests)
local errors_map = {}
local failures_map = {}
local errors = {}
local fails = 0
local errs = 0
cls()
print("test results: \0")
for testname, testaction in pairs(tests) do
try(function ()
testaction(self)
end,
function (e)
add(errors, e)
end,
function ()
end)
if #errors == 0 and #self.failures == 0 then
print("p\0")
end
if #self.failures ~= 0 then
print("f\0")
failures_map[testname] = self.failures
self.failures = {}
fails = fails + 1
end
if #errors ~= 0 then
print("e\0")
errors_map[testname] = errors
errors = {}
errs = errs + 1
end
end
print(nil,0)
for testname, testaction in pairs(tests) do
print(testname)
if failures_map[testname] or errors_map[testname] then
for _, failure in ipairs(failures_map[testname]) do
print(" " .. failure)
world.error(testname .. ": " .. failure)
end
for _, error in ipairs(errors_map[testname]) do
print(" error line " .. error)
world.error(testname .. ": " .. error)
print(error)
end
else
print(" pass")
end
end
return fails, errs, failures_map, errors_map
end,
fail = function(self, msg, header)
if not msg then
msg = ''
end
header = header or 'fail'
if self.false_is_error then
assert(false, header .. msg)
else
add(self.failures, header .. msg)
end
end,
ok = function(self, value, msg)
if not value then self:fail(msg, '\fanot ok: \f6') end
end,
eq = function(self, expected, actual, msg)
if expected ~= actual then self:fail('"' .. expected .. '" ~= "' .. actual .. '" '..(msg or ''), 'not eq: ') end
end,
}