diff --git a/lua/lean/_util.lua b/lua/lean/_util.lua index 48139343..361208ad 100644 --- a/lua/lean/_util.lua +++ b/lua/lean/_util.lua @@ -120,6 +120,11 @@ function M.load_mappings(mappings, buffer) end end +--- Build a single-line string out a multiline one, replacing \n with spaces. +function M.s(str) + return M.dedent(str):gsub('\n', ' ') +end + -- from mfussenegger/nvim-lsp-compl@29a81f3 function M.mk_handler(fn) return function(...) diff --git a/lua/tests/helpers.lua b/lua/tests/helpers.lua index e7b36c93..823fd3d9 100644 --- a/lua/tests/helpers.lua +++ b/lua/tests/helpers.lua @@ -59,7 +59,18 @@ end --- ---@param opts MoveCursorOpts function helpers.move_cursor(opts) - vim.api.nvim_win_set_cursor(opts.window or 0, opts.to) + local window = opts.window or 0 + + if not vim.deep_equal(opts.to, vim.api.nvim_win_get_cursor(window)) then + local message = util.s[[ + Cursor is already at %s. + If you just want to ensure the cursor is at this location, + use nvim_win_set_cursor directly. + ]] + error(message:format(vim.inspect(opts.to))) + end + + vim.api.nvim_win_set_cursor(window, opts.to) vim.cmd.doautocmd('CursorMoved') end diff --git a/lua/tests/infoview/contents_spec.lua b/lua/tests/infoview/contents_spec.lua index 94492951..c274120a 100644 --- a/lua/tests/infoview/contents_spec.lua +++ b/lua/tests/infoview/contents_spec.lua @@ -46,8 +46,6 @@ describe('infoview content (auto-)update', function() end) it('updates when the cursor moves', function() - assert.are_not.same(vim.api.nvim_win_get_cursor(0), {3, 0}) - helpers.move_cursor{ to = {3, 0} } -- FIXME: Trailing extra newline. assert.infoview_contents.are[[ @@ -101,7 +99,6 @@ describe('infoview content (auto-)update', function() it('does not error while closed and continues updating when reopened', function() assert.windows.are(lean_window, infoview.get_current_infoview().window) - assert.are_not.same(vim.api.nvim_win_get_cursor(0), {1, 0}) infoview.close() diff --git a/lua/tests/infoview/widgets_spec.lua b/lua/tests/infoview/widgets_spec.lua index 3f8d4e25..b2c8ad45 100644 --- a/lua/tests/infoview/widgets_spec.lua +++ b/lua/tests/infoview/widgets_spec.lua @@ -23,7 +23,7 @@ describe('infoview widgets', helpers.clean_buffer('#check Nat', function() ]] vim.api.nvim_set_current_win(current_infoview.window) - helpers.move_cursor{ to = {2, 4} } -- `Type` + helpers.move_cursor{ to = {2, 5} } -- `Type` local known_windows = { lean_window, current_infoview.window } assert.windows.are(known_windows) @@ -47,7 +47,7 @@ describe('infoview widgets', helpers.clean_buffer('#check Nat', function() helpers.move_cursor{ to = {1, 8} } helpers.wait_for_loading_pins() vim.api.nvim_set_current_win(tab2_infoview.window) - helpers.move_cursor{ to = {2, 4} } -- `Type` + helpers.move_cursor{ to = {2, 5} } -- `Type` helpers.feed('') helpers.wait_for_new_window({ tab2_window, tab2_infoview.window })