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