robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
7.1 KiB239 linesraw
1-- Test suite for testing interactions with API bindings
2local t = require('test.testutil')
3local n = require('test.functional.testnvim')()
4local Screen = require('test.functional.ui.screen')
5
6local describe, it, before_each = t.describe, t.it, t.before_each
7local fn = n.fn
8local api = n.api
9local clear = n.clear
10local sleep = vim.uv.sleep
11local feed = n.feed
12local eq = t.eq
13local eval = n.eval
14local matches = t.matches
15local exec_lua = n.exec_lua
16local retry = t.retry
17
18before_each(clear)
19
20describe('vim.uv', function()
21  it('version', function()
22    assert(fn.luaeval('vim.uv.version()') >= 72961, 'libuv version too old')
23    matches('(%d+)%.(%d+)%.(%d+)', fn.luaeval('vim.uv.version_string()'))
24  end)
25
26  it('timer', function()
27    exec_lua('vim.api.nvim_set_var("coroutine_cnt", 0)', {})
28
29    local code = function()
30      local touch = 0
31      local function wait(ms)
32        local this = coroutine.running()
33        assert(this)
34        local timer = assert(vim.uv.new_timer())
35        timer:start(
36          ms,
37          0,
38          vim.schedule_wrap(function()
39            timer:close()
40            touch = touch + 1
41            coroutine.resume(this)
42            touch = touch + 1
43            assert(touch == 3)
44            vim.api.nvim_set_var('coroutine_cnt_1', touch)
45          end)
46        )
47        coroutine.yield()
48        touch = touch + 1
49        return touch
50      end
51      coroutine.wrap(function()
52        local touched = wait(10)
53        assert(touched == touch)
54        vim.api.nvim_set_var('coroutine_cnt', touched)
55      end)()
56    end
57
58    eq(0, api.nvim_get_var('coroutine_cnt'))
59    exec_lua(code)
60    retry(2, nil, function()
61      sleep(50)
62      eq(2, api.nvim_get_var('coroutine_cnt'))
63    end)
64    eq(3, api.nvim_get_var('coroutine_cnt_1'))
65  end)
66
67  it('is API safe', function()
68    local screen = Screen.new(50, 10)
69
70    -- deferred API functions are disabled, as their safety can't be guaranteed
71    exec_lua([[
72      local timer = vim.uv.new_timer()
73      timer:start(20, 0, function ()
74        _G.is_fast = vim.in_fast_event()
75        timer:close()
76        vim.api.nvim_set_var("valid", true)
77        vim.api.nvim_command("echomsg 'howdy'")
78      end)
79    ]])
80
81    screen:expect([[
82                                                        |
83      {3:                                                  }|
84      {9:Lua callback:}                                     |
85      {9:[string "<nvim>"]:5: E5560: nvim_set_var must not }|
86      {9:be called in a fast event context}                 |
87      {9:stack traceback:}                                  |
88      {9:        [C]: in function 'nvim_set_var'}           |
89      {9:        [string "<nvim>"]:5: in function <[string }|
90      {9:"<nvim>"]:2>}                                      |
91      {6:Press ENTER or type command to continue}^           |
92    ]])
93    feed('<cr>')
94    eq(false, eval("get(g:, 'valid', v:false)"))
95    eq(true, exec_lua('return _G.is_fast'))
96
97    -- callbacks can be scheduled to be executed in the main event loop
98    -- where the entire API is available
99    exec_lua(function()
100      local timer = assert(vim.uv.new_timer())
101      timer:start(
102        20,
103        0,
104        vim.schedule_wrap(function()
105          _G.is_fast = vim.in_fast_event()
106          timer:close()
107          vim.api.nvim_set_var('valid', true)
108          vim.api.nvim_command("echomsg 'howdy'")
109        end)
110      )
111    end)
112
113    screen:expect([[
114      ^                                                  |
115      {1:~                                                 }|*8
116      howdy                                             |
117    ]])
118    eq(true, eval("get(g:, 'valid', v:false)"))
119    eq(false, exec_lua('return _G.is_fast'))
120
121    -- fast (not deferred) API functions are allowed to be called directly
122    exec_lua(function()
123      local timer = assert(vim.uv.new_timer())
124      timer:start(20, 0, function()
125        timer:close()
126        -- input is queued for processing after the callback returns
127        vim.api.nvim_input('isneaky')
128        _G.mode = vim.api.nvim_get_mode()
129      end)
130    end)
131    screen:expect([[
132      sneaky^                                            |
133      {1:~                                                 }|*8
134      {5:-- INSERT --}                                      |
135    ]])
136    eq({ blocking = false, mode = 'n' }, exec_lua('return _G.mode'))
137
138    exec_lua(function()
139      local timer = assert(vim.uv.new_timer())
140      timer:start(20, 0, function()
141        _G.is_fast = vim.in_fast_event()
142        timer:close()
143        _G.value = vim.fn.has('nvim-0.5')
144        _G.unvalue = vim.fn.has('python3')
145      end)
146    end)
147
148    screen:expect({ any = [[{9:Vim:E5560: Vimscript function must not be called i}]] })
149    feed('<cr>')
150    eq({ 1, nil }, exec_lua('return {_G.value, _G.unvalue}'))
151  end)
152
153  it("is equal to require('luv')", function()
154    eq(true, exec_lua("return vim.uv == require('luv')"))
155  end)
156
157  it('non-string error() #32595', function()
158    local screen = Screen.new(50, 10)
159    exec_lua(function()
160      local timer = assert(vim.uv.new_timer())
161      timer:start(0, 0, function()
162        timer:close()
163        error(nil)
164      end)
165    end)
166    local s = [[
167                                                        |
168      {1:~                                                 }|*5
169      {3:                                                  }|
170      {9:Lua callback:}                                     |
171      {9:nil}                                               |
172      {6:Press ENTER or type command to continue}^           |
173    ]]
174    screen:expect(s)
175    feed('<cr>')
176    n.assert_alive()
177    screen:expect([[
178      ^                                                  |
179      {1:~                                                 }|*8
180                                                        |
181    ]])
182    exec_lua(function()
183      vim.uv.fs_stat('non-existent-file', function()
184        error(nil)
185      end)
186    end)
187    screen:expect(s)
188    feed('<cr>')
189    n.assert_alive()
190  end)
191
192  it("doesn't crash on async callbacks throwing nil error", function()
193    local screen = Screen.new(50, 4)
194
195    exec_lua(function()
196      _G.idle = vim.uv.new_idle()
197      _G.idle:start(function()
198        _G.idle:stop()
199        error()
200      end)
201    end)
202
203    screen:expect([[
204      {3:                                                  }|
205      {9:Lua callback:}                                     |
206      {9:nil}                                               |
207      {6:Press ENTER or type command to continue}^           |
208    ]])
209    feed('<cr>')
210
211    exec_lua(function()
212      _G.idle:close()
213    end)
214  end)
215
216  it("doesn't crash on async callbacks throwing object as an error", function()
217    local screen = Screen.new(50, 4)
218
219    exec_lua(function()
220      _G.idle = vim.uv.new_idle()
221      _G.idle:start(function()
222        _G.idle:stop()
223        error(_G.idle) -- userdata with __tostring method
224      end)
225    end)
226
227    screen:expect([[
228      {3:                                                  }|
229      {9:Lua callback:}                                     |
230      {9:uv_idle_t: 0x{MATCH:%w+}}{MATCH: +}|
231      {6:Press ENTER or type command to continue}^           |
232    ]])
233    feed('<cr>')
234
235    exec_lua(function()
236      _G.idle:close()
237    end)
238  end)
239end)