Skip to content

Commit

Permalink
WIP: before_each
Browse files Browse the repository at this point in the history
  • Loading branch information
Conni2461 committed Jan 16, 2021
1 parent e9b81e2 commit eaf9bf6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lua/plenary/busted.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ local mod = {}

local results = {}
local current_description = {}
local current_before_each = {}

local add_description = function(desc)
table.insert(current_description, desc)
Expand All @@ -54,14 +55,24 @@ local pop_description = function()
current_description[#current_description] = nil
end

local add_new_before_each = function()
current_before_each[current_description[#current_description]] = {}
end

local clear_last_before_each = function()
current_before_each[current_description[#current_description]] = nil
end

local call_inner = function(desc, func)
local desc_stack = add_description(desc)
add_new_before_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_before_each()
pop_description()

return ok, msg, desc_stack
Expand Down Expand Up @@ -148,6 +159,14 @@ mod.inner_describe = function(desc, func)
end
end

mod.before_each = function(fn)
table.insert(current_before_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
Expand All @@ -159,6 +178,16 @@ local indent = function(msg, spaces)
end

mod.it = function(desc, func)
for k, v in pairs(current_before_each) do
for _, desc in ipairs(current_description) do
if k == desc then
for _, w in ipairs(v) do
if type(w) == 'function' then w() end
end
end
end
end

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

local test_result = {
Expand Down Expand Up @@ -195,6 +224,8 @@ _PlenaryBustedOldAssert = _PlenaryBustedOldAssert or assert
describe = mod.describe
it = mod.it
pending = mod.pending
before_each = mod.before_each
clear = mod.clear
assert = require("luassert")

mod.run = function(file)
Expand Down
30 changes: 30 additions & 0 deletions tests/plenary/simple_busted_spec.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local eq = assert.are.same

local tester_function = function()
error(7)
end
Expand All @@ -17,6 +19,34 @@ describe('busted specs', function()
pcall(tester_function)
end)

describe('before each clear, clears the lines', function()
before_each(clear)
it('inserts some text', function()
eq({''}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
vim.api.nvim_buf_set_lines(0, 0, -1, false, { 'foo bar baz' })
eq({'foo bar baz'}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)

it('no more content in the buffer', function()
eq({''}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)
-- Should we also clear here?
-- Before if the last tests put something in the buffer,
-- the next running test will have it in the buffer
end)

describe('without clear the buffer will not be cleared', function()
it('inserts some text', function()
eq({''}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
vim.api.nvim_buf_set_lines(0, 0, -1, false, { 'foo bar baz' })
eq({'foo bar baz'}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)

it('no more content in the buffer', function()
eq({ 'foo bar baz' }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end)
end)

pending("Other thing", function()
error()
end)
Expand Down

0 comments on commit eaf9bf6

Please sign in to comment.