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 | 345f28d | 2019-10-08 22:20:35 +0200 | [diff] [blame] | 6 | |
Bram Moolenaar | 79815f1 | 2016-07-09 17:07:29 +0200 | [diff] [blame] | 7 | func NestedEval() |
| 8 | let nested = execute('echo "nested\nlines"') |
| 9 | echo 'got: "' . nested . '"' |
| 10 | endfunc |
| 11 | |
| 12 | func NestedRedir() |
| 13 | redir => var |
| 14 | echo 'broken' |
| 15 | redir END |
| 16 | endfunc |
| 17 | |
| 18 | func Test_execute_string() |
| 19 | call assert_equal("\nnocompatible", execute('set compatible?')) |
| 20 | call assert_equal("\nsomething\nnice", execute('echo "something\nnice"')) |
| 21 | call assert_equal("noendofline", execute('echon "noendofline"')) |
| 22 | call assert_equal("", execute(123)) |
| 23 | |
| 24 | call assert_equal("\ngot: \"\nnested\nlines\"", execute('call NestedEval()')) |
| 25 | redir => redired |
| 26 | echo 'this' |
| 27 | let evaled = execute('echo "that"') |
| 28 | echo 'theend' |
| 29 | redir END |
| 30 | call assert_equal("\nthis\ntheend", redired) |
| 31 | call assert_equal("\nthat", evaled) |
| 32 | |
| 33 | call assert_fails('call execute("doesnotexist")', 'E492:') |
Bram Moolenaar | 79815f1 | 2016-07-09 17:07:29 +0200 | [diff] [blame] | 34 | call assert_fails('call execute("call NestedRedir()")', 'E930:') |
| 35 | |
| 36 | call assert_equal("\nsomething", execute('echo "something"', '')) |
| 37 | call assert_equal("\nsomething", execute('echo "something"', 'silent')) |
| 38 | call assert_equal("\nsomething", execute('echo "something"', 'silent!')) |
| 39 | call assert_equal("", execute('burp', 'silent!')) |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 40 | if has('float') |
Bram Moolenaar | 3cfa5b1 | 2021-06-06 14:14:39 +0200 | [diff] [blame^] | 41 | call assert_fails('call execute(3.4)', 'E492:') |
| 42 | call assert_equal("\nx", execute("echo \"x\"", 3.4)) |
| 43 | call CheckDefExecAndScriptFailure(['execute("echo \"x\"", 3.4)'], 'E806:') |
Bram Moolenaar | 5feabe0 | 2020-01-30 18:24:53 +0100 | [diff] [blame] | 44 | endif |
Bram Moolenaar | 79815f1 | 2016-07-09 17:07:29 +0200 | [diff] [blame] | 45 | endfunc |
| 46 | |
| 47 | func Test_execute_list() |
| 48 | call assert_equal("\nsomething\nnice", execute(['echo "something"', 'echo "nice"'])) |
| 49 | let l = ['for n in range(0, 3)', |
| 50 | \ 'echo n', |
| 51 | \ 'endfor'] |
| 52 | call assert_equal("\n0\n1\n2\n3", execute(l)) |
| 53 | |
| 54 | call assert_equal("", execute([])) |
Bram Moolenaar | 79815f1 | 2016-07-09 17:07:29 +0200 | [diff] [blame] | 55 | endfunc |
Bram Moolenaar | 10ccaa1 | 2018-12-07 16:38:23 +0100 | [diff] [blame] | 56 | |
| 57 | func Test_execute_does_not_change_col() |
| 58 | echo '' |
| 59 | echon 'abcd' |
| 60 | let x = execute('silent echo 234343') |
| 61 | echon 'xyz' |
| 62 | let text = '' |
| 63 | for col in range(1, 7) |
| 64 | let text .= nr2char(screenchar(&lines, col)) |
| 65 | endfor |
| 66 | call assert_equal('abcdxyz', text) |
| 67 | endfunc |
Bram Moolenaar | 446e7a3 | 2018-12-08 13:57:42 +0100 | [diff] [blame] | 68 | |
| 69 | func Test_execute_not_silent() |
| 70 | echo '' |
| 71 | echon 'abcd' |
| 72 | let x = execute('echon 234', '') |
| 73 | echo 'xyz' |
| 74 | let text1 = '' |
| 75 | for col in range(1, 8) |
| 76 | let text1 .= nr2char(screenchar(&lines - 1, col)) |
| 77 | endfor |
| 78 | call assert_equal('abcd234 ', text1) |
| 79 | let text2 = '' |
| 80 | for col in range(1, 4) |
| 81 | let text2 .= nr2char(screenchar(&lines, col)) |
| 82 | endfor |
| 83 | call assert_equal('xyz ', text2) |
| 84 | endfunc |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 85 | |
| 86 | func Test_win_execute() |
| 87 | let thiswin = win_getid() |
| 88 | new |
| 89 | let otherwin = win_getid() |
| 90 | call setline(1, 'the new window') |
| 91 | call win_gotoid(thiswin) |
| 92 | let line = win_execute(otherwin, 'echo getline(1)') |
| 93 | call assert_match('the new window', line) |
Bram Moolenaar | 37487e1 | 2021-01-12 22:08:53 +0100 | [diff] [blame] | 94 | let line = win_execute(134343, 'echo getline(1)') |
| 95 | call assert_equal('', line) |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 96 | |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 97 | if has('popupwin') |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 98 | let popupwin = popup_create('the popup win', {'line': 2, 'col': 3}) |
| 99 | redraw |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 100 | let line = 'echo getline(1)'->win_execute(popupwin) |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 101 | call assert_match('the popup win', line) |
| 102 | |
Bram Moolenaar | 868b7b6 | 2019-05-29 21:44:40 +0200 | [diff] [blame] | 103 | call popup_close(popupwin) |
| 104 | endif |
| 105 | |
| 106 | call win_gotoid(otherwin) |
| 107 | bwipe! |
| 108 | endfunc |
Bram Moolenaar | 820680b | 2019-08-09 14:56:22 +0200 | [diff] [blame] | 109 | |
Bram Moolenaar | 345f28d | 2019-10-08 22:20:35 +0200 | [diff] [blame] | 110 | func Test_win_execute_update_ruler() |
Bram Moolenaar | 5a4c308 | 2019-12-01 15:23:11 +0100 | [diff] [blame] | 111 | CheckFeature quickfix |
| 112 | |
Bram Moolenaar | 345f28d | 2019-10-08 22:20:35 +0200 | [diff] [blame] | 113 | enew |
| 114 | call setline(1, range(500)) |
| 115 | 20 |
| 116 | split |
| 117 | let winid = win_getid() |
| 118 | set ruler |
| 119 | wincmd w |
| 120 | let height = winheight(winid) |
| 121 | redraw |
| 122 | call assert_match('20,1', Screenline(height + 1)) |
| 123 | let line = win_execute(winid, 'call cursor(100, 1)') |
| 124 | redraw |
| 125 | call assert_match('100,1', Screenline(height + 1)) |
| 126 | |
| 127 | bwipe! |
| 128 | endfunc |
| 129 | |
Bram Moolenaar | 820680b | 2019-08-09 14:56:22 +0200 | [diff] [blame] | 130 | func Test_win_execute_other_tab() |
| 131 | let thiswin = win_getid() |
| 132 | tabnew |
| 133 | call win_execute(thiswin, 'let xyz = 1') |
| 134 | call assert_equal(1, xyz) |
| 135 | tabclose |
| 136 | unlet xyz |
| 137 | endfunc |
Bram Moolenaar | e2a8f07 | 2020-01-08 19:32:18 +0100 | [diff] [blame] | 138 | |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 139 | func Test_execute_func_with_null() |
Bram Moolenaar | e2a8f07 | 2020-01-08 19:32:18 +0100 | [diff] [blame] | 140 | call assert_equal("", execute(test_null_string())) |
| 141 | call assert_equal("", execute(test_null_list())) |
| 142 | call assert_fails('call execute(test_null_dict())', 'E731:') |
| 143 | call assert_fails('call execute(test_null_blob())', 'E976:') |
| 144 | call assert_fails('call execute(test_null_partial())','E729:') |
| 145 | if has('job') |
| 146 | call assert_fails('call execute(test_null_job())', 'E908:') |
| 147 | call assert_fails('call execute(test_null_channel())', 'E908:') |
| 148 | endif |
| 149 | endfunc |
Bram Moolenaar | 9d8d0b5 | 2020-04-24 22:47:31 +0200 | [diff] [blame] | 150 | |
| 151 | " vim: shiftwidth=2 sts=2 expandtab |