-
-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathbusted.lua
More file actions
400 lines (327 loc) · 9.25 KB
/
busted.lua
File metadata and controls
400 lines (327 loc) · 9.25 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
local opts = {}
local is_headless = require("plenary.nvim_meta").is_headless
local output_compact = false
local generic_marker = "▪"
local pass_marker = generic_marker or "●"
local fail_marker = generic_marker or "●"
local pending_marker = generic_marker or "●"
local mod = {}
local results = {}
local current_prints = {}
local current_description = {}
local current_before_each = {}
local current_after_each = {}
local color_table = {
magenta = 35,
blue = 34,
yellow = 33,
green = 32,
red = 31,
}
-- We are shadowing print so people can reliably print messages
print = function(...)
local container = current_prints[#current_prints]
if container then
container[#container + 1] = { ... }
else
for _, v in ipairs { ... } do
io.stdout:write(tostring(v))
io.stdout:write "\t"
end
io.stdout:write "\r\n"
end
end
print_append = function(...)
for _, v in ipairs { ... } do
io.stdout:write(tostring(v))
end
end
local indent = function(msg, spaces)
if spaces == nil then
spaces = 4
end
local prefix = string.rep(" ", spaces)
return prefix .. msg:gsub("\n", "\n" .. prefix)
end
local dirname = function(p)
return vim.fn.fnamemodify(p, ":h")
end
local function get_trace(element, level, msg)
local function trimTrace(info)
local index = info.traceback:find "\n%s*%[C]"
info.traceback = info.traceback:sub(1, index)
return info
end
level = level or 3
local thisdir = dirname(debug.getinfo(1, "Sl").source)
local info = debug.getinfo(level, "Sl")
while
info.what == "C"
or info.short_src:match "luassert[/\\].*%.lua$"
or (info.source:sub(1, 1) == "@" and thisdir == dirname(info.source))
do
level = level + 1
info = debug.getinfo(level, "Sl")
end
info.traceback = debug.traceback("", level)
info.message = msg
-- local file = busted.getFile(element)
local file = false
return file and file.getTrace(file.name, info) or trimTrace(info)
end
local add_description = function(desc)
table.insert(current_description, desc)
return vim.deepcopy(current_description)
end
local pop_description = function()
current_description[#current_description] = nil
end
local add_new_each = function()
current_before_each[current_description[#current_description]] = {}
current_after_each[current_description[#current_description]] = {}
end
local clear_last_each = function()
current_before_each[current_description[#current_description]] = nil
current_after_each[current_description[#current_description]] = nil
end
local push_print_capture = function(tbl)
current_prints[#current_prints + 1] = tbl
end
local pop_print_capture = function()
current_prints[#current_prints] = nil
end
local call_inner = function(desc, func, capture_prints)
local desc_stack = add_description(desc)
local prints = {}
if capture_prints then
push_print_capture(prints)
end
add_new_each()
local ok, msg = xpcall(func, function(msg)
-- debug.traceback
-- return vim.inspect(get_trace(nil, 3, msg))
if output_compact then
return msg
else
local trace = get_trace(nil, 3, msg)
return trace.message .. "\n" .. trace.traceback
end
end)
if capture_prints then
pop_print_capture()
end
clear_last_each()
pop_description()
return ok, msg, desc_stack, prints
end
local color_string = function(color, str)
if not is_headless then
return str
end
return string.format("%s[%sm%s%s[%sm", string.char(27), color_table[color] or 0, str, string.char(27), 0)
end
local SUCCESS = color_string("green", "Success")
local FAIL = color_string("red", "Fail")
local PENDING = color_string("yellow", "Pending")
local HEADER = string.rep("=", 40)
mod.format_results = function(res)
if output_compact then
print(
string.format(
"\n%s: %d / %s: %d / %s: %d / %s: %d",
color_string("green", "pass"),
#res.pass,
color_string("magenta", "pending"),
#res.pending,
color_string("red", "fail"),
#res.fail,
color_string("yellow", "fatal"),
#res.errs
)
)
if #res.pending > 0 or #res.fail > 0 or #res.errs > 0 then
print ""
end
for _, pending in ipairs(res.pending) do
local path = table.concat(pending.descriptions, " » ")
print(string.format("%s: %s", color_string("magenta", "pending"), path))
end
for _, fail in ipairs(res.fail) do
local path = table.concat(fail.descriptions, " » ")
print ""
print(string.format("%s: %s", color_string("red", "fail"), path))
print(indent(fail.msg, 2))
for _, print_params in ipairs(fail.prints) do
print ""
print(string.format("%s:", color_string("blue", "print")))
print(indent(table.concat(print_params, "\t"), 2))
end
end
for _, error in ipairs(res.errs) do
local path = table.concat(error.descriptions, " » ")
print ""
print(string.format("%s: %s", color_string("yellow", "fatal"), path))
print(indent(error.msg, 2))
end
else
print ""
print(color_string("green", "Success: "), #res.pass)
print(color_string("red", "Failed : "), #res.fail)
print(color_string("red", "Errors : "), #res.errs)
print(HEADER)
end
end
mod.describe = function(desc, func)
results.pass = results.pass or {}
results.fail = results.fail or {}
results.errs = results.errs or {}
results.pending = results.pending or {}
describe = mod.inner_describe
local ok, msg, desc_stack = call_inner(desc, func)
describe = mod.describe
if not ok then
table.insert(results.errs, {
descriptions = desc_stack,
msg = msg,
})
end
end
mod.inner_describe = function(desc, func)
local ok, msg, desc_stack = call_inner(desc, func)
if not ok then
table.insert(results.errs, {
descriptions = desc_stack,
msg = msg,
})
end
end
mod.before_each = function(fn)
table.insert(current_before_each[current_description[#current_description]], fn)
end
mod.after_each = function(fn)
table.insert(current_after_each[current_description[#current_description]], fn)
end
mod.clear = function()
vim.api.nvim_buf_set_lines(0, 0, -1, false, {})
end
local run_each = function(tbl)
for _, v in pairs(tbl) do
for _, w in ipairs(v) do
if type(w) == "function" then
w()
end
end
end
end
mod.it = function(desc, func)
run_each(current_before_each)
local ok, msg, desc_stack, prints = call_inner(desc, func, output_compact)
run_each(current_after_each)
local test_result = {
descriptions = desc_stack,
prints = prints,
msg = nil,
}
-- TODO: We should figure out how to determine whether
-- and assert failed or whether it was an error...
local to_insert
if not ok then
to_insert = results.fail
test_result.msg = msg
if output_compact then
print_append(color_string("red", fail_marker))
else
print(FAIL, "||", table.concat(test_result.descriptions, " "))
print(indent(msg, 12))
end
else
to_insert = results.pass
if output_compact then
print_append(color_string("green", pass_marker))
else
print(SUCCESS, "||", table.concat(test_result.descriptions, " "))
end
end
table.insert(to_insert, test_result)
end
mod.pending = function(desc, func)
local curr_stack = vim.deepcopy(current_description)
table.insert(curr_stack, desc)
table.insert(results.pending, { descriptions = curr_stack, msg = nil })
if output_compact then
print_append(color_string("magenta", pending_marker))
else
print(PENDING, "||", table.concat(curr_stack, " "))
end
end
_PlenaryBustedOldAssert = _PlenaryBustedOldAssert or assert
describe = mod.describe
it = mod.it
pending = mod.pending
before_each = mod.before_each
after_each = mod.after_each
clear = mod.clear
assert = require "luassert"
mod.run = function(input_file, input_opts_json)
local file = input_file
if type(input_opts_json) == "string" then
opts = vim.json.decode(input_opts_json)
output_compact = opts.output_format == "compact"
end
if output_compact then
local trimmed = vim.fn.fnamemodify(file, ":.")
print ""
print(string.format("%s: %s", color_string("blue", "test"), trimmed))
else
print("\n" .. HEADER)
print("Testing: ", file)
end
local loaded, msg = loadfile(file)
if not loaded then
if output_compact then
print(string.format("%s: %s", color_string("red", "fatal"), msg))
else
print(HEADER)
print "FAILED TO LOAD FILE"
print(color_string("red", msg))
print(HEADER)
end
if is_headless then
return vim.cmd "2cq"
else
return
end
end
coroutine.wrap(function()
loaded()
-- If nothing runs (empty file without top level describe)
if not results.pass then
if is_headless then
return vim.cmd "0cq"
else
return
end
end
mod.format_results(results)
if #results.errs ~= 0 then
if not output_compact then
print "\nFatal errors. Exit: 2"
end
if is_headless then
return vim.cmd "2cq"
end
elseif #results.fail > 0 then
if not output_compact then
print "\nTests Failed. Exit: 1"
end
if is_headless then
return vim.cmd "1cq"
end
else
if is_headless then
return vim.cmd "0cq"
end
end
end)()
end
return mod