|
| 1 | +local deps = ... |
| 2 | +local assertions = deps.assertions |
| 3 | + |
| 4 | +local println, style = deps.println, deps.style |
| 5 | +local instant = deps.instant |
| 6 | + |
| 7 | +local Testing = {} |
| 8 | +Testing.__index = Testing |
| 9 | + |
| 10 | +function Testing.new(name) |
| 11 | + local self = setmetatable({}, Testing) |
| 12 | + self._name = name |
| 13 | + self._tests = {} |
| 14 | + self._hooks = { |
| 15 | + before_all = {}, |
| 16 | + after_all = {}, |
| 17 | + before_each = {}, |
| 18 | + after_each = {}, |
| 19 | + } |
| 20 | + self._results = {} |
| 21 | + return self |
| 22 | +end |
| 23 | + |
| 24 | +-- Test context passed to each test function |
| 25 | +local TestContext = {} |
| 26 | +TestContext.__index = TestContext |
| 27 | + |
| 28 | +function TestContext.new(name) |
| 29 | + local self = setmetatable({}, TestContext) |
| 30 | + self.name = name |
| 31 | + return self |
| 32 | +end |
| 33 | + |
| 34 | +-- Forward assertion methods to the assertions module |
| 35 | +function TestContext.assert_eq(a, b, msg) |
| 36 | + assertions.assert_eq(a, b, msg) |
| 37 | +end |
| 38 | + |
| 39 | +function TestContext.assert_same(a, b, msg) |
| 40 | + assertions.assert_same(a, b, msg) |
| 41 | +end |
| 42 | + |
| 43 | +function TestContext.assert(cond, msg) |
| 44 | + if not cond then |
| 45 | + if msg ~= nil then |
| 46 | + error("assertion failed: " .. tostring(msg), 2) |
| 47 | + else |
| 48 | + error("assertion failed!", 2) |
| 49 | + end |
| 50 | + end |
| 51 | +end |
| 52 | + |
| 53 | +-- Add convenience methods |
| 54 | +function TestContext.skip(reason) |
| 55 | + error("__SKIP__: " .. (reason or "skipped"), 0) |
| 56 | +end |
| 57 | + |
| 58 | +-- Hooks registration |
| 59 | +function Testing:before_all(func) |
| 60 | + table.insert(self._hooks.before_all, func) |
| 61 | +end |
| 62 | + |
| 63 | +function Testing:after_all(func) |
| 64 | + table.insert(self._hooks.after_all, func) |
| 65 | +end |
| 66 | + |
| 67 | +function Testing:before_each(func) |
| 68 | + table.insert(self._hooks.before_each, func) |
| 69 | +end |
| 70 | + |
| 71 | +function Testing:after_each(func) |
| 72 | + table.insert(self._hooks.after_each, func) |
| 73 | +end |
| 74 | + |
| 75 | +-- Tests registration |
| 76 | +function Testing:test(name, func) |
| 77 | + table.insert(self._tests, { name = name, func = func }) |
| 78 | +end |
| 79 | + |
| 80 | +-- Run a single test |
| 81 | +function Testing:_run_single_test(test) |
| 82 | + local ctx = TestContext.new(test.name) |
| 83 | + local start_time = instant() |
| 84 | + local success, err = true, nil |
| 85 | + |
| 86 | + -- Run before_each hooks |
| 87 | + for _, func in ipairs(self._hooks.before_each) do |
| 88 | + local ok, hook_err = pcall(func) |
| 89 | + if not ok then |
| 90 | + return { |
| 91 | + name = test.name, |
| 92 | + passed = false, |
| 93 | + skipped = false, |
| 94 | + error = "before_each failed: " .. tostring(hook_err), |
| 95 | + duration = start_time:elapsed(), |
| 96 | + } |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + -- Run the test |
| 101 | + local test_ok, test_err = pcall(test.func, ctx) |
| 102 | + if not test_ok then |
| 103 | + if test_err and test_err:match("^__SKIP__:") then |
| 104 | + success, err = "skip", test_err:match("^__SKIP__: (.*)") |
| 105 | + else |
| 106 | + success, err = false, test_err |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + -- Run after_each hooks (even if test failed) |
| 111 | + for _, func in ipairs(self._hooks.after_each) do |
| 112 | + local ok, hook_err = pcall(func) |
| 113 | + if not ok then |
| 114 | + return { |
| 115 | + name = test.name, |
| 116 | + passed = false, |
| 117 | + skipped = false, |
| 118 | + error = "after_each failed: " .. tostring(hook_err), |
| 119 | + duration = start_time:elapsed(), |
| 120 | + } |
| 121 | + end |
| 122 | + end |
| 123 | + |
| 124 | + return { |
| 125 | + name = test.name, |
| 126 | + passed = success == true, |
| 127 | + skipped = success == "skip", |
| 128 | + error = err, |
| 129 | + duration = start_time:elapsed(), |
| 130 | + } |
| 131 | +end |
| 132 | + |
| 133 | +-- Run all tests |
| 134 | +function Testing:run(opts) |
| 135 | + opts = opts or {} |
| 136 | + local pattern = opts.pattern |
| 137 | + self._results = {} |
| 138 | + local start_time = instant() |
| 139 | + |
| 140 | + -- Run before_all hooks |
| 141 | + for _, func in ipairs(self._hooks.before_all) do |
| 142 | + func() |
| 143 | + end |
| 144 | + |
| 145 | + -- Run tests |
| 146 | + for _, test in ipairs(self._tests) do |
| 147 | + if not pattern or test.name:find(pattern) then |
| 148 | + local result = self:_run_single_test(test) |
| 149 | + table.insert(self._results, result) |
| 150 | + end |
| 151 | + end |
| 152 | + |
| 153 | + -- Run after_all hooks |
| 154 | + for _, func in ipairs(self._hooks.after_all) do |
| 155 | + func() |
| 156 | + end |
| 157 | + |
| 158 | + self._results.duration = start_time:elapsed() |
| 159 | + |
| 160 | + -- Print results unless quiet |
| 161 | + if not opts.quiet then |
| 162 | + self:_print_results() |
| 163 | + end |
| 164 | + |
| 165 | + -- Return success status |
| 166 | + local failed = 0 |
| 167 | + for _, result in ipairs(self._results) do |
| 168 | + if not result.passed and not result.skipped then |
| 169 | + failed = failed + 1 |
| 170 | + end |
| 171 | + end |
| 172 | + |
| 173 | + return failed == 0, self._results |
| 174 | +end |
| 175 | + |
| 176 | +function Testing:_print_results() |
| 177 | + local passed, failed, skipped = 0, 0, 0 |
| 178 | + |
| 179 | + for _, result in ipairs(self._results) do |
| 180 | + local status = style(result.passed and "✓" or (result.skipped and "⊝" or "✗")) |
| 181 | + status:color(result.passed and "green" or (result.skipped and "yellow" or "red")) |
| 182 | + |
| 183 | + println(status, result.name) |
| 184 | + if result.error then |
| 185 | + println(tostring(result.error)) |
| 186 | + end |
| 187 | + |
| 188 | + if result.passed then |
| 189 | + passed = passed + 1 |
| 190 | + elseif result.skipped then |
| 191 | + skipped = skipped + 1 |
| 192 | + else |
| 193 | + failed = failed + 1 |
| 194 | + end |
| 195 | + end |
| 196 | + |
| 197 | + local total = passed + failed + skipped |
| 198 | + if total == 0 then |
| 199 | + -- No tests were run |
| 200 | + return |
| 201 | + end |
| 202 | + |
| 203 | + local prefix = "test results:" |
| 204 | + if self._name then |
| 205 | + prefix = string.format("`%s` %s", self._name, prefix) |
| 206 | + end |
| 207 | + local duration = self._results.duration |
| 208 | + local stats = string.format( |
| 209 | + "%d passed, %d failed, %d skipped (%d total finished in %s)", |
| 210 | + passed, |
| 211 | + failed, |
| 212 | + skipped, |
| 213 | + total, |
| 214 | + duration |
| 215 | + ) |
| 216 | + println() |
| 217 | + println(prefix, stats) |
| 218 | +end |
| 219 | + |
| 220 | +-- Get results for the Rust integration |
| 221 | +function Testing:results() |
| 222 | + return self._results |
| 223 | +end |
| 224 | + |
| 225 | +return Testing |
0 commit comments