robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d393975b04
master
1" test execute() 2 3source view_util.vim 4source check.vim 5source vim9.vim 6source term_util.vim 7 8func NestedEval () 9let nested = execute ( 'echo "nested\nlines"' ) 10echo 'got: "' . nested . '"' 11endfunc 12 13func NestedRedir () 14redir => var 15echo 'broken' 16redir END 17endfunc 18 19func Test_execute_string () 20call assert_equal ( "\nnocompatible" , execute ( 'set compatible?' )) 21call assert_equal ( "\nsomething\nnice" , execute ( 'echo "something\nnice"' )) 22call assert_equal ( "noendofline" , execute ( 'echon "noendofline"' )) 23call assert_equal ( "" , execute ( 123 )) 24 25call assert_equal ( "\ngot: \"\nnested\nlines\"" , execute ( 'call NestedEval()' )) 26redir => redired 27echo 'this' 28let evaled = execute ( 'echo "that"' ) 29echo 'theend' 30redir END 31" Nvim supports execute('... :redir ...'), so this test is intentionally 32" disabled. 33" call assert_equal("\nthis\ntheend", redired) 34call assert_equal ( "\nthat" , evaled ) 35 36call assert_fails ( 'call execute("doesnotexist")' , 'E492:' ) 37" Nvim supports execute('... :redir ...'), so this test is intentionally 38" disabled. 39" call assert_fails('call execute("call NestedRedir()")', 'E930:') 40 41call assert_equal ( "\nsomething" , execute ( 'echo "something"' , '' )) 42call assert_equal ( "\nsomething" , execute ( 'echo "something"' , 'silent' )) 43call assert_equal ( "\nsomething" , execute ( 'echo "something"' , 'silent!' )) 44call assert_equal ( "" , execute ( 'burp' , 'silent!' )) 45if has ( 'float' ) 46call assert_fails ( 'call execute(3.4)' , 'E492:' ) 47call assert_equal ( "\nx" , execute ( "echo \"x\"" , 3.4 )) 48call CheckDefExecAndScriptFailure2 ([ 'execute("echo \"x\"", 3.4)' ], 'E1013: Argument 2: type mismatch, expected string but got float' , 'E806:' ) 49endif 50endfunc 51 52func Test_execute_list () 53call assert_equal ( "\nsomething\nnice" , execute ([ 'echo "something"' , 'echo "nice"' ])) 54let l = [ 'for n in range(0, 3)' , 55 \'echo n' , 56 \'endfor' ] 57call assert_equal ( "\n0\n1\n2\n3" , execute ( l )) 58 59call assert_equal ( "" , execute ([])) 60endfunc 61 62func Test_execute_does_not_change_col () 63echo '' 64echon 'abcd' 65let x = execute ( 'silent echo 234343' ) 66echon 'xyz' 67let text = '' 68for col in range ( 1 , 7 ) 69let text .= nr2char ( screenchar ( & lines , col )) 70endfor 71call assert_equal ( 'abcdxyz' , text ) 72endfunc 73 74func Test_execute_not_silent () 75echo '' 76echon 'abcd' 77let x = execute ( 'echon 234' , '' ) 78echo 'xyz' 79let text1 = '' 80for col in range ( 1 , 8 ) 81let text1 .= nr2char ( screenchar ( & lines - 1 , col )) 82endfor 83call assert_equal ( 'abcd234 ' , text1 ) 84let text2 = '' 85for col in range ( 1 , 4 ) 86let text2 .= nr2char ( screenchar ( & lines , col )) 87endfor 88call assert_equal ( 'xyz ' , text2 ) 89endfunc 90 91func Test_win_execute () 92let thiswin = win_getid () 93new 94let otherwin = win_getid () 95call setline ( 1 , 'the new window' ) 96call win_gotoid ( thiswin ) 97let line = win_execute ( otherwin , 'echo getline(1)' ) 98call assert_match ( 'the new window' , line ) 99let line = win_execute ( 134343 , 'echo getline(1)' ) 100call assert_equal ( '' , line ) 101 102if has ( 'popupwin' ) 103let popupwin = popup_create ( 'the popup win' , { 'line' : 2 , 'col' : 3 }) 104redraw 105let line = 'echo getline(1)' ->win_execute ( popupwin ) 106call assert_match ( 'the popup win' , line ) 107 108call popup_close ( popupwin ) 109endif 110 111call win_gotoid ( otherwin ) 112bwipe ! 113 114" check :lcd in another window does not change directory 115let curid = win_getid () 116let curdir = getcwd () 117split Xother 118lcd .. 119" Use :pwd to get the actual current directory 120let otherdir = execute ( 'pwd' ) 121call win_execute ( curid , 'lcd testdir' ) 122call assert_equal ( otherdir , execute ( 'pwd' )) 123bwipe ! 124execute 'cd ' .. curdir 125endfunc 126 127func Test_win_execute_update_ruler () 128CheckFeature quickfix 129 130enew 131call setline ( 1 , range ( 500 )) 13220 133split 134let winid = win_getid () 135set ruler 136wincmd w 137let height = winheight ( winid ) 138redraw 139call assert_match ( '20,1' , Screenline ( height + 1 )) 140let line = win_execute ( winid , 'call cursor(100, 1)' ) 141redraw 142call assert_match ( '100,1' , Screenline ( height + 1 )) 143 144bwipe ! 145endfunc 146 147func Test_win_execute_other_tab () 148let thiswin = win_getid () 149tabnew 150call win_execute ( thiswin , 'let xyz = 1' ) 151call assert_equal ( 1 , xyz ) 152tabclose 153unlet xyz 154endfunc 155 156func Test_win_execute_visual_redraw () 157call setline ( 1 , [ 'a' , 'b' , 'c' ]) 158new 159wincmd p 160" start Visual in current window, redraw in other window with fewer lines 161call feedkeys ( "G\<C-V>" , 'txn' ) 162call win_execute ( winnr ( '#' ) ->win_getid (), 'redraw' ) 163call feedkeys ( "\<Esc>" , 'txn' ) 164bwipe ! 165bwipe ! 166 167enew 168new 169call setline ( 1 , [ 'a' , 'b' , 'c' ]) 170let winid = win_getid () 171wincmd p 172" start Visual in current window, extend it in other window with more lines 173call feedkeys ( "\<C-V>" , 'txn' ) 174call win_execute ( winid , 'call feedkeys("G\<C-V>", ''txn'')' ) 175redraw 176 177bwipe ! 178bwipe ! 179endfunc 180 181func Test_win_execute_on_startup () 182CheckRunVimInTerminal 183 184let lines =<< trim END 185vim9script 186[repeat('x', &columns)]->writefile('Xfile1') 187silent tabedit Xfile2 188var id = win_getid() 189silent tabedit Xfile3 190autocmd VimEnter * win_execute(id, 'close') 191END 192call writefile ( lines , 'XwinExecute' ) 193let buf = RunVimInTerminal ( '-p Xfile1 -Nu XwinExecute' , {}) 194 195" this was crashing on exit with EXITFREE defined 196call StopVimInTerminal ( buf ) 197 198call delete ( 'XwinExecute' ) 199call delete ( 'Xfile1' ) 200endfunc 201 202func Test_execute_cmd_with_null () 203call assert_equal ( "" , execute ( v: _null_string )) 204call assert_equal ( "" , execute ( v: _null_list )) 205call assert_fails ( 'call execute(v:_null_dict)' , 'E731:' ) 206call assert_fails ( 'call execute(v:_null_blob)' , 'E976:' ) 207" Nvim doesn't have null partials 208" call assert_fails('call execute(test_null_partial())','E729:') 209if has ( 'job' ) 210call assert_fails ( 'call execute(test_null_job())' , 'E908:' ) 211call assert_fails ( 'call execute(test_null_channel())' , 'E908:' ) 212endif 213endfunc 214 215func Test_win_execute_tabpagewinnr () 216belowright split 217tab split 218belowright split 219call assert_equal ( 2 , tabpagewinnr ( 1 )) 220 221tabprevious 222wincmd p 223call assert_equal ( 1 , tabpagenr ()) 224call assert_equal ( 1 , tabpagewinnr ( 1 )) 225call assert_equal ( 2 , tabpagewinnr ( 2 )) 226 227call win_execute ( win_getid ( 1 , 2 ), 228 \'call assert_equal(2, tabpagenr())' 229 \.. '| call assert_equal(1, tabpagewinnr(1))' 230 \.. '| call assert_equal(1, tabpagewinnr(2))' ) 231 232call assert_equal ( 1 , tabpagenr ()) 233call assert_equal ( 1 , tabpagewinnr ( 1 )) 234call assert_equal ( 2 , tabpagewinnr ( 2 )) 235 236 %bwipe ! 237endfunc 238 239" vim: shiftwidth=2 sts=2 expandtab