Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
test: Included a test for harpoon issue #555 (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: abeldekat <abel@nomail.com>
  • Loading branch information
abeldekat and abeldekat authored Apr 8, 2024
1 parent b228c3f commit f7615e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
38 changes: 18 additions & 20 deletions lua/harpoonline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,50 +167,48 @@ H.create_autocommands = function()
})
end

-- Update the data when the user adds to or removes from a list.
-- Needed because those actions can be done without leaving the buffer.
-- All other update scenarios are covered by listening to the BufEnter event.
H.create_extensions = function(Extensions)
H.harpoon_plugin:extend({ [Extensions.event_names.ADD] = H.update })
H.harpoon_plugin:extend({ [Extensions.event_names.REMOVE] = H.update })
end

---@return HarpoonList
H.get_list = function() return H.harpoon_plugin:list(H.data.list_name) end

-- If the current buffer is harpooned, return the index of the harpoon mark
-- Otherwise, return nil
---@param list HarpoonList
---@return number|nil
H.buffer_idx = function()
H.buffer_idx = function(list)
if vim.bo.buftype ~= '' then return end -- not a normal buffer
if list:length() == 0 then return end -- no items in the list

local current_file = vim.fn.expand('%:p:.')
local marks = H.get_list().items
for idx, item in ipairs(marks) do
for idx, item in ipairs(list.items) do
if item.value == current_file then return idx end
end
end

-- Update the data when the user adds to or removes from a list.
-- Needed because those actions can be done without leaving the buffer.
-- All other update scenarios are covered by listening to the BufEnter event.
H.create_extensions = function(Extensions)
H.harpoon_plugin:extend({
[Extensions.event_names.ADD] = H.update,
})
H.harpoon_plugin:extend({
[Extensions.event_names.REMOVE] = H.update,
})
end

H.update_data = function()
H.data.list_length = H.get_list():length()
H.data.buffer_idx = H.buffer_idx()
---@param list HarpoonList
H.update_data = function(list)
H.data.list_length = list:length()
H.data.buffer_idx = H.buffer_idx(list)
end

-- To be invoked on any harpoon-related event
-- Performs action on_update if present
H.update = function()
H.update_data()
H.update_data(H.get_list())
H.cached_result = nil -- the format function should recompute

local on_update = H.get_config().on_update
if on_update then on_update() end
end

H.initialize = function() H.update_data() end
H.initialize = function() H.update_data(H.get_list()) end

-- Return either the icon or an empty string
---@return string
Expand Down
11 changes: 9 additions & 2 deletions tests/test_harpoonline.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
local MiniTest = require('mini.test')
local new_set = MiniTest.new_set
local eq = MiniTest.expect.equality
-- local expect = MiniTest.expect

---@class MiniTestChildNeovim
---@field stop function
---@field restart function
---@field lua function
---@field lua_get function
---@field cmd function
---@field api function

---@type MiniTestChildNeovim
local child = MiniTest.new_child_neovim() -- Create (but not start) child Neovim object
Expand Down Expand Up @@ -139,6 +137,15 @@ T['format()']['extended']['remove item'] = function()
child.lua([[ require("harpoon"):list():remove_at(3) ]])
eq(child.lua_get([[ M.format() ]]), icon .. ' 1 2 ')
end
T['format()']['extended']['remove all items'] = function()
child.lua([[M.setup()]])
add_files_to_list({ '1', '2' })
child.lua([[ require("harpoon"):list():remove_at(2) ]])
child.lua([[ require("harpoon"):list():remove_at(1) ]])
eq(child.lua_get([[ M.format() ]]), icon .. ' 1 ') -- should be empty

MiniTest.add_note('Incorrect, not empty! See harpoon issue #555')
end
T['format()']['extended']['switch list'] = function()
child.lua([[M.setup()]])
add_files_to_list({ '1', '2' }, 'dev')
Expand Down

0 comments on commit f7615e6

Please sign in to comment.