blob: 221ceb0f0bdb2d5fcefd5da1b309c30c1079de2d [file] [log] [blame]
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02001" Tests for various Ex commands.
2
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01003source check.vim
zeertzjq1fa3de12021-12-31 12:19:22 +00004source shared.vim
5source term_util.vim
Christian Brabandt97817882024-03-20 20:19:47 +01006source screendump.vim
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01007
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02008func Test_ex_delete()
9 new
10 call setline(1, ['a', 'b', 'c'])
11 2
12 " :dl is :delete with the "l" flag, not :dlist
13 .dl
14 call assert_equal(['a', 'c'], getline(1, 2))
15endfunc
Bram Moolenaar0acae7a2019-08-06 21:29:29 +020016
17func Test_range_error()
18 call assert_fails(':.echo 1', 'E481:')
19 call assert_fails(':$echo 1', 'E481:')
20 call assert_fails(':1,2echo 1', 'E481:')
21 call assert_fails(':+1echo 1', 'E481:')
22 call assert_fails(':/1/echo 1', 'E481:')
23 call assert_fails(':\/echo 1', 'E481:')
24 normal vv
25 call assert_fails(":'<,'>echo 1", 'E481:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010026 call assert_fails(":\\xcenter", 'E10:')
Bram Moolenaar0acae7a2019-08-06 21:29:29 +020027endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +010028
29func Test_buffers_lastused()
30 call test_settime(localtime() - 2000) " middle
31 edit bufa
32 enew
33 call test_settime(localtime() - 10) " newest
34 edit bufb
35 enew
36 call test_settime(1550010000) " oldest
37 edit bufc
38 enew
39 call test_settime(0)
40 enew
41
42 let ls = split(execute('buffers t', 'silent!'), '\n')
43 let bufs = ls->map({i,v->split(v, '"\s*')[1:2]})
44 call assert_equal(['bufb', 'bufa', 'bufc'], bufs[1:]->map({i,v->v[0]}))
45 call assert_match('1[0-3] seconds ago', bufs[1][1])
46 call assert_match('\d\d:\d\d:\d\d', bufs[2][1])
47 call assert_match('2019/02/1\d \d\d:\d\d:00', bufs[3][1])
48
49 bwipeout bufa
50 bwipeout bufb
51 bwipeout bufc
52endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010053
54" Test for the :copy command
55func Test_copy()
56 new
57
58 call setline(1, ['L1', 'L2', 'L3', 'L4'])
59 " copy lines in a range to inside the range
60 1,3copy 2
61 call assert_equal(['L1', 'L2', 'L1', 'L2', 'L3', 'L3', 'L4'], getline(1, 7))
62
Bram Moolenaar1671f442020-03-10 07:48:13 +010063 " Specifying a count before using : to run an ex-command
64 exe "normal! gg4:yank\<CR>"
65 call assert_equal("L1\nL2\nL1\nL2\n", @")
66
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010067 close!
68endfunc
69
70" Test for the :file command
71func Test_file_cmd()
72 call assert_fails('3file', 'E474:')
73 call assert_fails('0,0file', 'E474:')
74 call assert_fails('0file abc', 'E474:')
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +020075 if !has('win32')
76 " Change the name of the buffer to the same name
77 new Xfile1
78 file Xfile1
79 call assert_equal('Xfile1', @%)
80 call assert_equal('Xfile1', @#)
81 bw!
82 endif
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010083endfunc
84
85" Test for the :drop command
86func Test_drop_cmd()
Bram Moolenaar5c645a22022-09-21 22:00:03 +010087 call writefile(['L1', 'L2'], 'Xdropfile', 'D')
Rocco Maof96dc8d2024-01-23 21:27:19 +010088 " Test for reusing the current buffer
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010089 enew | only
Rocco Maof96dc8d2024-01-23 21:27:19 +010090 let expected_nr = bufnr()
Bram Moolenaarb18b4962022-09-02 21:55:50 +010091 drop Xdropfile
Rocco Maof96dc8d2024-01-23 21:27:19 +010092 call assert_equal(expected_nr, bufnr())
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010093 call assert_equal('L2', getline(2))
94 " Test for switching to an existing window
95 below new
Bram Moolenaarb18b4962022-09-02 21:55:50 +010096 drop Xdropfile
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010097 call assert_equal(1, winnr())
Rocco Maof96dc8d2024-01-23 21:27:19 +010098 " Test for splitting the current window (set nohidden)
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010099 enew | only
100 set modified
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100101 drop Xdropfile
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100102 call assert_equal(2, winnr('$'))
Rocco Maof96dc8d2024-01-23 21:27:19 +0100103 " Not splitting the current window even if modified (set hidden)
104 set hidden
105 enew | only
106 set modified
107 drop Xdropfile
108 call assert_equal(1, winnr('$'))
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100109 " Check for setting the argument list
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100110 call assert_equal(['Xdropfile'], argv())
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100111 enew | only!
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100112endfunc
113
114" Test for the :append command
115func Test_append_cmd()
116 new
117 call setline(1, [' L1'])
118 call feedkeys(":append\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
119 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
120 %delete _
121 " append after a specific line
122 call setline(1, [' L1', ' L2', ' L3'])
123 call feedkeys(":2append\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
124 call assert_equal([' L1', ' L2', ' L4', ' L5', ' L3'], getline(1, '$'))
125 %delete _
126 " append with toggling 'autoindent'
127 call setline(1, [' L1'])
128 call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
129 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
130 call assert_false(&autoindent)
131 %delete _
132 " append with 'autoindent' set and toggling 'autoindent'
133 set autoindent
134 call setline(1, [' L1'])
135 call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
136 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
137 call assert_true(&autoindent)
138 set autoindent&
139 close!
140endfunc
141
zeertzjq1fa3de12021-12-31 12:19:22 +0000142func Test_append_cmd_empty_buf()
143 CheckRunVimInTerminal
144 let lines =<< trim END
145 func Timer(timer)
146 append
147 aaaaa
148 bbbbb
149 .
150 endfunc
151 call timer_start(10, 'Timer')
152 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100153 call writefile(lines, 'Xtest_append_cmd_empty_buf', 'D')
zeertzjq1fa3de12021-12-31 12:19:22 +0000154 let buf = RunVimInTerminal('-S Xtest_append_cmd_empty_buf', {'rows': 6})
155 call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
156 call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
157
158 " clean up
159 call StopVimInTerminal(buf)
zeertzjq1fa3de12021-12-31 12:19:22 +0000160endfunc
161
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100162" Test for the :insert command
163func Test_insert_cmd()
164 new
165 call setline(1, [' L1'])
166 call feedkeys(":insert\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
167 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
168 %delete _
169 " insert before a specific line
170 call setline(1, [' L1', ' L2', ' L3'])
171 call feedkeys(":2insert\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
172 call assert_equal([' L1', ' L4', ' L5', ' L2', ' L3'], getline(1, '$'))
173 %delete _
174 " insert with toggling 'autoindent'
175 call setline(1, [' L1'])
176 call feedkeys(":insert!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
177 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
178 call assert_false(&autoindent)
179 %delete _
180 " insert with 'autoindent' set and toggling 'autoindent'
181 set autoindent
182 call setline(1, [' L1'])
183 call feedkeys(":insert!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
184 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
185 call assert_true(&autoindent)
186 set autoindent&
187 close!
188endfunc
189
zeertzjq1fa3de12021-12-31 12:19:22 +0000190func Test_insert_cmd_empty_buf()
191 CheckRunVimInTerminal
192 let lines =<< trim END
193 func Timer(timer)
194 insert
195 aaaaa
196 bbbbb
197 .
198 endfunc
199 call timer_start(10, 'Timer')
200 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100201 call writefile(lines, 'Xtest_insert_cmd_empty_buf', 'D')
zeertzjq1fa3de12021-12-31 12:19:22 +0000202 let buf = RunVimInTerminal('-S Xtest_insert_cmd_empty_buf', {'rows': 6})
203 call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
204 call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
205
206 " clean up
207 call StopVimInTerminal(buf)
zeertzjq1fa3de12021-12-31 12:19:22 +0000208endfunc
209
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100210" Test for the :change command
211func Test_change_cmd()
212 new
213 call setline(1, [' L1', 'L2', 'L3'])
214 call feedkeys(":change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
215 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
216 %delete _
217 " change a specific line
218 call setline(1, [' L1', ' L2', ' L3'])
219 call feedkeys(":2change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
220 call assert_equal([' L1', ' L4', ' L5', ' L3'], getline(1, '$'))
221 %delete _
222 " change with toggling 'autoindent'
223 call setline(1, [' L1', 'L2', 'L3'])
224 call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
225 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
226 call assert_false(&autoindent)
227 %delete _
228 " change with 'autoindent' set and toggling 'autoindent'
229 set autoindent
230 call setline(1, [' L1', 'L2', 'L3'])
231 call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
232 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
233 call assert_true(&autoindent)
234 set autoindent&
235 close!
236endfunc
237
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100238" Test for the :language command
239func Test_language_cmd()
240 CheckFeature multi_lang
241
242 call assert_fails('language ctype non_existing_lang', 'E197:')
243 call assert_fails('language time non_existing_lang', 'E197:')
244endfunc
245
246" Test for the :confirm command dialog
247func Test_confirm_cmd()
248 CheckNotGui
249 CheckRunVimInTerminal
250
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100251 call writefile(['foo1'], 'Xfoo', 'D')
252 call writefile(['bar1'], 'Xbar', 'D')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100253
254 " Test for saving all the modified buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200255 let lines =<< trim END
256 set nomore
257 new Xfoo
258 call setline(1, 'foo2')
259 new Xbar
260 call setline(1, 'bar2')
261 wincmd b
262 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100263 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200264 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100265 call term_sendkeys(buf, ":confirm qall\n")
266 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
267 call term_sendkeys(buf, "A")
268 call StopVimInTerminal(buf)
269
Bram Moolenaar27321db2020-07-06 21:24:57 +0200270 call assert_equal(['foo2'], readfile('Xfoo'))
271 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100272
273 " Test for discarding all the changes to modified buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200274 let lines =<< trim END
275 set nomore
276 new Xfoo
277 call setline(1, 'foo3')
278 new Xbar
279 call setline(1, 'bar3')
280 wincmd b
281 END
282 call writefile(lines, 'Xscript')
283 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100284 call term_sendkeys(buf, ":confirm qall\n")
285 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
286 call term_sendkeys(buf, "D")
287 call StopVimInTerminal(buf)
288
Bram Moolenaar27321db2020-07-06 21:24:57 +0200289 call assert_equal(['foo2'], readfile('Xfoo'))
290 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100291
292 " Test for saving and discarding changes to some buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200293 let lines =<< trim END
294 set nomore
295 new Xfoo
296 call setline(1, 'foo4')
297 new Xbar
298 call setline(1, 'bar4')
299 wincmd b
300 END
301 call writefile(lines, 'Xscript')
302 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100303 call term_sendkeys(buf, ":confirm qall\n")
304 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
305 call term_sendkeys(buf, "N")
306 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, (C)ancel: ', term_getline(buf, 20))}, 1000)
307 call term_sendkeys(buf, "Y")
308 call StopVimInTerminal(buf)
309
Bram Moolenaar27321db2020-07-06 21:24:57 +0200310 call assert_equal(['foo4'], readfile('Xfoo'))
311 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaar72749f02020-03-26 20:51:43 +0100312endfunc
313
314func Test_confirm_cmd_cancel()
Bram Moolenaarbea90232020-03-26 22:09:52 +0100315 CheckNotGui
316 CheckRunVimInTerminal
317
Bram Moolenaar406cd902020-02-18 21:54:41 +0100318 " Test for closing a window with a modified buffer
Bram Moolenaar27321db2020-07-06 21:24:57 +0200319 let lines =<< trim END
320 set nomore
321 new
322 call setline(1, 'abc')
323 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100324 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200325 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaar406cd902020-02-18 21:54:41 +0100326 call term_sendkeys(buf, ":confirm close\n")
327 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
328 \ term_getline(buf, 20))}, 1000)
329 call term_sendkeys(buf, "C")
Bram Moolenaar7b1b36b2020-03-28 21:48:55 +0100330 call WaitForAssert({-> assert_equal('', term_getline(buf, 20))}, 1000)
Bram Moolenaar406cd902020-02-18 21:54:41 +0100331 call term_sendkeys(buf, ":confirm close\n")
332 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
333 \ term_getline(buf, 20))}, 1000)
334 call term_sendkeys(buf, "N")
Bram Moolenaar9207d1f2020-03-27 19:41:02 +0100335 call WaitForAssert({-> assert_match('^ *0,0-1 All$',
336 \ term_getline(buf, 20))}, 1000)
Bram Moolenaar406cd902020-02-18 21:54:41 +0100337 call StopVimInTerminal(buf)
Bram Moolenaar27321db2020-07-06 21:24:57 +0200338endfunc
339
340" The ":confirm" prompt was sometimes used with the terminal in cooked mode.
341" This test verifies that a "\<CR>" character is NOT required to respond to a
342" prompt from the ":conf q" and ":conf wq" commands.
343func Test_confirm_q_wq()
344 CheckNotGui
345 CheckRunVimInTerminal
346
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100347 call writefile(['foo'], 'Xfoo', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200348
349 let lines =<< trim END
350 set hidden nomore
351 call setline(1, 'abc')
352 edit Xfoo
353 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100354 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200355 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
356 call term_sendkeys(buf, ":confirm q\n")
357 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
358 \ term_getline(buf, 20))}, 1000)
359 call term_sendkeys(buf, 'C')
360 call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
361 \ term_getline(buf, 20))}, 1000)
362
363 call term_sendkeys(buf, ":edit Xfoo\n")
364 call term_sendkeys(buf, ":confirm wq\n")
365 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
366 \ term_getline(buf, 20))}, 1000)
367 call term_sendkeys(buf, 'C')
368 call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
369 \ term_getline(buf, 20))}, 1000)
Bram Moolenaar27321db2020-07-06 21:24:57 +0200370
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100371 call StopVimInTerminal(buf)
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100372endfunc
373
Dominique Pelle2bf60342021-05-02 20:16:24 +0200374func Test_confirm_write_ro()
375 CheckNotGui
376 CheckRunVimInTerminal
377
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100378 call writefile(['foo'], 'Xconfirm_write_ro', 'D')
Dominique Pelle2bf60342021-05-02 20:16:24 +0200379 let lines =<< trim END
380 set nobackup ff=unix cmdheight=2
381 edit Xconfirm_write_ro
382 norm Abar
383 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100384 call writefile(lines, 'Xscript', 'D')
Dominique Pelle2bf60342021-05-02 20:16:24 +0200385 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
386
387 " Try to write with 'ro' option.
388 call term_sendkeys(buf, ":set ro | confirm w\n")
389 call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
390 \ term_getline(buf, 18))}, 1000)
391 call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
392 \ term_getline(buf, 19))}, 1000)
393 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
394 call term_sendkeys(buf, 'N')
395 call WaitForAssert({-> assert_match('^ *$', term_getline(buf, 19))}, 1000)
396 call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
397 call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
398
399 call term_sendkeys(buf, ":confirm w\n")
400 call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
401 \ term_getline(buf, 18))}, 1000)
402 call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
403 \ term_getline(buf, 19))}, 1000)
404 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
405 call term_sendkeys(buf, 'Y')
406 call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 7B written$',
407 \ term_getline(buf, 19))}, 1000)
408 call assert_equal(['foobar'], readfile('Xconfirm_write_ro'))
409
410 " Try to write with read-only file permissions.
411 call setfperm('Xconfirm_write_ro', 'r--r--r--')
412 call term_sendkeys(buf, ":set noro | undo | confirm w\n")
413 call WaitForAssert({-> assert_match("^File permissions of \"Xconfirm_write_ro\" are read-only\. *$",
414 \ term_getline(buf, 17))}, 1000)
415 call WaitForAssert({-> assert_match('^It may still be possible to write it\. *$',
416 \ term_getline(buf, 18))}, 1000)
417 call WaitForAssert({-> assert_match('^Do you wish to try? *$', term_getline(buf, 19))}, 1000)
418 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
419 call term_sendkeys(buf, 'Y')
420 call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 4B written$',
421 \ term_getline(buf, 19))}, 1000)
422 call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
423
424 call StopVimInTerminal(buf)
Dominique Pelle2bf60342021-05-02 20:16:24 +0200425endfunc
426
Dominique Pellebd9e7962021-08-09 21:04:44 +0200427func Test_confirm_write_partial_file()
428 CheckNotGui
429 CheckRunVimInTerminal
430
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100431 call writefile(['a', 'b', 'c', 'd'], 'Xwrite_partial', 'D')
Dominique Pellebd9e7962021-08-09 21:04:44 +0200432 call writefile(['set nobackup ff=unix cmdheight=2',
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100433 \ 'edit Xwrite_partial'], 'Xscript', 'D')
Dominique Pellebd9e7962021-08-09 21:04:44 +0200434 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
435
436 call term_sendkeys(buf, ":confirm 2,3w\n")
437 call WaitForAssert({-> assert_match('^Write partial file? *$',
438 \ term_getline(buf, 19))}, 1000)
439 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
440 \ term_getline(buf, 20))}, 1000)
441 call term_sendkeys(buf, 'N')
442 call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
443 call assert_equal(['a', 'b', 'c', 'd'], readfile('Xwrite_partial'))
444 call delete('Xwrite_partial')
445
446 call term_sendkeys(buf, ":confirm 2,3w\n")
447 call WaitForAssert({-> assert_match('^Write partial file? *$',
448 \ term_getline(buf, 19))}, 1000)
449 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
450 \ term_getline(buf, 20))}, 1000)
451 call term_sendkeys(buf, 'Y')
452 call WaitForAssert({-> assert_match('^"Xwrite_partial" \[New\] 2L, 4B written *$',
453 \ term_getline(buf, 19))}, 1000)
454 call WaitForAssert({-> assert_match('^Press ENTER or type command to continue *$',
455 \ term_getline(buf, 20))}, 1000)
456 call assert_equal(['b', 'c'], readfile('Xwrite_partial'))
457
458 call StopVimInTerminal(buf)
Dominique Pellebd9e7962021-08-09 21:04:44 +0200459endfunc
460
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100461" Test for the :print command
462func Test_print_cmd()
463 call assert_fails('print', 'E749:')
464endfunc
465
466" Test for the :winsize command
467func Test_winsize_cmd()
468 call assert_fails('winsize 1', 'E465:')
Bram Moolenaarf5a51162021-02-06 12:58:18 +0100469 call assert_fails('winsize 1 x', 'E465:')
470 call assert_fails('win_getid(1)', 'E475: Invalid argument: _getid(1)')
471 " Actually changing the window size would be flaky.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100472endfunc
473
474" Test for the :redir command
Bram Moolenaarf9a65502021-03-05 20:47:44 +0100475" NOTE: if you run tests as root this will fail. Don't run tests as root!
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100476func Test_redir_cmd()
477 call assert_fails('redir @@', 'E475:')
478 call assert_fails('redir abc', 'E475:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100479 call assert_fails('redir => 1abc', 'E474:')
480 call assert_fails('redir => a b', 'E488:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200481 call assert_fails('redir => abc[1]', 'E121:')
482 let b = 0zFF
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100483 call assert_fails('redir =>> b', 'E734:')
484 unlet b
485
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100486 if has('unix')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100487 " Redirecting to a directory name
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100488 call mkdir('Xredir')
489 call assert_fails('redir > Xredir', 'E17:')
490 call delete('Xredir', 'd')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100491 endif
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100492
493 " Test for redirecting to a register
494 redir @q> | echon 'clean ' | redir END
495 redir @q>> | echon 'water' | redir END
496 call assert_equal('clean water', @q)
497
498 " Test for redirecting to a variable
499 redir => color | echon 'blue ' | redir END
500 redir =>> color | echon 'sky' | redir END
501 call assert_equal('blue sky', color)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100502endfunc
503
Bram Moolenaar17709e22021-03-19 14:38:12 +0100504func Test_redir_cmd_readonly()
505 CheckNotRoot
Bram Moolenaar17709e22021-03-19 14:38:12 +0100506
507 " Redirecting to a read-only file
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100508 call writefile([], 'Xredirfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100509 call setfperm('Xredirfile', 'r--r--r--')
510 call assert_fails('redir! > Xredirfile', 'E190:')
Bram Moolenaar17709e22021-03-19 14:38:12 +0100511endfunc
512
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100513" Test for the :filetype command
514func Test_filetype_cmd()
515 call assert_fails('filetype abc', 'E475:')
516endfunc
517
518" Test for the :mode command
519func Test_mode_cmd()
520 call assert_fails('mode abc', 'E359:')
521endfunc
522
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100523" Test for the :sleep command
524func Test_sleep_cmd()
525 call assert_fails('sleep x', 'E475:')
526endfunc
527
528" Test for the :read command
529func Test_read_cmd()
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100530 call writefile(['one'], 'Xcmdfile', 'D')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100531 new
532 call assert_fails('read', 'E32:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100533 edit Xcmdfile
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100534 read
535 call assert_equal(['one', 'one'], getline(1, '$'))
536 close!
537 new
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100538 read Xcmdfile
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100539 call assert_equal(['', 'one'], getline(1, '$'))
540 call deletebufline('', 1, '$')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100541 call feedkeys("Qr Xcmdfile\<CR>visual\<CR>", 'xt')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100542 call assert_equal(['one'], getline(1, '$'))
543 close!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100544endfunc
545
546" Test for running Ex commands when text is locked.
547" <C-\>e in the command line is used to lock the text
548func Test_run_excmd_with_text_locked()
549 " :quit
550 let cmd = ":\<C-\>eexecute('quit')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200551 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100552
553 " :qall
554 let cmd = ":\<C-\>eexecute('qall')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200555 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100556
557 " :exit
558 let cmd = ":\<C-\>eexecute('exit')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200559 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100560
561 " :close - should be ignored
562 new
563 let cmd = ":\<C-\>eexecute('close')\<CR>\<C-C>"
564 call assert_equal(2, winnr('$'))
565 close
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100566
Bram Moolenaarff06f282020-04-21 22:01:14 +0200567 call assert_fails("call feedkeys(\":\<C-R>=execute('bnext')\<CR>\", 'xt')", 'E565:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200568
569 " :tabfirst
570 tabnew
571 call assert_fails("call feedkeys(\":\<C-R>=execute('tabfirst')\<CR>\", 'xt')", 'E565:')
572 tabclose
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100573endfunc
574
575" Test for the :verbose command
576func Test_verbose_cmd()
Bram Moolenaar60895f32022-04-12 14:23:19 +0100577 set verbose=3
578 call assert_match(' verbose=1\n\s*Last set from ', execute('verbose set vbs'), "\n")
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100579 call assert_equal([' verbose=0'], split(execute('0verbose set vbs'), "\n"))
Bram Moolenaar60895f32022-04-12 14:23:19 +0100580 set verbose=0
581 call assert_match(' verbose=4\n\s*Last set from .*\n verbose=0',
582 \ execute("4verbose set verbose | set verbose"))
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100583endfunc
584
585" Test for the :delete command and the related abbreviated commands
586func Test_excmd_delete()
587 new
588 call setline(1, ['foo', "\tbar"])
589 call assert_equal(['^Ibar$'], split(execute('dl'), "\n"))
590 call setline(1, ['foo', "\tbar"])
591 call assert_equal(['^Ibar$'], split(execute('dell'), "\n"))
592 call setline(1, ['foo', "\tbar"])
593 call assert_equal(['^Ibar$'], split(execute('delel'), "\n"))
594 call setline(1, ['foo', "\tbar"])
595 call assert_equal(['^Ibar$'], split(execute('deletl'), "\n"))
596 call setline(1, ['foo', "\tbar"])
597 call assert_equal(['^Ibar$'], split(execute('deletel'), "\n"))
598 call setline(1, ['foo', "\tbar"])
599 call assert_equal([' bar'], split(execute('dp'), "\n"))
600 call setline(1, ['foo', "\tbar"])
601 call assert_equal([' bar'], split(execute('dep'), "\n"))
602 call setline(1, ['foo', "\tbar"])
603 call assert_equal([' bar'], split(execute('delp'), "\n"))
604 call setline(1, ['foo', "\tbar"])
605 call assert_equal([' bar'], split(execute('delep'), "\n"))
606 call setline(1, ['foo', "\tbar"])
607 call assert_equal([' bar'], split(execute('deletp'), "\n"))
608 call setline(1, ['foo', "\tbar"])
609 call assert_equal([' bar'], split(execute('deletep'), "\n"))
610 close!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100611endfunc
612
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200613" Test for commands that are blocked in a sandbox
614func Sandbox_tests()
615 call assert_fails("call histadd(':', 'ls')", 'E48:')
616 call assert_fails("call mkdir('Xdir')", 'E48:')
617 call assert_fails("call rename('a', 'b')", 'E48:')
618 call assert_fails("call setbufvar(1, 'myvar', 1)", 'E48:')
619 call assert_fails("call settabvar(1, 'myvar', 1)", 'E48:')
620 call assert_fails("call settabwinvar(1, 1, 'myvar', 1)", 'E48:')
621 call assert_fails("call setwinvar(1, 'myvar', 1)", 'E48:')
622 call assert_fails("call timer_start(100, '')", 'E48:')
623 if has('channel')
624 call assert_fails("call prompt_setcallback(1, '')", 'E48:')
625 call assert_fails("call prompt_setinterrupt(1, '')", 'E48:')
626 call assert_fails("call prompt_setprompt(1, '')", 'E48:')
627 endif
628 call assert_fails("let $TESTVAR=1", 'E48:')
629 call assert_fails("call feedkeys('ivim')", 'E48:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100630 call assert_fails("source! Xsomefile", 'E48:')
631 call assert_fails("call delete('Xthatfile')", 'E48:')
632 call assert_fails("call writefile([], 'Xanotherfile')", 'E48:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200633 call assert_fails('!ls', 'E48:')
634 call assert_fails('shell', 'E48:')
635 call assert_fails('stop', 'E48:')
636 call assert_fails('exe "normal \<C-Z>"', 'E48:')
637 set insertmode
638 call assert_fails('call feedkeys("\<C-Z>", "xt")', 'E48:')
639 set insertmode&
640 call assert_fails('suspend', 'E48:')
641 call assert_fails('call system("ls")', 'E48:')
642 call assert_fails('call systemlist("ls")', 'E48:')
643 if has('clientserver')
644 call assert_fails('let s=remote_expr("gvim", "2+2")', 'E48:')
645 if !has('win32')
Dominique Pelle923dce22021-11-21 11:36:04 +0000646 " remote_foreground() doesn't throw an error message on MS-Windows
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200647 call assert_fails('call remote_foreground("gvim")', 'E48:')
648 endif
649 call assert_fails('let s=remote_peek("gvim")', 'E48:')
650 call assert_fails('let s=remote_read("gvim")', 'E48:')
651 call assert_fails('let s=remote_send("gvim", "abc")', 'E48:')
652 call assert_fails('let s=server2client("gvim", "abc")', 'E48:')
653 endif
654 if has('terminal')
655 call assert_fails('terminal', 'E48:')
656 call assert_fails('call term_start("vim")', 'E48:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100657 call assert_fails('call term_dumpwrite(1, "Xdumpfile")', 'E48:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200658 endif
659 if has('channel')
660 call assert_fails("call ch_logfile('chlog')", 'E48:')
661 call assert_fails("call ch_open('localhost:8765')", 'E48:')
662 endif
663 if has('job')
664 call assert_fails("call job_start('vim')", 'E48:')
665 endif
666 if has('unix') && has('libcall')
667 call assert_fails("echo libcall('libc.so', 'getenv', 'HOME')", 'E48:')
668 endif
669 if has('unix')
670 call assert_fails('cd `pwd`', 'E48:')
671 endif
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200672 " some options cannot be changed in a sandbox
673 call assert_fails('set exrc', 'E48:')
674 call assert_fails('set cdpath', 'E48:')
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200675 if has('xim') && has('gui_gtk')
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200676 call assert_fails('set imstyle', 'E48:')
677 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200678endfunc
679
680func Test_sandbox()
681 sandbox call Sandbox_tests()
682endfunc
683
Dominique Pelle6d37e8e2021-05-06 17:36:55 +0200684func Test_command_not_implemented_E319()
685 if !has('mzscheme')
686 call assert_fails('mzscheme', 'E319:')
687 endif
688endfunc
689
kuuote08d7b1c2021-10-04 22:17:36 +0100690func Test_not_break_expression_register()
691 call setreg('=', '1+1')
692 if 0
693 put =1
694 endif
695 call assert_equal('1+1', getreg('=', 1))
696endfunc
697
Bram Moolenaar03725c52021-11-24 12:17:53 +0000698func Test_address_line_overflow()
699 if v:sizeoflong < 8
700 throw 'Skipped: only works with 64 bit long ints'
701 endif
702 new
Bram Moolenaar3cf21b32022-01-11 19:34:16 +0000703 call setline(1, range(100))
Bram Moolenaar03725c52021-11-24 12:17:53 +0000704 call assert_fails('|.44444444444444444444444', 'E1247:')
705 call assert_fails('|.9223372036854775806', 'E1247:')
Bram Moolenaar3cf21b32022-01-11 19:34:16 +0000706
707 $
708 yank 77777777777777777777
709 call assert_equal("99\n", @")
710
Bram Moolenaar03725c52021-11-24 12:17:53 +0000711 bwipe!
712endfunc
713
Bram Moolenaar4d97a562022-05-28 14:25:35 +0100714" This was leaving the cursor in line zero
715func Test_using_zero_in_range()
716 new
717 norm o00
718 silent! 0;s/\%')
719 bwipe!
720endfunc
721
ii141f0dc5e2022-07-26 19:44:56 +0100722" Test :write after changing name with :file and loading it with :edit
723func Test_write_after_rename()
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100724 call writefile(['text'], 'Xafterfile', 'D')
ii141f0dc5e2022-07-26 19:44:56 +0100725
726 enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100727 file Xafterfile
ii141f0dc5e2022-07-26 19:44:56 +0100728 call assert_fails('write', 'E13: File exists (add ! to override)')
729
730 " works OK after ":edit"
731 edit
732 write
733
ii141f0dc5e2022-07-26 19:44:56 +0100734 bwipe!
735endfunc
736
Christian Brabandt060623e2023-11-14 21:33:29 +0100737" catch address lines overflow
738func Test_ex_address_range_overflow()
739 call assert_fails(':--+foobar', 'E492:')
740endfunc
Bram Moolenaar03725c52021-11-24 12:17:53 +0000741
Christian Brabandt97817882024-03-20 20:19:47 +0100742func Test_drop_modified_file()
743 CheckScreendump
744 let lines =<< trim END
745 call setline(1, 'The quick brown fox jumped over the lazy dogs')
746 END
747 call writefile([''], 'Xdrop_modified.txt', 'D')
748 call writefile(lines, 'Xtest_drop_modified', 'D')
749 let buf = RunVimInTerminal('-S Xtest_drop_modified Xdrop_modified.txt', {'rows': 10,'columns': 40})
750 call term_sendkeys(buf, ":drop Xdrop_modified.txt\<CR>")
751 call VerifyScreenDump(buf, 'Test_drop_modified_1', {})
752
753 " clean up
754 call StopVimInTerminal(buf)
755endfunc
756
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100757" vim: shiftwidth=2 sts=2 expandtab