test: allow exec_lua to handle functions

Problem:

Tests have lots of exec_lua calls which input blocks of code
provided as unformatted strings.

Solution:

Teach exec_lua how to handle functions.
This commit is contained in:
Lewis Russell
2024-07-29 11:20:15 +01:00
committed by Lewis Russell
parent f32557ca67
commit 7d24c4d6b0
16 changed files with 4122 additions and 3891 deletions

View File

@@ -27,30 +27,35 @@ local origlines = {
before_each(function()
clear()
exec_lua [[
local evname = ...
exec_lua(function()
local events = {}
function test_register(bufnr, evname, id, changedtick, utf_sizes, preview)
function _G.test_register(bufnr, evname, id, changedtick, utf_sizes, preview)
local function callback(...)
table.insert(events, {id, ...})
if test_unreg == id then
table.insert(events, { id, ... })
if _G.test_unreg == id then
return true
end
end
local opts = {[evname]=callback, on_detach=callback, on_reload=callback, utf_sizes=utf_sizes, preview=preview}
local opts = {
[evname] = callback,
on_detach = callback,
on_reload = callback,
utf_sizes = utf_sizes,
preview = preview,
}
if changedtick then
opts.on_changedtick = callback
end
vim.api.nvim_buf_attach(bufnr, false, opts)
end
function get_events()
function _G.get_events()
local ret_events = events
events = {}
return ret_events
end
]]
end)
end)
describe('lua buffer event callbacks: on_lines', function()
@@ -257,13 +262,13 @@ describe('lua buffer event callbacks: on_lines', function()
it('has valid cursor position while shifting', function()
api.nvim_buf_set_lines(0, 0, -1, true, { 'line1' })
exec_lua([[
exec_lua(function()
vim.api.nvim_buf_attach(0, false, {
on_lines = function()
vim.api.nvim_set_var('listener_cursor_line', vim.api.nvim_win_get_cursor(0)[1])
end,
})
]])
end)
feed('>>')
eq(1, api.nvim_get_var('listener_cursor_line'))
end)
@@ -302,13 +307,13 @@ describe('lua buffer event callbacks: on_lines', function()
it('#12718 lnume', function()
api.nvim_buf_set_lines(0, 0, -1, true, { '1', '2', '3' })
exec_lua([[
exec_lua(function()
vim.api.nvim_buf_attach(0, false, {
on_lines = function(...)
vim.api.nvim_set_var('linesev', { ... })
end,
})
]])
end)
feed('1G0')
feed('y<C-v>2j')
feed('G0')
@@ -326,13 +331,13 @@ describe('lua buffer event callbacks: on_lines', function()
end)
it('nvim_buf_call() from callback does not cause wrong Normal mode CTRL-A #16729', function()
exec_lua([[
exec_lua(function()
vim.api.nvim_buf_attach(0, false, {
on_lines = function(...)
on_lines = function()
vim.api.nvim_buf_call(0, function() end)
end,
})
]])
end)
feed('itest123<Esc><C-A>')
eq('test124', api.nvim_get_current_line())
end)
@@ -342,19 +347,19 @@ describe('lua buffer event callbacks: on_lines', function()
screen:attach()
api.nvim_buf_set_lines(0, 0, -1, true, { 'aaa', 'bbb', 'ccc' })
exec_lua([[
exec_lua(function()
local ns = vim.api.nvim_create_namespace('')
vim.api.nvim_buf_attach(0, false, {
on_lines = function(_, _, _, row, _, end_row)
vim.api.nvim_buf_clear_namespace(0, ns, row, end_row)
for i = row, end_row - 1 do
local id = vim.api.nvim_buf_set_extmark(0, ns, i, 0, {
virt_text = {{ 'NEW' .. tostring(i), 'WarningMsg' }},
vim.api.nvim_buf_set_extmark(0, ns, i, 0, {
virt_text = { { 'NEW' .. tostring(i), 'WarningMsg' } },
})
end
end,
})
]])
end)
feed('o')
screen:expect({
@@ -383,7 +388,7 @@ describe('lua buffer event callbacks: on_lines', function()
it('line lengths are correct when pressing TAB with folding #29119', function()
api.nvim_buf_set_lines(0, 0, -1, true, { 'a', 'b' })
exec_lua([[
exec_lua(function()
_G.res = {}
vim.o.foldmethod = 'indent'
vim.o.softtabstop = -1
@@ -391,9 +396,9 @@ describe('lua buffer event callbacks: on_lines', function()
on_lines = function(_, bufnr, _, row, _, end_row)
local lines = vim.api.nvim_buf_get_lines(bufnr, row, end_row, true)
table.insert(_G.res, lines)
end
end,
})
]])
end)
feed('i<Tab>')
eq({ '\ta' }, exec_lua('return _G.res[#_G.res]'))