robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local eq =t . eq 7local eval =n . eval 8local clear =n . clear 9local source =n . source 10local pcall_err =t . pcall_err 11local fn =n . fn 12local command =n . command 13local feed =n . feed 14local is_os =t . is_os 15 16describe ( 'execute()' , function () 17before_each ( clear ) 18 19it ( 'captures the same result as :redir' , function () 20command ( [[ 21echomsg 'foo 1' 22echomsg 'foo 2' 23redir => g:__redir_output 24silent! messages 25redir END 26]] ) 27eq ( eval ( 'g:__redir_output' ), fn . execute ( 'messages' )) 28end ) 29 30it ( 'captures the concatenated outputs of a List of commands' , function () 31eq ( 'foobar' , fn . execute ( { 'echon "foo"' , 'echon "bar"' } )) 32eq ( '\nfoo\nbar' , fn . execute ( { 'echo "foo"' , 'echo "bar"' } )) 33end ) 34 35it ( 'supports nested execute("execute(...)")' , function () 36eq ( '42' , fn . execute ( [[echon execute("echon execute('echon 42')")]] )) 37end ) 38 39it ( 'supports nested :redir to a variable' , function () 40source ( [[ 41function! g:Foo() 42let a = '' 43redir => a 44silent echon "foo" 45redir END 46return a 47endfunction 48function! g:Bar() 49let a = '' 50redir => a 51silent echon "bar1" 52call g:Foo() 53silent echon "bar2" 54redir END 55silent echon "bar3" 56return a 57endfunction 58]] ) 59eq ( 'top1bar1foobar2bar3' , fn . execute ( 'echon "top1"|call g:Bar()' )) 60end ) 61 62it ( 'supports nested :redir to a register' , function () 63source ( [[ 64let @a = '' 65function! g:Foo() 66redir @a>> 67silent echon "foo" 68redir END 69return @a 70endfunction 71function! g:Bar() 72redir @a>> 73silent echon "bar1" 74call g:Foo() 75silent echon "bar2" 76redir END 77silent echon "bar3" 78return @a 79endfunction 80]] ) 81eq ( 'top1bar1foobar2bar3' , fn . execute ( 'echon "top1"|call g:Bar()' )) 82-- :redir itself doesn't nest, so the redirection ends in g:Foo 83eq ( 'bar1foo' , eval ( '@a' )) 84end ) 85 86it ( 'captures a transformed string' , function () 87eq ( '^A' , fn . execute ( 'echon "\\<C-a>"' )) 88end ) 89 90it ( 'returns empty string if the argument list is empty' , function () 91eq ( '' , fn . execute ( {} )) 92command ( 'let g:ret = execute(v:_null_list)' ) 93eq ( '' , eval ( 'g:ret' )) 94end ) 95 96it ( 'captures errors' , function () 97local ret 98ret =pcall_err ( command , 'call execute(v:_null_dict)' ) 99eq ( 'Vim(call):E731: Using a Dictionary as a String' , ret ) 100ret =pcall_err ( command , 'call execute(function("tr"))' ) 101eq ( 'Vim(call):E729: Using a Funcref as a String' , ret ) 102ret =pcall_err ( command , 'call execute(["echo 42", v:_null_dict, "echo 44"])' ) 103eq ( 'Vim:E731: Using a Dictionary as a String' , ret ) 104ret =pcall_err ( command , 'call execute(["echo 42", function("tr"), "echo 44"])' ) 105eq ( 'Vim:E729: Using a Funcref as a String' , ret ) 106end ) 107 108it ( 'captures output with highlights' , function () 109eq ( 110'\nErrorMsg xxx ctermfg=15 ctermbg=1 guifg=White guibg=Red' , 111eval ( 'execute("hi ErrorMsg")' ) 112) 113end ) 114 115it ( 'does not corrupt the command display #5422' , function () 116local screen =Screen . new ( 70 , 7 ) 117feed ( ':echo execute("hi ErrorMsg")<CR>' ) 118screen : expect ( 119[[ 120| 121{1:~ }|*2 122{2: }| 123| 124ErrorMsg xxx ctermfg=15 ctermbg=1 guifg=White guibg=Red | 125{3:Press ENTER or type command to continue}^ | 126]] , 127{ 128[ 1 ] ={ bold =true , foreground =Screen . colors . Blue1 } , 129[ 2 ] ={ bold =true , reverse =true } , 130[ 3 ] ={ bold =true , foreground =Screen . colors . SeaGreen4 } , 131} 132) 133feed ( '<CR>' ) 134end ) 135 136it ( 'places cursor correctly #6035' , function () 137local screen =Screen . new ( 40 , 6 ) 138source ( [=[ 139" test 1: non-silenced output goes as usual 140function! Test1() 141echo 1234 142let x = execute('echon "abcdef"', '') 143echon 'ABCD' 144endfunction 145 146" test 2: silenced output does not affect ui 147function! Test2() 148echo 1234 149let x = execute('echon "abcdef"', 'silent') 150echon 'ABCD' 151endfunction 152 153" test 3: silenced! error does not affect ui 154function! Test3() 155echo 1234 156let x = execute('echoerr "abcdef"', 'silent!') 157echon 'ABCDXZYZ' 158endfunction 159 160" test 4: silenced echoerr goes as usual 161" bug here 162function! Test4() 163echo 1234 164let x = execute('echoerr "abcdef"', 'silent') 165echon 'ABCD' 166endfunction 167 168" test 5: silenced! echoerr does not affect ui 169function! Test5() 170echo 1234 171let x = execute('echoerr "abcdef"', 'silent!') 172echon 'ABCD' 173endfunction 174 175" test 6: silenced error goes as usual 176function! Test6() 177echo 1234 178let x = execute('echo undefined', 'silent') 179echon 'ABCD' 180endfunction 181 182" test 7: existing error does not mess the result 183function! Test7() 184" display from Test6() is still visible 185" why does the "abcdef" goes into a newline 186let x = execute('echon "abcdef"', '') 187echon 'ABCD' 188endfunction 189]=] ) 190 191feed ( [[:call Test1()<cr>]] ) 192screen : expect ( [[ 193^ | 194{1:~ }|*4 195ABCD | 196]] ) 197 198feed ( [[:call Test2()<cr>]] ) 199screen : expect ( [[ 200^ | 201{1:~ }|*4 2021234ABCD | 203]] ) 204 205feed ( [[:call Test3()<cr>]] ) 206screen : expect ( [[ 207^ | 208{1:~ }|*4 2091234ABCDXZYZ | 210]] ) 211 212feed ( [[:call Test4()<cr>]] ) 213-- unexpected: need to fix 214-- echoerr does not set did_emsg 215-- "ef" was overwritten since msg_col was recovered wrongly 216screen : expect ( [[ 217{3: }| 2181234 | 219{9:Error in function Test4:} | 220{8:line 2:} | 221{9:abcd}ABCD | 222{6:Press ENTER or type command to continue}^ | 223]] ) 224 225feed ( [[<cr>]] ) -- to clear screen 226feed ( [[:call Test5()<cr>]] ) 227screen : expect ( [[ 228^ | 229{1:~ }|*4 2301234ABCD | 231]] ) 232 233feed ( [[:call Test6()<cr>]] ) 234screen : expect ( [[ 235{3: }| 236| 237{9:Error in function Test6:} | 238{8:line 2:} | 239{9:E121}ABCD | 240{6:Press ENTER or type command to continue}^ | 241]] ) 242 243feed ( [[:call Test7()<cr>]] ) 244screen : expect ( [[ 245| 246{9:Error in function Test6:} | 247{8:line 2:} | 248{9:E121}ABCD | 249ABCD | 250{6:Press ENTER or type command to continue}^ | 251]] ) 252end ) 253 254-- This deviates from vim behavior, but is consistent 255-- with how nvim currently displays the output. 256it ( 'captures shell-command output' , function () 257local win_lf =is_os ( 'win' ) and '\13' or '' 258eq ( '\n:!echo foo\r\n\nfoo' .. win_lf .. '\n' , fn . execute ( '!echo foo' )) 259end ) 260 261describe ( '{silent} argument' , function () 262it ( 'captures & displays output for ""' , function () 263local screen =Screen . new ( 40 , 5 ) 264command ( 'let g:mes = execute("echon 42", "")' ) 265screen : expect ( [[ 266^ | 267{1:~ }|*3 26842 | 269]] ) 270eq ( '42' , eval ( 'g:mes' )) 271end ) 272 273it ( 'gives E493 instead of prompting on backwards range for ""' , function () 274command ( 'split' ) 275eq ( 276'Vim(windo):E493: Backwards range given: 2,1windo echo' , 277pcall_err ( fn . execute , '2,1windo echo' , '' ) 278) 279eq ( 280'Vim(windo):E493: Backwards range given: 2,1windo echo' , 281pcall_err ( fn . execute , { '2,1windo echo' } , '' ) 282) 283end ) 284 285it ( 'captures but does not display output for "silent"' , function () 286local screen =Screen . new ( 40 , 5 ) 287command ( 'let g:mes = execute("echon 42")' ) 288screen : expect ( [[ 289^ | 290{1:~ }|*3 291| 292]] ) 293eq ( '42' , eval ( 'g:mes' )) 294 295command ( 'let g:mes = execute("echon 13", "silent")' ) 296screen : expect { 297grid =[[ 298^ | 299{1:~ }|*3 300| 301]] , 302unchanged =true , 303} 304eq ( '13' , eval ( 'g:mes' )) 305end ) 306 307it ( 'suppresses errors for "silent!"' , function () 308command ( 'let g:mes = execute(0.0, "silent!")' ) 309eq ( '' , eval ( 'g:mes' )) 310 311command ( 'let g:mes = execute("echon add(1, 1)", "silent!")' ) 312eq ( '1' , eval ( 'g:mes' )) 313 314command ( 'let g:mes = execute(["echon 42", "echon add(1, 1)"], "silent!")' ) 315eq ( '421' , eval ( 'g:mes' )) 316end ) 317 318it ( 'propagates errors for "" and "silent"' , function () 319local ret 320ret =pcall_err ( command , 'call execute(v:_null_dict, "silent")' ) 321eq ( 'Vim(call):E731: Using a Dictionary as a String' , ret ) 322 323ret =pcall_err ( command , 'call execute("echo add(1, 1)", "")' ) 324eq ( 'Vim(echo):E897: List or Blob required' , ret ) 325 326ret =pcall_err ( command , 'call execute(["echon 42", "echo add(1, 1)"], "")' ) 327eq ( 'Vim(echo):E897: List or Blob required' , ret ) 328 329ret =pcall_err ( command , 'call execute("echo add(1, 1)", "silent")' ) 330eq ( 'Vim(echo):E897: List or Blob required' , ret ) 331 332ret =pcall_err ( command , 'call execute(["echon 42", "echo add(1, 1)"], "silent")' ) 333eq ( 'Vim(echo):E897: List or Blob required' , ret ) 334end ) 335end ) 336end )