Skip to content

Commit

Permalink
chore(tests): add test for Namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tanvirtin committed Jul 4, 2024
1 parent 081077b commit 1f29e74
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 26 deletions.
44 changes: 18 additions & 26 deletions lua/vgit/core/Namespace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -40,18 +41,23 @@ 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 = {
from = from - 1,
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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -118,41 +118,33 @@ 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)
row_range = row_range or {}
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
110 changes: 110 additions & 0 deletions tests/unit/core/Namespace_spec.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 1f29e74

Please sign in to comment.