blob: 8cd5e7d270d4a77abc1cca2154bb54250217939b [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 Moolenaar3cfa5b12021-06-06 14:14:39 +02005source vim9.vim
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 Moolenaar5feabe02020-01-30 18:24:53 +010041 if has('float')
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +020042 call assert_fails('call execute(3.4)', 'E492:')
43 call assert_equal("\nx", execute("echo \"x\"", 3.4))
Bram Moolenaar86b3ab42021-12-19 18:33:23 +000044 call CheckDefExecAndScriptFailure(['execute("echo \"x\"", 3.4)'], ['E1013: Argument 2: type mismatch, expected string but got float', 'E1174:'])
Bram Moolenaar5feabe02020-01-30 18:24:53 +010045 endif
Bram Moolenaar79815f12016-07-09 17:07:29 +020046endfunc
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([]))
Bram Moolenaar79815f12016-07-09 17:07:29 +020056endfunc
Bram Moolenaar10ccaa12018-12-07 16:38:23 +010057
58func 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)
68endfunc
Bram Moolenaar446e7a32018-12-08 13:57:42 +010069
70func 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)
85endfunc
Bram Moolenaar868b7b62019-05-29 21:44:40 +020086
87func 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 Moolenaar37487e12021-01-12 22:08:53 +010095 let line = win_execute(134343, 'echo getline(1)')
96 call assert_equal('', line)
Bram Moolenaar868b7b62019-05-29 21:44:40 +020097
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +010098 if has('popupwin')
Bram Moolenaar868b7b62019-05-29 21:44:40 +020099 let popupwin = popup_create('the popup win', {'line': 2, 'col': 3})
100 redraw
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200101 let line = 'echo getline(1)'->win_execute(popupwin)
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200102 call assert_match('the popup win', line)
103
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200104 call popup_close(popupwin)
105 endif
106
107 call win_gotoid(otherwin)
108 bwipe!
Bram Moolenaar7f13b242021-11-14 11:41:31 +0000109
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 Moolenaar868b7b62019-05-29 21:44:40 +0200121endfunc
Bram Moolenaar820680b2019-08-09 14:56:22 +0200122
Bram Moolenaar345f28d2019-10-08 22:20:35 +0200123func Test_win_execute_update_ruler()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100124 CheckFeature quickfix
125
Bram Moolenaar345f28d2019-10-08 22:20:35 +0200126 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!
141endfunc
142
Bram Moolenaar820680b2019-08-09 14:56:22 +0200143func 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
150endfunc
Bram Moolenaare2a8f072020-01-08 19:32:18 +0100151
Bram Moolenaar18f47402022-01-06 13:24:51 +0000152func Test_win_execute_visual_redraw()
153 call setline(1, ['a', 'b', 'c'])
154 new
155 wincmd p
Bram Moolenaare664a322022-01-07 14:08:03 +0000156 " start Visual in current window, redraw in other window with fewer lines
Bram Moolenaar18f47402022-01-06 13:24:51 +0000157 call feedkeys("G\<C-V>", 'txn')
158 call win_execute(winnr('#')->win_getid(), 'redraw')
Bram Moolenaare664a322022-01-07 14:08:03 +0000159 call feedkeys("\<Esc>", 'txn')
160 bwipe!
161 bwipe!
162
163 enew
164 new
165 call setline(1, ['a', 'b', 'c'])
166 let winid = win_getid()
167 wincmd p
168 " start Visual in current window, extend it in other window with more lines
169 call feedkeys("\<C-V>", 'txn')
170 call win_execute(winid, 'call feedkeys("G\<C-V>", ''txn'')')
171 redraw
172
Bram Moolenaar18f47402022-01-06 13:24:51 +0000173 bwipe!
174 bwipe!
175endfunc
176
Bram Moolenaardab17a02021-12-20 21:35:59 +0000177func Test_win_execute_on_startup()
178 CheckRunVimInTerminal
179
180 let lines =<< trim END
181 vim9script
182 [repeat('x', &columns)]->writefile('Xfile1')
183 silent tabedit Xfile2
184 var id = win_getid()
185 silent tabedit Xfile3
186 autocmd VimEnter * win_execute(id, 'close')
187 END
188 call writefile(lines, 'XwinExecute')
189 let buf = RunVimInTerminal('-p Xfile1 -Nu XwinExecute', {})
190
191 " this was crashing on exit with EXITFREE defined
192 call StopVimInTerminal(buf)
193
194 call delete('XwinExecute')
195 call delete('Xfile1')
196endfunc
197
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200198func Test_execute_func_with_null()
Bram Moolenaare2a8f072020-01-08 19:32:18 +0100199 call assert_equal("", execute(test_null_string()))
200 call assert_equal("", execute(test_null_list()))
201 call assert_fails('call execute(test_null_dict())', 'E731:')
202 call assert_fails('call execute(test_null_blob())', 'E976:')
203 call assert_fails('call execute(test_null_partial())','E729:')
204 if has('job')
205 call assert_fails('call execute(test_null_job())', 'E908:')
206 call assert_fails('call execute(test_null_channel())', 'E908:')
207 endif
208endfunc
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200209
210" vim: shiftwidth=2 sts=2 expandtab