blob: b25f211f8b935bd11ea2bb539b15ff2bb6ba0c63 [file] [log] [blame]
Bram Moolenaar79815f12016-07-09 17:07:29 +02001" test execute()
2
Bram Moolenaar345f28d2019-10-08 22:20:35 +02003source view_util.vim
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004source check.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00005import './vim9.vim' as v9
Bram Moolenaardab17a02021-12-20 21:35:59 +00006source term_util.vim
Bram Moolenaar345f28d2019-10-08 22:20:35 +02007
Bram Moolenaar79815f12016-07-09 17:07:29 +02008func NestedEval()
9 let nested = execute('echo "nested\nlines"')
10 echo 'got: "' . nested . '"'
11endfunc
12
13func NestedRedir()
14 redir => var
15 echo 'broken'
16 redir END
17endfunc
18
19func 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 Moolenaar79815f12016-07-09 17:07:29 +020035 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 Moolenaar73e28dc2022-09-17 21:08:33 +010041 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 Moolenaar79815f12016-07-09 17:07:29 +020044endfunc
45
46func 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 Moolenaar79815f12016-07-09 17:07:29 +020054endfunc
Bram Moolenaar10ccaa12018-12-07 16:38:23 +010055
56func 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)
66endfunc
Bram Moolenaar446e7a32018-12-08 13:57:42 +010067
68func 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)
83endfunc
Bram Moolenaar868b7b62019-05-29 21:44:40 +020084
85func 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 Moolenaar37487e12021-01-12 22:08:53 +010093 let line = win_execute(134343, 'echo getline(1)')
94 call assert_equal('', line)
Bram Moolenaar868b7b62019-05-29 21:44:40 +020095
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010096 if has('popupwin')
Bram Moolenaar868b7b62019-05-29 21:44:40 +020097 let popupwin = popup_create('the popup win', {'line': 2, 'col': 3})
98 redraw
Bram Moolenaarf92e58c2019-09-08 21:51:41 +020099 let line = 'echo getline(1)'->win_execute(popupwin)
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200100 call assert_match('the popup win', line)
101
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200102 call popup_close(popupwin)
103 endif
104
105 call win_gotoid(otherwin)
106 bwipe!
Bram Moolenaar7f13b242021-11-14 11:41:31 +0000107
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 Moolenaar868b7b62019-05-29 21:44:40 +0200119endfunc
Bram Moolenaar820680b2019-08-09 14:56:22 +0200120
Bram Moolenaar345f28d2019-10-08 22:20:35 +0200121func Test_win_execute_update_ruler()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100122 CheckFeature quickfix
123
Bram Moolenaar345f28d2019-10-08 22:20:35 +0200124 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!
139endfunc
140
Bram Moolenaar820680b2019-08-09 14:56:22 +0200141func 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
148endfunc
Bram Moolenaare2a8f072020-01-08 19:32:18 +0100149
Bram Moolenaar18f47402022-01-06 13:24:51 +0000150func Test_win_execute_visual_redraw()
151 call setline(1, ['a', 'b', 'c'])
152 new
153 wincmd p
Bram Moolenaare664a322022-01-07 14:08:03 +0000154 " start Visual in current window, redraw in other window with fewer lines
Bram Moolenaar18f47402022-01-06 13:24:51 +0000155 call feedkeys("G\<C-V>", 'txn')
156 call win_execute(winnr('#')->win_getid(), 'redraw')
Bram Moolenaare664a322022-01-07 14:08:03 +0000157 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 Moolenaar18f47402022-01-06 13:24:51 +0000171 bwipe!
172 bwipe!
173endfunc
174
Bram Moolenaardab17a02021-12-20 21:35:59 +0000175func 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
186 call writefile(lines, 'XwinExecute')
187 let buf = RunVimInTerminal('-p Xfile1 -Nu XwinExecute', {})
188
189 " this was crashing on exit with EXITFREE defined
190 call StopVimInTerminal(buf)
191
192 call delete('XwinExecute')
193 call delete('Xfile1')
194endfunc
195
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200196func Test_execute_func_with_null()
Bram Moolenaare2a8f072020-01-08 19:32:18 +0100197 call assert_equal("", execute(test_null_string()))
198 call assert_equal("", execute(test_null_list()))
199 call assert_fails('call execute(test_null_dict())', 'E731:')
200 call assert_fails('call execute(test_null_blob())', 'E976:')
201 call assert_fails('call execute(test_null_partial())','E729:')
202 if has('job')
203 call assert_fails('call execute(test_null_job())', 'E908:')
204 call assert_fails('call execute(test_null_channel())', 'E908:')
205 endif
206endfunc
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200207
208" vim: shiftwidth=2 sts=2 expandtab