repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by admin
neovim/tests/std/auto_spec.lua
Rawlocal auto = require('std.auto')
describe('std.auto', function()
after_each(function()
vim.api.nvim_del_augroup_by_name('test_group')
end)
describe('group-scoped autocmds (no buffer)', function()
it('should register and fire a group autocmd', function()
local fired = false
auto 'test_group' {
events = 'User',
pattern = 'TestEvent',
command = function() fired = true end,
}
vim.cmd('doautocmd User TestEvent')
assert.is_true(fired)
end)
it('should pass group to nvim_create_autocmd', function()
auto 'test_group' {
events = 'User',
pattern = 'TestGroupScoped',
command = function() end,
}
local autocmds = vim.api.nvim_get_autocmds({ group = 'test_group' })
assert.is_true(#autocmds > 0)
end)
end)
describe('buffer-local autocmds (with buffer)', function()
it('should fire a buffer-local autocmd', function()
local buf = vim.api.nvim_create_buf(true, false)
local fired = false
auto 'test_group' {
events = 'BufWinEnter',
buffer = buf,
command = function() fired = true end,
}
vim.api.nvim_exec_autocmds('BufWinEnter', { buf = buf })
assert.is_true(fired)
vim.api.nvim_buf_delete(buf, { force = true })
end)
it('should respect once = true for buffer-local autocmds', function()
local buf = vim.api.nvim_create_buf(true, false)
local count = 0
auto 'test_group' {
events = 'BufWinEnter',
buffer = buf,
once = true,
command = function() count = count + 1 end,
}
vim.api.nvim_exec_autocmds('BufWinEnter', { buf = buf })
assert.equal(1, count)
vim.api.nvim_exec_autocmds('BufWinEnter', { buf = buf })
assert.equal(1, count)
vim.api.nvim_buf_delete(buf, { force = true })
end)
it('should auto-clean when buffer is deleted', function()
local buf = vim.api.nvim_create_buf(true, false)
auto 'test_group' {
events = 'BufWinEnter',
buffer = buf,
command = function() end,
}
vim.api.nvim_buf_delete(buf, { force = true })
local autocmds = vim.api.nvim_get_autocmds({ event = 'BufWinEnter' })
local found = false
for _, a in ipairs(autocmds) do
if a.buf == buf then
found = true
break
end
end
assert.is_false(found)
end)
end)
describe('multiple autocmds in one call', function()
it('should fire both autocmds when their events fire', function()
local fired_a, fired_b = false, false
auto 'test_group' {
{
events = 'User',
pattern = 'MultiA',
command = function() fired_a = true end,
},
{
events = 'User',
pattern = 'MultiB',
command = function() fired_b = true end,
},
}
vim.cmd('doautocmd User MultiA')
vim.cmd('doautocmd User MultiB')
assert.is_true(fired_a)
assert.is_true(fired_b)
end)
end)
describe('description field', function()
it('should preserve description in the autocmd', function()
auto 'test_group' {
events = 'User',
pattern = 'DescTest',
description = 'my test description',
command = function() end,
}
local autocmds = vim.api.nvim_get_autocmds({ group = 'test_group' })
assert.equal(1, #autocmds)
assert.equal('my test description', autocmds[1].desc)
end)
end)
describe('pattern filter', function()
it('should only fire when pattern matches', function()
local fired = false
auto 'test_group' {
events = 'BufWritePost',
pattern = '*.lua',
command = function() fired = true end,
}
vim.api.nvim_exec_autocmds('BufWritePost', { pattern = 'foo.txt' })
assert.is_false(fired)
vim.api.nvim_exec_autocmds('BufWritePost', { pattern = 'bar.lua' })
assert.is_true(fired)
end)
end)
describe('group clearing (idempotency)', function()
it('should clear previous autocmds when re-calling with same group name', function()
local first_called, second_called = false, false
auto 'test_group' {
events = 'User',
pattern = 'IdempotencyTest',
command = function() first_called = true end,
}
auto 'test_group' {
events = 'User',
pattern = 'IdempotencyTest',
command = function() second_called = true end,
}
vim.cmd('doautocmd User IdempotencyTest')
assert.is_false(first_called)
assert.is_true(second_called)
end)
end)
describe('command as Vim string', function()
it('should execute a Vim command string', function()
vim.g.auto_test_vim_cmd = nil
auto 'test_group' {
events = 'User',
pattern = 'VimCmdTest',
command = 'let g:auto_test_vim_cmd = 42',
}
vim.cmd('doautocmd User VimCmdTest')
assert.equal(42, vim.g.auto_test_vim_cmd)
vim.g.auto_test_vim_cmd = nil
end)
end)
end)