feat(test): misc tests for more robustness
This commit is contained in:
parent
0e3bbaaf95
commit
c16699004a
1 changed files with 155 additions and 0 deletions
|
|
@ -150,4 +150,159 @@ describe('diffs', function()
|
|||
vim.api.nvim_buf_delete(bufnr, { force = true })
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('is_fugitive_buffer', function()
|
||||
it('returns true for fugitive:// URLs', function()
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr, 'fugitive:///path/to/repo/.git//abc123:file.lua')
|
||||
assert.is_true(diffs.is_fugitive_buffer(bufnr))
|
||||
vim.api.nvim_buf_delete(bufnr, { force = true })
|
||||
end)
|
||||
|
||||
it('returns false for normal paths', function()
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
vim.api.nvim_buf_set_name(bufnr, '/home/user/project/file.lua')
|
||||
assert.is_false(diffs.is_fugitive_buffer(bufnr))
|
||||
vim.api.nvim_buf_delete(bufnr, { force = true })
|
||||
end)
|
||||
|
||||
it('returns false for empty buffer names', function()
|
||||
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||
assert.is_false(diffs.is_fugitive_buffer(bufnr))
|
||||
vim.api.nvim_buf_delete(bufnr, { force = true })
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('diff mode', function()
|
||||
local function create_diff_window()
|
||||
vim.cmd('new')
|
||||
local win = vim.api.nvim_get_current_win()
|
||||
local buf = vim.api.nvim_get_current_buf()
|
||||
vim.wo[win].diff = true
|
||||
return win, buf
|
||||
end
|
||||
|
||||
local function close_window(win)
|
||||
if vim.api.nvim_win_is_valid(win) then
|
||||
vim.api.nvim_win_close(win, true)
|
||||
end
|
||||
end
|
||||
|
||||
before_each(function()
|
||||
diffs.setup({ enabled = true })
|
||||
end)
|
||||
|
||||
describe('attach_diff', function()
|
||||
it('applies winhighlight to diff windows', function()
|
||||
local win, _ = create_diff_window()
|
||||
diffs.attach_diff()
|
||||
|
||||
local whl = vim.api.nvim_get_option_value('winhighlight', { win = win })
|
||||
assert.is_not_nil(whl:match('DiffAdd:DiffsDiffAdd'))
|
||||
assert.is_not_nil(whl:match('DiffDelete:DiffsDiffDelete'))
|
||||
|
||||
close_window(win)
|
||||
end)
|
||||
|
||||
it('does nothing when enabled=false', function()
|
||||
diffs.setup({ enabled = false })
|
||||
local win, _ = create_diff_window()
|
||||
diffs.attach_diff()
|
||||
|
||||
local whl = vim.api.nvim_get_option_value('winhighlight', { win = win })
|
||||
assert.are.equal('', whl)
|
||||
|
||||
close_window(win)
|
||||
end)
|
||||
|
||||
it('is idempotent', function()
|
||||
local win, _ = create_diff_window()
|
||||
assert.has_no.errors(function()
|
||||
diffs.attach_diff()
|
||||
diffs.attach_diff()
|
||||
diffs.attach_diff()
|
||||
end)
|
||||
|
||||
local whl = vim.api.nvim_get_option_value('winhighlight', { win = win })
|
||||
assert.is_not_nil(whl:match('DiffAdd:DiffsDiffAdd'))
|
||||
|
||||
close_window(win)
|
||||
end)
|
||||
|
||||
it('applies to multiple diff windows', function()
|
||||
local win1, _ = create_diff_window()
|
||||
local win2, _ = create_diff_window()
|
||||
diffs.attach_diff()
|
||||
|
||||
local whl1 = vim.api.nvim_get_option_value('winhighlight', { win = win1 })
|
||||
local whl2 = vim.api.nvim_get_option_value('winhighlight', { win = win2 })
|
||||
assert.is_not_nil(whl1:match('DiffAdd:DiffsDiffAdd'))
|
||||
assert.is_not_nil(whl2:match('DiffAdd:DiffsDiffAdd'))
|
||||
|
||||
close_window(win1)
|
||||
close_window(win2)
|
||||
end)
|
||||
|
||||
it('ignores non-diff windows', function()
|
||||
vim.cmd('new')
|
||||
local non_diff_win = vim.api.nvim_get_current_win()
|
||||
|
||||
local diff_win, _ = create_diff_window()
|
||||
diffs.attach_diff()
|
||||
|
||||
local non_diff_whl = vim.api.nvim_get_option_value('winhighlight', { win = non_diff_win })
|
||||
local diff_whl = vim.api.nvim_get_option_value('winhighlight', { win = diff_win })
|
||||
|
||||
assert.are.equal('', non_diff_whl)
|
||||
assert.is_not_nil(diff_whl:match('DiffAdd:DiffsDiffAdd'))
|
||||
|
||||
close_window(non_diff_win)
|
||||
close_window(diff_win)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('detach_diff', function()
|
||||
it('clears winhighlight from tracked windows', function()
|
||||
local win, _ = create_diff_window()
|
||||
diffs.attach_diff()
|
||||
diffs.detach_diff()
|
||||
|
||||
local whl = vim.api.nvim_get_option_value('winhighlight', { win = win })
|
||||
assert.are.equal('', whl)
|
||||
|
||||
close_window(win)
|
||||
end)
|
||||
|
||||
it('does not error when no windows are tracked', function()
|
||||
assert.has_no.errors(function()
|
||||
diffs.detach_diff()
|
||||
end)
|
||||
end)
|
||||
|
||||
it('handles already-closed windows gracefully', function()
|
||||
local win, _ = create_diff_window()
|
||||
diffs.attach_diff()
|
||||
close_window(win)
|
||||
|
||||
assert.has_no.errors(function()
|
||||
diffs.detach_diff()
|
||||
end)
|
||||
end)
|
||||
|
||||
it('clears all tracked windows', function()
|
||||
local win1, _ = create_diff_window()
|
||||
local win2, _ = create_diff_window()
|
||||
diffs.attach_diff()
|
||||
diffs.detach_diff()
|
||||
|
||||
local whl1 = vim.api.nvim_get_option_value('winhighlight', { win = win1 })
|
||||
local whl2 = vim.api.nvim_get_option_value('winhighlight', { win = win2 })
|
||||
assert.are.equal('', whl1)
|
||||
assert.are.equal('', whl2)
|
||||
|
||||
close_window(win1)
|
||||
close_window(win2)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue