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