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

Declutter infoview tests. #104

Merged
merged 13 commits into from
Jul 27, 2021
4 changes: 3 additions & 1 deletion lua/lean/infoview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ function Infoview:is_empty()
end

--- Close all open infoviews (across all tabs).
function infoview.close_all()
function infoview.close_all(pre_close_hook, post_close_hook)
rish987 marked this conversation as resolved.
Show resolved Hide resolved
for _, each in pairs(infoview._by_id) do
if pre_close_hook then pre_close_hook(each) end
each:close()
if post_close_hook then post_close_hook(each) end
end
end

Expand Down
135 changes: 130 additions & 5 deletions lua/tests/helpers.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local assert = require('luassert')
local infoview = require('lean.infoview')

local lean = require('lean')

Expand Down Expand Up @@ -98,6 +99,11 @@ end
--- The number of current windows.
function helpers.get_num_wins() return #vim.api.nvim_list_wins() end

local last_num_wins

local function set_num_wins() last_num_wins = helpers.get_num_wins() end
set_num_wins()

--- Assert about the entire buffer contents.
local function has_buf_contents(_, arguments)
local buf = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), '\n')
Expand All @@ -116,11 +122,130 @@ local function has_all(_, arguments)
return true
end

local prev_buf_max = -1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems quite fragile to me because it adds global mutable state to all infoview-touching test runs. Would really prefer if we avoided doing that, we're already struggling with #94 showing us we have too much mutable state.

Copy link
Collaborator Author

@rish987 rish987 Jul 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something that you'd "opt-in" to on certain tests, and should be backwards compatible with the way we did things before (see e.g. infoview/lsp_spec.lua). ISTM that mutable state is pretty much the only way to get this kind of coverage, and the mutable state before was just littered throughout the tests themselves rather than centralized in a helper (not to mention being hopelessly incomplete).

Did you perhaps mean #93? I'm pretty sure at this point the best way to handle it would be to:

  1. Split up infoview/run_spec.lua.
  2. Add a feature to nvim-lua/plenary.nvim that allows tests to early-abort as soon as an it() fails, so only the first error is shown.

local prev_win_max = -1
local prev_buf
local prev_win

local function change_infoview(state, _)
local this_infoview = infoview.get_current_infoview()
local buf = this_infoview.bufnr
local win = this_infoview.window
local result =
((not buf and not this_infoview.is_open) or (buf and prev_buf ~= buf and this_infoview.is_open
and vim.api.nvim_buf_is_valid(buf))) and
((not win and not this_infoview.is_open) or (win and prev_win ~= win and this_infoview.is_open
and vim.api.nvim_win_is_valid(win))) and
helpers.get_num_wins() == last_num_wins
prev_buf = buf
prev_win = win
state.failure_message = table.concat({
"Failed to change: ",
("prev_buf: %s, buf: %s, buf valid: %s"):format(vim.inspect(prev_buf), vim.inspect(buf),
vim.inspect(buf and vim.api.nvim_buf_is_valid(buf))),
("prev_win: %s, win: %s, win valid: %s"):format(vim.inspect(prev_win), vim.inspect(win),
vim.inspect(win and vim.api.nvim_win_is_valid(win))),
"is_open: " .. vim.inspect(this_infoview.is_open),
("num_wins: %d, last_num_wins: %d"):format(helpers.get_num_wins(), last_num_wins)
}, "\n")

return result
end

local function open_infoview(state, arguments)
rish987 marked this conversation as resolved.
Show resolved Hide resolved
local result
local this_infoview = infoview.get_current_infoview()

local maintain = arguments[1]

local failure_message = {}

local buf = this_infoview.bufnr
local win = this_infoview.window
if state.mod then
vim.list_extend(failure_message,
{
"Failed to open: ",
"maintain: " .. vim.inspect(maintain),
("prev_buf_max: %s, buf: %s, prev_buf: %s, buf valid: %s"):format(vim.inspect(prev_buf_max),
vim.inspect(buf), vim.inspect(prev_buf),
vim.inspect(buf and vim.api.nvim_buf_is_valid(buf))),
("prev_win_max: %s, win: %s, prev_win: %s, win valid: %s"):format(vim.inspect(prev_win_max),
vim.inspect(win), vim.inspect(prev_win),
vim.inspect(win and vim.api.nvim_win_is_valid(win))),
"is_open: " .. vim.inspect(this_infoview.is_open),
("num_wins: %d, last_num_wins: %d"):format(helpers.get_num_wins(), last_num_wins)
})
if maintain then
result = this_infoview.is_open and
buf and prev_buf == buf and vim.api.nvim_buf_is_valid(buf) and
win and prev_win == win and vim.api.nvim_win_is_valid(win) and
helpers.get_num_wins() == last_num_wins
else
-- make sure this is a brand new buffer/window
result = this_infoview.is_open and
prev_buf_max < buf and vim.api.nvim_buf_is_valid(buf) and
prev_win_max < win and vim.api.nvim_win_is_valid(win) and
helpers.get_num_wins() == last_num_wins + 1
prev_buf_max = buf
prev_win_max = win
end
else
vim.list_extend(failure_message,
{
"Failed to close: ",
"maintain: " .. vim.inspect(maintain),
("buf: %s, prev_buf: %s, buf valid: %s"):format(vim.inspect(buf), vim.inspect(prev_buf),
prev_buf and vim.inspect(vim.api.nvim_buf_is_valid(prev_buf))),
("win: %s, prev_win: %s, win valid: %s"):format(vim.inspect(win), vim.inspect(prev_win),
prev_win and vim.inspect(vim.api.nvim_win_is_valid(prev_win))),
"is_open: " .. vim.inspect(this_infoview.is_open),
("num_wins: %d, last_num_wins: %d"):format(helpers.get_num_wins(), last_num_wins)
})
if maintain then
result = buf or win or prev_win or prev_buf or this_infoview.is_open
or helpers.get_num_wins() ~= last_num_wins
else
local unopened = arguments[2]
vim.list_extend(failure_message, {"unopened: " .. vim.inspect(unopened)})
if unopened then
-- if not previously opened, we don't care about prev_win or prev_buf being accurate
result = buf or win or this_infoview.is_open
or helpers.get_num_wins() ~= last_num_wins
else
-- make sure the previous window was closed
result = this_infoview.is_open or buf or win or
(not prev_buf) or vim.api.nvim_buf_is_valid(prev_buf) or
(not prev_win) or vim.api.nvim_win_is_valid(prev_win) or
helpers.get_num_wins() ~= last_num_wins - 1
end
end
end
prev_buf = buf
prev_win = win
state.failure_message = table.concat(failure_message, "\n")

