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