From 53e865454bda09b02079d310bced71e15a7b87a5 Mon Sep 17 00:00:00 2001 From: Melker Ulander Date: Tue, 21 Jan 2025 17:46:40 +0100 Subject: [PATCH] test: add unit test for out of bounds issue (#364) --- tests/custom_surrounds_spec.lua | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tests/custom_surrounds_spec.lua diff --git a/tests/custom_surrounds_spec.lua b/tests/custom_surrounds_spec.lua new file mode 100644 index 0000000..ab72ee1 --- /dev/null +++ b/tests/custom_surrounds_spec.lua @@ -0,0 +1,41 @@ +local set_lines = function(lines) + vim.api.nvim_buf_set_lines(0, 0, -1, false, lines) +end + +local check_lines = function(lines) + assert.are.same(lines, vim.api.nvim_buf_get_lines(0, 0, -1, false)) +end + +describe("custom surrounds", function() + before_each(function() + local bufnr = vim.api.nvim_create_buf(true, true) + vim.api.nvim_win_set_buf(0, bufnr) + end) + + it("deletes surrounding code block", function() + require("nvim-surround").buffer_setup({ + surrounds = { + c = { + add = function() + return { { '```', '' }, { '', '```' } } + end, + find = '(```[a-zA-Z]*\n)().-(\n```)()', + delete = '(```[a-zA-Z]*\n)().-(\n```)()', + }, + }, + }) + + set_lines({ + [[```lua]], + [[print('foo')]], + [[```]], + }) + + -- The out of bounds issue doesn't occur on the first line + vim.cmd("normal j") + local success, err = pcall(vim.cmd.normal, "dsc") + + assert(success, err) + check_lines({ "print('foo')" }) + end) +end)