set_num_wins()

return result
end

local function close_win()
local result = helpers.get_num_wins() == last_num_wins - 1
set_num_wins()
return result
end

local function new_win()
local result = helpers.get_num_wins() == last_num_wins + 1
set_num_wins()
return result
end

assert:register("assertion", "has_all", has_all)
assert:register(
"assertion",
"open_infoview",
function() return require('lean.infoview').get_current_infoview().is_open end
)
assert:register("assertion", "open_infoview", open_infoview)
assert:register("assertion", "change_infoview", change_infoview)
assert:register("assertion", "close_win", close_win)
assert:register("assertion", "new_win", new_win)

return helpers
4 changes: 1 addition & 3 deletions lua/tests/infoview/autoopen_false_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ require('tests.helpers').setup {
infoview = { autoopen = false },
}
describe('infoview', function()
vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file)

it('does not automatically open',
function(_)
vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file)
assert.is_not.open_infoview()
assert.is_not.open_infoview(false, true)
end)

it('can be opened after no autoopen',
Expand Down
24 changes: 14 additions & 10 deletions lua/tests/infoview/autoopen_spec.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
local infoview = require('lean.infoview')
local fixtures = require('tests.fixtures')
local helpers = require('tests.helpers')

require('tests.helpers').setup {
helpers.setup {
infoview = { autoopen = true },
}
describe('infoview', function()
vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file)

it('automatically opens',
function(_)
vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file)
Expand All @@ -15,7 +14,9 @@ describe('infoview', function()

it('new tab automatically opens',
function(_)
vim.api.nvim_command('tabedit ' .. fixtures.lean3_project.some_existing_file)
vim.api.nvim_command('tabnew')
assert.new_win()
vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file)
assert.open_infoview()
end)

Expand All @@ -25,19 +26,21 @@ describe('infoview', function()
assert.is_not.open_infoview()
end)

vim.api.nvim_command("tabnew")
it('opens automatically after having closen previous infoviews',
function(_)
vim.api.nvim_command("tabnew")
assert.new_win()
vim.api.nvim_command("edit lua/tests/fixtures/example-lean3-project/test/test1.lean")
assert.open_infoview()
end)

vim.api.nvim_command("tabnew")
infoview.set_autoopen(false)
it('auto-open disable',
function(_)
vim.api.nvim_command("tabnew")
infoview.set_autoopen(false)
assert.new_win()
vim.api.nvim_command("edit lua/tests/fixtures/example-lean3-project/test/test1.lean")
assert.is_not.open_infoview()
assert.is_not.open_infoview(false, true)
end)

it('open after auto-open disable',
Expand All @@ -52,10 +55,11 @@ describe('infoview', function()
assert.is_not.open_infoview()
end)

vim.api.nvim_command("tabnew")
infoview.set_autoopen(true)
it('auto-open re-enable',
function(_)
vim.api.nvim_command("tabnew")
infoview.set_autoopen(true)
assert.new_win()
vim.api.nvim_command("edit lua/tests/fixtures/example-lean3-project/test/test1.lean")
assert.open_infoview()
end)
Expand Down
54 changes: 54 additions & 0 deletions lua/tests/infoview/close_all_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
local infoview = require('lean.infoview')

require('tests.helpers').setup {
infoview = {},
}
describe('infoview', function()
it('close_all succeeds',
function(_)
vim.api.nvim_command("edit temp.lean")
infoview.get_current_infoview():open()
assert.open_infoview()

vim.api.nvim_command("tabnew")
assert.new_win()
vim.api.nvim_command("edit temp.lean")
infoview.get_current_infoview():open()
assert.open_infoview()

vim.api.nvim_command("tabnew")
assert.new_win()
vim.api.nvim_command("edit temp.lean")
infoview.get_current_infoview():open()
assert.open_infoview()
infoview.get_current_infoview():close()
assert.is_not.open_infoview()

vim.api.nvim_command("tabnew")
assert.new_win()
vim.api.nvim_command("edit temp.lean")
infoview.get_current_infoview():open()
assert.open_infoview()


local already_closed = false
local already_closed_count = 0
infoview.close_all(
function(info)
if info.window ~= vim.api.nvim_get_current_win() then
vim.api.nvim_set_current_win(info.window)
assert.change_infoview()
end
if not info.is_open then
already_closed = true
already_closed_count = already_closed_count + 1
end
end,
function()
assert.is_not.open_infoview(already_closed)
already_closed = false
end
)
assert.equals(1, already_closed_count)
end)
end)
Loading