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 clear =n . clear 6local eq =t . eq 7local exec_lua =n . exec_lua 8 9describe ( '|api-fast| functions' , function () 10before_each ( clear ) 11 12it ( 'callable in a fast event' , function () 13local out =exec_lua ( function () 14local result 15local timer =assert ( vim . uv . new_timer ()) 16timer : start ( 0 , 0 , function () 17timer : close () 18local ok 19ok , result =pcall ( function () 20return { 21byteidx =vim . fn . byteidx ( 'héllo' , 2 ), 22char2nr =vim . fn . char2nr ( 'A' ), 23charidx =vim . fn . charidx ( 'héllo' , 3 ), 24in_fast =vim . in_fast_event (), 25keycode =vim . keycode ( '<C-a>' ), 26keytrans =vim . fn . keytrans ( vim . keycode ( '<C-Home>' )), 27nr2char =vim . fn . nr2char ( 65 ), 28nvim_create_autocmd =vim . api . nvim_create_autocmd ( 'User' , { 29pattern ='Fast' , 30callback =function () end , 31} ) > 0 , 32nvim_replace_termcodes =vim . api . nvim_replace_termcodes ( '<Esc>' , true , true , true ), 33str2list =vim . fn . str2list ( 'AB' ), 34strcharlen =vim . fn . strcharlen ( 'abc' ), 35strcharpart =vim . fn . strcharpart ( 'héllo' , 1 , 2 ), 36strchars =vim . fn . strchars ( 'abc' ), 37strdisplaywidth =vim . fn . strdisplaywidth ( 'ab' ), 38strgetchar =vim . fn . strgetchar ( 'abc' , 1 ), 39strlen =vim . fn . strlen ( 'abc' ), 40strpart =vim . fn . strpart ( 'abcdef' , 1 , 3 ), 41strtrans =vim . fn . strtrans ( 'a\001b' ), 42tr =vim . fn . tr ( 'abc' , 'ab' , 'AB' ), 43trim =vim . fn . trim ( ' x ' ), 44utf16idx =vim . fn . utf16idx ( 'abc' , 2 ), 45} 46end ) 47end ) 48vim . wait ( 2000 , function () 49return result ~= nil 50end ) 51return result 52end ) 53eq ( { 54byteidx =3 , 55char2nr =65 , 56charidx =2 , 57in_fast =true , 58keycode ='\1' , -- <C-a> 59keytrans ='<C-Home>' , 60nr2char ='A' , 61nvim_create_autocmd =true , 62nvim_replace_termcodes ='\27' , -- <Esc> 63str2list ={ 65 , 66 } , 64strcharlen =3 , 65strcharpart ='él' , 66strchars =3 , 67strdisplaywidth =2 , 68strgetchar =98 , 69strlen =3 , 70strpart ='bcd' , 71strtrans ='a^Ab' , 72tr ='ABc' , 73trim ='x' , 74utf16idx =2 , 75} , out ) 76end ) 77 78it ( 'do not trigger os_breakcheck()' , function () 79local res =exec_lua ( function () 80local polls =0 81-- os_breakcheck() polls the event loop (loop_poll_events), which fires a uv_check handle. 82local chk =assert ( vim . uv . new_check ()) 83chk : start ( function () 84polls =polls + 1 85end ) 86local function polled ( fn ) 87polls =0 88fn () 89return polls ~= 0 90end 91 92-- Big (multibyte) input => per-char breakcheck would exceed BREAKCHECK_SKIP (1000, or 100_000 93-- for `veryfast_breakcheck`). Small input could pass (false negative). 94local big =( 'héllo wörld ' ): rep ( 100000 ) -- 1.2M chars. 95 96local aupat =( 'a' ): rep ( 80000 ) -- Must stay under the "E339: Pattern too long" limit. 97vim . api . nvim_create_autocmd ( 'User' , { pattern =aupat , callback =function () end } ) 98 99local cases ={ 100byteidx =function () 101vim . fn . byteidx ( big , 500000 ) 102end , 103char2nr =function () 104vim . fn . char2nr ( 'A' ) 105end , 106charidx =function () 107vim . fn . charidx ( big , 500000 ) 108end , 109keycode =function () 110vim . keycode ( '<C-a>' ) 111end , 112keytrans =function () 113vim . fn . keytrans ( vim . keycode ( '<C-Home>' )) 114end , 115nr2char =function () 116vim . fn . nr2char ( 65 ) 117end , 118nvim_create_autocmd =function () 119vim . api . nvim_exec_autocmds ( 'User' , { pattern =aupat } ) 120end , 121nvim_replace_termcodes =function () 122vim . api . nvim_replace_termcodes ( '<Esc>' , true , true , true ) 123end , 124str2list =function () 125vim . fn . str2list ( big ) 126end , 127strcharlen =function () 128vim . fn . strcharlen ( big ) 129end , 130strcharpart =function () 131vim . fn . strcharpart ( big , 1 , 2 ) 132end , 133strchars =function () 134vim . fn . strchars ( big ) 135end , 136strdisplaywidth =function () 137vim . fn . strdisplaywidth ( big ) 138end , 139strgetchar =function () 140vim . fn . strgetchar ( big , 500000 ) 141end , 142strlen =function () 143vim . fn . strlen ( big ) 144end , 145strpart =function () 146vim . fn . strpart ( big , 1 , 3 ) 147end , 148strtrans =function () 149vim . fn . strtrans ( big ) 150end , 151tr =function () 152vim . fn . tr ( big , 'ho' , 'HO' ) 153end , 154trim =function () 155vim . fn . trim ( big ) 156end , 157utf16idx =function () 158vim . fn . utf16idx ( big , 500000 ) 159end , 160} 161 162local bad ={} 163for name , fn in pairs ( cases ) do 164if polled ( fn ) then 165bad [ # bad + 1 ] =name 166end 167end 168table . sort ( bad ) 169 170-- Positive control: a big Vimscript loop calls line_breakcheck() every iteration. 171local control =polled ( function () 172vim . api . nvim_exec2 ( 'for i in range(1500000)|endfor' , {} ) 173end ) 174 175chk : stop () 176chk : close () 177return { bad =bad , control =control } 178end ) 179 180eq ( true , res . control ) -- Confirm our detector actually works: breakcheck was seen. 181eq ( {} , res . bad ) -- No api-fast function polled the event-loop. 182end ) 183end )