diff options
author | kylo252 <59826753+kylo252@users.noreply.github.com> | 2022-06-09 15:18:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-09 07:18:56 -0600 |
commit | 3da3cfc864e89a2dca6917183915683373c85af8 (patch) | |
tree | 8233b38ee94a3aa3c8cede35dabb9ee8c774993a /test/functional/api/autocmd_spec.lua | |
parent | c5720c72213810adb75d3277ac645eb6fc8dafa8 (diff) | |
download | rneovim-3da3cfc864e89a2dca6917183915683373c85af8.tar.gz rneovim-3da3cfc864e89a2dca6917183915683373c85af8.tar.bz2 rneovim-3da3cfc864e89a2dca6917183915683373c85af8.zip |
feat(autocmds): retrieve lua callback (#18642)
add a new `callback` field to `nvim_get_autocmds`
Diffstat (limited to 'test/functional/api/autocmd_spec.lua')
-rw-r--r-- | test/functional/api/autocmd_spec.lua | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/test/functional/api/autocmd_spec.lua b/test/functional/api/autocmd_spec.lua index 491dac9f35..5dfcd09438 100644 --- a/test/functional/api/autocmd_spec.lua +++ b/test/functional/api/autocmd_spec.lua @@ -135,17 +135,22 @@ describe('autocmd api', function() local desc = 'Can show description' meths.set_var('desc', desc) - exec_lua([[ + local result = exec_lua([[ local callback = function() print 'Should Not Have Errored' end vim.api.nvim_create_autocmd("BufReadPost", { pattern = "*.py", callback = callback, desc = vim.g.desc, }) + local aus = vim.api.nvim_get_autocmds({ event = 'BufReadPost' }) + local first = aus[1] + return { + desc = first.desc, + cbtype = type(first.callback) + } ]]) - eq(desc, meths.get_autocmds({ event = 'BufReadPost' })[1].desc) - matches('<lua: %d+>', meths.get_autocmds({ event = 'BufReadPost' })[1].command) + eq({ desc = desc, cbtype = 'function' }, result) end) it('will not add a description unless it was provided', function() @@ -465,6 +470,49 @@ describe('autocmd api', function() -- 3-7 for the 5 we make in the autocmd eq({1, 2, 3, 4, 5, 6, 7}, bufs) end) + + it('can retrieve a callback from an autocmd', function() + local content = 'I Am A Callback' + meths.set_var('content', content) + + local result = exec_lua([[ + local cb = function() return vim.g.content end + vim.api.nvim_create_autocmd("User", { + pattern = "TestTrigger", + desc = "A test autocommand with a callback", + callback = cb, + }) + local aus = vim.api.nvim_get_autocmds({ event = 'User', pattern = 'TestTrigger'}) + local first = aus[1] + return { + cb = { + type = type(first.callback), + can_retrieve = first.callback() == vim.g.content + } + } + ]]) + + eq("function", result.cb.type) + eq(true, result.cb.can_retrieve) + end) + + it('will return an empty string as the command for an autocmd that uses a callback', function() + local result = exec_lua([[ + local callback = function() print 'I Am A Callback' end + vim.api.nvim_create_autocmd("BufWritePost", { + pattern = "*.py", + callback = callback, + }) + local aus = vim.api.nvim_get_autocmds({ event = 'BufWritePost' }) + local first = aus[1] + return { + command = first.command, + cbtype = type(first.callback) + } + ]]) + + eq({ command = "", cbtype = 'function' }, result) + end) end) describe('groups', function() |