1" Test for timers 2 3if !has('timers') 4 finish 5endif 6 7source shared.vim 8source screendump.vim 9 10func MyHandler(timer) 11 let g:val += 1 12endfunc 13 14func MyHandlerWithLists(lists, timer) 15 let x = string(a:lists) 16endfunc 17 18func Test_oneshot() 19 let g:val = 0 20 let timer = timer_start(50, 'MyHandler') 21 let slept = WaitFor('g:val == 1') 22 call assert_equal(1, g:val) 23 if has('reltime') 24 call assert_inrange(49, 100, slept) 25 else 26 call assert_inrange(20, 100, slept) 27 endif 28endfunc 29 30func Test_repeat_three() 31 let g:val = 0 32 let timer = timer_start(50, 'MyHandler', {'repeat': 3}) 33 let slept = WaitFor('g:val == 3') 34 call assert_equal(3, g:val) 35 if has('reltime') 36 call assert_inrange(149, 250, slept) 37 else 38 call assert_inrange(80, 200, slept) 39 endif 40endfunc 41 42func Test_repeat_many() 43 let g:val = 0 44 let timer = timer_start(50, 'MyHandler', {'repeat': -1}) 45 sleep 200m 46 call timer_stop(timer) 47 call assert_inrange(2, 4, g:val) 48endfunc 49 50func Test_with_partial_callback() 51 let g:val = 0 52 let meow = {'one': 1} 53 function meow.bite(...) 54 let g:val += self.one 55 endfunction 56 57 call timer_start(50, meow.bite) 58 let slept = WaitFor('g:val == 1') 59 call assert_equal(1, g:val) 60 if has('reltime') 61 call assert_inrange(49, 130, slept) 62 else 63 call assert_inrange(20, 100, slept) 64 endif 65endfunc 66 67func Test_retain_partial() 68 call timer_start(50, function('MyHandlerWithLists', [['a']])) 69 call test_garbagecollect_now() 70 sleep 100m 71endfunc 72 73func Test_info() 74 let id = timer_start(1000, 'MyHandler') 75 let info = timer_info(id) 76 call assert_equal(id, info[0]['id']) 77 call assert_equal(1000, info[0]['time']) 78 call assert_true(info[0]['remaining'] > 500) 79 call assert_true(info[0]['remaining'] <= 1000) 80 call assert_equal(1, info[0]['repeat']) 81 call assert_equal("function('MyHandler')", string(info[0]['callback'])) 82 83 let found = 0 84 for info in timer_info() 85 if info['id'] == id 86 let found += 1 87 endif 88 endfor 89 call assert_equal(1, found) 90 91 call timer_stop(id) 92 call assert_equal([], timer_info(id)) 93endfunc 94 95func Test_stopall() 96 let id1 = timer_start(1000, 'MyHandler') 97 let id2 = timer_start(2000, 'MyHandler') 98 let info = timer_info() 99 call assert_equal(2, len(info)) 100 101 call timer_stopall() 102 let info = timer_info() 103 call assert_equal(0, len(info)) 104endfunc 105 106func Test_paused() 107 let g:val = 0 108 109 let id = timer_start(50, 'MyHandler') 110 let info = timer_info(id) 111 call assert_equal(0, info[0]['paused']) 112 113 call timer_pause(id, 1) 114 let info = timer_info(id) 115 call assert_equal(1, info[0]['paused']) 116 sleep 100m 117 call assert_equal(0, g:val) 118 119 call timer_pause(id, 0) 120 let info = timer_info(id) 121 call assert_equal(0, info[0]['paused']) 122 123 let slept = WaitFor('g:val == 1') 124 call assert_equal(1, g:val) 125 if has('reltime') 126 if has('mac') 127 " The travis Mac machines appear to be very busy. 128 call assert_inrange(0, 50, slept) 129 else 130 call assert_inrange(0, 30, slept) 131 endif 132 else 133 call assert_inrange(0, 10, slept) 134 endif 135endfunc 136 137func StopMyself(timer) 138 let g:called += 1 139 if g:called == 2 140 call timer_stop(a:timer) 141 endif 142endfunc 143 144func Test_delete_myself() 145 let g:called = 0 146 let t = timer_start(10, 'StopMyself', {'repeat': -1}) 147 call WaitForAssert({-> assert_equal(2, g:called)}) 148 call assert_equal(2, g:called) 149 call assert_equal([], timer_info(t)) 150endfunc 151 152func StopTimer1(timer) 153 let g:timer2 = timer_start(10, 'StopTimer2') 154 " avoid maxfuncdepth error 155 call timer_pause(g:timer1, 1) 156 sleep 40m 157endfunc 158 159func StopTimer2(timer) 160 call timer_stop(g:timer1) 161endfunc 162 163func Test_stop_in_callback() 164 let g:timer1 = timer_start(10, 'StopTimer1') 165 sleep 40m 166endfunc 167 168func StopTimerAll(timer) 169 call timer_stopall() 170endfunc 171 172func Test_stop_all_in_callback() 173 let g:timer1 = timer_start(10, 'StopTimerAll') 174 let info = timer_info() 175 call assert_equal(1, len(info)) 176 sleep 40m 177 let info = timer_info() 178 call assert_equal(0, len(info)) 179endfunc 180 181func FeedkeysCb(timer) 182 call feedkeys("hello\<CR>", 'nt') 183endfunc 184 185func InputCb(timer) 186 call timer_start(10, 'FeedkeysCb') 187 let g:val = input('?') 188 call Resume() 189endfunc 190 191func Test_input_in_timer() 192 let g:val = '' 193 call timer_start(10, 'InputCb') 194 call Standby(1000) 195 call assert_equal('hello', g:val) 196endfunc 197 198func FuncWithError(timer) 199 let g:call_count += 1 200 if g:call_count == 4 201 return 202 endif 203 doesnotexist 204endfunc 205 206func Test_timer_errors() 207 let g:call_count = 0 208 let timer = timer_start(10, 'FuncWithError', {'repeat': -1}) 209 " Timer will be stopped after failing 3 out of 3 times. 210 call WaitForAssert({-> assert_equal(3, g:call_count)}) 211 sleep 50m 212 call assert_equal(3, g:call_count) 213endfunc 214 215func FuncWithCaughtError(timer) 216 let g:call_count += 1 217 try 218 doesnotexist 219 catch 220 " nop 221 endtry 222endfunc 223 224func Test_timer_catch_error() 225 let g:call_count = 0 226 let timer = timer_start(10, 'FuncWithCaughtError', {'repeat': 4}) 227 " Timer will not be stopped. 228 call WaitForAssert({-> assert_equal(4, g:call_count)}) 229 sleep 50m 230 call assert_equal(4, g:call_count) 231endfunc 232 233func FeedAndPeek(timer) 234 call test_feedinput('a') 235 call getchar(1) 236endfunc 237 238func Interrupt(timer) 239 call test_feedinput("\<C-C>") 240endfunc 241 242func Test_peek_and_get_char() 243 if !has('unix') && !has('gui_running') 244 return 245 endif 246 call timer_start(0, 'FeedAndPeek') 247 let intr = timer_start(100, 'Interrupt') 248 let c = getchar() 249 call assert_equal(char2nr('a'), c) 250 call timer_stop(intr) 251endfunc 252 253func Test_getchar_zero() 254 if has('win32') && !has('gui_running') 255 " Console: no low-level input 256 return 257 endif 258 259 " Measure the elapsed time to avoid a hang when it fails. 260 let start = reltime() 261 let id = timer_start(20, {-> feedkeys('x', 'L')}) 262 let c = 0 263 while c == 0 && reltimefloat(reltime(start)) < 0.2 264 let c = getchar(0) 265 sleep 10m 266 endwhile 267 call assert_equal('x', nr2char(c)) 268 call timer_stop(id) 269endfunc 270 271func Test_ex_mode() 272 " Function with an empty line. 273 func Foo(...) 274 275 endfunc 276 let timer = timer_start(40, function('g:Foo'), {'repeat':-1}) 277 " This used to throw error E749. 278 exe "normal Qsleep 100m\rvi\r" 279 call timer_stop(timer) 280endfunc 281 282func Test_restore_count() 283 if !CanRunVimInTerminal() 284 return 285 endif 286 " Check that v:count is saved and restored, not changed by a timer. 287 call writefile([ 288 \ 'nnoremap <expr><silent> L v:count ? v:count . "l" : "l"', 289 \ 'func Doit(id)', 290 \ ' normal 3j', 291 \ 'endfunc', 292 \ 'call timer_start(100, "Doit")', 293 \ ], 'Xtrcscript') 294 call writefile([ 295 \ '1-1234', 296 \ '2-1234', 297 \ '3-1234', 298 \ ], 'Xtrctext') 299 let buf = RunVimInTerminal('-S Xtrcscript Xtrctext', {}) 300 301 " Wait for the timer to move the cursor to the third line. 302 call WaitForAssert({-> assert_equal(3, term_getcursor(buf)[0])}) 303 call assert_equal(1, term_getcursor(buf)[1]) 304 " Now check that v:count has not been set to 3 305 call term_sendkeys(buf, 'L') 306 call WaitForAssert({-> assert_equal(2, term_getcursor(buf)[1])}) 307 308 call StopVimInTerminal(buf) 309 call delete('Xtrcscript') 310 call delete('Xtrctext') 311endfunc 312 313" vim: shiftwidth=2 sts=2 expandtab 314