blob: 8c3282855ec1912b829fc975a9eab0573bfee987 [file] [log] [blame]
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02001" Tests for various Ex commands.
2
Christian Brabandteb380b92025-07-07 20:53:55 +02003source util/screendump.vim
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01004
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02005func Test_ex_delete()
6 new
7 call setline(1, ['a', 'b', 'c'])
8 2
9 " :dl is :delete with the "l" flag, not :dlist
10 .dl
11 call assert_equal(['a', 'c'], getline(1, 2))
12endfunc
Bram Moolenaar0acae7a2019-08-06 21:29:29 +020013
14func Test_range_error()
15 call assert_fails(':.echo 1', 'E481:')
16 call assert_fails(':$echo 1', 'E481:')
17 call assert_fails(':1,2echo 1', 'E481:')
18 call assert_fails(':+1echo 1', 'E481:')
19 call assert_fails(':/1/echo 1', 'E481:')
20 call assert_fails(':\/echo 1', 'E481:')
21 normal vv
22 call assert_fails(":'<,'>echo 1", 'E481:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010023 call assert_fails(":\\xcenter", 'E10:')
Bram Moolenaar0acae7a2019-08-06 21:29:29 +020024endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +010025
26func Test_buffers_lastused()
27 call test_settime(localtime() - 2000) " middle
28 edit bufa
29 enew
30 call test_settime(localtime() - 10) " newest
31 edit bufb
32 enew
33 call test_settime(1550010000) " oldest
34 edit bufc
35 enew
36 call test_settime(0)
37 enew
38
39 let ls = split(execute('buffers t', 'silent!'), '\n')
40 let bufs = ls->map({i,v->split(v, '"\s*')[1:2]})
41 call assert_equal(['bufb', 'bufa', 'bufc'], bufs[1:]->map({i,v->v[0]}))
42 call assert_match('1[0-3] seconds ago', bufs[1][1])
43 call assert_match('\d\d:\d\d:\d\d', bufs[2][1])
44 call assert_match('2019/02/1\d \d\d:\d\d:00', bufs[3][1])
45
46 bwipeout bufa
47 bwipeout bufb
48 bwipeout bufc
49endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010050
51" Test for the :copy command
52func Test_copy()
53 new
54
55 call setline(1, ['L1', 'L2', 'L3', 'L4'])
56 " copy lines in a range to inside the range
57 1,3copy 2
58 call assert_equal(['L1', 'L2', 'L1', 'L2', 'L3', 'L3', 'L4'], getline(1, 7))
59
Bram Moolenaar1671f442020-03-10 07:48:13 +010060 " Specifying a count before using : to run an ex-command
61 exe "normal! gg4:yank\<CR>"
62 call assert_equal("L1\nL2\nL1\nL2\n", @")
63
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010064 close!
65endfunc
66
67" Test for the :file command
68func Test_file_cmd()
69 call assert_fails('3file', 'E474:')
70 call assert_fails('0,0file', 'E474:')
71 call assert_fails('0file abc', 'E474:')
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +020072 if !has('win32')
73 " Change the name of the buffer to the same name
74 new Xfile1
75 file Xfile1
76 call assert_equal('Xfile1', @%)
77 call assert_equal('Xfile1', @#)
78 bw!
79 endif
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010080endfunc
81
82" Test for the :drop command
83func Test_drop_cmd()
Bram Moolenaar5c645a22022-09-21 22:00:03 +010084 call writefile(['L1', 'L2'], 'Xdropfile', 'D')
Rocco Maof96dc8d2024-01-23 21:27:19 +010085 " Test for reusing the current buffer
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010086 enew | only
Rocco Maof96dc8d2024-01-23 21:27:19 +010087 let expected_nr = bufnr()
Bram Moolenaarb18b4962022-09-02 21:55:50 +010088 drop Xdropfile
Rocco Maof96dc8d2024-01-23 21:27:19 +010089 call assert_equal(expected_nr, bufnr())
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010090 call assert_equal('L2', getline(2))
91 " Test for switching to an existing window
92 below new
Bram Moolenaarb18b4962022-09-02 21:55:50 +010093 drop Xdropfile
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010094 call assert_equal(1, winnr())
Rocco Maof96dc8d2024-01-23 21:27:19 +010095 " Test for splitting the current window (set nohidden)
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010096 enew | only
97 set modified
Bram Moolenaarb18b4962022-09-02 21:55:50 +010098 drop Xdropfile
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010099 call assert_equal(2, winnr('$'))
Rocco Maof96dc8d2024-01-23 21:27:19 +0100100 " Not splitting the current window even if modified (set hidden)
101 set hidden
102 enew | only
103 set modified
104 drop Xdropfile
105 call assert_equal(1, winnr('$'))
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100106 " Check for setting the argument list
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100107 call assert_equal(['Xdropfile'], argv())
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100108 enew | only!
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100109endfunc
110
111" Test for the :append command
112func Test_append_cmd()
113 new
114 call setline(1, [' L1'])
115 call feedkeys(":append\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
116 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
117 %delete _
118 " append after a specific line
119 call setline(1, [' L1', ' L2', ' L3'])
120 call feedkeys(":2append\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
121 call assert_equal([' L1', ' L2', ' L4', ' L5', ' L3'], getline(1, '$'))
122 %delete _
123 " append with toggling 'autoindent'
124 call setline(1, [' L1'])
125 call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
126 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
127 call assert_false(&autoindent)
128 %delete _
129 " append with 'autoindent' set and toggling 'autoindent'
130 set autoindent
131 call setline(1, [' L1'])
132 call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
133 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
134 call assert_true(&autoindent)
135 set autoindent&
136 close!
137endfunc
138
zeertzjq1fa3de12021-12-31 12:19:22 +0000139func Test_append_cmd_empty_buf()
140 CheckRunVimInTerminal
141 let lines =<< trim END
142 func Timer(timer)
143 append
144 aaaaa
145 bbbbb
146 .
147 endfunc
148 call timer_start(10, 'Timer')
149 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100150 call writefile(lines, 'Xtest_append_cmd_empty_buf', 'D')
zeertzjq1fa3de12021-12-31 12:19:22 +0000151 let buf = RunVimInTerminal('-S Xtest_append_cmd_empty_buf', {'rows': 6})
152 call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
153 call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
154
155 " clean up
156 call StopVimInTerminal(buf)
zeertzjq1fa3de12021-12-31 12:19:22 +0000157endfunc
158
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100159" Test for the :insert command
160func Test_insert_cmd()
161 new
162 call setline(1, [' L1'])
163 call feedkeys(":insert\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
164 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
165 %delete _
166 " insert before a specific line
167 call setline(1, [' L1', ' L2', ' L3'])
168 call feedkeys(":2insert\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
169 call assert_equal([' L1', ' L4', ' L5', ' L2', ' L3'], getline(1, '$'))
170 %delete _
171 " insert with toggling 'autoindent'
172 call setline(1, [' L1'])
173 call feedkeys(":insert!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
174 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
175 call assert_false(&autoindent)
176 %delete _
177 " insert with 'autoindent' set and toggling 'autoindent'
178 set autoindent
179 call setline(1, [' L1'])
180 call feedkeys(":insert!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
181 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
182 call assert_true(&autoindent)
183 set autoindent&
184 close!
185endfunc
186
zeertzjq1fa3de12021-12-31 12:19:22 +0000187func Test_insert_cmd_empty_buf()
188 CheckRunVimInTerminal
189 let lines =<< trim END
190 func Timer(timer)
191 insert
192 aaaaa
193 bbbbb
194 .
195 endfunc
196 call timer_start(10, 'Timer')
197 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100198 call writefile(lines, 'Xtest_insert_cmd_empty_buf', 'D')
zeertzjq1fa3de12021-12-31 12:19:22 +0000199 let buf = RunVimInTerminal('-S Xtest_insert_cmd_empty_buf', {'rows': 6})
200 call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
201 call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
202
203 " clean up
204 call StopVimInTerminal(buf)
zeertzjq1fa3de12021-12-31 12:19:22 +0000205endfunc
206
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100207" Test for the :change command
208func Test_change_cmd()
209 new
210 call setline(1, [' L1', 'L2', 'L3'])
211 call feedkeys(":change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
212 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
213 %delete _
214 " change a specific line
215 call setline(1, [' L1', ' L2', ' L3'])
216 call feedkeys(":2change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
217 call assert_equal([' L1', ' L4', ' L5', ' L3'], getline(1, '$'))
218 %delete _
219 " change with toggling 'autoindent'
220 call setline(1, [' L1', 'L2', 'L3'])
221 call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
222 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
223 call assert_false(&autoindent)
224 %delete _
225 " change with 'autoindent' set and toggling 'autoindent'
226 set autoindent
227 call setline(1, [' L1', 'L2', 'L3'])
228 call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
229 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
230 call assert_true(&autoindent)
231 set autoindent&
232 close!
233endfunc
234
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100235" Test for the :language command
236func Test_language_cmd()
237 CheckFeature multi_lang
238
239 call assert_fails('language ctype non_existing_lang', 'E197:')
240 call assert_fails('language time non_existing_lang', 'E197:')
241endfunc
242
243" Test for the :confirm command dialog
244func Test_confirm_cmd()
245 CheckNotGui
246 CheckRunVimInTerminal
247
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100248 call writefile(['foo1'], 'Xfoo', 'D')
249 call writefile(['bar1'], 'Xbar', 'D')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100250
251 " Test for saving all the modified buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200252 let lines =<< trim END
253 set nomore
254 new Xfoo
255 call setline(1, 'foo2')
256 new Xbar
257 call setline(1, 'bar2')
258 wincmd b
259 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100260 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200261 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100262 call term_sendkeys(buf, ":confirm qall\n")
263 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
264 call term_sendkeys(buf, "A")
265 call StopVimInTerminal(buf)
266
Bram Moolenaar27321db2020-07-06 21:24:57 +0200267 call assert_equal(['foo2'], readfile('Xfoo'))
268 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100269
270 " Test for discarding all the changes to modified buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200271 let lines =<< trim END
272 set nomore
273 new Xfoo
274 call setline(1, 'foo3')
275 new Xbar
276 call setline(1, 'bar3')
277 wincmd b
278 END
279 call writefile(lines, 'Xscript')
280 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100281 call term_sendkeys(buf, ":confirm qall\n")
282 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
283 call term_sendkeys(buf, "D")
284 call StopVimInTerminal(buf)
285
Bram Moolenaar27321db2020-07-06 21:24:57 +0200286 call assert_equal(['foo2'], readfile('Xfoo'))
287 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100288
289 " Test for saving and discarding changes to some buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200290 let lines =<< trim END
291 set nomore
292 new Xfoo
293 call setline(1, 'foo4')
294 new Xbar
295 call setline(1, 'bar4')
296 wincmd b
297 END
298 call writefile(lines, 'Xscript')
299 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100300 call term_sendkeys(buf, ":confirm qall\n")
301 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
302 call term_sendkeys(buf, "N")
303 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, (C)ancel: ', term_getline(buf, 20))}, 1000)
304 call term_sendkeys(buf, "Y")
305 call StopVimInTerminal(buf)
306
Bram Moolenaar27321db2020-07-06 21:24:57 +0200307 call assert_equal(['foo4'], readfile('Xfoo'))
308 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaar72749f02020-03-26 20:51:43 +0100309endfunc
310
311func Test_confirm_cmd_cancel()
Bram Moolenaarbea90232020-03-26 22:09:52 +0100312 CheckNotGui
313 CheckRunVimInTerminal
314
Bram Moolenaar406cd902020-02-18 21:54:41 +0100315 " Test for closing a window with a modified buffer
Bram Moolenaar27321db2020-07-06 21:24:57 +0200316 let lines =<< trim END
317 set nomore
318 new
319 call setline(1, 'abc')
320 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100321 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200322 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaar406cd902020-02-18 21:54:41 +0100323 call term_sendkeys(buf, ":confirm close\n")
324 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
325 \ term_getline(buf, 20))}, 1000)
326 call term_sendkeys(buf, "C")
Bram Moolenaar7b1b36b2020-03-28 21:48:55 +0100327 call WaitForAssert({-> assert_equal('', term_getline(buf, 20))}, 1000)
Bram Moolenaar406cd902020-02-18 21:54:41 +0100328 call term_sendkeys(buf, ":confirm close\n")
329 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
330 \ term_getline(buf, 20))}, 1000)
331 call term_sendkeys(buf, "N")
Bram Moolenaar9207d1f2020-03-27 19:41:02 +0100332 call WaitForAssert({-> assert_match('^ *0,0-1 All$',
333 \ term_getline(buf, 20))}, 1000)
Bram Moolenaar406cd902020-02-18 21:54:41 +0100334 call StopVimInTerminal(buf)
Bram Moolenaar27321db2020-07-06 21:24:57 +0200335endfunc
336
337" The ":confirm" prompt was sometimes used with the terminal in cooked mode.
338" This test verifies that a "\<CR>" character is NOT required to respond to a
339" prompt from the ":conf q" and ":conf wq" commands.
340func Test_confirm_q_wq()
341 CheckNotGui
342 CheckRunVimInTerminal
343
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100344 call writefile(['foo'], 'Xfoo', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200345
346 let lines =<< trim END
347 set hidden nomore
348 call setline(1, 'abc')
349 edit Xfoo
350 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100351 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200352 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
353 call term_sendkeys(buf, ":confirm q\n")
354 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
355 \ term_getline(buf, 20))}, 1000)
356 call term_sendkeys(buf, 'C')
357 call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
358 \ term_getline(buf, 20))}, 1000)
359
360 call term_sendkeys(buf, ":edit Xfoo\n")
361 call term_sendkeys(buf, ":confirm wq\n")
362 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
363 \ term_getline(buf, 20))}, 1000)
364 call term_sendkeys(buf, 'C')
365 call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
366 \ term_getline(buf, 20))}, 1000)
Bram Moolenaar27321db2020-07-06 21:24:57 +0200367
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100368 call StopVimInTerminal(buf)
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100369endfunc
370
Dominique Pelle2bf60342021-05-02 20:16:24 +0200371func Test_confirm_write_ro()
372 CheckNotGui
373 CheckRunVimInTerminal
374
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100375 call writefile(['foo'], 'Xconfirm_write_ro', 'D')
Dominique Pelle2bf60342021-05-02 20:16:24 +0200376 let lines =<< trim END
377 set nobackup ff=unix cmdheight=2
378 edit Xconfirm_write_ro
379 norm Abar
380 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100381 call writefile(lines, 'Xscript', 'D')
Dominique Pelle2bf60342021-05-02 20:16:24 +0200382 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
383
384 " Try to write with 'ro' option.
385 call term_sendkeys(buf, ":set ro | confirm w\n")
386 call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
387 \ term_getline(buf, 18))}, 1000)
388 call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
389 \ term_getline(buf, 19))}, 1000)
390 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
391 call term_sendkeys(buf, 'N')
392 call WaitForAssert({-> assert_match('^ *$', term_getline(buf, 19))}, 1000)
393 call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
394 call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
395
396 call term_sendkeys(buf, ":confirm w\n")
397 call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
398 \ term_getline(buf, 18))}, 1000)
399 call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
400 \ term_getline(buf, 19))}, 1000)
401 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
402 call term_sendkeys(buf, 'Y')
403 call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 7B written$',
404 \ term_getline(buf, 19))}, 1000)
405 call assert_equal(['foobar'], readfile('Xconfirm_write_ro'))
406
407 " Try to write with read-only file permissions.
408 call setfperm('Xconfirm_write_ro', 'r--r--r--')
409 call term_sendkeys(buf, ":set noro | undo | confirm w\n")
410 call WaitForAssert({-> assert_match("^File permissions of \"Xconfirm_write_ro\" are read-only\. *$",
411 \ term_getline(buf, 17))}, 1000)
412 call WaitForAssert({-> assert_match('^It may still be possible to write it\. *$',
413 \ term_getline(buf, 18))}, 1000)
414 call WaitForAssert({-> assert_match('^Do you wish to try? *$', term_getline(buf, 19))}, 1000)
415 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
416 call term_sendkeys(buf, 'Y')
417 call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 4B written$',
418 \ term_getline(buf, 19))}, 1000)
419 call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
420
421 call StopVimInTerminal(buf)
Dominique Pelle2bf60342021-05-02 20:16:24 +0200422endfunc
423
Dominique Pellebd9e7962021-08-09 21:04:44 +0200424func Test_confirm_write_partial_file()
425 CheckNotGui
426 CheckRunVimInTerminal
427
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100428 call writefile(['a', 'b', 'c', 'd'], 'Xwrite_partial', 'D')
Dominique Pellebd9e7962021-08-09 21:04:44 +0200429 call writefile(['set nobackup ff=unix cmdheight=2',
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100430 \ 'edit Xwrite_partial'], 'Xscript', 'D')
Dominique Pellebd9e7962021-08-09 21:04:44 +0200431 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
432
433 call term_sendkeys(buf, ":confirm 2,3w\n")
434 call WaitForAssert({-> assert_match('^Write partial file? *$',
435 \ term_getline(buf, 19))}, 1000)
436 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
437 \ term_getline(buf, 20))}, 1000)
438 call term_sendkeys(buf, 'N')
439 call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
440 call assert_equal(['a', 'b', 'c', 'd'], readfile('Xwrite_partial'))
441 call delete('Xwrite_partial')
442
443 call term_sendkeys(buf, ":confirm 2,3w\n")
444 call WaitForAssert({-> assert_match('^Write partial file? *$',
445 \ term_getline(buf, 19))}, 1000)
446 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
447 \ term_getline(buf, 20))}, 1000)
448 call term_sendkeys(buf, 'Y')
449 call WaitForAssert({-> assert_match('^"Xwrite_partial" \[New\] 2L, 4B written *$',
450 \ term_getline(buf, 19))}, 1000)
451 call WaitForAssert({-> assert_match('^Press ENTER or type command to continue *$',
452 \ term_getline(buf, 20))}, 1000)
453 call assert_equal(['b', 'c'], readfile('Xwrite_partial'))
454
455 call StopVimInTerminal(buf)
Dominique Pellebd9e7962021-08-09 21:04:44 +0200456endfunc
457
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100458" Test for the :print command
459func Test_print_cmd()
460 call assert_fails('print', 'E749:')
461endfunc
462
463" Test for the :winsize command
464func Test_winsize_cmd()
465 call assert_fails('winsize 1', 'E465:')
Bram Moolenaarf5a51162021-02-06 12:58:18 +0100466 call assert_fails('winsize 1 x', 'E465:')
467 call assert_fails('win_getid(1)', 'E475: Invalid argument: _getid(1)')
468 " Actually changing the window size would be flaky.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100469endfunc
470
471" Test for the :redir command
Bram Moolenaarf9a65502021-03-05 20:47:44 +0100472" NOTE: if you run tests as root this will fail. Don't run tests as root!
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100473func Test_redir_cmd()
474 call assert_fails('redir @@', 'E475:')
475 call assert_fails('redir abc', 'E475:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100476 call assert_fails('redir => 1abc', 'E474:')
477 call assert_fails('redir => a b', 'E488:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200478 call assert_fails('redir => abc[1]', 'E121:')
479 let b = 0zFF
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100480 call assert_fails('redir =>> b', 'E734:')
481 unlet b
482
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100483 if has('unix')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100484 " Redirecting to a directory name
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100485 call mkdir('Xredir')
486 call assert_fails('redir > Xredir', 'E17:')
487 call delete('Xredir', 'd')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100488 endif
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100489
490 " Test for redirecting to a register
491 redir @q> | echon 'clean ' | redir END
492 redir @q>> | echon 'water' | redir END
493 call assert_equal('clean water', @q)
494
495 " Test for redirecting to a variable
496 redir => color | echon 'blue ' | redir END
497 redir =>> color | echon 'sky' | redir END
498 call assert_equal('blue sky', color)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100499endfunc
500
Bram Moolenaar17709e22021-03-19 14:38:12 +0100501func Test_redir_cmd_readonly()
502 CheckNotRoot
Bram Moolenaar17709e22021-03-19 14:38:12 +0100503
504 " Redirecting to a read-only file
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100505 call writefile([], 'Xredirfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100506 call setfperm('Xredirfile', 'r--r--r--')
507 call assert_fails('redir! > Xredirfile', 'E190:')
Bram Moolenaar17709e22021-03-19 14:38:12 +0100508endfunc
509
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100510" Test for the :filetype command
511func Test_filetype_cmd()
512 call assert_fails('filetype abc', 'E475:')
513endfunc
514
515" Test for the :mode command
516func Test_mode_cmd()
517 call assert_fails('mode abc', 'E359:')
518endfunc
519
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100520" Test for the :sleep command
521func Test_sleep_cmd()
522 call assert_fails('sleep x', 'E475:')
523endfunc
524
525" Test for the :read command
526func Test_read_cmd()
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100527 call writefile(['one'], 'Xcmdfile', 'D')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100528 new
529 call assert_fails('read', 'E32:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100530 edit Xcmdfile
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100531 read
532 call assert_equal(['one', 'one'], getline(1, '$'))
533 close!
534 new
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100535 read Xcmdfile
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100536 call assert_equal(['', 'one'], getline(1, '$'))
537 call deletebufline('', 1, '$')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100538 call feedkeys("Qr Xcmdfile\<CR>visual\<CR>", 'xt')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100539 call assert_equal(['one'], getline(1, '$'))
540 close!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100541endfunc
542
543" Test for running Ex commands when text is locked.
544" <C-\>e in the command line is used to lock the text
545func Test_run_excmd_with_text_locked()
546 " :quit
547 let cmd = ":\<C-\>eexecute('quit')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200548 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100549
550 " :qall
551 let cmd = ":\<C-\>eexecute('qall')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200552 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100553
554 " :exit
555 let cmd = ":\<C-\>eexecute('exit')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200556 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100557
558 " :close - should be ignored
559 new
560 let cmd = ":\<C-\>eexecute('close')\<CR>\<C-C>"
561 call assert_equal(2, winnr('$'))
562 close
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100563
Bram Moolenaarff06f282020-04-21 22:01:14 +0200564 call assert_fails("call feedkeys(\":\<C-R>=execute('bnext')\<CR>\", 'xt')", 'E565:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200565
566 " :tabfirst
567 tabnew
568 call assert_fails("call feedkeys(\":\<C-R>=execute('tabfirst')\<CR>\", 'xt')", 'E565:')
569 tabclose
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100570endfunc
571
572" Test for the :verbose command
573func Test_verbose_cmd()
Bram Moolenaar60895f32022-04-12 14:23:19 +0100574 set verbose=3
575 call assert_match(' verbose=1\n\s*Last set from ', execute('verbose set vbs'), "\n")
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100576 call assert_equal([' verbose=0'], split(execute('0verbose set vbs'), "\n"))
Bram Moolenaar60895f32022-04-12 14:23:19 +0100577 set verbose=0
578 call assert_match(' verbose=4\n\s*Last set from .*\n verbose=0',
579 \ execute("4verbose set verbose | set verbose"))
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100580endfunc
581
582" Test for the :delete command and the related abbreviated commands
583func Test_excmd_delete()
584 new
585 call setline(1, ['foo', "\tbar"])
586 call assert_equal(['^Ibar$'], split(execute('dl'), "\n"))
587 call setline(1, ['foo', "\tbar"])
588 call assert_equal(['^Ibar$'], split(execute('dell'), "\n"))
589 call setline(1, ['foo', "\tbar"])
590 call assert_equal(['^Ibar$'], split(execute('delel'), "\n"))
591 call setline(1, ['foo', "\tbar"])
592 call assert_equal(['^Ibar$'], split(execute('deletl'), "\n"))
593 call setline(1, ['foo', "\tbar"])
594 call assert_equal(['^Ibar$'], split(execute('deletel'), "\n"))
595 call setline(1, ['foo', "\tbar"])
596 call assert_equal([' bar'], split(execute('dp'), "\n"))
597 call setline(1, ['foo', "\tbar"])
598 call assert_equal([' bar'], split(execute('dep'), "\n"))
599 call setline(1, ['foo', "\tbar"])
600 call assert_equal([' bar'], split(execute('delp'), "\n"))
601 call setline(1, ['foo', "\tbar"])
602 call assert_equal([' bar'], split(execute('delep'), "\n"))
603 call setline(1, ['foo', "\tbar"])
604 call assert_equal([' bar'], split(execute('deletp'), "\n"))
605 call setline(1, ['foo', "\tbar"])
606 call assert_equal([' bar'], split(execute('deletep'), "\n"))
607 close!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100608endfunc
609
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200610" Test for commands that are blocked in a sandbox
611func Sandbox_tests()
612 call assert_fails("call histadd(':', 'ls')", 'E48:')
613 call assert_fails("call mkdir('Xdir')", 'E48:')
614 call assert_fails("call rename('a', 'b')", 'E48:')
615 call assert_fails("call setbufvar(1, 'myvar', 1)", 'E48:')
616 call assert_fails("call settabvar(1, 'myvar', 1)", 'E48:')
617 call assert_fails("call settabwinvar(1, 1, 'myvar', 1)", 'E48:')
618 call assert_fails("call setwinvar(1, 'myvar', 1)", 'E48:')
619 call assert_fails("call timer_start(100, '')", 'E48:')
620 if has('channel')
621 call assert_fails("call prompt_setcallback(1, '')", 'E48:')
622 call assert_fails("call prompt_setinterrupt(1, '')", 'E48:')
623 call assert_fails("call prompt_setprompt(1, '')", 'E48:')
624 endif
625 call assert_fails("let $TESTVAR=1", 'E48:')
626 call assert_fails("call feedkeys('ivim')", 'E48:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100627 call assert_fails("source! Xsomefile", 'E48:')
628 call assert_fails("call delete('Xthatfile')", 'E48:')
629 call assert_fails("call writefile([], 'Xanotherfile')", 'E48:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200630 call assert_fails('!ls', 'E48:')
631 call assert_fails('shell', 'E48:')
632 call assert_fails('stop', 'E48:')
633 call assert_fails('exe "normal \<C-Z>"', 'E48:')
634 set insertmode
635 call assert_fails('call feedkeys("\<C-Z>", "xt")', 'E48:')
636 set insertmode&
637 call assert_fails('suspend', 'E48:')
638 call assert_fails('call system("ls")', 'E48:')
639 call assert_fails('call systemlist("ls")', 'E48:')
640 if has('clientserver')
641 call assert_fails('let s=remote_expr("gvim", "2+2")', 'E48:')
642 if !has('win32')
Dominique Pelle923dce22021-11-21 11:36:04 +0000643 " remote_foreground() doesn't throw an error message on MS-Windows
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200644 call assert_fails('call remote_foreground("gvim")', 'E48:')
645 endif
646 call assert_fails('let s=remote_peek("gvim")', 'E48:')
647 call assert_fails('let s=remote_read("gvim")', 'E48:')
648 call assert_fails('let s=remote_send("gvim", "abc")', 'E48:')
649 call assert_fails('let s=server2client("gvim", "abc")', 'E48:')
650 endif
651 if has('terminal')
652 call assert_fails('terminal', 'E48:')
653 call assert_fails('call term_start("vim")', 'E48:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100654 call assert_fails('call term_dumpwrite(1, "Xdumpfile")', 'E48:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200655 endif
656 if has('channel')
657 call assert_fails("call ch_logfile('chlog')", 'E48:')
658 call assert_fails("call ch_open('localhost:8765')", 'E48:')
659 endif
660 if has('job')
661 call assert_fails("call job_start('vim')", 'E48:')
662 endif
663 if has('unix') && has('libcall')
664 call assert_fails("echo libcall('libc.so', 'getenv', 'HOME')", 'E48:')
665 endif
666 if has('unix')
667 call assert_fails('cd `pwd`', 'E48:')
668 endif
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200669 " some options cannot be changed in a sandbox
670 call assert_fails('set exrc', 'E48:')
671 call assert_fails('set cdpath', 'E48:')
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200672 if has('xim') && has('gui_gtk')
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200673 call assert_fails('set imstyle', 'E48:')
674 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200675endfunc
676
677func Test_sandbox()
678 sandbox call Sandbox_tests()
679endfunc
680
Dominique Pelle6d37e8e2021-05-06 17:36:55 +0200681func Test_command_not_implemented_E319()
682 if !has('mzscheme')
683 call assert_fails('mzscheme', 'E319:')
684 endif
685endfunc
686
kuuote08d7b1c2021-10-04 22:17:36 +0100687func Test_not_break_expression_register()
688 call setreg('=', '1+1')
689 if 0
690 put =1
691 endif
692 call assert_equal('1+1', getreg('=', 1))
693endfunc
694
Bram Moolenaar03725c52021-11-24 12:17:53 +0000695func Test_address_line_overflow()
696 if v:sizeoflong < 8
697 throw 'Skipped: only works with 64 bit long ints'
698 endif
699 new
Bram Moolenaar3cf21b32022-01-11 19:34:16 +0000700 call setline(1, range(100))
Bram Moolenaar03725c52021-11-24 12:17:53 +0000701 call assert_fails('|.44444444444444444444444', 'E1247:')
702 call assert_fails('|.9223372036854775806', 'E1247:')
zeertzjqd1b5ea92024-07-13 19:04:10 +0200703 call assert_fails('.44444444444444444444444d', 'E1247:')
704 call assert_equal(range(100)->map('string(v:val)'), getline(1, '$'))
Bram Moolenaar3cf21b32022-01-11 19:34:16 +0000705
706 $
707 yank 77777777777777777777
708 call assert_equal("99\n", @")
709
Bram Moolenaar03725c52021-11-24 12:17:53 +0000710 bwipe!
711endfunc
712
Bram Moolenaar4d97a562022-05-28 14:25:35 +0100713" This was leaving the cursor in line zero
714func Test_using_zero_in_range()
715 new
716 norm o00
717 silent! 0;s/\%')
718 bwipe!
719endfunc
720
ii141f0dc5e2022-07-26 19:44:56 +0100721" Test :write after changing name with :file and loading it with :edit
722func Test_write_after_rename()
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100723 call writefile(['text'], 'Xafterfile', 'D')
ii141f0dc5e2022-07-26 19:44:56 +0100724
725 enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100726 file Xafterfile
ii141f0dc5e2022-07-26 19:44:56 +0100727 call assert_fails('write', 'E13: File exists (add ! to override)')
728
729 " works OK after ":edit"
730 edit
731 write
732
ii141f0dc5e2022-07-26 19:44:56 +0100733 bwipe!
734endfunc
735
Christian Brabandt060623e2023-11-14 21:33:29 +0100736" catch address lines overflow
737func Test_ex_address_range_overflow()
738 call assert_fails(':--+foobar', 'E492:')
739endfunc
Bram Moolenaar03725c52021-11-24 12:17:53 +0000740
Christian Brabandt97817882024-03-20 20:19:47 +0100741func Test_drop_modified_file()
742 CheckScreendump
743 let lines =<< trim END
744 call setline(1, 'The quick brown fox jumped over the lazy dogs')
745 END
746 call writefile([''], 'Xdrop_modified.txt', 'D')
747 call writefile(lines, 'Xtest_drop_modified', 'D')
748 let buf = RunVimInTerminal('-S Xtest_drop_modified Xdrop_modified.txt', {'rows': 10,'columns': 40})
749 call term_sendkeys(buf, ":drop Xdrop_modified.txt\<CR>")
750 call VerifyScreenDump(buf, 'Test_drop_modified_1', {})
751
752 " clean up
753 call StopVimInTerminal(buf)
754endfunc
755
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100756" vim: shiftwidth=2 sts=2 expandtab