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 | 3cfa5b1 | 2021-06-06 14:14:39 +0200 | [diff] [blame] | 5 | source vim9.vim |
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 | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 41 | if has('float') |
Bram Moolenaar | 3cfa5b1 | 2021-06-06 14:14:39 +0200 | [diff] [blame] | 42 | call assert_fails('call execute(3.4)', 'E492:') |
| 43 | call assert_equal("\nx", execute("echo \"x\"", 3.4)) |
Bram Moolenaar | 86b3ab4 | 2021-12-19 18:33:23 +0000 | [diff] [blame] | 44 | call CheckDefExecAndScriptFailure(['execute("echo \"x\"", 3.4)'], ['E1013: Argument 2: type mismatch, expected string but got float', 'E1174:']) |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 45 | endif |
Bram Moolenaar | 79815f1 | 2016-07-09 17:07:29 +0200 | [diff] [blame] | 46 | endfunc |
| 47 | |
| 48 | func 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([])) |
Bram Moolenaar | 79815f1 | 2016-07-09 17:07:29 +0200 | [diff] [blame] | 56 | endfunc |
Bram Moolenaar | 10ccaa1 | 2018-12-07 16:38:23 +0100 | [diff] [blame] | 57 | |
| 58 | func Test_execute_does_not_change_col() |
| 59 | echo '' |
| 60 | echon 'abcd' |
| 61 | let x = execute('silent echo 234343') |
| 62 | echon 'xyz' |
| 63 | let text = '' |
| 64 | for col in range(1, 7) |
| 65 | let text .= nr2char(screenchar(&lines, col)) |
| 66 | endfor |
| 67 | call assert_equal('abcdxyz', text) |
| 68 | endfunc |
Bram Moolenaar | 446e7a3 | 2018-12-08 13:57:42 +0100 | [diff] [blame] | 69 | |
| 70 | func Test_execute_not_silent() |
| 71 | echo '' |
| 72 | echon 'abcd' |
| 73 | let x = execute('echon 234', '') |
| 74 | echo 'xyz' |
| 75 | let text1 = '' |
| 76 | for col in range(1, 8) |
| 77 | let text1 .= nr2char(screenchar(&lines - 1, col)) |
| 78 | endfor |
| 79 | call assert_equal('abcd234 ', text1) |
| 80 | let text2 = '' |
| 81 | for col in range(1, 4) |
| 82 | let text2 .= nr2char(screenchar(&lines, col)) |
| 83 | endfor |
| 84 | call assert_equal('xyz ', text2) |
| 85 | endfunc |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 86 | |
| 87 | func Test_win_execute() |
| 88 | let thiswin = win_getid() |
| 89 | new |
| 90 | let otherwin = win_getid() |
| 91 | call setline(1, 'the new window') |
| 92 | call win_gotoid(thiswin) |
| 93 | let line = win_execute(otherwin, 'echo getline(1)') |
| 94 | call assert_match('the new window', line) |
Bram Moolenaar | 37487e1 | 2021-01-12 22:08:53 +0100 | [diff] [blame] | 95 | let line = win_execute(134343, 'echo getline(1)') |
| 96 | call assert_equal('', line) |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 97 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 98 | if has('popupwin') |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 99 | let popupwin = popup_create('the popup win', {'line': 2, 'col': 3}) |
| 100 | redraw |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 101 | let line = 'echo getline(1)'->win_execute(popupwin) |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 102 | call assert_match('the popup win', line) |
| 103 | |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 104 | call popup_close(popupwin) |
| 105 | endif |
| 106 | |
| 107 | call win_gotoid(otherwin) |
| 108 | bwipe! |
Bram Moolenaar | 7f13b24 | 2021-11-14 11:41:31 +0000 | [diff] [blame] | 109 | |
| 110 | " check :lcd in another window does not change directory |
| 111 | let curid = win_getid() |
| 112 | let curdir = getcwd() |
| 113 | split Xother |
| 114 | lcd .. |
| 115 | " Use :pwd to get the actual current directory |
| 116 | let otherdir = execute('pwd') |
| 117 | call win_execute(curid, 'lcd testdir') |
| 118 | call assert_equal(otherdir, execute('pwd')) |
| 119 | bwipe! |
| 120 | execute 'cd ' .. curdir |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 121 | endfunc |
Bram Moolenaar | 820680b | 2019-08-09 14:56:22 +0200 | [diff] [blame] | 122 | |
Bram Moolenaar | 345f28d | 2019-10-08 22:20:35 +0200 | [diff] [blame] | 123 | func Test_win_execute_update_ruler() |
Bram Moolenaar | 5a4c308 | 2019-12-01 15:23:11 +0100 | [diff] [blame] | 124 | CheckFeature quickfix |
| 125 | |
Bram Moolenaar | 345f28d | 2019-10-08 22:20:35 +0200 | [diff] [blame] | 126 | enew |
| 127 | call setline(1, range(500)) |
| 128 | 20 |
| 129 | split |
| 130 | let winid = win_getid() |
| 131 | set ruler |
| 132 | wincmd w |
| 133 | let height = winheight(winid) |
| 134 | redraw |
| 135 | call assert_match('20,1', Screenline(height + 1)) |
| 136 | let line = win_execute(winid, 'call cursor(100, 1)') |
| 137 | redraw |
| 138 | call assert_match('100,1', Screenline(height + 1)) |
| 139 | |
| 140 | bwipe! |
| 141 | endfunc |
| 142 | |
Bram Moolenaar | 820680b | 2019-08-09 14:56:22 +0200 | [diff] [blame] | 143 | func Test_win_execute_other_tab() |
| 144 | let thiswin = win_getid() |
| 145 | tabnew |
| 146 | call win_execute(thiswin, 'let xyz = 1') |
| 147 | call assert_equal(1, xyz) |
| 148 | tabclose |
| 149 | unlet xyz |
| 150 | endfunc |
Bram Moolenaar | e2a8f07 | 2020-01-08 19:32:18 +0100 | [diff] [blame] | 151 | |
Bram Moolenaar | dab17a0 | 2021-12-20 21:35:59 +0000 | [diff] [blame] | 152 | func Test_win_execute_on_startup() |
| 153 | CheckRunVimInTerminal |
| 154 | |
| 155 | let lines =<< trim END |
| 156 | vim9script |
| 157 | [repeat('x', &columns)]->writefile('Xfile1') |
| 158 | silent tabedit Xfile2 |
| 159 | var id = win_getid() |
| 160 | silent tabedit Xfile3 |
| 161 | autocmd VimEnter * win_execute(id, 'close') |
| 162 | END |
| 163 | call writefile(lines, 'XwinExecute') |
| 164 | let buf = RunVimInTerminal('-p Xfile1 -Nu XwinExecute', {}) |
| 165 | |
| 166 | " this was crashing on exit with EXITFREE defined |
| 167 | call StopVimInTerminal(buf) |
| 168 | |
| 169 | call delete('XwinExecute') |
| 170 | call delete('Xfile1') |
| 171 | endfunc |
| 172 | |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 173 | func Test_execute_func_with_null() |
Bram Moolenaar | e2a8f07 | 2020-01-08 19:32:18 +0100 | [diff] [blame] | 174 | call assert_equal("", execute(test_null_string())) |
| 175 | call assert_equal("", execute(test_null_list())) |
| 176 | call assert_fails('call execute(test_null_dict())', 'E731:') |
| 177 | call assert_fails('call execute(test_null_blob())', 'E976:') |
| 178 | call assert_fails('call execute(test_null_partial())','E729:') |
| 179 | if has('job') |
| 180 | call assert_fails('call execute(test_null_job())', 'E908:') |
| 181 | call assert_fails('call execute(test_null_channel())', 'E908:') |
| 182 | endif |
| 183 | endfunc |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 184 | |
| 185 | " vim: shiftwidth=2 sts=2 expandtab |