2019-06-10 05:13:18 -07:00
|
|
|
-- Test suite for testing interactions with API bindings
|
2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
2019-06-23 11:10:28 -07:00
|
|
|
local Screen = require('test.functional.ui.screen')
|
2024-04-20 08:44:13 -07:00
|
|
|
|
|
|
|
local fn = n.fn
|
|
|
|
local api = n.api
|
|
|
|
local clear = n.clear
|
2024-01-12 04:41:09 -07:00
|
|
|
local sleep = vim.uv.sleep
|
2024-04-20 08:44:13 -07:00
|
|
|
local feed = n.feed
|
2024-04-08 02:03:20 -07:00
|
|
|
local eq = t.eq
|
2024-04-20 08:44:13 -07:00
|
|
|
local eval = n.eval
|
2024-04-08 02:03:20 -07:00
|
|
|
local matches = t.matches
|
2024-04-20 08:44:13 -07:00
|
|
|
local exec_lua = n.exec_lua
|
2024-04-08 02:03:20 -07:00
|
|
|
local retry = t.retry
|
2019-06-10 05:13:18 -07:00
|
|
|
|
|
|
|
before_each(clear)
|
|
|
|
|
2023-06-03 03:06:00 -07:00
|
|
|
describe('vim.uv', function()
|
2019-06-10 05:13:18 -07:00
|
|
|
it('version', function()
|
2024-01-12 10:59:57 -07:00
|
|
|
assert(fn.luaeval('vim.uv.version()') >= 72961, 'libuv version too old')
|
|
|
|
matches('(%d+)%.(%d+)%.(%d+)', fn.luaeval('vim.uv.version_string()'))
|
2019-06-10 05:13:18 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('timer', function()
|
2019-06-23 11:10:28 -07:00
|
|
|
exec_lua('vim.api.nvim_set_var("coroutine_cnt", 0)', {})
|
2019-06-10 05:13:18 -07:00
|
|
|
|
2024-08-11 01:27:48 -07:00
|
|
|
local code = function()
|
2019-06-10 05:13:18 -07:00
|
|
|
local touch = 0
|
|
|
|
local function wait(ms)
|
|
|
|
local this = coroutine.running()
|
|
|
|
assert(this)
|
2024-08-11 01:27:48 -07:00
|
|
|
local timer = assert(vim.uv.new_timer())
|
|
|
|
timer:start(
|
|
|
|
ms,
|
|
|
|
0,
|
|
|
|
vim.schedule_wrap(function()
|
|
|
|
timer:close()
|
|
|
|
touch = touch + 1
|
|
|
|
coroutine.resume(this)
|
|
|
|
touch = touch + 1
|
|
|
|
assert(touch == 3)
|
|
|
|
vim.api.nvim_set_var('coroutine_cnt_1', touch)
|
|
|
|
end)
|
|
|
|
)
|
2019-06-10 05:13:18 -07:00
|
|
|
coroutine.yield()
|
|
|
|
touch = touch + 1
|
|
|
|
return touch
|
|
|
|
end
|
|
|
|
coroutine.wrap(function()
|
|
|
|
local touched = wait(10)
|
2024-08-11 01:27:48 -07:00
|
|
|
assert(touched == touch)
|
|
|
|
vim.api.nvim_set_var('coroutine_cnt', touched)
|
2019-06-10 05:13:18 -07:00
|
|
|
end)()
|
2024-08-11 01:27:48 -07:00
|
|
|
end
|
2019-06-10 05:13:18 -07:00
|
|
|
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(0, api.nvim_get_var('coroutine_cnt'))
|
2019-06-23 11:10:28 -07:00
|
|
|
exec_lua(code)
|
2019-07-04 07:42:10 -07:00
|
|
|
retry(2, nil, function()
|
|
|
|
sleep(50)
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(2, api.nvim_get_var('coroutine_cnt'))
|
2019-07-04 07:42:10 -07:00
|
|
|
end)
|
2024-01-12 10:59:57 -07:00
|
|
|
eq(3, api.nvim_get_var('coroutine_cnt_1'))
|
2019-06-10 05:13:18 -07:00
|
|
|
end)
|
2019-06-23 11:10:28 -07:00
|
|
|
|
|
|
|
it('is API safe', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
local screen = Screen.new(50, 10)
|
2019-06-23 11:10:28 -07:00
|
|
|
screen:set_default_attr_ids({
|
2024-01-02 18:09:18 -07:00
|
|
|
[1] = { bold = true, foreground = Screen.colors.Blue1 },
|
|
|
|
[2] = { bold = true, reverse = true },
|
|
|
|
[3] = { foreground = Screen.colors.Grey100, background = Screen.colors.Red },
|
|
|
|
[4] = { bold = true, foreground = Screen.colors.SeaGreen4 },
|
|
|
|
[5] = { bold = true },
|
2019-06-23 11:10:28 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
-- deferred API functions are disabled, as their safety can't be guaranteed
|
|
|
|
exec_lua([[
|
2023-06-03 03:06:00 -07:00
|
|
|
local timer = vim.uv.new_timer()
|
2019-06-23 11:10:28 -07:00
|
|
|
timer:start(20, 0, function ()
|
2019-08-04 12:56:29 -07:00
|
|
|
_G.is_fast = vim.in_fast_event()
|
2019-06-23 11:10:28 -07:00
|
|
|
timer:close()
|
|
|
|
vim.api.nvim_set_var("valid", true)
|
|
|
|
vim.api.nvim_command("echomsg 'howdy'")
|
|
|
|
end)
|
|
|
|
]])
|
|
|
|
|
|
|
|
screen:expect([[
|
|
|
|
|
|
|
|
|
{2: }|
|
2024-06-20 05:48:06 -07:00
|
|
|
{3:Error executing callback:} |
|
2019-08-04 12:56:29 -07:00
|
|
|
{3:[string "<nvim>"]:5: E5560: nvim_set_var must not }|
|
2024-06-20 05:48:06 -07:00
|
|
|
{3:be called in a fast event context} |
|
2021-11-06 07:26:10 -07:00
|
|
|
{3:stack traceback:} |
|
|
|
|
{3: [C]: in function 'nvim_set_var'} |
|
|
|
|
{3: [string "<nvim>"]:5: in function <[string }|
|
|
|
|
{3:"<nvim>"]:2>} |
|
2019-06-23 11:10:28 -07:00
|
|
|
{4:Press ENTER or type command to continue}^ |
|
|
|
|
]])
|
|
|
|
feed('<cr>')
|
|
|
|
eq(false, eval("get(g:, 'valid', v:false)"))
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(true, exec_lua('return _G.is_fast'))
|
2019-06-23 11:10:28 -07:00
|
|
|
|
|
|
|
-- callbacks can be scheduled to be executed in the main event loop
|
|
|
|
-- where the entire API is available
|
2024-08-11 01:27:48 -07:00
|
|
|
exec_lua(function()
|
|
|
|
local timer = assert(vim.uv.new_timer())
|
|
|
|
timer:start(
|
|
|
|
20,
|
|
|
|
0,
|
|
|
|
vim.schedule_wrap(function()
|
|
|
|
_G.is_fast = vim.in_fast_event()
|
|
|
|
timer:close()
|
|
|
|
vim.api.nvim_set_var('valid', true)
|
|
|
|
vim.api.nvim_command("echomsg 'howdy'")
|
|
|
|
end)
|
|
|
|
)
|
|
|
|
end)
|
2019-06-23 11:10:28 -07:00
|
|
|
|
|
|
|
screen:expect([[
|
|
|
|
^ |
|
2023-12-09 05:42:00 -07:00
|
|
|
{1:~ }|*8
|
2019-06-23 11:10:28 -07:00
|
|
|
howdy |
|
|
|
|
]])
|
|
|
|
eq(true, eval("get(g:, 'valid', v:false)"))
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(false, exec_lua('return _G.is_fast'))
|
2019-06-23 11:10:28 -07:00
|
|
|
|
|
|
|
-- fast (not deferred) API functions are allowed to be called directly
|
2024-08-11 01:27:48 -07:00
|
|
|
exec_lua(function()
|
|
|
|
local timer = assert(vim.uv.new_timer())
|
|
|
|
timer:start(20, 0, function()
|
2019-06-23 11:10:28 -07:00
|
|
|
timer:close()
|
|
|
|
-- input is queued for processing after the callback returns
|
2024-08-11 01:27:48 -07:00
|
|
|
vim.api.nvim_input('isneaky')
|
2019-06-23 11:10:28 -07:00
|
|
|
_G.mode = vim.api.nvim_get_mode()
|
|
|
|
end)
|
2024-08-11 01:27:48 -07:00
|
|
|
end)
|
2019-06-23 11:10:28 -07:00
|
|
|
screen:expect([[
|
|
|
|
sneaky^ |
|
2023-12-09 05:42:00 -07:00
|
|
|
{1:~ }|*8
|
2019-06-23 11:10:28 -07:00
|
|
|
{5:-- INSERT --} |
|
|
|
|
]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ blocking = false, mode = 'n' }, exec_lua('return _G.mode'))
|
2024-04-24 09:40:03 -07:00
|
|
|
|
2024-08-11 01:27:48 -07:00
|
|
|
exec_lua(function()
|
|
|
|
local timer = assert(vim.uv.new_timer())
|
|
|
|
timer:start(20, 0, function()
|
2024-04-24 09:40:03 -07:00
|
|
|
_G.is_fast = vim.in_fast_event()
|
|
|
|
timer:close()
|
2024-08-11 01:27:48 -07:00
|
|
|
_G.value = vim.fn.has('nvim-0.5')
|
|
|
|
_G.unvalue = vim.fn.has('python3')
|
2024-04-24 09:40:03 -07:00
|
|
|
end)
|
2024-08-11 01:27:48 -07:00
|
|
|
end)
|
2024-04-24 09:40:03 -07:00
|
|
|
|
|
|
|
screen:expect({ any = [[{3:Vim:E5560: Vimscript function must not be called i}]] })
|
|
|
|
feed('<cr>')
|
|
|
|
eq({ 1, nil }, exec_lua('return {_G.value, _G.unvalue}'))
|
2019-06-23 11:10:28 -07:00
|
|
|
end)
|
2019-06-26 05:33:48 -07:00
|
|
|
|
|
|
|
it("is equal to require('luv')", function()
|
2023-06-03 03:06:00 -07:00
|
|
|
eq(true, exec_lua("return vim.uv == require('luv')"))
|
2019-06-26 05:33:48 -07:00
|
|
|
end)
|
2019-06-10 05:13:18 -07:00
|
|
|
end)
|