43
43
44
44
local mod = {}
45
45
46
+ local describes = {}
46
47
local results = {}
47
48
local current_description = {}
48
49
local current_before_each = {}
@@ -116,16 +117,18 @@ mod.describe = function(desc, func)
116
117
results .fail = results .fail or {}
117
118
results .errs = results .errs or {}
118
119
119
- describe = mod .inner_describe
120
- local ok , msg , desc_stack = call_inner (desc , func )
121
- describe = mod .describe
120
+ table.insert (describes , function ()
121
+ describe = mod .inner_describe
122
+ local ok , msg , desc_stack = call_inner (desc , func )
123
+ describe = mod .describe
122
124
123
- if not ok then
124
- table.insert (results .errs , {
125
- descriptions = desc_stack ,
126
- msg = msg ,
127
- })
128
- end
125
+ if not ok then
126
+ table.insert (results .errs , {
127
+ descriptions = desc_stack ,
128
+ msg = msg ,
129
+ })
130
+ end
131
+ end )
129
132
end
130
133
131
134
mod .inner_describe = function (desc , func )
@@ -214,13 +217,47 @@ after_each = mod.after_each
214
217
clear = mod .clear
215
218
assert = require " luassert"
216
219
220
+ local main = function ()
221
+ for _ , d in ipairs (describes ) do
222
+ d ()
223
+ end
224
+
225
+ -- If nothing runs (empty file without top level describe)
226
+ if not results .pass then
227
+ if is_headless then
228
+ return vim .cmd " 0cq"
229
+ else
230
+ return
231
+ end
232
+ end
233
+
234
+ mod .format_results (results )
235
+
236
+ if # results .errs ~= 0 then
237
+ print (" We had an unexpected error: " , vim .inspect (results .errs ), vim .inspect (results ))
238
+ if is_headless then
239
+ return vim .cmd " 2cq"
240
+ end
241
+ elseif # results .fail > 0 then
242
+ print " Tests Failed. Exit: 1"
243
+
244
+ if is_headless then
245
+ return vim .cmd " 1cq"
246
+ end
247
+ else
248
+ if is_headless then
249
+ return vim .cmd " 0cq"
250
+ end
251
+ end
252
+ end
253
+
217
254
mod .run = function (file )
218
255
print (" \n " .. HEADER )
219
256
print (" Testing: " , file )
220
257
221
- local loaded , msg = loadfile ( file )
258
+ local ok , msg = pcall ( dofile , file )
222
259
223
- if not loaded then
260
+ if not ok then
224
261
print (HEADER )
225
262
print " FAILED TO LOAD FILE"
226
263
print (color_string (" red" , msg ))
@@ -232,37 +269,11 @@ mod.run = function(file)
232
269
end
233
270
end
234
271
235
- coroutine.wrap (function ()
236
- loaded ()
237
-
238
- -- If nothing runs (empty file without top level describe)
239
- if not results .pass then
240
- if is_headless then
241
- return vim .cmd " 0cq"
242
- else
243
- return
244
- end
245
- end
246
-
247
- mod .format_results (results )
248
-
249
- if # results .errs ~= 0 then
250
- print (" We had an unexpected error: " , vim .inspect (results .errs ), vim .inspect (results ))
251
- if is_headless then
252
- return vim .cmd " 2cq"
253
- end
254
- elseif # results .fail > 0 then
255
- print " Tests Failed. Exit: 1"
256
-
257
- if is_headless then
258
- return vim .cmd " 1cq"
259
- end
260
- else
261
- if is_headless then
262
- return vim .cmd " 0cq"
263
- end
264
- end
265
- end )()
272
+ if RUN_INSIDE_COROUTINE then
273
+ coroutine.wrap (main )()
274
+ else
275
+ main ()
276
+ end
266
277
end
267
278
268
279
return mod
0 commit comments