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