diff --git a/lua/tests/helpers.lua b/lua/tests/helpers.lua index b65f1eec..b3c49710 100644 --- a/lua/tests/helpers.lua +++ b/lua/tests/helpers.lua @@ -1,4 +1,5 @@ local assert = require('luassert') +local infoview = require('lean.infoview') local lean = require('lean') @@ -95,9 +96,6 @@ function helpers.wait_for_line_diagnostics() assert.message("Waited for line diagnostics but none came.").True(succeeded) end ---- The number of current windows. -function helpers.get_num_wins() return #vim.api.nvim_list_wins() end - --- 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') @@ -116,11 +114,244 @@ local function has_all(_, arguments) return true end +--- The number of current windows. +function helpers.get_num_wins() return #vim.api.nvim_list_wins() end + +local last_wins = {} +local last_win = nil +local last_win_max = -1 + +local function get_wins() + local wins = {} + for _, win in pairs(vim.api.nvim_list_wins()) do + wins[win] = true + end + + return wins +end + +local function update_wins(_, arguments) + -- inductive hypothesis: last_wins is accurate to immediately before creating/closing any of the given windows + local expected_wins = vim.deepcopy(last_wins) + + local opened_wins = arguments[1] or {} + local closed_wins = arguments[2] or {} + + -- for ensuring no collisions + local opened_win_set = {} + + for _, opened_win in pairs(opened_wins) do + -- should be an actual window + assert.is_truthy(vim.api.nvim_win_is_valid(opened_win)) + + -- should be brand new + assert.is_truthy(opened_win > last_win_max) + assert.is_falsy(opened_win_set[opened_win]) + + expected_wins[opened_win] = true + opened_win_set[opened_win] = true + end + + for _, closed_win in pairs(closed_wins) do + -- should not be a window + assert.is_falsy(vim.api.nvim_win_is_valid(closed_win)) + + -- should have previously existed (should not pass a random previously/never closed window) + assert.is_truthy(expected_wins[closed_win]) + + expected_wins[closed_win] = nil + end + + assert.message("expected: " .. vim.inspect(expected_wins) .. "\n got: " .. vim.inspect(get_wins())).is_truthy( + vim.deep_equal(expected_wins, get_wins())) + + for _, opened_win in pairs(opened_wins) do + if opened_win > last_win_max then last_win_max = opened_win end + end + + -- maintain IH + last_wins = get_wins() + + -- also maintain IH for closed_win + last_win = vim.api.nvim_get_current_win() + + return true +end + +local function created_win(_, arguments) + local new_wins = arguments[1] + if new_wins then + assert.update_wins(arguments[1], nil) + else + assert.are_not.equal(last_win, vim.api.nvim_get_current_win()) + assert.update_wins({vim.api.nvim_get_current_win()}, nil) + end + + return true +end + +local function closed_win(_, arguments) + local closed_wins = arguments[1] + if closed_wins then + assert.update_wins(nil, arguments[1]) + else + -- inductive hypothesis: in addition to that of update_wins, + -- last_win must be the window we were in immediately before closing + assert.are_not.equal(last_win, vim.api.nvim_get_current_win()) + assert.update_wins(nil, {last_win}) + end + + return true +end + +local function changed_win(_, _) + assert.are_not.equal(last_win, vim.api.nvim_get_current_win()) + + assert.update_wins() + return true +end + +local function kept_win(_, _) + assert.are_equal(last_win, vim.api.nvim_get_current_win()) + + assert.update_wins() + return true +end + +local function opened_infoview(_, arguments) + local this_info = arguments[1] + + assert.is_truthy(this_info.is_open) + assert.is_truthy(this_info.bufnr) + assert.is_truthy(this_info.window) + assert.is_falsy(this_info.prev_buf) + assert.is_falsy(this_info.prev_win) + + return true +end + +local function opened_infoview_kept(_, arguments) + local this_info = arguments[1] + + assert.is_truthy(this_info.is_open) + assert.is_truthy(this_info.bufnr) + assert.is_truthy(this_info.window) + assert.is_truthy(this_info.bufnr == this_info.prev_buf) + assert.is_truthy(this_info.window == this_info.prev_win) + + return true +end + +local function closed_infoview(_, arguments) + local this_info = arguments[1] + + assert.is_falsy(this_info.is_open) + assert.is_falsy(this_info.bufnr) + assert.is_falsy(this_info.window) + assert.is_truthy(this_info.prev_buf) + assert.is_truthy(this_info.prev_win) + + return true +end + +local function closed_infoview_kept(_, arguments) + local this_info = arguments[1] + + assert.is_falsy(this_info.is_open) + assert.is_falsy(this_info.bufnr) + assert.is_falsy(this_info.window) + assert.is_falsy(this_info.prev_buf) + assert.is_falsy(this_info.prev_win) + + return true +end + +local function infoview_check(list) + local opened_wins = {} + local closed_wins = {} + + for id, this_info in pairs(infoview._by_id) do + local check = list[id] + + if not check then + -- all unspecified infoviews must have previously been accounted for + assert.is_truthy(this_info.prev_check) + -- infer check + if this_info.prev_check == "opened" then + check = "opened_kept" + elseif this_info.prev_check == "opened_kept" then + check = "opened_kept" + elseif this_info.prev_check == "closed" then + check = "closed_kept" + elseif this_info.prev_check == "closed_kept" then + check = "closed_kept" + end + else + this_info.checked = true + end + + if check == "opened" then + vim.list_extend(opened_wins, {this_info.window}) + assert.opened_infoview_state(this_info) + elseif check == "opened_kept" then + assert.opened_infoview_kept_state(this_info) + elseif check == "closed" then + vim.list_extend(closed_wins, {this_info.prev_win}) + assert.closed_infoview_state(this_info) + elseif check == "closed_kept" then + assert.closed_infoview_kept_state(this_info) + end + + this_info.prev_buf = this_info.bufnr + this_info.prev_win = this_info.window + this_info.prev_check = check + end + + -- make sure all specified infoviews were hit + for id, check in pairs(list) do + assert.is_truthy(infoview._by_id[id].checked) + assert.are_equal(check, infoview._by_id[id].prev_check) + infoview._by_id[id].checked = nil + end + + assert.update_wins(opened_wins, closed_wins) + + return true +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", "updated_infoviews", function(_, arguments) + return infoview_check(arguments[1] or {}) +end) +assert:register("assertion", "opened_infoview", function(_, arguments) + return infoview_check(arguments[1] or {[vim.api.nvim_win_get_tabpage(0)] = "opened"}) +end) +assert:register("assertion", "opened_infoview_kept", function(_, arguments) + return infoview_check(arguments[1] or {[vim.api.nvim_win_get_tabpage(0)] = "opened_kept"}) +end) +assert:register("assertion", "closed_infoview", function(_, arguments) + return infoview_check(arguments[1] or {[vim.api.nvim_win_get_tabpage(0)] = "closed"}) +end) +assert:register("assertion", "closed_infoview_kept", function(_, arguments) + return infoview_check(arguments[1] or {[vim.api.nvim_win_get_tabpage(0)] = "closed_kept"}) +end) +assert:register("assertion", "unopened_infoview", function(_, arguments) + return infoview_check(arguments[1] or {[vim.api.nvim_win_get_tabpage(0)] = "closed_kept"}) +end) + +-- internal state checks +assert:register("assertion", "opened_infoview_state", opened_infoview) +assert:register("assertion", "opened_infoview_kept_state", opened_infoview_kept) +assert:register("assertion", "closed_infoview_state", closed_infoview) +assert:register("assertion", "closed_infoview_kept_state", closed_infoview_kept) + +assert:register("assertion", "update_wins", update_wins) +assert:register("assertion", "closed_win", closed_win) +assert:register("assertion", "created_win", created_win) +assert:register("assertion", "changed_win", changed_win) +assert:register("assertion", "kept_win", kept_win) + +-- initialize on very first nvim window (base case satisfied pretty trivially) +assert.created_win() return helpers diff --git a/lua/tests/infoview/autocmd_spec.lua b/lua/tests/infoview/autocmd_spec.lua new file mode 100644 index 00000000..fc5d1ad8 --- /dev/null +++ b/lua/tests/infoview/autocmd_spec.lua @@ -0,0 +1,142 @@ +local infoview = require('lean.infoview') +local fixtures = require('tests.fixtures') + +require('tests.helpers').setup {} +describe('infoview', function() + local function update_enabled(state, _) + local cursor_hold = string.find(vim.api.nvim_exec("autocmd CursorHold ", true), "LeanInfoviewUpdate") + local cursor_hold_i = string.find(vim.api.nvim_exec("autocmd CursorHoldI ", true), "LeanInfoviewUpdate") + if state.mod then + return cursor_hold and cursor_hold_i + end + return cursor_hold or cursor_hold_i + end + + assert:register("assertion", "update_enabled", update_enabled) + + describe('CursorHold(I)', function() + it('enabled when opened', + function(_) + vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file) + infoview.get_current_infoview():open() + assert.opened_infoview() + assert.update_enabled() + end) + + it('remains enabled on BufEnter', + function(_) + vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_nested_existing_file) + assert.opened_infoview_kept() + assert.update_enabled() + end) + + it('remains enabled on WinEnter', + function(_) + vim.api.nvim_command('split ' .. fixtures.lean3_project.some_existing_file) + assert.created_win() + assert.opened_infoview_kept() + assert.update_enabled() + + vim.api.nvim_command("close") + assert.closed_win() + + vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file) + assert.opened_infoview_kept() + end) + + it('disabled when closed', + function(_) + infoview.get_current_infoview():close() + assert.closed_infoview() + assert.is_not.update_enabled() + end) + + it('remains disabled on BufEnter', + function(_) + vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_nested_existing_file) + assert.closed_infoview_kept() + assert.is_not.update_enabled() + end) + + it('remains disabled on WinEnter', + function(_) + vim.api.nvim_command('split ' .. fixtures.lean3_project.some_existing_file) + assert.created_win() + assert.closed_infoview_kept() + assert.is_not.update_enabled() + + vim.api.nvim_command("close") + assert.closed_win() + + vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file) + assert.closed_infoview_kept() + end) + + it('re-enabled when re-opened', + function(_) + infoview.get_current_infoview():open() + assert.opened_infoview() + assert.update_enabled() + end) + describe('new tab', function() + it('disables independently', + function(_) + vim.api.nvim_command("tabnew") + assert.created_win() + vim.api.nvim_command("edit " .. fixtures.lean_project.some_existing_file) + assert.unopened_infoview() + assert.is_not.update_enabled() + vim.api.nvim_command("tabprevious") + assert.changed_win() + assert.opened_infoview_kept() + assert.update_enabled() + end) + + it('enables independently', + function(_) + infoview.get_current_infoview():close() + assert.closed_infoview() + assert.is_not.update_enabled() + vim.api.nvim_command("tabnext") + assert.changed_win() + infoview.get_current_infoview():open() + assert.opened_infoview() + assert.update_enabled() + vim.api.nvim_command("tabprevious") + assert.changed_win() + assert.closed_infoview_kept() + assert.is_not.update_enabled() + end) + + it('does not enable on irrelevant file BufEnter', + function(_) + vim.api.nvim_command("tabnext") + assert.changed_win() + assert.opened_infoview_kept() + assert.update_enabled() + vim.api.nvim_command("edit temp") + assert.opened_infoview_kept() + assert.is_not.update_enabled() + end) + + it('does not enable when re-opening on irrelevant file', + function(_) + infoview.get_current_infoview():close() + assert.closed_infoview() + assert.is_not.update_enabled() + infoview.get_current_infoview():open() + assert.opened_infoview() + assert.is_not.update_enabled() + end) + + it('enabled on relevant file BufEnter', + function(_) + vim.api.nvim_command("edit " .. fixtures.lean_project.some_existing_file) + assert.opened_infoview_kept() + assert.update_enabled() + end) + end) + end) + + +end) diff --git a/lua/tests/infoview/autoopen_false_spec.lua b/lua/tests/infoview/autoopen_false_spec.lua index 5a61c2b1..2adcc018 100644 --- a/lua/tests/infoview/autoopen_false_spec.lua +++ b/lua/tests/infoview/autoopen_false_spec.lua @@ -5,17 +5,15 @@ 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.unopened_infoview() end) it('can be opened after no autoopen', function(_) infoview.get_current_infoview():open() - assert.open_infoview() + assert.opened_infoview() end) end) diff --git a/lua/tests/infoview/autoopen_spec.lua b/lua/tests/infoview/autoopen_spec.lua index e9c10b0e..5948c289 100644 --- a/lua/tests/infoview/autoopen_spec.lua +++ b/lua/tests/infoview/autoopen_spec.lua @@ -1,63 +1,67 @@ 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) - assert.open_infoview() + assert.opened_infoview() end) it('new tab automatically opens', function(_) - vim.api.nvim_command('tabedit ' .. fixtures.lean3_project.some_existing_file) - assert.open_infoview() + vim.api.nvim_command('tabnew') + assert.created_win() + vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file) + assert.opened_infoview() end) it('can be closed after autoopen', function(_) infoview.get_current_infoview():close() - assert.is_not.open_infoview() + assert.closed_infoview() end) - vim.api.nvim_command("tabnew") it('opens automatically after having closen previous infoviews', function(_) + vim.api.nvim_command("tabnew") + assert.created_win() vim.api.nvim_command("edit lua/tests/fixtures/example-lean3-project/test/test1.lean") - assert.open_infoview() + assert.opened_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.created_win() vim.api.nvim_command("edit lua/tests/fixtures/example-lean3-project/test/test1.lean") - assert.is_not.open_infoview() + assert.unopened_infoview() end) it('open after auto-open disable', function(_) infoview.get_current_infoview():open() - assert.open_infoview() + assert.opened_infoview() end) it('close after auto-open disable', function(_) infoview.get_current_infoview():close() - assert.is_not.open_infoview() + assert.closed_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.created_win() vim.api.nvim_command("edit lua/tests/fixtures/example-lean3-project/test/test1.lean") - assert.open_infoview() + assert.opened_infoview() end) it('no auto-open for irrelevant file', diff --git a/lua/tests/infoview/close_all_spec.lua b/lua/tests/infoview/close_all_spec.lua new file mode 100644 index 00000000..b6724a00 --- /dev/null +++ b/lua/tests/infoview/close_all_spec.lua @@ -0,0 +1,63 @@ +local infoview = require('lean.infoview') + +require('tests.helpers').setup { + infoview = {}, +} +describe('infoview', function() + describe('close_all succeeds', function() + it('single infoview', + function(_) + local info_changes = {} + + vim.api.nvim_command("edit temp.lean") + infoview.get_current_infoview():open() + assert.opened_infoview() + info_changes[vim.api.nvim_win_get_tabpage(0)] = "closed" + + infoview.close_all() + + assert.updated_infoviews(info_changes) + end) + + it('multiple infoviews, not all opened', + function(_) + local info_changes = {} + + vim.api.nvim_command("tabnew") + assert.created_win() + vim.api.nvim_command("edit temp.lean") + infoview.get_current_infoview():open() + assert.opened_infoview() + info_changes[vim.api.nvim_win_get_tabpage(0)] = "closed" + + vim.api.nvim_command("tabnew") + assert.created_win() + vim.api.nvim_command("edit temp.lean") + infoview.get_current_infoview():open() + assert.opened_infoview() + info_changes[vim.api.nvim_win_get_tabpage(0)] = "closed" + + vim.api.nvim_command("tabnew") + assert.created_win() + vim.api.nvim_command("edit temp.lean") + infoview.get_current_infoview():open() + assert.opened_infoview() + infoview.get_current_infoview():close() + assert.closed_infoview() + -- can actually omit this because it would be inferred by assert.updated_infoviews() + info_changes[vim.api.nvim_win_get_tabpage(0)] = "closed_kept" + + vim.api.nvim_command("tabnew") + assert.created_win() + vim.api.nvim_command("edit temp.lean") + infoview.get_current_infoview():open() + assert.opened_infoview() + info_changes[vim.api.nvim_win_get_tabpage(0)] = "closed" + + + infoview.close_all() + + assert.updated_infoviews(info_changes) + end) + end) +end) diff --git a/lua/tests/infoview/run_spec.lua b/lua/tests/infoview/run_spec.lua index 782707ef..a5df69ec 100644 --- a/lua/tests/infoview/run_spec.lua +++ b/lua/tests/infoview/run_spec.lua @@ -1,210 +1,110 @@ local infoview = require('lean.infoview') -local get_num_wins = require('tests.helpers').get_num_wins local fixtures = require('tests.fixtures') -require('tests.helpers').setup { - infoview = { autoopen = true }, -} +require('tests.helpers').setup{} describe('infoview', function() - vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file) - - local win = vim.api.nvim_get_current_win() - local infoview_info = infoview.get_current_infoview():open() - - local function update_enabled(state, _) - local cursor_hold = string.find(vim.api.nvim_exec("autocmd CursorHold ", true), "LeanInfoviewUpdate") - local cursor_hold_i = string.find(vim.api.nvim_exec("autocmd CursorHoldI ", true), "LeanInfoviewUpdate") - if state.mod then - return cursor_hold and cursor_hold_i - end - return cursor_hold or cursor_hold_i - end - - assert:register("assertion", "update_enabled", update_enabled) - describe('initial', function() - it('CursorHold(I) enabled when opened', - function(_) - assert.update_enabled() - end) - - it('closes', + it('opens', function(_) - local num_wins = get_num_wins() - infoview.get_current_infoview():close() - assert.is_false(vim.api.nvim_win_is_valid(infoview_info.window)) - assert.is.equal(num_wins - 1, get_num_wins()) - assert.is_not.open_infoview() + vim.api.nvim_command("edit " .. fixtures.lean3_project.some_existing_file) + infoview.get_current_infoview():open() + assert.opened_infoview() end) - it('remains closed on BufEnter', + it('remains open on BufEnter', function(_) - local num_wins = get_num_wins() - vim.api.nvim_command("edit lua/tests/fixtures/example-lean3-project/test/test1.lean") - assert.is.equal(num_wins, get_num_wins()) - assert.is_not.open_infoview() + vim.api.nvim_command("edit " .. fixtures.lean3_project.some_existing_file) + assert.opened_infoview_kept() end) - it('remains closed on WinEnter', + it('remains open on WinEnter', function(_) - local num_wins = get_num_wins() - vim.api.nvim_command("split lua/tests/fixtures/example-lean3-project/test.lean") - assert.is.equal(num_wins + 1, get_num_wins()) - assert.is_not.open_infoview() - end) - vim.api.nvim_command("close") - - it('CursorHold(I) disabled when closed', - function(_) - assert.is_not.update_enabled() + vim.api.nvim_command("split " .. fixtures.lean3_project.some_existing_file) + assert.created_win() + assert.opened_infoview_kept() + vim.api.nvim_command("close") + assert.closed_win() + assert.opened_infoview_kept() end) - it('opens', + it('closes', function(_) - local num_wins = get_num_wins() - infoview_info = infoview.get_current_infoview():open() - assert.is_true(vim.api.nvim_win_is_valid(infoview_info.window)) - assert.is.equal(num_wins + 1, get_num_wins()) - assert.open_infoview() + vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file) + assert.opened_infoview_kept() + infoview.get_current_infoview():close() + assert.closed_infoview() end) - it('remains open on BufWinEnter', + it('remains closed on BufEnter', function(_) - local num_wins = get_num_wins() - vim.api.nvim_command("edit lua/tests/fixtures/example-lean3-project/test.lean") - assert.is_true(vim.api.nvim_win_is_valid(infoview_info.window)) - assert.is.equal(num_wins, get_num_wins()) + vim.api.nvim_command("edit " .. fixtures.lean3_project.some_nested_existing_file) + -- would be equivalent to just do assert.updated_infoviews() here, because this would + -- correctly infer closed_infoview_kept; kept it (and others like it) for readability + assert.closed_infoview_kept() end) - it('CursorHold(I) enabled when re-opened', + it('remains closed on WinEnter', function(_) - assert.update_enabled() + vim.api.nvim_command("split " .. fixtures.lean3_project.some_existing_file) + assert.created_win() + assert.closed_infoview_kept() + vim.api.nvim_command("close") + assert.closed_win() + assert.closed_infoview_kept() end) it('manual quit succeeds and updates internal state', function(_) - local num_wins = get_num_wins() - vim.api.nvim_set_current_win(infoview_info.window) + infoview.get_current_infoview():open() + assert.opened_infoview() + vim.api.nvim_command("wincmd l") + assert.changed_win() vim.api.nvim_command("quit") - assert.is_false(vim.api.nvim_win_is_valid(infoview_info.window)) - assert.is.equal(num_wins - 1, get_num_wins()) - assert.is.equal(win, vim.api.nvim_get_current_win()) - assert.is_not.open_infoview() + assert.closed_infoview() end) it('manual close succeeds and updates internal state', function(_) - infoview_info = infoview.get_current_infoview():open() - local num_wins = get_num_wins() - vim.api.nvim_set_current_win(infoview_info.window) + infoview.get_current_infoview():open() + assert.opened_infoview() + vim.api.nvim_command("wincmd l") + assert.changed_win() vim.api.nvim_command("close") - assert.is_false(vim.api.nvim_win_is_valid(infoview_info.window)) - assert.is.equal(num_wins - 1, get_num_wins()) - assert.is.equal(win, vim.api.nvim_get_current_win()) - assert.is_not.open_infoview() + assert.closed_infoview() end) end) - infoview_info = infoview.get_current_infoview():open() - - vim.api.nvim_command("tabedit lua/tests/fixtures/example-lean4-project/Test.lean") - - local new_buf = vim.api.nvim_get_current_buf() - local new_win = vim.api.nvim_get_current_win() - local new_infoview_info = infoview.get_current_infoview():open() - describe('new tab', function() it('closes independently', function(_) - local num_wins = get_num_wins() + infoview.get_current_infoview():open() + assert.opened_infoview() + vim.api.nvim_command("tabnew") + assert.created_win() + vim.api.nvim_command("edit lua/tests/fixtures/example-lean4-project/Test.lean") + infoview.get_current_infoview():open() + assert.opened_infoview() infoview.get_current_infoview():close() - assert.is_true(vim.api.nvim_win_is_valid(infoview_info.window)) - assert.is_false(vim.api.nvim_win_is_valid(new_infoview_info.window)) - assert.is.equal(num_wins - 1, get_num_wins()) - assert.is_not.open_infoview() - end) - - it('CursorHold(I) disabled independently', - function(_) - assert.is_not.update_enabled() - vim.api.nvim_set_current_win(win) - assert.is.update_enabled() - end) - - it('CursorHold(I) updated on BufEnter', - function(_) - vim.api.nvim_set_current_buf(new_buf) - assert.is.update_enabled() - end) - - it('CursorHold(I) updated on WinEnter', - function(_) - vim.api.nvim_set_current_win(new_win) - assert.is_not.update_enabled() - end) - - it('remains closed on BufWinEnter', - function(_) - local num_wins = get_num_wins() - vim.api.nvim_command("edit lua/tests/fixtures/example-lean4-project/Test/Test1.lean") - assert.is.equal(num_wins, get_num_wins()) + assert.closed_infoview() + vim.api.nvim_command("tabprevious") + assert.changed_win() + assert.opened_infoview_kept() end) it('opens independently', function(_) - vim.api.nvim_set_current_win(win) infoview.get_current_infoview():close() - vim.api.nvim_set_current_win(new_win) - local num_wins = get_num_wins() - new_infoview_info = infoview.get_current_infoview():open() - assert.is_false(vim.api.nvim_win_is_valid(infoview_info.window)) - assert.is_true(vim.api.nvim_win_is_valid(new_infoview_info.window)) - assert.is.equal(num_wins + 1, get_num_wins()) - assert.open_infoview() + assert.closed_infoview() + vim.api.nvim_command("tabnext") + assert.changed_win() + infoview.get_current_infoview():open() + assert.opened_infoview() + vim.api.nvim_command("tabprevious") + assert.changed_win() + assert.closed_infoview_kept() + vim.api.nvim_command("tabnext") + assert.changed_win() + assert.opened_infoview_kept() end) - - it('remains open on BufWinEnter', - function(_) - local num_wins = get_num_wins() - vim.api.nvim_command("edit lua/tests/fixtures/example-lean4-project/Test/Test1.lean") - assert.is_true(vim.api.nvim_win_is_valid(new_infoview_info.window)) - assert.is.equal(num_wins, get_num_wins()) - end) - - it('does not set CursorHold(I) on irrelevant file BufEnter', - function(_) - vim.api.nvim_command("edit temp") - assert.is_not.update_enabled() - end) - - it('does not set CursorHold(I) when re-opening on irrelevant file', - function(_) - infoview.get_current_infoview():close() - new_infoview_info = infoview.get_current_infoview():open() - assert.is_not.update_enabled() - end) - - it('updates CursorHold(I) on relevant file BufEnter', - function(_) - vim.api.nvim_set_current_buf(new_buf) - assert.update_enabled() - end) - end) - - -- TODO - it('close_all succeeds', - function(_) - local num_wins = get_num_wins() - infoview.close_all() - - -- should be exactly 1 open at the moment - assert.is.equal(num_wins - 1, get_num_wins()) - - for _, tab in pairs(vim.api.nvim_list_tabpages()) do - vim.api.nvim_set_current_tabpage(tab) - if infoview.get_current_infoview() then - assert.is_not.open_infoview() - end - end end) end) diff --git a/lua/tests/infoview/start_spec.lua b/lua/tests/infoview/start_spec.lua index db21ca74..3b55ffe3 100644 --- a/lua/tests/infoview/start_spec.lua +++ b/lua/tests/infoview/start_spec.lua @@ -5,18 +5,15 @@ require('tests.helpers').setup { infoview = { autoopen = true } } describe('infoview', function() describe("startup", function() local src_win = vim.api.nvim_get_current_win() - vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file) - local infoview_info = infoview.get_current_infoview():open() - it('created valid infoview', function(_) - assert.is_true(vim.api.nvim_win_is_valid(infoview_info.window)) - assert.is_true(vim.api.nvim_buf_is_valid(infoview_info.bufnr)) + vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file) + assert.opened_infoview() end) it('starts with the window position at the top', function(_) - local cursor = vim.api.nvim_win_get_cursor(infoview_info.window) + local cursor = vim.api.nvim_win_get_cursor(infoview.get_current_infoview().window) assert.is.same(1, cursor[1]) end) @@ -26,25 +23,21 @@ describe('infoview', function() end) end) - local orig_infoview_info = infoview.get_current_infoview():open() - - vim.api.nvim_command("tabnew") describe("new tab", function() - local src_win = vim.api.nvim_get_current_win() - vim.api.nvim_command('edit ' .. fixtures.lean_project.some_existing_file) - local infoview_info = infoview.get_current_infoview():open() + local src_win it('created valid distinct infoview', function(_) - assert.is_true(vim.api.nvim_win_is_valid(infoview_info.window)) - assert.is_true(vim.api.nvim_buf_is_valid(infoview_info.bufnr)) - assert.are_not.equal(orig_infoview_info.bufnr, infoview_info.bufnr) - assert.are_not.equal(orig_infoview_info.window, infoview_info.window) + vim.api.nvim_command("tabnew") + assert.created_win() + src_win = vim.api.nvim_get_current_win() + vim.api.nvim_command('edit ' .. fixtures.lean_project.some_existing_file) + assert.opened_infoview() end) it('starts with the window position at the top', function(_) - local cursor = vim.api.nvim_win_get_cursor(infoview_info.window) + local cursor = vim.api.nvim_win_get_cursor(infoview.get_current_infoview().window) assert.is.same(1, cursor[1]) end) @@ -53,6 +46,4 @@ describe('infoview', function() assert.is.same(src_win, vim.api.nvim_get_current_win()) end) end) - - infoview.get_current_infoview():close() end) diff --git a/lua/tests/infoview/toggle_spec.lua b/lua/tests/infoview/toggle_spec.lua index 5c10d5f7..d3a64ae4 100644 --- a/lua/tests/infoview/toggle_spec.lua +++ b/lua/tests/infoview/toggle_spec.lua @@ -5,26 +5,26 @@ require('tests.helpers').setup { infoview = { autoopen = true }, } describe('Infoview.toggle', function() - vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file) - it('closes an open infoview', function() - assert.open_infoview() + vim.api.nvim_command('edit ' .. fixtures.lean3_project.some_existing_file) + assert.opened_infoview() infoview.get_current_infoview():toggle() - assert.is_not.open_infoview() + assert.closed_infoview() end) it('opens a closed infoview', function() - assert.is_not.open_infoview() infoview.get_current_infoview():toggle() - assert.open_infoview() + assert.opened_infoview() end) it('toggles back and forth', function() - assert.open_infoview() infoview.get_current_infoview():toggle() + assert.closed_infoview() infoview.get_current_infoview():toggle() + assert.opened_infoview() infoview.get_current_infoview():toggle() + assert.closed_infoview() infoview.get_current_infoview():toggle() - assert.open_infoview() + assert.opened_infoview() end) end)