2016-04-21 08:06:03 -07:00
|
|
|
" Test for timers
|
|
|
|
|
|
|
|
if !has('timers')
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
|
2017-03-05 08:25:40 -07:00
|
|
|
source shared.vim
|
2019-01-16 13:20:38 -07:00
|
|
|
source load.vim
|
2017-03-05 08:25:40 -07:00
|
|
|
|
2016-04-21 08:06:03 -07:00
|
|
|
func MyHandler(timer)
|
2017-03-05 08:25:40 -07:00
|
|
|
let g:val += 1
|
2016-04-21 08:06:03 -07:00
|
|
|
endfunc
|
|
|
|
|
2016-11-02 22:11:25 -07:00
|
|
|
func MyHandlerWithLists(lists, timer)
|
|
|
|
let x = string(a:lists)
|
|
|
|
endfunc
|
|
|
|
|
2019-01-16 13:20:38 -07:00
|
|
|
func s:assert_inrange(lower, upper, actual)
|
|
|
|
return assert_inrange(a:lower, LoadAdjust(a:upper), a:actual)
|
|
|
|
endfunc
|
|
|
|
|
2016-04-21 08:06:03 -07:00
|
|
|
func Test_oneshot()
|
2017-03-05 08:25:40 -07:00
|
|
|
let g:val = 0
|
2016-04-21 08:06:03 -07:00
|
|
|
let timer = timer_start(50, 'MyHandler')
|
2017-03-05 08:25:40 -07:00
|
|
|
let slept = WaitFor('g:val == 1')
|
|
|
|
call assert_equal(1, g:val)
|
2017-03-20 12:52:54 -07:00
|
|
|
if has('reltime')
|
2019-01-16 13:20:38 -07:00
|
|
|
call s:assert_inrange(40, 120, slept)
|
2017-03-20 12:52:54 -07:00
|
|
|
else
|
2018-10-07 23:56:13 -07:00
|
|
|
call assert_inrange(20, 120, slept)
|
2017-03-20 12:52:54 -07:00
|
|
|
endif
|
2016-04-21 08:06:03 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_repeat_three()
|
2017-03-05 08:25:40 -07:00
|
|
|
let g:val = 0
|
2016-04-21 08:06:03 -07:00
|
|
|
let timer = timer_start(50, 'MyHandler', {'repeat': 3})
|
2017-03-05 08:25:40 -07:00
|
|
|
let slept = WaitFor('g:val == 3')
|
|
|
|
call assert_equal(3, g:val)
|
2017-03-20 12:52:54 -07:00
|
|
|
if has('reltime')
|
2019-01-16 13:20:38 -07:00
|
|
|
call s:assert_inrange(120, 250, slept)
|
2017-03-20 12:52:54 -07:00
|
|
|
else
|
|
|
|
call assert_inrange(80, 200, slept)
|
|
|
|
endif
|
2016-04-21 08:06:03 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_repeat_many()
|
2018-10-07 23:56:13 -07:00
|
|
|
call timer_stopall()
|
2017-03-05 08:25:40 -07:00
|
|
|
let g:val = 0
|
2016-04-21 08:06:03 -07:00
|
|
|
let timer = timer_start(50, 'MyHandler', {'repeat': -1})
|
2019-03-16 07:14:13 -07:00
|
|
|
if has('mac')
|
|
|
|
sleep 200m
|
|
|
|
endif
|
2016-04-21 08:06:03 -07:00
|
|
|
sleep 200m
|
|
|
|
call timer_stop(timer)
|
2019-03-17 12:13:19 -07:00
|
|
|
call s:assert_inrange((has('mac') ? 1 : 2), 4, g:val)
|
2016-04-21 08:06:03 -07:00
|
|
|
endfunc
|
2016-10-30 15:10:11 -07:00
|
|
|
|
2016-11-02 22:11:25 -07:00
|
|
|
func Test_with_partial_callback()
|
2017-03-05 08:25:40 -07:00
|
|
|
let g:val = 0
|
2017-04-06 05:58:18 -07:00
|
|
|
let meow = {'one': 1}
|
|
|
|
function meow.bite(...)
|
|
|
|
let g:val += self.one
|
2016-11-02 22:11:25 -07:00
|
|
|
endfunction
|
2016-11-01 02:54:32 -07:00
|
|
|
|
2017-04-06 05:58:18 -07:00
|
|
|
call timer_start(50, meow.bite)
|
2017-03-05 08:25:40 -07:00
|
|
|
let slept = WaitFor('g:val == 1')
|
|
|
|
call assert_equal(1, g:val)
|
2017-03-20 12:52:54 -07:00
|
|
|
if has('reltime')
|
2019-01-16 13:20:38 -07:00
|
|
|
call s:assert_inrange(40, 130, slept)
|
2017-03-20 12:52:54 -07:00
|
|
|
else
|
|
|
|
call assert_inrange(20, 100, slept)
|
|
|
|
endif
|
2016-11-02 22:11:25 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_retain_partial()
|
2017-03-05 08:25:40 -07:00
|
|
|
call timer_start(50, function('MyHandlerWithLists', [['a']]))
|
2016-11-02 22:11:25 -07:00
|
|
|
call garbagecollect()
|
2017-03-05 08:25:40 -07:00
|
|
|
sleep 100m
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_info()
|
|
|
|
let id = timer_start(1000, 'MyHandler')
|
|
|
|
let info = timer_info(id)
|
|
|
|
call assert_equal(id, info[0]['id'])
|
|
|
|
call assert_equal(1000, info[0]['time'])
|
|
|
|
call assert_equal("function('MyHandler')", string(info[0]['callback']))
|
|
|
|
|
|
|
|
let found = 0
|
|
|
|
for info in timer_info()
|
|
|
|
if info['id'] == id
|
|
|
|
let found += 1
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
call assert_equal(1, found)
|
|
|
|
|
|
|
|
call timer_stop(id)
|
|
|
|
call assert_equal([], timer_info(id))
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_stopall()
|
2018-10-07 23:56:13 -07:00
|
|
|
call timer_stopall()
|
2017-03-05 08:25:40 -07:00
|
|
|
let id1 = timer_start(1000, 'MyHandler')
|
|
|
|
let id2 = timer_start(2000, 'MyHandler')
|
|
|
|
let info = timer_info()
|
|
|
|
call assert_equal(2, len(info))
|
|
|
|
|
|
|
|
call timer_stopall()
|
|
|
|
let info = timer_info()
|
|
|
|
call assert_equal(0, len(info))
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_paused()
|
|
|
|
let g:val = 0
|
|
|
|
|
|
|
|
let id = timer_start(50, 'MyHandler')
|
|
|
|
let info = timer_info(id)
|
|
|
|
call assert_equal(0, info[0]['paused'])
|
|
|
|
|
|
|
|
call timer_pause(id, 1)
|
|
|
|
let info = timer_info(id)
|
|
|
|
call assert_equal(1, info[0]['paused'])
|
2016-11-02 22:11:25 -07:00
|
|
|
sleep 200m
|
2017-03-05 08:25:40 -07:00
|
|
|
call assert_equal(0, g:val)
|
|
|
|
|
|
|
|
call timer_pause(id, 0)
|
|
|
|
let info = timer_info(id)
|
|
|
|
call assert_equal(0, info[0]['paused'])
|
|
|
|
|
|
|
|
let slept = WaitFor('g:val == 1')
|
|
|
|
call assert_equal(1, g:val)
|
2017-03-20 12:52:54 -07:00
|
|
|
if has('reltime')
|
2019-01-16 13:20:38 -07:00
|
|
|
call s:assert_inrange(0, 140, slept)
|
2017-03-20 12:52:54 -07:00
|
|
|
else
|
|
|
|
call assert_inrange(0, 10, slept)
|
|
|
|
endif
|
2016-11-02 22:11:25 -07:00
|
|
|
endfunc
|
2017-03-14 01:58:13 -07:00
|
|
|
|
2017-04-06 05:55:51 -07:00
|
|
|
func StopMyself(timer)
|
|
|
|
let g:called += 1
|
|
|
|
if g:called == 2
|
|
|
|
call timer_stop(a:timer)
|
|
|
|
endif
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_delete_myself()
|
|
|
|
let g:called = 0
|
|
|
|
let t = timer_start(10, 'StopMyself', {'repeat': -1})
|
|
|
|
call WaitFor('g:called == 2')
|
|
|
|
call assert_equal(2, g:called)
|
|
|
|
call assert_equal([], timer_info(t))
|
|
|
|
endfunc
|
|
|
|
|
2017-04-06 05:56:53 -07:00
|
|
|
func StopTimer1(timer)
|
|
|
|
let g:timer2 = timer_start(10, 'StopTimer2')
|
|
|
|
" avoid maxfuncdepth error
|
|
|
|
call timer_pause(g:timer1, 1)
|
|
|
|
sleep 40m
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func StopTimer2(timer)
|
|
|
|
call timer_stop(g:timer1)
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_stop_in_callback()
|
|
|
|
let g:timer1 = timer_start(10, 'StopTimer1')
|
|
|
|
sleep 40m
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func StopTimerAll(timer)
|
|
|
|
call timer_stopall()
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_stop_all_in_callback()
|
2018-10-07 23:56:13 -07:00
|
|
|
call timer_stopall()
|
2017-04-06 05:56:53 -07:00
|
|
|
let g:timer1 = timer_start(10, 'StopTimerAll')
|
|
|
|
let info = timer_info()
|
|
|
|
call assert_equal(1, len(info))
|
2018-11-04 05:16:24 -07:00
|
|
|
if has('mac')
|
|
|
|
sleep 100m
|
|
|
|
endif
|
2017-04-06 05:56:53 -07:00
|
|
|
sleep 40m
|
|
|
|
let info = timer_info()
|
|
|
|
call assert_equal(0, len(info))
|
|
|
|
endfunc
|
|
|
|
|
2018-10-15 18:16:00 -07:00
|
|
|
func FeedkeysCb(timer)
|
|
|
|
call feedkeys("hello\<CR>", 'nt')
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func InputCb(timer)
|
|
|
|
call timer_start(10, 'FeedkeysCb')
|
|
|
|
let g:val = input('?')
|
|
|
|
call Resume()
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_input_in_timer()
|
|
|
|
let g:val = ''
|
|
|
|
call timer_start(10, 'InputCb')
|
|
|
|
call Standby(1000)
|
|
|
|
call assert_equal('hello', g:val)
|
|
|
|
endfunc
|
|
|
|
|
2018-10-15 21:02:10 -07:00
|
|
|
func FuncWithCaughtError(timer)
|
|
|
|
let g:call_count += 1
|
|
|
|
try
|
|
|
|
doesnotexist
|
|
|
|
catch
|
|
|
|
" nop
|
|
|
|
endtry
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_timer_catch_error()
|
|
|
|
let g:call_count = 0
|
|
|
|
let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4})
|
|
|
|
" Timer will not be stopped.
|
|
|
|
call WaitFor('g:call_count == 4')
|
|
|
|
sleep 50m
|
|
|
|
call assert_equal(4, g:call_count)
|
|
|
|
endfunc
|
|
|
|
|
2018-08-22 12:39:38 -07:00
|
|
|
func FeedAndPeek(timer)
|
|
|
|
call test_feedinput('a')
|
|
|
|
call getchar(1)
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Interrupt(timer)
|
|
|
|
call test_feedinput("\<C-C>")
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_peek_and_get_char()
|
|
|
|
throw 'skipped: Nvim does not support test_feedinput()'
|
|
|
|
if !has('unix') && !has('gui_running')
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
call timer_start(0, 'FeedAndPeek')
|
|
|
|
let intr = timer_start(100, 'Interrupt')
|
|
|
|
let c = getchar()
|
|
|
|
call assert_equal(char2nr('a'), c)
|
|
|
|
call timer_stop(intr)
|
|
|
|
endfunc
|
2017-04-06 05:56:53 -07:00
|
|
|
|
2017-03-14 01:58:13 -07:00
|
|
|
" vim: shiftwidth=2 sts=2 expandtab
|