Skip to content

Commit

Permalink
(mini.test) FEATURE: Add child.lua_func() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
stasjok authored and echasnovski committed Oct 17, 2023
1 parent c7c018a commit 3817614
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/mini-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ Fields~
{lua_notify} `(function)` Execute Lua code without waiting for output.
{lua_get} `(function)` Execute Lua code and return result. A wrapper
for |nvim_exec_lua()| but prepends string code with `return`.
{lua_func} `(function)` Execute Lua function and return it's result.
Function will be called with all extra parameters of lua_func(). No upvalues.

{is_blocked} `(function)` Check whether child process is blocked.
{is_running} `(function)` Check whether child process is currently running.
Expand Down
11 changes: 11 additions & 0 deletions lua/mini/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,15 @@ MiniTest.new_child_neovim = function()
return child.api.nvim_exec_lua('return ' .. str, args or {})
end

child.lua_func = function(f, ...)
ensure_running()
prevent_hanging('lua_func')
return child.api.nvim_exec_lua(
'local f = ...; return assert(loadstring(f))(select(2, ...))',
{ string.dump(f), ... }
)
end

child.is_blocked = function()
ensure_running()
return child.api.nvim_get_mode()['blocking']
Expand Down Expand Up @@ -1407,6 +1416,8 @@ end
---@field lua_notify function Execute Lua code without waiting for output.
---@field lua_get function Execute Lua code and return result. A wrapper
--- for |nvim_exec_lua()| but prepends string code with `return`.
---@field lua_func function Execute Lua function and return it's result.
--- Function will be called with all extra parameters of lua_func(). No upvalues.
---
---@field is_blocked function Check whether child process is blocked.
---@field is_running function Check whether child process is currently running.
Expand Down
31 changes: 31 additions & 0 deletions tests/test_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,37 @@ T['child']['lua_get()'] = function()
validate_child_method(method, { name = 'lua_get' })
end

T['child']['lua_func()'] = function()
-- Can execute functions in child neovim
local method = function()
return child.lua_func(function() _G.n = 0 end)
end

eq(child.lua_get('_G.n'), vim.NIL)
method()
eq(child.lua_get('_G.n'), 0)

-- Can return values
method = function()
return child.lua_func(function() return 1 + 1 end)
end
eq(method(), 2)

-- Can error
method = function()
return child.lua_func(function() error('test error') end)
end
expect.error(method, 'test error')

-- Can take arguments
method = function()
return child.lua_func(function(a, b) return a + b end, 1, 2)
end
eq(method(), 3)

validate_child_method(method, { name = 'lua_func' })
end

T['child']['is_blocked()'] = function()
eq(child.is_blocked(), false)

Expand Down

0 comments on commit 3817614

Please sign in to comment.