robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
8.9 KiB313 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local feed, eq, eval, ok = n.feed, t.eq, n.eval, t.ok
7local source, async_meths, run = n.source, n.async_meths, n.run
8local clear, command, fn = n.clear, n.command, n.fn
9local api = n.api
10local load_adjust = n.load_adjust
11local retry = t.retry
12local pcall_err = t.pcall_err
13
14describe('timers', function()
15  before_each(function()
16    clear()
17    source([[
18      let g:val = 0
19      func MyHandler(timer)
20        let g:val += 1
21      endfunc
22    ]])
23  end)
24
25  it('works one-shot', function()
26    eq(0, eval("[timer_start(10, 'MyHandler'), g:val][1]"))
27    run(nil, nil, nil, load_adjust(100))
28    eq(1, eval('g:val'))
29  end)
30
31  it('works one-shot when repeat=0', function()
32    eq(0, eval("[timer_start(10, 'MyHandler', {'repeat': 0}), g:val][1]"))
33    run(nil, nil, nil, load_adjust(100))
34    eq(1, eval('g:val'))
35  end)
36
37  it('works with repeat two', function()
38    eq(0, eval("[timer_start(10, 'MyHandler', {'repeat': 2}), g:val][1]"))
39    run(nil, nil, nil, load_adjust(20))
40    retry(nil, load_adjust(300), function()
41      eq(2, eval('g:val'))
42    end)
43  end)
44
45  it('are triggered during sleep', function()
46    source([[
47      let g:val = -1
48      func! MyHandler(timer)
49        if g:val >= 0
50          let g:val += 1
51          if g:val == 2
52            call timer_stop(a:timer)
53          endif
54        endif
55      endfunc
56    ]])
57    eval("timer_start(10, 'MyHandler', {'repeat': -1})")
58    async_meths.nvim_command('sleep 10')
59    eq(-1, eval('g:val')) -- timer did nothing yet.
60    async_meths.nvim_command('let g:val = 0')
61    run(nil, nil, nil, load_adjust(20))
62    retry(nil, nil, function()
63      eq(2, eval('g:val'))
64    end)
65  end)
66
67  it('works with zero timeout', function()
68    -- timer_start does still not invoke the callback immediately
69    eq(0, eval("[timer_start(0, 'MyHandler', {'repeat': 1000}), g:val][1]"))
70    retry(nil, nil, function()
71      eq(1000, eval('g:val'))
72    end)
73  end)
74
75  it('can be started during sleep', function()
76    async_meths.nvim_command('sleep 10')
77    -- this also tests that remote requests works during sleep
78    eq(0, eval("[timer_start(10, 'MyHandler', {'repeat': 2}), g:val][1]"))
79    run(nil, nil, nil, load_adjust(20))
80    retry(nil, load_adjust(300), function()
81      eq(2, eval('g:val'))
82    end)
83  end)
84
85  it('are paused when event processing is disabled', function()
86    command("call timer_start(5, 'MyHandler', {'repeat': -1})")
87    run(nil, nil, nil, load_adjust(10))
88    local count = eval('g:val')
89    -- shows two line error message and thus invokes the return prompt.
90    -- if we start to allow event processing here, we need to change this test.
91    feed(':throw "fatal error"<CR>')
92    run(nil, nil, nil, load_adjust(30))
93    feed('<cr>')
94    local diff = eval('g:val') - count
95    assert(0 <= diff and diff <= 4, 'expected (0 <= diff <= 4), got: ' .. tostring(diff))
96  end)
97
98  it('are triggered in inputlist() call #7857', function()
99    async_meths.nvim_exec2(
100      [[
101        call timer_start(5, 'MyHandler', {'repeat': -1})
102        let g:val = 0
103        let g:n = inputlist(['input0', 'input1'])
104      ]],
105      {}
106    )
107    retry(nil, nil, function()
108      local val = eval('g:val')
109      ok(val >= 2, '>= 2', tostring(val))
110      eq(0, eval("exists('g:n')"))
111    end)
112    feed('42<CR>')
113    eq(42, eval('g:n'))
114  end)
115
116  it('are triggered in confirm() call', function()
117    api.nvim_ui_attach(80, 24, {}) -- needed for confirm() to work
118    async_meths.nvim_exec2(
119      [[
120        call timer_start(5, 'MyHandler', {'repeat': -1})
121        let g:val = 0
122        let g:n = confirm('Are you sure?', "&Yes\n&No\n&Cancel")
123      ]],
124      {}
125    )
126    retry(nil, nil, function()
127      local val = eval('g:val')
128      ok(val >= 2, '>= 2', tostring(val))
129      eq(0, eval("exists('g:n')"))
130    end)
131    feed('c')
132    eq(3, eval('g:n'))
133  end)
134
135  it('are triggered in blocking getchar() call', function()
136    async_meths.nvim_exec2(
137      [[
138        call timer_start(5, 'MyHandler', {'repeat': -1})
139        let g:val = 0
140        let g:c = getchar()
141      ]],
142      {}
143    )
144    retry(nil, nil, function()
145      local val = eval('g:val')
146      ok(val >= 2, '>= 2', tostring(val))
147      eq(0, eval("exists('g:c')"))
148      eq(0, eval('getchar(1)'))
149    end)
150    feed('c')
151    eq(99, eval('g:c'))
152  end)
153
154  it('can invoke redraw in blocking getchar() call', function()
155    local screen = Screen.new(40, 6)
156
157    api.nvim_buf_set_lines(0, 0, -1, true, { 'ITEM 1', 'ITEM 2' })
158    source([[
159      let g:cont = 0
160      func! AddItem(timer)
161        if !g:cont
162          return
163        endif
164        call timer_stop(a:timer)
165
166        call nvim_buf_set_lines(0, 2, 2, v:true, ['ITEM 3'])
167
168        " Meant to test for what Vim tests in Test_peek_and_get_char.
169        call getchar(1)
170
171        redraw
172      endfunc
173    ]])
174    async_meths.nvim_command("let g:c2 = getchar(-1, {'cursor': 'msg'})")
175    async_meths.nvim_command(
176      'call timer_start(' .. load_adjust(100) .. ", 'AddItem', {'repeat': -1})"
177    )
178
179    screen:expect([[
180      ITEM 1                                  |
181      ITEM 2                                  |
182      {1:~                                       }|*3
183      ^                                        |
184    ]])
185    async_meths.nvim_command('let g:cont = 1')
186
187    screen:expect([[
188      ITEM 1                                  |
189      ITEM 2                                  |
190      ITEM 3                                  |
191      {1:~                                       }|*2
192      ^                                        |
193    ]])
194
195    feed('3')
196    eq(51, eval('g:c2'))
197    screen:expect([[
198      ^ITEM 1                                  |
199      ITEM 2                                  |
200      ITEM 3                                  |
201      {1:~                                       }|*2
202                                              |
203    ]])
204  end)
205
206  it('can be stopped', function()
207    local t_init_val = eval("[timer_start(5, 'MyHandler', {'repeat': -1}), g:val]")
208    eq(0, t_init_val[2])
209    run(nil, nil, nil, load_adjust(30))
210    fn.timer_stop(t_init_val[1])
211    local count = eval('g:val')
212    run(nil, load_adjust(300), nil, load_adjust(30))
213    local count2 = eval('g:val')
214    -- when count is eval:ed after timer_stop this should be non-racy
215    eq(count, count2)
216  end)
217
218  it('can be stopped from the handler', function()
219    source([[
220      func! MyHandler(timer)
221        let g:val += 1
222        if g:val == 3
223          call timer_stop(a:timer)
224          " check double stop is ignored
225          call timer_stop(a:timer)
226        endif
227      endfunc
228    ]])
229    eq(0, eval('g:val'))
230    command("call timer_start(10, 'MyHandler', {'repeat': -1})")
231    retry(nil, nil, function()
232      eq(3, eval('g:val'))
233    end)
234  end)
235
236  it('can have two timers', function()
237    source([[
238      let g:val2 = 0
239      func! MyHandler2(timer)
240        let g:val2 += 1
241      endfunc
242    ]])
243    command("call timer_start(2, 'MyHandler',  {'repeat': 3})")
244    command("call timer_start(4, 'MyHandler2', {'repeat': 2})")
245    retry(nil, nil, function()
246      eq(3, eval('g:val'))
247      eq(2, eval('g:val2'))
248    end)
249  end)
250
251  it('do not crash when processing events in the handler', function()
252    source([[
253      let g:val = 0
254      func! MyHandler(timer)
255        call timer_stop(a:timer)
256        sleep 10m
257        let g:val += 1
258      endfunc
259    ]])
260    command("call timer_start(5, 'MyHandler', {'repeat': 1})")
261    run(nil, nil, nil, load_adjust(20))
262    retry(nil, load_adjust(150), function()
263      eq(1, eval('g:val'))
264    end)
265  end)
266
267  it("doesn't mess up the cmdline", function()
268    local screen = Screen.new(40, 6)
269    source([[
270      let g:val = 0
271      func! MyHandler(timer)
272        while !g:val
273          return
274        endwhile
275        call timer_stop(a:timer)
276
277        echo "evil"
278        redraw
279        let g:val = 2
280      endfunc
281    ]])
282    command("call timer_start(100,  'MyHandler', {'repeat': -1})")
283    feed(':good')
284    screen:expect([[
285                                              |
286      {1:~                                       }|*4
287      :good^                                   |
288    ]])
289    command('let g:val = 1')
290    screen:expect_unchanged(true, load_adjust(200))
291
292    eq(2, eval('g:val'))
293  end)
294
295  it("timer_start can't be used in the sandbox", function()
296    source [[
297      function! Scary(timer) abort
298        call execute('echo ''execute() should be disallowed''', '')
299      endfunction
300    ]]
301    eq(
302      'Vim(call):E48: Not allowed in sandbox',
303      pcall_err(command, "sandbox call timer_start(0, 'Scary')")
304    )
305  end)
306
307  it('can be triggered after an empty string <expr> mapping #17257', function()
308    local screen = Screen.new(40, 6)
309    command([=[imap <expr> <F2> [timer_start(0, { _ -> execute("throw 'x'", "") }), ''][-1]]=])
310    feed('i<F2>')
311    screen:expect({ any = 'E605: Exception not caught: x' })
312  end)
313end)