robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Tests for core Vimscript "eval" behavior. 2-- 3-- See also: 4-- let_spec.lua 5-- null_spec.lua 6-- operators_spec.lua 7-- 8-- Tests for the Vimscript |vimscript-functions| library should live in: 9-- test/functional/vimscript/<funcname>_spec.lua 10-- test/functional/vimscript/functions_spec.lua 11 12local t =require ( 'test.testutil' ) 13local n =require ( 'test.functional.testnvim' )() 14local Screen =require ( 'test.functional.ui.screen' ) 15 16local describe , it , before_each , setup , teardown , pending , finally = 17t . describe , t . it , t . before_each , t . setup , t . teardown , t . pending , t . finally 18local mkdir =t . mkdir 19local pcall_err =t . pcall_err 20local clear =n . clear 21local eq =t . eq 22local exec =n . exec 23local exec_lua =n . exec_lua 24local exec_capture =n . exec_capture 25local eval =n . eval 26local command =n . command 27local write_file =t . write_file 28local api =n . api 29local fn =n . fn 30local sleep =vim . uv . sleep 31local assert_alive =n . assert_alive 32local poke_eventloop =n . poke_eventloop 33local feed =n . feed 34local expect_exit =n . expect_exit 35 36describe ( 'Up to MAX_FUNC_ARGS arguments are handled by' , function () 37local max_func_args =20 -- from eval.h 38local range =n . fn . range 39 40before_each ( clear ) 41 42it ( 'printf()' , function () 43local printf =n . fn . printf 44local rep =n . fn [ 'repeat' ] 45local expected ='2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,' 46eq ( expected , printf ( rep ( '%d,' , max_func_args - 1 ), unpack ( range ( 2 , max_func_args )))) 47local ret = 48pcall_err ( command , 'call printf("", 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)' ) 49eq ( 'Vim(call):E740: Too many arguments for function printf' , ret ) 50end ) 51 52it ( 'rpcnotify()' , function () 53local rpcnotify =n . fn . rpcnotify 54local ret =rpcnotify ( 0 , 'foo' , unpack ( range ( 3 , max_func_args ))) 55eq ( 1 , ret ) 56ret =pcall_err ( 57command , 58'call rpcnotify(0, "foo", 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)' 59) 60eq ( 'Vim(call):E740: Too many arguments for function rpcnotify' , ret ) 61end ) 62end ) 63 64describe ( 'backtick expansion' , function () 65setup ( function () 66clear () 67mkdir ( 'test-backticks' ) 68write_file ( 'test-backticks/file1' , 'test file 1' ) 69write_file ( 'test-backticks/file2' , 'test file 2' ) 70write_file ( 'test-backticks/file3' , 'test file 3' ) 71mkdir ( 'test-backticks/subdir' ) 72write_file ( 'test-backticks/subdir/file4' , 'test file 4' ) 73-- Long path might cause "Press ENTER" prompt; use :silent to avoid it. 74command ( 'silent cd test-backticks' ) 75end ) 76 77teardown ( function () 78n . rmdir ( 'test-backticks' ) 79end ) 80 81it ( "with default 'shell'" , function () 82if t . is_os ( 'win' ) then 83command ( ':silent args `dir /b *2`' ) 84else 85command ( ':silent args `echo ***2`' ) 86end 87eq ( { 'file2' } , eval ( 'argv()' )) 88if t . is_os ( 'win' ) then 89command ( ':silent args `dir /s/b *4`' ) 90eq ( { 'subdir/file4' } , eval ( 'map(argv(), \'fnamemodify(v:val, ":.")\')' )) 91else 92command ( ':silent args `echo */*4`' ) 93eq ( { 'subdir/file4' } , eval ( 'argv()' )) 94end 95end ) 96 97it ( 'with shell=fish' , function () 98t . skip ( fn . executable ( 'fish' ) == 0 , 'N/A: missing "fish" command' ) 99 100command ( 'set shell=fish' ) 101command ( ':silent args `echo ***2`' ) 102eq ( { 'file2' } , eval ( 'argv()' )) 103command ( ':silent args `echo */*4`' ) 104eq ( { 'subdir/file4' } , eval ( 'argv()' )) 105end ) 106end ) 107 108describe ( 'List support code' , function () 109local dur 110local min_dur =8 111local len =131072 112 113if not pending ( 'does not actually allows interrupting with just got_int' , function () end ) then 114return 115end 116-- The following tests are confirmed to work with os_breakcheck() just before 117-- `if (got_int) {break;}` in tv_list_copy and list_join_inner() and not to 118-- work without. 119setup ( function () 120clear () 121dur =0 122while true do 123command (( [[ 124let rt = reltime() 125let bl = range(%u) 126let dur = reltimestr(reltime(rt)) 127]] ): format ( len )) 128dur =tonumber ( api . nvim_get_var ( 'dur' )) 129if dur >= min_dur then 130-- print(('Using len %u, dur %g'):format(len, dur)) 131break 132else 133len =len * 2 134end 135end 136end ) 137it ( 'allows interrupting copy' , function () 138feed ( ':let t_rt = reltime()<CR>:let t_bl = copy(bl)<CR>' ) 139sleep ( min_dur / 16 * 1000 ) 140feed ( '<C-c>' ) 141poke_eventloop () 142command ( 'let t_dur = reltimestr(reltime(t_rt))' ) 143local t_dur =tonumber ( api . nvim_get_var ( 't_dur' )) 144if t_dur >= dur / 8 then 145eq ( nil , ( 'Took too long to cancel: %g >= %g' ): format ( t_dur , dur / 8 )) 146end 147end ) 148it ( 'allows interrupting join' , function () 149feed ( ':let t_rt = reltime()<CR>:let t_j = join(bl)<CR>' ) 150sleep ( min_dur / 16 * 1000 ) 151feed ( '<C-c>' ) 152poke_eventloop () 153command ( 'let t_dur = reltimestr(reltime(t_rt))' ) 154local t_dur =tonumber ( api . nvim_get_var ( 't_dur' )) 155(( 't_dur: %g' ): format ( t_dur )) 156if t_dur >= dur / 8 then 157eq ( nil , ( 'Took too long to cancel: %g >= %g' ): format ( t_dur , dur / 8 )) 158end 159end ) 160end ) 161 162describe ( 'uncaught exception' , function () 163before_each ( clear ) 164 165it ( 'is not forgotten #13490' , function () 166command ( 'autocmd BufWinEnter * throw "i am error"' ) 167t . matches ( 'i am error' , pcall_err ( command , 'try | new | endtry' )) 168 169-- Like Vim, throwing here aborts the processing of the script, but does not stop :runtime! 170-- from processing the others. 171-- Only the first thrown exception should be rethrown from the :try below, though. 172for i =1 , 3 do 173write_file ( 174'throw' .. i .. '.vim' , 175( [[ 176let result ..= '%d' 177throw 'throw%d' 178let result ..= 'X' 179]] ): format ( i , i ) 180) 181end 182finally ( function () 183for i =1 , 3 do 184os . remove ( 'throw' .. i .. '.vim' ) 185end 186end ) 187 188command ( 'set runtimepath+=. | let result = ""' ) 189t . matches ( 'throw1' , pcall_err ( command , 'try | runtime! throw*.vim | endtry' )) 190eq ( '123' , eval ( 'result' )) 191end ) 192 193it ( 'multiline exception remains multiline #25350' , function () 194local screen =Screen . new ( 80 , 11 ) 195exec_lua ( [[ 196function _G.Oops() 197error("oops") 198end 199]] ) 200feed ( ':try\rlua _G.Oops()\rendtry\r' ) 201screen : expect ( [[ 202{3: }| 203:try | 204: lua _G.Oops() | 205: endtry | 206{9:Error in :} | 207{9:E5108: Lua: [string "<nvim>"]:2: oops} | 208{9:stack traceback:} | 209{9: [C]: in function 'error'} | 210{9: [string "<nvim>"]:2: in function 'Oops'} | 211{9: [string ":lua"]:1: in main chunk} | 212{6:Press ENTER or type command to continue}^ | 213]] ) 214end ) 215end ) 216 217describe ( 'listing functions using :function' , function () 218before_each ( clear ) 219 220it ( 'works for lambda functions with <lambda> #20466' , function () 221command ( 'let A = {-> 1}' ) 222local num =exec_capture ( 'echo A' ): match ( "function%('<lambda>(%d+)'%)" ) 223eq ( 224t . dedent ( [[ 225function <lambda>%s(...) 2261 return 1 227endfunction]] ): format ( num ), 228exec_capture (( 'function <lambda>%s' ): format ( num )) 229) 230end ) 231end ) 232 233it ( 'no double-free in garbage collection #16287' , function () 234clear () 235-- Don't use exec() here as using a named script reproduces the issue better. 236write_file ( 237'Xgarbagecollect.vim' , 238[[ 239func Foo() abort 240let s:args = [a:000] 241let foo0 = "" 242let foo1 = "" 243let foo2 = "" 244let foo3 = "" 245let foo4 = "" 246let foo5 = "" 247let foo6 = "" 248let foo7 = "" 249let foo8 = "" 250let foo9 = "" 251let foo10 = "" 252let foo11 = "" 253let foo12 = "" 254let foo13 = "" 255let foo14 = "" 256endfunc 257 258set updatetime=1 259call Foo() 260call Foo() 261]] 262) 263finally ( function () 264os . remove ( 'Xgarbagecollect.vim' ) 265end ) 266command ( 'source Xgarbagecollect.vim' ) 267sleep ( 10 ) 268assert_alive () 269end ) 270 271it ( 'no heap-use-after-free with EXITFREE and partial as prompt callback' , function () 272clear () 273exec ( [[ 274func PromptCallback(text) 275endfunc 276setlocal buftype=prompt 277call prompt_setcallback('', funcref('PromptCallback')) 278]] ) 279expect_exit ( command , 'qall!' ) 280end )