robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
2.5 KiB93 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local call = n.call
6local clear = n.clear
7local command = n.command
8local eval = n.eval
9local eq = t.eq
10local feed = n.feed
11local feed_command = n.feed_command
12local next_msg = n.next_msg
13local api = n.api
14local source = n.source
15local pcall_err = t.pcall_err
16
17before_each(function()
18  clear()
19  local channel = api.nvim_get_chan_info(0).id
20  api.nvim_set_var('channel', channel)
21end)
22
23describe('wait()', function()
24  it('waits and returns 0 when condition is satisfied', function()
25    source([[
26    let g:_awake = 0
27    call timer_start(100, { -> nvim_command('let g:_awake = 1') })
28    ]])
29    eq(0, eval('g:_awake'))
30    eq(0, eval('wait(1500, { -> g:_awake })'))
31    eq(1, eval('g:_awake'))
32
33    eq(0, eval('wait(0, 1)'))
34  end)
35
36  it('returns -1 on timeout', function()
37    eq(-1, eval('wait(0, 0)'))
38    eq(-1, eval('wait(50, 0)'))
39  end)
40
41  it('returns -2 when interrupted', function()
42    feed_command(
43      'call rpcnotify(g:channel, "ready") | ' .. 'call rpcnotify(g:channel, "wait", wait(-1, 0))'
44    )
45    eq({ 'notification', 'ready', {} }, next_msg())
46    feed('<c-c>')
47    eq({ 'notification', 'wait', { -2 } }, next_msg())
48  end)
49
50  it('returns -3 on error', function()
51    command('silent! let ret = wait(-1, "error")')
52    eq(-3, eval('ret'))
53    command('let ret = 0 | silent! let ret = wait(-1, { -> error })')
54    eq(-3, eval('ret'))
55  end)
56
57  it('evaluates the condition on given interval', function()
58    source([[
59    function Count()
60      let g:counter += 1
61      return g:counter
62    endfunction
63    ]])
64
65    -- XXX: flaky (#11137)
66    t.retry(nil, nil, function()
67      api.nvim_set_var('counter', 0)
68      eq(-1, call('wait', 20, 'Count() >= 5', 99999))
69    end)
70
71    api.nvim_set_var('counter', 0)
72    eq(0, call('wait', 10000, 'Count() >= 5', 5))
73    eq(5, api.nvim_get_var('counter'))
74  end)
75
76  it('validates args', function()
77    eq('Vim:E475: Invalid value for argument 1', pcall_err(call, 'wait', '', 1))
78    eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, -1))
79    eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, 0))
80    eq('Vim:E475: Invalid value for argument 3', pcall_err(call, 'wait', 0, 1, ''))
81  end)
82
83  it('does not leak when Nvim exits while waiting', function()
84    n.expect_exit(
85      500,
86      source,
87      [[
88        call timer_start(10, {-> execute('qall!')})
89        call wait(10000, 0)
90      ]]
91    )
92  end)
93end)