robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Test suite for testing interactions with API bindings 2local t =require ( 'test.testutil' ) 3local n =require ( 'test.functional.testnvim' )() 4local Screen =require ( 'test.functional.ui.screen' ) 5 6local describe , it , before_each =t . describe , t . it , t . before_each 7local fn =n . fn 8local api =n . api 9local clear =n . clear 10local sleep =vim . uv . sleep 11local feed =n . feed 12local eq =t . eq 13local eval =n . eval 14local matches =t . matches 15local exec_lua =n . exec_lua 16local retry =t . retry 17 18before_each ( clear ) 19 20describe ( 'vim.uv' , function () 21it ( 'version' , function () 22assert ( fn . luaeval ( 'vim.uv.version()' ) >= 72961 , 'libuv version too old' ) 23matches ( '(%d+)%.(%d+)%.(%d+)' , fn . luaeval ( 'vim.uv.version_string()' )) 24end ) 25 26it ( 'timer' , function () 27exec_lua ( 'vim.api.nvim_set_var("coroutine_cnt", 0)' , {} ) 28 29local code =function () 30local touch =0 31local function wait ( ms ) 32local this =coroutine . running () 33assert ( this ) 34local timer =assert ( vim . uv . new_timer ()) 35timer : start ( 36ms , 370 , 38vim . schedule_wrap ( function () 39timer : close () 40touch =touch + 1 41coroutine . resume ( this ) 42touch =touch + 1 43assert ( touch == 3 ) 44vim . api . nvim_set_var ( 'coroutine_cnt_1' , touch ) 45end ) 46) 47coroutine . yield () 48touch =touch + 1 49return touch 50end 51coroutine . wrap ( function () 52local touched =wait ( 10 ) 53assert ( touched == touch ) 54vim . api . nvim_set_var ( 'coroutine_cnt' , touched ) 55end )() 56end 57 58eq ( 0 , api . nvim_get_var ( 'coroutine_cnt' )) 59exec_lua ( code ) 60retry ( 2 , nil , function () 61sleep ( 50 ) 62eq ( 2 , api . nvim_get_var ( 'coroutine_cnt' )) 63end ) 64eq ( 3 , api . nvim_get_var ( 'coroutine_cnt_1' )) 65end ) 66 67it ( 'is API safe' , function () 68local screen =Screen . new ( 50 , 10 ) 69 70-- deferred API functions are disabled, as their safety can't be guaranteed 71exec_lua ( [[ 72local timer = vim.uv.new_timer() 73timer:start(20, 0, function () 74_G.is_fast = vim.in_fast_event() 75timer:close() 76vim.api.nvim_set_var("valid", true) 77vim.api.nvim_command("echomsg 'howdy'") 78end) 79]] ) 80 81screen : expect ( [[ 82| 83{3: }| 84{9:Lua callback:} | 85{9:[string "<nvim>"]:5: E5560: nvim_set_var must not }| 86{9:be called in a fast event context} | 87{9:stack traceback:} | 88{9: [C]: in function 'nvim_set_var'} | 89{9: [string "<nvim>"]:5: in function <[string }| 90{9:"<nvim>"]:2>} | 91{6:Press ENTER or type command to continue}^ | 92]] ) 93feed ( '<cr>' ) 94eq ( false , eval ( "get(g:, 'valid', v:false)" )) 95eq ( true , exec_lua ( 'return _G.is_fast' )) 96 97-- callbacks can be scheduled to be executed in the main event loop 98-- where the entire API is available 99exec_lua ( function () 100local timer =assert ( vim . uv . new_timer ()) 101timer : start ( 10220 , 1030 , 104vim . schedule_wrap ( function () 105_G . is_fast =vim . in_fast_event () 106timer : close () 107vim . api . nvim_set_var ( 'valid' , true ) 108vim . api . nvim_command ( "echomsg 'howdy'" ) 109end ) 110) 111end ) 112 113screen : expect ( [[ 114^ | 115{1:~ }|*8 116howdy | 117]] ) 118eq ( true , eval ( "get(g:, 'valid', v:false)" )) 119eq ( false , exec_lua ( 'return _G.is_fast' )) 120 121-- fast (not deferred) API functions are allowed to be called directly 122exec_lua ( function () 123local timer =assert ( vim . uv . new_timer ()) 124timer : start ( 20 , 0 , function () 125timer : close () 126-- input is queued for processing after the callback returns 127vim . api . nvim_input ( 'isneaky' ) 128_G . mode =vim . api . nvim_get_mode () 129end ) 130end ) 131screen : expect ( [[ 132sneaky^ | 133{1:~ }|*8 134{5:-- INSERT --} | 135]] ) 136eq ( { blocking =false , mode ='n' } , exec_lua ( 'return _G.mode' )) 137 138exec_lua ( function () 139local timer =assert ( vim . uv . new_timer ()) 140timer : start ( 20 , 0 , function () 141_G . is_fast =vim . in_fast_event () 142timer : close () 143_G . value =vim . fn . has ( 'nvim-0.5' ) 144_G . unvalue =vim . fn . has ( 'python3' ) 145end ) 146end ) 147 148screen : expect ( { any =[[{9:Vim:E5560: Vimscript function must not be called i}]] } ) 149feed ( '<cr>' ) 150eq ( { 1 , nil } , exec_lua ( 'return {_G.value, _G.unvalue}' )) 151end ) 152 153it ( "is equal to require('luv')" , function () 154eq ( true , exec_lua ( "return vim.uv == require('luv')" )) 155end ) 156 157it ( 'non-string error() #32595' , function () 158local screen =Screen . new ( 50 , 10 ) 159exec_lua ( function () 160local timer =assert ( vim . uv . new_timer ()) 161timer : start ( 0 , 0 , function () 162timer : close () 163error ( nil ) 164end ) 165end ) 166local s =[[ 167| 168{1:~ }|*5 169{3: }| 170{9:Lua callback:} | 171{9:nil} | 172{6:Press ENTER or type command to continue}^ | 173]] 174screen : expect ( s ) 175feed ( '<cr>' ) 176n . assert_alive () 177screen : expect ( [[ 178^ | 179{1:~ }|*8 180| 181]] ) 182exec_lua ( function () 183vim . uv . fs_stat ( 'non-existent-file' , function () 184error ( nil ) 185end ) 186end ) 187screen : expect ( s ) 188feed ( '<cr>' ) 189n . assert_alive () 190end ) 191 192it ( "doesn't crash on async callbacks throwing nil error" , function () 193local screen =Screen . new ( 50 , 4 ) 194 195exec_lua ( function () 196_G . idle =vim . uv . new_idle () 197_G . idle : start ( function () 198_G . idle : stop () 199error () 200end ) 201end ) 202 203screen : expect ( [[ 204{3: }| 205{9:Lua callback:} | 206{9:nil} | 207{6:Press ENTER or type command to continue}^ | 208]] ) 209feed ( '<cr>' ) 210 211exec_lua ( function () 212_G . idle : close () 213end ) 214end ) 215 216it ( "doesn't crash on async callbacks throwing object as an error" , function () 217local screen =Screen . new ( 50 , 4 ) 218 219exec_lua ( function () 220_G . idle =vim . uv . new_idle () 221_G . idle : start ( function () 222_G . idle : stop () 223error ( _G . idle ) -- userdata with __tostring method 224end ) 225end ) 226 227screen : expect ( [[ 228{3: }| 229{9:Lua callback:} | 230{9:uv_idle_t: 0x{MATCH:%w+}}{MATCH: +}| 231{6:Press ENTER or type command to continue}^ | 232]] ) 233feed ( '<cr>' ) 234 235exec_lua ( function () 236_G . idle : close () 237end ) 238end ) 239end )