From ab45ad29825a73fbb1d6358294816dc0e4583045 Mon Sep 17 00:00:00 2001 From: mpaulson Date: Fri, 3 Nov 2023 18:30:35 -0600 Subject: [PATCH] feat: good ass testing going on here --- lua/harpoon2/test/harpoon_spec.lua | 48 ++++++++++++++++++++++++++---- lua/harpoon2/test/utils.lua | 17 +++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 lua/harpoon2/test/utils.lua diff --git a/lua/harpoon2/test/harpoon_spec.lua b/lua/harpoon2/test/harpoon_spec.lua index 3c80711..e37427b 100644 --- a/lua/harpoon2/test/harpoon_spec.lua +++ b/lua/harpoon2/test/harpoon_spec.lua @@ -1,3 +1,4 @@ +local utils = require("harpoon2.test.utils") local Data = require("harpoon2.data") local harpoon = require("harpoon2") @@ -18,16 +19,13 @@ describe("harpoon", function() local file_name = "/tmp/harpoon-test" local row = 3 local col = 1 - local bufnr = vim.fn.bufnr(file_name, true) local default_key = harpoon:info().default_key - vim.api.nvim_set_current_buf(bufnr) - vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, { + local bufnr = utils.create_file(file_name, { "foo", "bar", "baz", "qux" - }) - vim.api.nvim_win_set_cursor(0, {row, col}) + }, row, col) local list = harpoon:list():append() harpoon:sync() @@ -36,6 +34,46 @@ describe("harpoon", function() [default_key] = list:encode() }) end) + + it("prepend/append double add", function() + local default_key = harpoon:info().default_key + local file_name_1 = "/tmp/harpoon-test" + local row_1 = 3 + local col_1 = 1 + + local file_name_2 = "/tmp/harpoon-test-2" + local row_2 = 1 + local col_2 = 2 + + local contents = { "foo", "bar", "baz", "qux" } + + local bufnr_1 = utils.create_file(file_name_1, contents, row_1, col_1) + local list = harpoon:list():append() + + utils.create_file(file_name_2, contents, row_2, col_2) + harpoon:list():prepend() + + harpoon:sync() + + eq(harpoon:dump(), { + [default_key] = list:encode() + }) + + eq(list.items, { + {value = file_name_2, context = {row = row_2, col = col_2}}, + {value = file_name_1, context = {row = row_1, col = col_1}}, + }) + + harpoon:list():append() + vim.api.nvim_set_current_buf(bufnr_1) + harpoon:list():prepend() + + eq(list.items, { + {value = file_name_2, context = {row = row_2, col = col_2}}, + {value = file_name_1, context = {row = row_1, col = col_1}}, + }) + + end) end) diff --git a/lua/harpoon2/test/utils.lua b/lua/harpoon2/test/utils.lua new file mode 100644 index 0000000..e40b3bf --- /dev/null +++ b/lua/harpoon2/test/utils.lua @@ -0,0 +1,17 @@ + +local M = {} + +---@param name string +---@param contents string[] +function M.create_file(name, contents, row, col) + local bufnr = vim.fn.bufnr(name, true) + vim.api.nvim_set_current_buf(bufnr) + vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, contents) + if row then + vim.api.nvim_win_set_cursor(0, {row, col}) + end + + return bufnr +end + +return M