From 1f29e74d1247129db8ff36f304ec0a00a0c0a89a Mon Sep 17 00:00:00 2001 From: tanvirtin Date: Wed, 3 Jul 2024 20:53:02 -0400 Subject: [PATCH] chore(tests): add test for Namespace --- lua/vgit/core/Namespace.lua | 44 +++++------- tests/unit/core/Namespace_spec.lua | 110 +++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 26 deletions(-) create mode 100644 tests/unit/core/Namespace_spec.lua diff --git a/lua/vgit/core/Namespace.lua b/lua/vgit/core/Namespace.lua index 6c1cf7a4..8b0c0621 100644 --- a/lua/vgit/core/Namespace.lua +++ b/lua/vgit/core/Namespace.lua @@ -23,14 +23,15 @@ function Namespace:add_highlight(buffer, opts) local row = opts.row local col_range = opts.col_range - pcall(vim.api.nvim_buf_add_highlight, buffer.bufnr, self.ns_id, hl, row, col_range.from, col_range.to) - - return self + return pcall(vim.api.nvim_buf_add_highlight, buffer.bufnr, self.ns_id, hl, row, col_range.from, col_range.to) end function Namespace:add_pattern_highlight(buffer, pattern, hl) local lines = buffer:get_lines() + local err + local is_successful = true + for i = 1, #lines do local line = lines[i] @@ -40,7 +41,7 @@ function Namespace:add_pattern_highlight(buffer, pattern, hl) j = from if from == nil then break end - self:add_highlight(buffer, { + local success, hl_err = self:add_highlight(buffer, { hl = hl, row = i - 1, col_range = { @@ -48,10 +49,15 @@ function Namespace:add_pattern_highlight(buffer, pattern, hl) to = to, }, }) + + if not success then + err = hl_err + is_successful = false + end end end - return self + return is_successful, err end function Namespace:transpose_virtual_text(buffer, opts) @@ -64,15 +70,13 @@ function Namespace:transpose_virtual_text(buffer, opts) local id = row + 1 + col - pcall(vim.api.nvim_buf_set_extmark, buffer.bufnr, self.ns_id, row, col, { + return pcall(vim.api.nvim_buf_set_extmark, buffer.bufnr, self.ns_id, row, col, { id = id, virt_text = { { text, hl } }, virt_text_pos = pos or 'overlay', hl_mode = 'combine', priority = priority, }) - - return id end function Namespace:transpose_virtual_line(buffer, opts) @@ -83,15 +87,13 @@ function Namespace:transpose_virtual_line(buffer, opts) local id = row + 1 - pcall(vim.api.nvim_buf_set_extmark, buffer.bufnr, self.ns_id, row, 0, { + return pcall(vim.api.nvim_buf_set_extmark, buffer.bufnr, self.ns_id, row, 0, { id = id, virt_text = texts, virt_text_pos = pos or 'overlay', hl_mode = 'combine', priority = priority, }) - - return id end function Namespace:transpose_virtual_line_number(buffer, opts) @@ -100,14 +102,12 @@ function Namespace:transpose_virtual_line_number(buffer, opts) local text = opts.text local id = self.virtual_line_number_id + row + 1 - pcall(vim.api.nvim_buf_set_extmark, buffer.bufnr, self.ns_id, row, 0, { + return pcall(vim.api.nvim_buf_set_extmark, buffer.bufnr, self.ns_id, row, 0, { id = id, virt_text = { { text, hl } }, virt_text_pos = 'inline', hl_mode = 'combine', }) - - return id end function Namespace:insert_virtual_line(buffer, opts) @@ -118,31 +118,25 @@ function Namespace:insert_virtual_line(buffer, opts) local id = self.virtual_line_number_id + row + 1 - pcall(vim.api.nvim_buf_set_extmark, buffer.bufnr, self.ns_id, row, 0, { + return pcall(vim.api.nvim_buf_set_extmark, buffer.bufnr, self.ns_id, row, 0, { id = id, virt_lines = { { { text, hl } } }, virt_lines_above = true, priority = priority, }) - - return id end function Namespace:sign_place(buffer, lnum, sign_name) - pcall(vim.fn.sign_place, lnum, self:get_sign_ns_id(buffer), sign_name, buffer.bufnr, { + return pcall(vim.fn.sign_place, lnum, self:get_sign_ns_id(buffer), sign_name, buffer.bufnr, { id = lnum, lnum = lnum, buffer = buffer.bufnr, priority = signs_setting:get('priority'), }) - - return self end function Namespace:sign_unplace(buffer, lnum) - pcall(vim.fn.sign_unplace, self:get_sign_ns_id(buffer), { buffer = buffer.bufnr, id = lnum }) - - return self + return pcall(vim.fn.sign_unplace, self:get_sign_ns_id(buffer), { buffer = buffer.bufnr, id = lnum }) end function Namespace:clear(buffer, row_range) @@ -150,9 +144,7 @@ function Namespace:clear(buffer, row_range) local row_from = row_range.from or 0 local row_to = row_range.to or -1 - pcall(vim.api.nvim_buf_clear_namespace, buffer.bufnr, self.ns_id, row_from, row_to) - - return self + return pcall(vim.api.nvim_buf_clear_namespace, buffer.bufnr, self.ns_id, row_from, row_to) end return Namespace diff --git a/tests/unit/core/Namespace_spec.lua b/tests/unit/core/Namespace_spec.lua new file mode 100644 index 00000000..20e235f8 --- /dev/null +++ b/tests/unit/core/Namespace_spec.lua @@ -0,0 +1,110 @@ +local sign = require('vgit.core.sign') +local Buffer = require('vgit.core.Buffer') +local highlight = require('vgit.core.highlight') +local Namespace = require('vgit.core.Namespace') + +describe('Namespace', function() + local buffer = Buffer(0) + + highlight.register_module(function() + sign.register_module() + end) + + describe('constructor', function() + it('should create a new namespace with a unique ID', function() + local namespace = Namespace() + assert.is_number(namespace.ns_id) + end) + end) + + describe('get_sign_ns_id', function() + it('should return the correct sign namespace ID for a buffer', function() + local namespace = Namespace() + local sign_ns_id = namespace:get_sign_ns_id(buffer) + assert.equals('tanvirtin/vgit.nvim/hunk/signs/' .. buffer.bufnr, sign_ns_id) + end) + end) + + describe('add_highlight', function() + it('should add a highlight to a buffer', function() + local namespace = Namespace() + local opts = { hl = 'Error', row = 0, col_range = { from = 0, to = 1 } } + local success = namespace:add_highlight(buffer, opts) + assert.is_true(success) + end) + end) + + describe('add_pattern_highlight', function() + it('should add highlights based on pattern', function() + local namespace = Namespace() + vim.api.nvim_buf_set_lines(buffer.bufnr, 0, -1, false, { 'test pattern' }) + local success = namespace:add_pattern_highlight(buffer, 'pattern', 'Error') + assert.is_true(success) + end) + end) + + describe('transpose_virtual_text', function() + it('should add virtual text to a buffer', function() + local namespace = Namespace() + local opts = { text = 'virtual', hl = 'Error', row = 0, col = 0 } + local success, id = namespace:transpose_virtual_text(buffer, opts) + assert.is_true(success) + assert.is_number(id) + end) + end) + + describe('transpose_virtual_line', function() + it('should add virtual lines to a buffer', function() + local namespace = Namespace() + local opts = { texts = { { 'virtual', 'Error' } }, row = 0 } + local success, id = namespace:transpose_virtual_line(buffer, opts) + assert.is_true(success) + assert.is_number(id) + end) + end) + + describe('transpose_virtual_line_number', function() + it('should add virtual line numbers to a buffer', function() + local namespace = Namespace() + local opts = { text = 'virtual', hl = 'Error', row = 0 } + local success, id = namespace:transpose_virtual_line_number(buffer, opts) + assert.is_true(success) + assert.is_number(id) + end) + end) + + describe('insert_virtual_line', function() + it('should insert virtual lines above a row in a buffer', function() + local namespace = Namespace() + local opts = { text = 'virtual', hl = 'Error', row = 0 } + local success, id = namespace:insert_virtual_line(buffer, opts) + assert.is_true(success) + assert.is_number(id) + end) + end) + + describe('sign_place', function() + it('should place a sign in the buffer', function() + local namespace = Namespace() + local success = namespace:sign_place(buffer, 1, 'GitSignsAdd') + assert.is_true(success) + end) + end) + + describe('sign_unplace', function() + it('should remove a sign from the buffer', function() + local namespace = Namespace() + namespace:sign_place(buffer, 1, 'GitSignsAdd') + local success = namespace:sign_unplace(buffer, 1) + assert.is_true(success) + end) + end) + + describe('clear', function() + it('should clear highlights from the buffer', function() + local namespace = Namespace() + local success = namespace:clear(buffer) + assert.is_true(success) + end) + end) +end)