|
| 1 | +-- Compatibility for Lua 5.1 |
| 2 | +local unpack = unpack or table.unpack -- luacheck: ignore 143/table |
| 3 | + |
| 4 | +local gambiarra = { |
| 5 | + _VERSION = 'gambiarra 0.4.1-0', |
| 6 | + _DESCRIPTION = 'A tiny lua unit-testing library.', |
| 7 | + _URL = 'https://codeberg.org/imo/gambiarra', |
| 8 | + _LICENSE = 'MIT', |
| 9 | + passed = 0, |
| 10 | + failed = 0, |
| 11 | + report = function(self) |
| 12 | + if self.failed == 0 then |
| 13 | + print('All ' .. self.passed .. ' tests passed.') |
| 14 | + else |
| 15 | + local total = self.passed + self.failed |
| 16 | + print(self.failed .. ' tests failed out of ' .. total) |
| 17 | + end |
| 18 | + end |
| 19 | +} |
| 20 | + |
| 21 | +local function default_handler(e, test, msg) |
| 22 | + if e == 'pass' then |
| 23 | + gambiarra.passed = gambiarra.passed + 1 |
| 24 | + print(('[32mPASS[0m %s: %s'):format(test, msg)) |
| 25 | + elseif e == 'fail' then |
| 26 | + gambiarra.failed = gambiarra.failed + 1 |
| 27 | + print(('[31mFAIL[0m %s: %s'):format(test, msg)) |
| 28 | + elseif e == 'except' then |
| 29 | + gambiarra.failed = gambiarra.failed + 1 |
| 30 | + print(('[31mECPT[0m %s: %s'):format(test, msg)) |
| 31 | + end |
| 32 | +end |
| 33 | + |
| 34 | +local function deepeq(a, b) |
| 35 | + -- Different types: false |
| 36 | + if type(a) ~= type(b) then return false end |
| 37 | + -- Functions |
| 38 | + if type(a) == 'function' then |
| 39 | + return string.dump(a) == string.dump(b) |
| 40 | + end |
| 41 | + -- Primitives and equal pointers |
| 42 | + if a == b then return true end |
| 43 | + -- Only equal tables could have passed previous tests |
| 44 | + if type(a) ~= 'table' then return false end |
| 45 | + -- Compare tables size |
| 46 | + if #a ~= #b then return false end |
| 47 | + -- Compare tables field by field |
| 48 | + for k, v in pairs(a) do |
| 49 | + if b[k] == nil or not deepeq(v, b[k]) then return false end |
| 50 | + end |
| 51 | + return true |
| 52 | +end |
| 53 | + |
| 54 | +-- Compatibility for Lua 5.1 and Lua 5.2 |
| 55 | +local function args(...) |
| 56 | + return { n = select('#', ...), ... } |
| 57 | +end |
| 58 | + |
| 59 | +local function spy(f) |
| 60 | + local s = {} |
| 61 | + s.called = {} |
| 62 | + setmetatable(s, { __call = function(_s, ...) |
| 63 | + local a = args(...) |
| 64 | + table.insert(_s.called, { ... }) |
| 65 | + if f then |
| 66 | + local r = args(pcall(f, unpack(a, 1, a.n))) |
| 67 | + if not r[1] then |
| 68 | + _s.errors = _s.errors or {} |
| 69 | + _s.errors[#_s.called] = r[2] |
| 70 | + else |
| 71 | + return unpack(r, 2, r.n) |
| 72 | + end |
| 73 | + end |
| 74 | + end }) |
| 75 | + return s |
| 76 | +end |
| 77 | + |
| 78 | +local pendingtests = {} |
| 79 | +local env = _G |
| 80 | +local handler = default_handler |
| 81 | + |
| 82 | +local function runpending() |
| 83 | + if pendingtests[1] ~= nil then pendingtests[1](runpending) end |
| 84 | +end |
| 85 | + |
| 86 | +local function test_function(name, f, async) |
| 87 | + if type(name) == 'function' then |
| 88 | + handler = name |
| 89 | + env = f or _G |
| 90 | + return |
| 91 | + end |
| 92 | + |
| 93 | + local function testfn(next) |
| 94 | + local prev = { |
| 95 | + ok = env.ok, |
| 96 | + spy = env.spy, |
| 97 | + eq = env.eq, |
| 98 | + } |
| 99 | + local exp, act |
| 100 | + |
| 101 | + local function restore() |
| 102 | + env.ok = prev.ok |
| 103 | + env.spy = prev.spy |
| 104 | + env.eq = prev.eq |
| 105 | + handler('end', name) |
| 106 | + table.remove(pendingtests, 1) |
| 107 | + if next then next() end |
| 108 | + end |
| 109 | + |
| 110 | + function env.ok(cond, msg) |
| 111 | + if not msg then |
| 112 | + local d = debug.getinfo(2, 'Sl') |
| 113 | + msg = d.short_src .. ':' .. d.currentline |
| 114 | + end |
| 115 | + if cond then |
| 116 | + handler('pass', name, msg) |
| 117 | + else |
| 118 | + handler('fail', name, |
| 119 | + ('%s: Expected %q, but got %q.'):format(msg, tostring(exp), tostring(act))) |
| 120 | + end |
| 121 | + act, exp = nil, nil |
| 122 | + end |
| 123 | + |
| 124 | + function env.eq(a, b) |
| 125 | + act, exp = a, b |
| 126 | + return deepeq(exp, act) |
| 127 | + end |
| 128 | + |
| 129 | + env.spy = spy |
| 130 | + |
| 131 | + handler('begin', name) |
| 132 | + local ok, err = pcall(f, restore) |
| 133 | + if not ok then |
| 134 | + handler('except', name, err) |
| 135 | + end |
| 136 | + |
| 137 | + if not async then |
| 138 | + handler('end', name) |
| 139 | + env.ok = prev.ok |
| 140 | + env.spy = prev.spy |
| 141 | + env.eq = prev.eq |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + if not async then |
| 146 | + testfn() |
| 147 | + else |
| 148 | + table.insert(pendingtests, testfn) |
| 149 | + if #pendingtests == 1 then |
| 150 | + runpending() |
| 151 | + end |
| 152 | + end |
| 153 | +end |
| 154 | + |
| 155 | +setmetatable(gambiarra, |
| 156 | + { __call = function(_, name, f, async) |
| 157 | + return test_function(name, f, async) |
| 158 | + end }) |
| 159 | +return gambiarra |
0 commit comments