blob: 6a7829b78584f106480f0c24c9822d26c3f38374 [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
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01006
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02007func Test_ex_delete()
8 new
9 call setline(1, ['a', 'b', 'c'])
10 2
11 " :dl is :delete with the "l" flag, not :dlist
12 .dl
13 call assert_equal(['a', 'c'], getline(1, 2))
14endfunc
Bram Moolenaar0acae7a2019-08-06 21:29:29 +020015
16func Test_range_error()
17 call assert_fails(':.echo 1', 'E481:')
18 call assert_fails(':$echo 1', 'E481:')
19 call assert_fails(':1,2echo 1', 'E481:')
20 call assert_fails(':+1echo 1', 'E481:')
21 call assert_fails(':/1/echo 1', 'E481:')
22 call assert_fails(':\/echo 1', 'E481:')
23 normal vv
24 call assert_fails(":'<,'>echo 1", 'E481:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010025 call assert_fails(":\\xcenter", 'E10:')
Bram Moolenaar0acae7a2019-08-06 21:29:29 +020026endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +010027
28func Test_buffers_lastused()
29 call test_settime(localtime() - 2000) " middle
30 edit bufa
31 enew
32 call test_settime(localtime() - 10) " newest
33 edit bufb
34 enew
35 call test_settime(1550010000) " oldest
36 edit bufc
37 enew
38 call test_settime(0)
39 enew
40
41 let ls = split(execute('buffers t', 'silent!'), '\n')
42 let bufs = ls->map({i,v->split(v, '"\s*')[1:2]})
43 call assert_equal(['bufb', 'bufa', 'bufc'], bufs[1:]->map({i,v->v[0]}))
44 call assert_match('1[0-3] seconds ago', bufs[1][1])
45 call assert_match('\d\d:\d\d:\d\d', bufs[2][1])
46 call assert_match('2019/02/1\d \d\d:\d\d:00', bufs[3][1])
47
48 bwipeout bufa
49 bwipeout bufb
50 bwipeout bufc
51endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010052
53" Test for the :copy command
54func Test_copy()
55 new
56
57 call setline(1, ['L1', 'L2', 'L3', 'L4'])
58 " copy lines in a range to inside the range
59 1,3copy 2
60 call assert_equal(['L1', 'L2', 'L1', 'L2', 'L3', 'L3', 'L4'], getline(1, 7))
61
Bram Moolenaar1671f442020-03-10 07:48:13 +010062 " Specifying a count before using : to run an ex-command
63 exe "normal! gg4:yank\<CR>"
64 call assert_equal("L1\nL2\nL1\nL2\n", @")
65
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010066 close!
67endfunc
68
69" Test for the :file command
70func Test_file_cmd()
71 call assert_fails('3file', 'E474:')
72 call assert_fails('0,0file', 'E474:')
73 call assert_fails('0file abc', 'E474:')
Yegappan Lakshmanan59b26232021-06-05 20:59:22 +020074 if !has('win32')
75 " Change the name of the buffer to the same name
76 new Xfile1
77 file Xfile1
78 call assert_equal('Xfile1', @%)
79 call assert_equal('Xfile1', @#)
80 bw!
81 endif
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010082endfunc
83
84" Test for the :drop command
85func Test_drop_cmd()
Bram Moolenaar5c645a22022-09-21 22:00:03 +010086 call writefile(['L1', 'L2'], 'Xdropfile', 'D')
Rocco Maof96dc8d2024-01-23 21:27:19 +010087 " Test for reusing the current buffer
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010088 enew | only
Rocco Maof96dc8d2024-01-23 21:27:19 +010089 let expected_nr = bufnr()
Bram Moolenaarb18b4962022-09-02 21:55:50 +010090 drop Xdropfile
Rocco Maof96dc8d2024-01-23 21:27:19 +010091 call assert_equal(expected_nr, bufnr())
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010092 call assert_equal('L2', getline(2))
93 " Test for switching to an existing window
94 below new
Bram Moolenaarb18b4962022-09-02 21:55:50 +010095 drop Xdropfile
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010096 call assert_equal(1, winnr())
Rocco Maof96dc8d2024-01-23 21:27:19 +010097 " Test for splitting the current window (set nohidden)
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010098 enew | only
99 set modified
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100100 drop Xdropfile
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100101 call assert_equal(2, winnr('$'))
Rocco Maof96dc8d2024-01-23 21:27:19 +0100102 " Not splitting the current window even if modified (set hidden)
103 set hidden
104 enew | only
105 set modified
106 drop Xdropfile
107 call assert_equal(1, winnr('$'))
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100108 " Check for setting the argument list
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100109 call assert_equal(['Xdropfile'], argv())
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100110 enew | only!
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100111endfunc
112
113" Test for the :append command
114func Test_append_cmd()
115 new
116 call setline(1, [' L1'])
117 call feedkeys(":append\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
118 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
119 %delete _
120 " append after a specific line
121 call setline(1, [' L1', ' L2', ' L3'])
122 call feedkeys(":2append\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
123 call assert_equal([' L1', ' L2', ' L4', ' L5', ' L3'], getline(1, '$'))
124 %delete _
125 " append with toggling 'autoindent'
126 call setline(1, [' L1'])
127 call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
128 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
129 call assert_false(&autoindent)
130 %delete _
131 " append with 'autoindent' set and toggling 'autoindent'
132 set autoindent
133 call setline(1, [' L1'])
134 call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
135 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
136 call assert_true(&autoindent)
137 set autoindent&
138 close!
139endfunc
140
zeertzjq1fa3de12021-12-31 12:19:22 +0000141func Test_append_cmd_empty_buf()
142 CheckRunVimInTerminal
143 let lines =<< trim END
144 func Timer(timer)
145 append
146 aaaaa
147 bbbbb
148 .
149 endfunc
150 call timer_start(10, 'Timer')
151 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100152 call writefile(lines, 'Xtest_append_cmd_empty_buf', 'D')
zeertzjq1fa3de12021-12-31 12:19:22 +0000153 let buf = RunVimInTerminal('-S Xtest_append_cmd_empty_buf', {'rows': 6})
154 call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
155 call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
156
157 " clean up
158 call StopVimInTerminal(buf)
zeertzjq1fa3de12021-12-31 12:19:22 +0000159endfunc
160
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100161" Test for the :insert command
162func Test_insert_cmd()
163 new
164 call setline(1, [' L1'])
165 call feedkeys(":insert\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
166 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
167 %delete _
168 " insert before a specific line
169 call setline(1, [' L1', ' L2', ' L3'])
170 call feedkeys(":2insert\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
171 call assert_equal([' L1', ' L4', ' L5', ' L2', ' L3'], getline(1, '$'))
172 %delete _
173 " insert with toggling 'autoindent'
174 call setline(1, [' L1'])
175 call feedkeys(":insert!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
176 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
177 call assert_false(&autoindent)
178 %delete _
179 " insert with 'autoindent' set and toggling 'autoindent'
180 set autoindent
181 call setline(1, [' L1'])
182 call feedkeys(":insert!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
183 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
184 call assert_true(&autoindent)
185 set autoindent&
186 close!
187endfunc
188
zeertzjq1fa3de12021-12-31 12:19:22 +0000189func Test_insert_cmd_empty_buf()
190 CheckRunVimInTerminal
191 let lines =<< trim END
192 func Timer(timer)
193 insert
194 aaaaa
195 bbbbb
196 .
197 endfunc
198 call timer_start(10, 'Timer')
199 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100200 call writefile(lines, 'Xtest_insert_cmd_empty_buf', 'D')
zeertzjq1fa3de12021-12-31 12:19:22 +0000201 let buf = RunVimInTerminal('-S Xtest_insert_cmd_empty_buf', {'rows': 6})
202 call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
203 call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
204
205 " clean up
206 call StopVimInTerminal(buf)
zeertzjq1fa3de12021-12-31 12:19:22 +0000207endfunc
208
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100209" Test for the :change command
210func Test_change_cmd()
211 new
212 call setline(1, [' L1', 'L2', 'L3'])
213 call feedkeys(":change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
214 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
215 %delete _
216 " change a specific line
217 call setline(1, [' L1', ' L2', ' L3'])
218 call feedkeys(":2change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
219 call assert_equal([' L1', ' L4', ' L5', ' L3'], getline(1, '$'))
220 %delete _
221 " change with toggling 'autoindent'
222 call setline(1, [' L1', 'L2', 'L3'])
223 call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
224 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
225 call assert_false(&autoindent)
226 %delete _
227 " change with 'autoindent' set and toggling 'autoindent'
228 set autoindent
229 call setline(1, [' L1', 'L2', 'L3'])
230 call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
231 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
232 call assert_true(&autoindent)
233 set autoindent&
234 close!
235endfunc
236
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100237" Test for the :language command
238func Test_language_cmd()
239 CheckFeature multi_lang
240
241 call assert_fails('language ctype non_existing_lang', 'E197:')
242 call assert_fails('language time non_existing_lang', 'E197:')
243endfunc
244
245" Test for the :confirm command dialog
246func Test_confirm_cmd()
247 CheckNotGui
248 CheckRunVimInTerminal
249
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100250 call writefile(['foo1'], 'Xfoo', 'D')
251 call writefile(['bar1'], 'Xbar', 'D')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100252
253 " Test for saving all the modified buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200254 let lines =<< trim END
255 set nomore
256 new Xfoo
257 call setline(1, 'foo2')
258 new Xbar
259 call setline(1, 'bar2')
260 wincmd b
261 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100262 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200263 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100264 call term_sendkeys(buf, ":confirm qall\n")
265 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
266 call term_sendkeys(buf, "A")
267 call StopVimInTerminal(buf)
268
Bram Moolenaar27321db2020-07-06 21:24:57 +0200269 call assert_equal(['foo2'], readfile('Xfoo'))
270 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100271
272 " Test for discarding all the changes to modified buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200273 let lines =<< trim END
274 set nomore
275 new Xfoo
276 call setline(1, 'foo3')
277 new Xbar
278 call setline(1, 'bar3')
279 wincmd b
280 END
281 call writefile(lines, 'Xscript')
282 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100283 call term_sendkeys(buf, ":confirm qall\n")
284 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
285 call term_sendkeys(buf, "D")
286 call StopVimInTerminal(buf)
287
Bram Moolenaar27321db2020-07-06 21:24:57 +0200288 call assert_equal(['foo2'], readfile('Xfoo'))
289 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100290
291 " Test for saving and discarding changes to some buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200292 let lines =<< trim END
293 set nomore
294 new Xfoo
295 call setline(1, 'foo4')
296 new Xbar
297 call setline(1, 'bar4')
298 wincmd b
299 END
300 call writefile(lines, 'Xscript')
301 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100302 call term_sendkeys(buf, ":confirm qall\n")
303 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
304 call term_sendkeys(buf, "N")
305 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, (C)ancel: ', term_getline(buf, 20))}, 1000)
306 call term_sendkeys(buf, "Y")
307 call StopVimInTerminal(buf)
308
Bram Moolenaar27321db2020-07-06 21:24:57 +0200309 call assert_equal(['foo4'], readfile('Xfoo'))
310 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaar72749f02020-03-26 20:51:43 +0100311endfunc
312
313func Test_confirm_cmd_cancel()
Bram Moolenaarbea90232020-03-26 22:09:52 +0100314 CheckNotGui
315 CheckRunVimInTerminal
316
Bram Moolenaar406cd902020-02-18 21:54:41 +0100317 " Test for closing a window with a modified buffer
Bram Moolenaar27321db2020-07-06 21:24:57 +0200318 let lines =<< trim END
319 set nomore
320 new
321 call setline(1, 'abc')
322 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100323 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200324 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaar406cd902020-02-18 21:54:41 +0100325 call term_sendkeys(buf, ":confirm close\n")
326 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
327 \ term_getline(buf, 20))}, 1000)
328 call term_sendkeys(buf, "C")
Bram Moolenaar7b1b36b2020-03-28 21:48:55 +0100329 call WaitForAssert({-> assert_equal('', term_getline(buf, 20))}, 1000)
Bram Moolenaar406cd902020-02-18 21:54:41 +0100330 call term_sendkeys(buf, ":confirm close\n")
331 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
332 \ term_getline(buf, 20))}, 1000)
333 call term_sendkeys(buf, "N")
Bram Moolenaar9207d1f2020-03-27 19:41:02 +0100334 call WaitForAssert({-> assert_match('^ *0,0-1 All$',
335 \ term_getline(buf, 20))}, 1000)
Bram Moolenaar406cd902020-02-18 21:54:41 +0100336 call StopVimInTerminal(buf)
Bram Moolenaar27321db2020-07-06 21:24:57 +0200337endfunc
338
339" The ":confirm" prompt was sometimes used with the terminal in cooked mode.
340" This test verifies that a "\<CR>" character is NOT required to respond to a
341" prompt from the ":conf q" and ":conf wq" commands.
342func Test_confirm_q_wq()
343 CheckNotGui
344 CheckRunVimInTerminal
345
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100346 call writefile(['foo'], 'Xfoo', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200347
348 let lines =<< trim END
349 set hidden nomore
350 call setline(1, 'abc')
351 edit Xfoo
352 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100353 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200354 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
355 call term_sendkeys(buf, ":confirm q\n")
356 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
357 \ term_getline(buf, 20))}, 1000)
358 call term_sendkeys(buf, 'C')
359 call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
360 \ term_getline(buf, 20))}, 1000)
361
362 call term_sendkeys(buf, ":edit Xfoo\n")
363 call term_sendkeys(buf, ":confirm wq\n")
364 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
365 \ term_getline(buf, 20))}, 1000)
366 call term_sendkeys(buf, 'C')
367 call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
368 \ term_getline(buf, 20))}, 1000)
Bram Moolenaar27321db2020-07-06 21:24:57 +0200369
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100370 call StopVimInTerminal(buf)
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100371endfunc
372
Dominique Pelle2bf60342021-05-02 20:16:24 +0200373func Test_confirm_write_ro()
374 CheckNotGui
375 CheckRunVimInTerminal
376
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100377 call writefile(['foo'], 'Xconfirm_write_ro', 'D')
Dominique Pelle2bf60342021-05-02 20:16:24 +0200378 let lines =<< trim END
379 set nobackup ff=unix cmdheight=2
380 edit Xconfirm_write_ro
381 norm Abar
382 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100383 call writefile(lines, 'Xscript', 'D')
Dominique Pelle2bf60342021-05-02 20:16:24 +0200384 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
385
386 " Try to write with 'ro' option.
387 call term_sendkeys(buf, ":set ro | confirm w\n")
388 call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
389 \ term_getline(buf, 18))}, 1000)
390 call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
391 \ term_getline(buf, 19))}, 1000)
392 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
393 call term_sendkeys(buf, 'N')
394 call WaitForAssert({-> assert_match('^ *$', term_getline(buf, 19))}, 1000)
395 call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
396 call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
397
398 call term_sendkeys(buf, ":confirm w\n")
399 call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
400 \ term_getline(buf, 18))}, 1000)
401 call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
402 \ term_getline(buf, 19))}, 1000)
403 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
404 call term_sendkeys(buf, 'Y')
405 call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 7B written$',
406 \ term_getline(buf, 19))}, 1000)
407 call assert_equal(['foobar'], readfile('Xconfirm_write_ro'))
408
409 " Try to write with read-only file permissions.
410 call setfperm('Xconfirm_write_ro', 'r--r--r--')
411 call term_sendkeys(buf, ":set noro | undo | confirm w\n")
412 call WaitForAssert({-> assert_match("^File permissions of \"Xconfirm_write_ro\" are read-only\. *$",
413 \ term_getline(buf, 17))}, 1000)
414 call WaitForAssert({-> assert_match('^It may still be possible to write it\. *$',
415 \ term_getline(buf, 18))}, 1000)
416 call WaitForAssert({-> assert_match('^Do you wish to try? *$', term_getline(buf, 19))}, 1000)
417 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
418 call term_sendkeys(buf, 'Y')
419 call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 4B written$',
420 \ term_getline(buf, 19))}, 1000)
421 call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
422
423 call StopVimInTerminal(buf)
Dominique Pelle2bf60342021-05-02 20:16:24 +0200424endfunc
425
Dominique Pellebd9e7962021-08-09 21:04:44 +0200426func Test_confirm_write_partial_file()
427 CheckNotGui
428 CheckRunVimInTerminal
429
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100430 call writefile(['a', 'b', 'c', 'd'], 'Xwrite_partial', 'D')
Dominique Pellebd9e7962021-08-09 21:04:44 +0200431 call writefile(['set nobackup ff=unix cmdheight=2',
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100432 \ 'edit Xwrite_partial'], 'Xscript', 'D')
Dominique Pellebd9e7962021-08-09 21:04:44 +0200433 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
434
435 call term_sendkeys(buf, ":confirm 2,3w\n")
436 call WaitForAssert({-> assert_match('^Write partial file? *$',
437 \ term_getline(buf, 19))}, 1000)
438 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
439 \ term_getline(buf, 20))}, 1000)
440 call term_sendkeys(buf, 'N')
441 call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
442 call assert_equal(['a', 'b', 'c', 'd'], readfile('Xwrite_partial'))
443 call delete('Xwrite_partial')
444
445 call term_sendkeys(buf, ":confirm 2,3w\n")
446 call WaitForAssert({-> assert_match('^Write partial file? *$',
447 \ term_getline(buf, 19))}, 1000)
448 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
449 \ term_getline(buf, 20))}, 1000)
450 call term_sendkeys(buf, 'Y')
451 call WaitForAssert({-> assert_match('^"Xwrite_partial" \[New\] 2L, 4B written *$',
452 \ term_getline(buf, 19))}, 1000)
453 call WaitForAssert({-> assert_match('^Press ENTER or type command to continue *$',
454 \ term_getline(buf, 20))}, 1000)
455 call assert_equal(['b', 'c'], readfile('Xwrite_partial'))
456
457 call StopVimInTerminal(buf)
Dominique Pellebd9e7962021-08-09 21:04:44 +0200458endfunc
459
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100460" Test for the :print command
461func Test_print_cmd()
462 call assert_fails('print', 'E749:')
463endfunc
464
465" Test for the :winsize command
466func Test_winsize_cmd()
467 call assert_fails('winsize 1', 'E465:')
Bram Moolenaarf5a51162021-02-06 12:58:18 +0100468 call assert_fails('winsize 1 x', 'E465:')
469 call assert_fails('win_getid(1)', 'E475: Invalid argument: _getid(1)')
470 " Actually changing the window size would be flaky.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100471endfunc
472
473" Test for the :redir command
Bram Moolenaarf9a65502021-03-05 20:47:44 +0100474" NOTE: if you run tests as root this will fail. Don't run tests as root!
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100475func Test_redir_cmd()
476 call assert_fails('redir @@', 'E475:')
477 call assert_fails('redir abc', 'E475:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100478 call assert_fails('redir => 1abc', 'E474:')
479 call assert_fails('redir => a b', 'E488:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200480 call assert_fails('redir => abc[1]', 'E121:')
481 let b = 0zFF
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100482 call assert_fails('redir =>> b', 'E734:')
483 unlet b
484
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100485 if has('unix')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100486 " Redirecting to a directory name
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100487 call mkdir('Xredir')
488 call assert_fails('redir > Xredir', 'E17:')
489 call delete('Xredir', 'd')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100490 endif
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100491
492 " Test for redirecting to a register
493 redir @q> | echon 'clean ' | redir END
494 redir @q>> | echon 'water' | redir END
495 call assert_equal('clean water', @q)
496
497 " Test for redirecting to a variable
498 redir => color | echon 'blue ' | redir END
499 redir =>> color | echon 'sky' | redir END
500 call assert_equal('blue sky', color)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100501endfunc
502
Bram Moolenaar17709e22021-03-19 14:38:12 +0100503func Test_redir_cmd_readonly()
504 CheckNotRoot
Bram Moolenaar17709e22021-03-19 14:38:12 +0100505
506 " Redirecting to a read-only file
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100507 call writefile([], 'Xredirfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100508 call setfperm('Xredirfile', 'r--r--r--')
509 call assert_fails('redir! > Xredirfile', 'E190:')
Bram Moolenaar17709e22021-03-19 14:38:12 +0100510endfunc
511
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100512" Test for the :filetype command
513func Test_filetype_cmd()
514 call assert_fails('filetype abc', 'E475:')
515endfunc
516
517" Test for the :mode command
518func Test_mode_cmd()
519 call assert_fails('mode abc', 'E359:')
520endfunc
521
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100522" Test for the :sleep command
523func Test_sleep_cmd()
524 call assert_fails('sleep x', 'E475:')
525endfunc
526
527" Test for the :read command
528func Test_read_cmd()
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100529 call writefile(['one'], 'Xcmdfile', 'D')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100530 new
531 call assert_fails('read', 'E32:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100532 edit Xcmdfile
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100533 read
534 call assert_equal(['one', 'one'], getline(1, '$'))
535 close!
536 new
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100537 read Xcmdfile
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100538 call assert_equal(['', 'one'], getline(1, '$'))
539 call deletebufline('', 1, '$')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100540 call feedkeys("Qr Xcmdfile\<CR>visual\<CR>", 'xt')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100541 call assert_equal(['one'], getline(1, '$'))
542 close!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100543endfunc
544
545" Test for running Ex commands when text is locked.
546" <C-\>e in the command line is used to lock the text
547func Test_run_excmd_with_text_locked()
548 " :quit
549 let cmd = ":\<C-\>eexecute('quit')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200550 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100551
552 " :qall
553 let cmd = ":\<C-\>eexecute('qall')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200554 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100555
556 " :exit
557 let cmd = ":\<C-\>eexecute('exit')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200558 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100559
560 " :close - should be ignored
561 new
562 let cmd = ":\<C-\>eexecute('close')\<CR>\<C-C>"
563 call assert_equal(2, winnr('$'))
564 close
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100565
Bram Moolenaarff06f282020-04-21 22:01:14 +0200566 call assert_fails("call feedkeys(\":\<C-R>=execute('bnext')\<CR>\", 'xt')", 'E565:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200567
568 " :tabfirst
569 tabnew
570 call assert_fails("call feedkeys(\":\<C-R>=execute('tabfirst')\<CR>\", 'xt')", 'E565:')
571 tabclose
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100572endfunc
573
574" Test for the :verbose command
575func Test_verbose_cmd()
Bram Moolenaar60895f32022-04-12 14:23:19 +0100576 set verbose=3
577 call assert_match(' verbose=1\n\s*Last set from ', execute('verbose set vbs'), "\n")
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100578 call assert_equal([' verbose=0'], split(execute('0verbose set vbs'), "\n"))
Bram Moolenaar60895f32022-04-12 14:23:19 +0100579 set verbose=0
580 call assert_match(' verbose=4\n\s*Last set from .*\n verbose=0',
581 \ execute("4verbose set verbose | set verbose"))
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100582endfunc
583
584" Test for the :delete command and the related abbreviated commands
585func Test_excmd_delete()
586 new
587 call setline(1, ['foo', "\tbar"])
588 call assert_equal(['^Ibar$'], split(execute('dl'), "\n"))
589 call setline(1, ['foo', "\tbar"])
590 call assert_equal(['^Ibar$'], split(execute('dell'), "\n"))
591 call setline(1, ['foo', "\tbar"])
592 call assert_equal(['^Ibar$'], split(execute('delel'), "\n"))
593 call setline(1, ['foo', "\tbar"])
594 call assert_equal(['^Ibar$'], split(execute('deletl'), "\n"))
595 call setline(1, ['foo', "\tbar"])
596 call assert_equal(['^Ibar$'], split(execute('deletel'), "\n"))
597 call setline(1, ['foo', "\tbar"])
598 call assert_equal([' bar'], split(execute('dp'), "\n"))
599 call setline(1, ['foo', "\tbar"])
600 call assert_equal([' bar'], split(execute('dep'), "\n"))
601 call setline(1, ['foo', "\tbar"])
602 call assert_equal([' bar'], split(execute('delp'), "\n"))
603 call setline(1, ['foo', "\tbar"])
604 call assert_equal([' bar'], split(execute('delep'), "\n"))
605 call setline(1, ['foo', "\tbar"])
606 call assert_equal([' bar'], split(execute('deletp'), "\n"))
607 call setline(1, ['foo', "\tbar"])
608 call assert_equal([' bar'], split(execute('deletep'), "\n"))
609 close!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100610endfunc
611
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200612" Test for commands that are blocked in a sandbox
613func Sandbox_tests()
614 call assert_fails("call histadd(':', 'ls')", 'E48:')
615 call assert_fails("call mkdir('Xdir')", 'E48:')
616 call assert_fails("call rename('a', 'b')", 'E48:')
617 call assert_fails("call setbufvar(1, 'myvar', 1)", 'E48:')
618 call assert_fails("call settabvar(1, 'myvar', 1)", 'E48:')
619 call assert_fails("call settabwinvar(1, 1, 'myvar', 1)", 'E48:')
620 call assert_fails("call setwinvar(1, 'myvar', 1)", 'E48:')
621 call assert_fails("call timer_start(100, '')", 'E48:')
622 if has('channel')
623 call assert_fails("call prompt_setcallback(1, '')", 'E48:')
624 call assert_fails("call prompt_setinterrupt(1, '')", 'E48:')
625 call assert_fails("call prompt_setprompt(1, '')", 'E48:')
626 endif
627 call assert_fails("let $TESTVAR=1", 'E48:')
628 call assert_fails("call feedkeys('ivim')", 'E48:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100629 call assert_fails("source! Xsomefile", 'E48:')
630 call assert_fails("call delete('Xthatfile')", 'E48:')
631 call assert_fails("call writefile([], 'Xanotherfile')", 'E48:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200632 call assert_fails('!ls', 'E48:')
633 call assert_fails('shell', 'E48:')
634 call assert_fails('stop', 'E48:')
635 call assert_fails('exe "normal \<C-Z>"', 'E48:')
636 set insertmode
637 call assert_fails('call feedkeys("\<C-Z>", "xt")', 'E48:')
638 set insertmode&
639 call assert_fails('suspend', 'E48:')
640 call assert_fails('call system("ls")', 'E48:')
641 call assert_fails('call systemlist("ls")', 'E48:')
642 if has('clientserver')
643 call assert_fails('let s=remote_expr("gvim", "2+2")', 'E48:')
644 if !has('win32')
Dominique Pelle923dce22021-11-21 11:36:04 +0000645 " remote_foreground() doesn't throw an error message on MS-Windows
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200646 call assert_fails('call remote_foreground("gvim")', 'E48:')
647 endif
648 call assert_fails('let s=remote_peek("gvim")', 'E48:')
649 call assert_fails('let s=remote_read("gvim")', 'E48:')
650 call assert_fails('let s=remote_send("gvim", "abc")', 'E48:')
651 call assert_fails('let s=server2client("gvim", "abc")', 'E48:')
652 endif
653 if has('terminal')
654 call assert_fails('terminal', 'E48:')
655 call assert_fails('call term_start("vim")', 'E48:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100656 call assert_fails('call term_dumpwrite(1, "Xdumpfile")', 'E48:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200657 endif
658 if has('channel')
659 call assert_fails("call ch_logfile('chlog')", 'E48:')
660 call assert_fails("call ch_open('localhost:8765')", 'E48:')
661 endif
662 if has('job')
663 call assert_fails("call job_start('vim')", 'E48:')
664 endif
665 if has('unix') && has('libcall')
666 call assert_fails("echo libcall('libc.so', 'getenv', 'HOME')", 'E48:')
667 endif
668 if has('unix')
669 call assert_fails('cd `pwd`', 'E48:')
670 endif
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200671 " some options cannot be changed in a sandbox
672 call assert_fails('set exrc', 'E48:')
673 call assert_fails('set cdpath', 'E48:')
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200674 if has('xim') && has('gui_gtk')
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200675 call assert_fails('set imstyle', 'E48:')
676 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200677endfunc
678
679func Test_sandbox()
680 sandbox call Sandbox_tests()
681endfunc
682
Dominique Pelle6d37e8e2021-05-06 17:36:55 +0200683func Test_command_not_implemented_E319()
684 if !has('mzscheme')
685 call assert_fails('mzscheme', 'E319:')
686 endif
687endfunc
688
kuuote08d7b1c2021-10-04 22:17:36 +0100689func Test_not_break_expression_register()
690 call setreg('=', '1+1')
691 if 0
692 put =1
693 endif
694 call assert_equal('1+1', getreg('=', 1))
695endfunc
696
Bram Moolenaar03725c52021-11-24 12:17:53 +0000697func Test_address_line_overflow()
698 if v:sizeoflong < 8
699 throw 'Skipped: only works with 64 bit long ints'
700 endif
701 new
Bram Moolenaar3cf21b32022-01-11 19:34:16 +0000702 call setline(1, range(100))
Bram Moolenaar03725c52021-11-24 12:17:53 +0000703 call assert_fails('|.44444444444444444444444', 'E1247:')
704 call assert_fails('|.9223372036854775806', 'E1247:')
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
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100741" vim: shiftwidth=2 sts=2 expandtab