1" test execute() 2 3source view_util.vim 4 5func NestedEval() 6 let nested = execute('echo "nested\nlines"') 7 echo 'got: "' . nested . '"' 8endfunc 9 10func NestedRedir() 11 redir => var 12 echo 'broken' 13 redir END 14endfunc 15 16func Test_execute_string() 17 call assert_equal("\nnocompatible", execute('set compatible?')) 18 call assert_equal("\nsomething\nnice", execute('echo "something\nnice"')) 19 call assert_equal("noendofline", execute('echon "noendofline"')) 20 call assert_equal("", execute(123)) 21 22 call assert_equal("\ngot: \"\nnested\nlines\"", execute('call NestedEval()')) 23 redir => redired 24 echo 'this' 25 let evaled = execute('echo "that"') 26 echo 'theend' 27 redir END 28" Nvim supports execute('... :redir ...'), so this test is intentionally 29" disabled. 30" call assert_equal("\nthis\ntheend", redired) 31 call assert_equal("\nthat", evaled) 32 33 call assert_fails('call execute("doesnotexist")', 'E492:') 34 call assert_fails('call execute(3.4)', 'E806:') 35" Nvim supports execute('... :redir ...'), so this test is intentionally 36" disabled. 37" call assert_fails('call execute("call NestedRedir()")', 'E930:') 38 39 call assert_equal("\nsomething", execute('echo "something"', '')) 40 call assert_equal("\nsomething", execute('echo "something"', 'silent')) 41 call assert_equal("\nsomething", execute('echo "something"', 'silent!')) 42 call assert_equal("", execute('burp', 'silent!')) 43 call assert_fails('call execute("echo \"x\"", 3.4)', 'E806:') 44 45 call assert_equal("", execute("")) 46endfunc 47 48func Test_execute_list() 49 call assert_equal("\nsomething\nnice", execute(['echo "something"', 'echo "nice"'])) 50 let l = ['for n in range(0, 3)', 51 \ 'echo n', 52 \ 'endfor'] 53 call assert_equal("\n0\n1\n2\n3", execute(l)) 54 55 call assert_equal("", execute([])) 56 call assert_equal("", execute(v:_null_list)) 57endfunc 58 59func Test_execute_does_not_change_col() 60 echo '' 61 echon 'abcd' 62 let x = execute('silent echo 234343') 63 echon 'xyz' 64 let text = '' 65 for col in range(1, 7) 66 let text .= nr2char(screenchar(&lines, col)) 67 endfor 68 call assert_equal('abcdxyz', text) 69endfunc 70 71func Test_execute_not_silent() 72 echo '' 73 echon 'abcd' 74 let x = execute('echon 234', '') 75 echo 'xyz' 76 let text1 = '' 77 for col in range(1, 8) 78 let text1 .= nr2char(screenchar(&lines - 1, col)) 79 endfor 80 call assert_equal('abcd234 ', text1) 81 let text2 = '' 82 for col in range(1, 4) 83 let text2 .= nr2char(screenchar(&lines, col)) 84 endfor 85 call assert_equal('xyz ', text2) 86endfunc 87 88func Test_win_execute() 89 let thiswin = win_getid() 90 new 91 let otherwin = win_getid() 92 call setline(1, 'the new window') 93 call win_gotoid(thiswin) 94 let line = win_execute(otherwin, 'echo getline(1)') 95 call assert_match('the new window', line) 96 let line = win_execute(134343, 'echo getline(1)') 97 call assert_equal('', line) 98 99 if has('textprop') 100 let popupwin = popup_create('the popup win', {'line': 2, 'col': 3}) 101 redraw 102 let line = win_execute(popupwin, 'echo getline(1)') 103 call assert_match('the popup win', line) 104 105 call popup_close(popupwin) 106 endif 107 108 call win_gotoid(otherwin) 109 bwipe! 110 111 " check :lcd in another window does not change directory 112 let curid = win_getid() 113 let curdir = getcwd() 114 split Xother 115 lcd .. 116 " Use :pwd to get the actual current directory 117 let otherdir = execute('pwd') 118 call win_execute(curid, 'lcd testdir') 119 call assert_equal(otherdir, execute('pwd')) 120 bwipe! 121 execute 'cd ' .. curdir 122endfunc 123 124func Test_win_execute_update_ruler() 125 enew 126 call setline(1, range(500)) 127 20 128 split 129 let winid = win_getid() 130 set ruler 131 wincmd w 132 let height = winheight(winid) 133 redraw 134 call assert_match('20,1', Screenline(height + 1)) 135 let line = win_execute(winid, 'call cursor(100, 1)') 136 redraw 137 call assert_match('100,1', Screenline(height + 1)) 138 139 bwipe! 140endfunc 141 142func Test_win_execute_other_tab() 143 let thiswin = win_getid() 144 tabnew 145 call win_execute(thiswin, 'let xyz = 1') 146 call assert_equal(1, xyz) 147 tabclose 148 unlet xyz 149endfunc 150