Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: before_each #54

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions lua/plenary/busted.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,24 @@ local pop_description = function()
current_description[#current_description] = nil
end

local execution_stack = {}

local call_inner = function(desc, func)
local desc_stack = add_description(desc)

-- Push function onto stack
table.insert(execution_stack, func)

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)

-- Remove function from stack
execution_stack[#execution_stack] = nil

pop_description()

return ok, msg, desc_stack
Expand Down Expand Up @@ -159,6 +169,15 @@ local indent = function(msg, spaces)
end

mod.it = function(desc, func)
-- TODO: Should probably clean this up.
-- TODO: Also needs after_each
-- call before_each
for _, exec_func in ipairs(execution_stack) do
for _, before in ipairs(mod._before_each_map[exec_func] or {}) do
before()
end
end

local ok, msg, desc_stack = call_inner(desc, func)

local test_result = {
Expand All @@ -169,7 +188,7 @@ mod.it = function(desc, func)
-- TODO: We should figure out how to determine whether
-- and assert failed or whether it was an error...

local to_insert, printed
local to_insert
if not ok then
to_insert = results.fail
test_result.msg = msg
Expand All @@ -185,8 +204,21 @@ mod.it = function(desc, func)
end

mod.pending = function(desc, func)
local _, _, desc_stack = call_inner(desc, func)
print(PENDING, "||", table.concat(desc_stack, " "))
-- TODO: Probably want to show description stack
-- local _, _, desc_stack = call_inner(desc, func)
print(PENDING, "||", desc)
end

mod._before_each_map = {}
mod.before_each = function(func)
-- Add a reference to the before each for the current execution.
-- When we pop this off the stack later, we won't run it then!
local current_func = execution_stack[#execution_stack]
if not mod._before_each_map[current_func] then
mod._before_each_map[current_func] = {}
end

table.insert(mod._before_each_map[current_func], func)
end

_PlenaryBustedOldAssert = _PlenaryBustedOldAssert or assert
Expand All @@ -197,6 +229,8 @@ it = mod.it
pending = mod.pending
assert = require("luassert")

before_each = mod.before_each

mod.run = function(file)
local ok, msg = pcall(dofile, file)

Expand Down
4 changes: 2 additions & 2 deletions lua/plenary/job.lua
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@ function Job:start()
self:_execute()
end

function Job:sync(timeout, wait_interval)
function Job:sync(timeout, wait_interval, should_redraw)
self:start()
self:wait(timeout, wait_interval)
self:wait(timeout, wait_interval, should_redraw)

return self.enable_recording and self:result() or nil
end
Expand Down