-
-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathbusted.lua
More file actions
264 lines (214 loc) · 5.99 KB
/
busted.lua
File metadata and controls
264 lines (214 loc) · 5.99 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
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, ":h")
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 is_headless = require("plenary.nvim_meta").is_headless
-- We are shadowing print so people can reliably print messages
print = function(...)
for _, v in ipairs { ... } do
io.stdout:write(tostring(v))
io.stdout:write "\t"
end
io.stdout:write "\r\n"
end
local mod = {}
local results = {}
local current_description = {}
local current_before_each = {}
local current_after_each = {}
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 call_inner = function(desc, func)
local desc_stack = add_description(desc)
add_new_each()
local ok, msg = xpcall(func, function(msg)
-- debug.traceback
-- return vim.inspect(get_trace(nil, 3, msg))
local trace = get_trace(nil, 3, msg)
return trace.message .. "\n" .. trace.traceback
end)
clear_last_each()
pop_description()
return ok, msg, desc_stack
end
local color_table = {
yellow = 33,
green = 32,
red = 31,
}
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)
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
mod.describe = function(desc, func)
results.pass = results.pass or {}
results.fail = results.fail or {}
results.errs = results.errs 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 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 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 = call_inner(desc, func)
run_each(current_after_each)
local test_result = {
descriptions = desc_stack,
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
print(FAIL, "||", table.concat(test_result.descriptions, " "))
print(indent(msg, 12))
else
to_insert = results.pass
print(SUCCESS, "||", table.concat(test_result.descriptions, " "))
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)
print(PENDING, "||", table.concat(curr_stack, " "))
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(file)
print("\n" .. HEADER)
print("Testing: ", file)
local ok, msg = pcall(dofile, file)
if not ok then
print(HEADER)
print "FAILED TO LOAD FILE"
print(color_string("red", msg))
print(HEADER)
if is_headless then
return vim.cmd "2cq"
else
return
end
end
-- 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
print("We had an unexpected error: ", vim.inspect(results.errs), vim.inspect(results))
if is_headless then
return vim.cmd "2cq"
end
elseif #results.fail > 0 then
print "Tests Failed. Exit: 1"
if is_headless then
return vim.cmd "1cq"
end
else
if is_headless then
return vim.cmd "0cq"
end
end
end
return mod