1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
-- Test suite for testing interactions with API bindings
local helpers = require('test.functional.helpers')(after_each)
local Screen = require('test.functional.ui.screen')
local funcs = helpers.funcs
local meths = helpers.meths
local clear = helpers.clear
local sleep = helpers.sleep
local feed = helpers.feed
local eq = helpers.eq
local eval = helpers.eval
local matches = helpers.matches
local exec_lua = helpers.exec_lua
local retry = helpers.retry
before_each(clear)
describe('vim.uv', function()
it('version', function()
assert(funcs.luaeval('vim.uv.version()')>=72961, "libuv version too old")
matches("(%d+)%.(%d+)%.(%d+)", funcs.luaeval('vim.uv.version_string()'))
end)
it('timer', function()
exec_lua('vim.api.nvim_set_var("coroutine_cnt", 0)', {})
local code=[[
local uv = vim.uv
local touch = 0
local function wait(ms)
local this = coroutine.running()
assert(this)
local timer = 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))
coroutine.yield()
touch = touch + 1
return touch
end
coroutine.wrap(function()
local touched = wait(10)
assert(touched==touch)
vim.api.nvim_set_var("coroutine_cnt", touched)
end)()
]]
eq(0, meths.get_var('coroutine_cnt'))
exec_lua(code)
retry(2, nil, function()
sleep(50)
eq(2, meths.get_var('coroutine_cnt'))
end)
eq(3, meths.get_var('coroutine_cnt_1'))
end)
it('is API safe', function()
local screen = Screen.new(50,10)
screen:attach()
screen:set_default_attr_ids({
[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},
})
-- deferred API functions are disabled, as their safety can't be guaranteed
exec_lua([[
local timer = vim.uv.new_timer()
timer:start(20, 0, function ()
_G.is_fast = vim.in_fast_event()
timer:close()
vim.api.nvim_set_var("valid", true)
vim.api.nvim_command("echomsg 'howdy'")
end)
]])
screen:expect([[
|
{2: }|
{3:Error executing luv callback:} |
{3:[string "<nvim>"]:5: E5560: nvim_set_var must not }|
{3:be called in a lua loop callback} |
{3:stack traceback:} |
{3: [C]: in function 'nvim_set_var'} |
{3: [string "<nvim>"]:5: in function <[string }|
{3:"<nvim>"]:2>} |
{4:Press ENTER or type command to continue}^ |
]])
feed('<cr>')
eq(false, eval("get(g:, 'valid', v:false)"))
eq(true, exec_lua("return _G.is_fast"))
-- callbacks can be scheduled to be executed in the main event loop
-- where the entire API is available
exec_lua([[
local timer = 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))
]])
screen:expect([[
^ |
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
howdy |
]])
eq(true, eval("get(g:, 'valid', v:false)"))
eq(false, exec_lua("return _G.is_fast"))
-- fast (not deferred) API functions are allowed to be called directly
exec_lua([[
local timer = vim.uv.new_timer()
timer:start(20, 0, function ()
timer:close()
-- input is queued for processing after the callback returns
vim.api.nvim_input("isneaky")
_G.mode = vim.api.nvim_get_mode()
end)
]])
screen:expect([[
sneaky^ |
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{1:~ }|
{5:-- INSERT --} |
]])
eq({blocking=false, mode='n'}, exec_lua("return _G.mode"))
end)
it("is equal to require('luv')", function()
eq(true, exec_lua("return vim.uv == require('luv')"))
end)
end)
|