blob: 3637351f636c0214cbc40f679facbbaad5aceca9 [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')
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010087 enew | only
Bram Moolenaarb18b4962022-09-02 21:55:50 +010088 drop Xdropfile
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010089 call assert_equal('L2', getline(2))
90 " Test for switching to an existing window
91 below new
Bram Moolenaarb18b4962022-09-02 21:55:50 +010092 drop Xdropfile
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010093 call assert_equal(1, winnr())
94 " Test for splitting the current window
95 enew | only
96 set modified
Bram Moolenaarb18b4962022-09-02 21:55:50 +010097 drop Xdropfile
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010098 call assert_equal(2, winnr('$'))
99 " Check for setting the argument list
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100100 call assert_equal(['Xdropfile'], argv())
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100101 enew | only!
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100102endfunc
103
104" Test for the :append command
105func Test_append_cmd()
106 new
107 call setline(1, [' L1'])
108 call feedkeys(":append\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
109 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
110 %delete _
111 " append after a specific line
112 call setline(1, [' L1', ' L2', ' L3'])
113 call feedkeys(":2append\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
114 call assert_equal([' L1', ' L2', ' L4', ' L5', ' L3'], getline(1, '$'))
115 %delete _
116 " append with toggling 'autoindent'
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 call assert_false(&autoindent)
121 %delete _
122 " append with 'autoindent' set and toggling 'autoindent'
123 set 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_true(&autoindent)
128 set autoindent&
129 close!
130endfunc
131
zeertzjq1fa3de12021-12-31 12:19:22 +0000132func Test_append_cmd_empty_buf()
133 CheckRunVimInTerminal
134 let lines =<< trim END
135 func Timer(timer)
136 append
137 aaaaa
138 bbbbb
139 .
140 endfunc
141 call timer_start(10, 'Timer')
142 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100143 call writefile(lines, 'Xtest_append_cmd_empty_buf', 'D')
zeertzjq1fa3de12021-12-31 12:19:22 +0000144 let buf = RunVimInTerminal('-S Xtest_append_cmd_empty_buf', {'rows': 6})
145 call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
146 call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
147
148 " clean up
149 call StopVimInTerminal(buf)
zeertzjq1fa3de12021-12-31 12:19:22 +0000150endfunc
151
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100152" Test for the :insert command
153func Test_insert_cmd()
154 new
155 call setline(1, [' L1'])
156 call feedkeys(":insert\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
157 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
158 %delete _
159 " insert before a specific line
160 call setline(1, [' L1', ' L2', ' L3'])
161 call feedkeys(":2insert\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
162 call assert_equal([' L1', ' L4', ' L5', ' L2', ' L3'], getline(1, '$'))
163 %delete _
164 " insert with toggling 'autoindent'
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 call assert_false(&autoindent)
169 %delete _
170 " insert with 'autoindent' set and toggling 'autoindent'
171 set 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_true(&autoindent)
176 set autoindent&
177 close!
178endfunc
179
zeertzjq1fa3de12021-12-31 12:19:22 +0000180func Test_insert_cmd_empty_buf()
181 CheckRunVimInTerminal
182 let lines =<< trim END
183 func Timer(timer)
184 insert
185 aaaaa
186 bbbbb
187 .
188 endfunc
189 call timer_start(10, 'Timer')
190 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100191 call writefile(lines, 'Xtest_insert_cmd_empty_buf', 'D')
zeertzjq1fa3de12021-12-31 12:19:22 +0000192 let buf = RunVimInTerminal('-S Xtest_insert_cmd_empty_buf', {'rows': 6})
193 call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
194 call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
195
196 " clean up
197 call StopVimInTerminal(buf)
zeertzjq1fa3de12021-12-31 12:19:22 +0000198endfunc
199
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100200" Test for the :change command
201func Test_change_cmd()
202 new
203 call setline(1, [' L1', 'L2', 'L3'])
204 call feedkeys(":change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
205 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
206 %delete _
207 " change a specific line
208 call setline(1, [' L1', ' L2', ' L3'])
209 call feedkeys(":2change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
210 call assert_equal([' L1', ' L4', ' L5', ' L3'], getline(1, '$'))
211 %delete _
212 " change with toggling 'autoindent'
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 call assert_false(&autoindent)
217 %delete _
218 " change with 'autoindent' set and toggling 'autoindent'
219 set 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_true(&autoindent)
224 set autoindent&
225 close!
226endfunc
227
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100228" Test for the :language command
229func Test_language_cmd()
230 CheckFeature multi_lang
231
232 call assert_fails('language ctype non_existing_lang', 'E197:')
233 call assert_fails('language time non_existing_lang', 'E197:')
234endfunc
235
236" Test for the :confirm command dialog
237func Test_confirm_cmd()
238 CheckNotGui
239 CheckRunVimInTerminal
240
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100241 call writefile(['foo1'], 'Xfoo', 'D')
242 call writefile(['bar1'], 'Xbar', 'D')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100243
244 " Test for saving all the modified buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200245 let lines =<< trim END
246 set nomore
247 new Xfoo
248 call setline(1, 'foo2')
249 new Xbar
250 call setline(1, 'bar2')
251 wincmd b
252 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100253 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200254 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100255 call term_sendkeys(buf, ":confirm qall\n")
256 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
257 call term_sendkeys(buf, "A")
258 call StopVimInTerminal(buf)
259
Bram Moolenaar27321db2020-07-06 21:24:57 +0200260 call assert_equal(['foo2'], readfile('Xfoo'))
261 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100262
263 " Test for discarding all the changes to modified buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200264 let lines =<< trim END
265 set nomore
266 new Xfoo
267 call setline(1, 'foo3')
268 new Xbar
269 call setline(1, 'bar3')
270 wincmd b
271 END
272 call writefile(lines, 'Xscript')
273 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100274 call term_sendkeys(buf, ":confirm qall\n")
275 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
276 call term_sendkeys(buf, "D")
277 call StopVimInTerminal(buf)
278
Bram Moolenaar27321db2020-07-06 21:24:57 +0200279 call assert_equal(['foo2'], readfile('Xfoo'))
280 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100281
282 " Test for saving and discarding changes to some buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200283 let lines =<< trim END
284 set nomore
285 new Xfoo
286 call setline(1, 'foo4')
287 new Xbar
288 call setline(1, 'bar4')
289 wincmd b
290 END
291 call writefile(lines, 'Xscript')
292 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100293 call term_sendkeys(buf, ":confirm qall\n")
294 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
295 call term_sendkeys(buf, "N")
296 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, (C)ancel: ', term_getline(buf, 20))}, 1000)
297 call term_sendkeys(buf, "Y")
298 call StopVimInTerminal(buf)
299
Bram Moolenaar27321db2020-07-06 21:24:57 +0200300 call assert_equal(['foo4'], readfile('Xfoo'))
301 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaar72749f02020-03-26 20:51:43 +0100302endfunc
303
304func Test_confirm_cmd_cancel()
Bram Moolenaarbea90232020-03-26 22:09:52 +0100305 CheckNotGui
306 CheckRunVimInTerminal
307
Bram Moolenaar406cd902020-02-18 21:54:41 +0100308 " Test for closing a window with a modified buffer
Bram Moolenaar27321db2020-07-06 21:24:57 +0200309 let lines =<< trim END
310 set nomore
311 new
312 call setline(1, 'abc')
313 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100314 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200315 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaar406cd902020-02-18 21:54:41 +0100316 call term_sendkeys(buf, ":confirm close\n")
317 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
318 \ term_getline(buf, 20))}, 1000)
319 call term_sendkeys(buf, "C")
Bram Moolenaar7b1b36b2020-03-28 21:48:55 +0100320 call WaitForAssert({-> assert_equal('', term_getline(buf, 20))}, 1000)
Bram Moolenaar406cd902020-02-18 21:54:41 +0100321 call term_sendkeys(buf, ":confirm close\n")
322 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
323 \ term_getline(buf, 20))}, 1000)
324 call term_sendkeys(buf, "N")
Bram Moolenaar9207d1f2020-03-27 19:41:02 +0100325 call WaitForAssert({-> assert_match('^ *0,0-1 All$',
326 \ term_getline(buf, 20))}, 1000)
Bram Moolenaar406cd902020-02-18 21:54:41 +0100327 call StopVimInTerminal(buf)
Bram Moolenaar27321db2020-07-06 21:24:57 +0200328endfunc
329
330" The ":confirm" prompt was sometimes used with the terminal in cooked mode.
331" This test verifies that a "\<CR>" character is NOT required to respond to a
332" prompt from the ":conf q" and ":conf wq" commands.
333func Test_confirm_q_wq()
334 CheckNotGui
335 CheckRunVimInTerminal
336
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100337 call writefile(['foo'], 'Xfoo', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200338
339 let lines =<< trim END
340 set hidden nomore
341 call setline(1, 'abc')
342 edit Xfoo
343 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100344 call writefile(lines, 'Xscript', 'D')
Bram Moolenaar27321db2020-07-06 21:24:57 +0200345 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
346 call term_sendkeys(buf, ":confirm q\n")
347 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
348 \ term_getline(buf, 20))}, 1000)
349 call term_sendkeys(buf, 'C')
350 call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
351 \ term_getline(buf, 20))}, 1000)
352
353 call term_sendkeys(buf, ":edit Xfoo\n")
354 call term_sendkeys(buf, ":confirm wq\n")
355 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
356 \ term_getline(buf, 20))}, 1000)
357 call term_sendkeys(buf, 'C')
358 call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
359 \ term_getline(buf, 20))}, 1000)
Bram Moolenaar27321db2020-07-06 21:24:57 +0200360
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100361 call StopVimInTerminal(buf)
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100362endfunc
363
Dominique Pelle2bf60342021-05-02 20:16:24 +0200364func Test_confirm_write_ro()
365 CheckNotGui
366 CheckRunVimInTerminal
367
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100368 call writefile(['foo'], 'Xconfirm_write_ro', 'D')
Dominique Pelle2bf60342021-05-02 20:16:24 +0200369 let lines =<< trim END
370 set nobackup ff=unix cmdheight=2
371 edit Xconfirm_write_ro
372 norm Abar
373 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100374 call writefile(lines, 'Xscript', 'D')
Dominique Pelle2bf60342021-05-02 20:16:24 +0200375 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
376
377 " Try to write with 'ro' option.
378 call term_sendkeys(buf, ":set ro | confirm w\n")
379 call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
380 \ term_getline(buf, 18))}, 1000)
381 call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
382 \ term_getline(buf, 19))}, 1000)
383 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
384 call term_sendkeys(buf, 'N')
385 call WaitForAssert({-> assert_match('^ *$', term_getline(buf, 19))}, 1000)
386 call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
387 call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
388
389 call term_sendkeys(buf, ":confirm w\n")
390 call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
391 \ term_getline(buf, 18))}, 1000)
392 call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
393 \ term_getline(buf, 19))}, 1000)
394 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
395 call term_sendkeys(buf, 'Y')
396 call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 7B written$',
397 \ term_getline(buf, 19))}, 1000)
398 call assert_equal(['foobar'], readfile('Xconfirm_write_ro'))
399
400 " Try to write with read-only file permissions.
401 call setfperm('Xconfirm_write_ro', 'r--r--r--')
402 call term_sendkeys(buf, ":set noro | undo | confirm w\n")
403 call WaitForAssert({-> assert_match("^File permissions of \"Xconfirm_write_ro\" are read-only\. *$",
404 \ term_getline(buf, 17))}, 1000)
405 call WaitForAssert({-> assert_match('^It may still be possible to write it\. *$',
406 \ term_getline(buf, 18))}, 1000)
407 call WaitForAssert({-> assert_match('^Do you wish to try? *$', term_getline(buf, 19))}, 1000)
408 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
409 call term_sendkeys(buf, 'Y')
410 call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 4B written$',
411 \ term_getline(buf, 19))}, 1000)
412 call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
413
414 call StopVimInTerminal(buf)
Dominique Pelle2bf60342021-05-02 20:16:24 +0200415endfunc
416
Dominique Pellebd9e7962021-08-09 21:04:44 +0200417func Test_confirm_write_partial_file()
418 CheckNotGui
419 CheckRunVimInTerminal
420
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100421 call writefile(['a', 'b', 'c', 'd'], 'Xwrite_partial', 'D')
Dominique Pellebd9e7962021-08-09 21:04:44 +0200422 call writefile(['set nobackup ff=unix cmdheight=2',
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100423 \ 'edit Xwrite_partial'], 'Xscript', 'D')
Dominique Pellebd9e7962021-08-09 21:04:44 +0200424 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
425
426 call term_sendkeys(buf, ":confirm 2,3w\n")
427 call WaitForAssert({-> assert_match('^Write partial file? *$',
428 \ term_getline(buf, 19))}, 1000)
429 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
430 \ term_getline(buf, 20))}, 1000)
431 call term_sendkeys(buf, 'N')
432 call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
433 call assert_equal(['a', 'b', 'c', 'd'], readfile('Xwrite_partial'))
434 call delete('Xwrite_partial')
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, 'Y')
442 call WaitForAssert({-> assert_match('^"Xwrite_partial" \[New\] 2L, 4B written *$',
443 \ term_getline(buf, 19))}, 1000)
444 call WaitForAssert({-> assert_match('^Press ENTER or type command to continue *$',
445 \ term_getline(buf, 20))}, 1000)
446 call assert_equal(['b', 'c'], readfile('Xwrite_partial'))
447
448 call StopVimInTerminal(buf)
Dominique Pellebd9e7962021-08-09 21:04:44 +0200449endfunc
450
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100451" Test for the :print command
452func Test_print_cmd()
453 call assert_fails('print', 'E749:')
454endfunc
455
456" Test for the :winsize command
457func Test_winsize_cmd()
458 call assert_fails('winsize 1', 'E465:')
Bram Moolenaarf5a51162021-02-06 12:58:18 +0100459 call assert_fails('winsize 1 x', 'E465:')
460 call assert_fails('win_getid(1)', 'E475: Invalid argument: _getid(1)')
461 " Actually changing the window size would be flaky.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100462endfunc
463
464" Test for the :redir command
Bram Moolenaarf9a65502021-03-05 20:47:44 +0100465" NOTE: if you run tests as root this will fail. Don't run tests as root!
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100466func Test_redir_cmd()
467 call assert_fails('redir @@', 'E475:')
468 call assert_fails('redir abc', 'E475:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100469 call assert_fails('redir => 1abc', 'E474:')
470 call assert_fails('redir => a b', 'E488:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200471 call assert_fails('redir => abc[1]', 'E121:')
472 let b = 0zFF
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100473 call assert_fails('redir =>> b', 'E734:')
474 unlet b
475
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100476 if has('unix')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100477 " Redirecting to a directory name
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100478 call mkdir('Xredir')
479 call assert_fails('redir > Xredir', 'E17:')
480 call delete('Xredir', 'd')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100481 endif
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100482
483 " Test for redirecting to a register
484 redir @q> | echon 'clean ' | redir END
485 redir @q>> | echon 'water' | redir END
486 call assert_equal('clean water', @q)
487
488 " Test for redirecting to a variable
489 redir => color | echon 'blue ' | redir END
490 redir =>> color | echon 'sky' | redir END
491 call assert_equal('blue sky', color)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100492endfunc
493
Bram Moolenaar17709e22021-03-19 14:38:12 +0100494func Test_redir_cmd_readonly()
495 CheckNotRoot
Bram Moolenaar17709e22021-03-19 14:38:12 +0100496
497 " Redirecting to a read-only file
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100498 call writefile([], 'Xredirfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100499 call setfperm('Xredirfile', 'r--r--r--')
500 call assert_fails('redir! > Xredirfile', 'E190:')
Bram Moolenaar17709e22021-03-19 14:38:12 +0100501endfunc
502
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100503" Test for the :filetype command
504func Test_filetype_cmd()
505 call assert_fails('filetype abc', 'E475:')
506endfunc
507
508" Test for the :mode command
509func Test_mode_cmd()
510 call assert_fails('mode abc', 'E359:')
511endfunc
512
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100513" Test for the :sleep command
514func Test_sleep_cmd()
515 call assert_fails('sleep x', 'E475:')
516endfunc
517
518" Test for the :read command
519func Test_read_cmd()
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100520 call writefile(['one'], 'Xcmdfile', 'D')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100521 new
522 call assert_fails('read', 'E32:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100523 edit Xcmdfile
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100524 read
525 call assert_equal(['one', 'one'], getline(1, '$'))
526 close!
527 new
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100528 read Xcmdfile
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100529 call assert_equal(['', 'one'], getline(1, '$'))
530 call deletebufline('', 1, '$')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100531 call feedkeys("Qr Xcmdfile\<CR>visual\<CR>", 'xt')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100532 call assert_equal(['one'], getline(1, '$'))
533 close!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100534endfunc
535
536" Test for running Ex commands when text is locked.
537" <C-\>e in the command line is used to lock the text
538func Test_run_excmd_with_text_locked()
539 " :quit
540 let cmd = ":\<C-\>eexecute('quit')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200541 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100542
543 " :qall
544 let cmd = ":\<C-\>eexecute('qall')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200545 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100546
547 " :exit
548 let cmd = ":\<C-\>eexecute('exit')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200549 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100550
551 " :close - should be ignored
552 new
553 let cmd = ":\<C-\>eexecute('close')\<CR>\<C-C>"
554 call assert_equal(2, winnr('$'))
555 close
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100556
Bram Moolenaarff06f282020-04-21 22:01:14 +0200557 call assert_fails("call feedkeys(\":\<C-R>=execute('bnext')\<CR>\", 'xt')", 'E565:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200558
559 " :tabfirst
560 tabnew
561 call assert_fails("call feedkeys(\":\<C-R>=execute('tabfirst')\<CR>\", 'xt')", 'E565:')
562 tabclose
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100563endfunc
564
565" Test for the :verbose command
566func Test_verbose_cmd()
Bram Moolenaar60895f32022-04-12 14:23:19 +0100567 set verbose=3
568 call assert_match(' verbose=1\n\s*Last set from ', execute('verbose set vbs'), "\n")
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100569 call assert_equal([' verbose=0'], split(execute('0verbose set vbs'), "\n"))
Bram Moolenaar60895f32022-04-12 14:23:19 +0100570 set verbose=0
571 call assert_match(' verbose=4\n\s*Last set from .*\n verbose=0',
572 \ execute("4verbose set verbose | set verbose"))
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100573endfunc
574
575" Test for the :delete command and the related abbreviated commands
576func Test_excmd_delete()
577 new
578 call setline(1, ['foo', "\tbar"])
579 call assert_equal(['^Ibar$'], split(execute('dl'), "\n"))
580 call setline(1, ['foo', "\tbar"])
581 call assert_equal(['^Ibar$'], split(execute('dell'), "\n"))
582 call setline(1, ['foo', "\tbar"])
583 call assert_equal(['^Ibar$'], split(execute('delel'), "\n"))
584 call setline(1, ['foo', "\tbar"])
585 call assert_equal(['^Ibar$'], split(execute('deletl'), "\n"))
586 call setline(1, ['foo', "\tbar"])
587 call assert_equal(['^Ibar$'], split(execute('deletel'), "\n"))
588 call setline(1, ['foo', "\tbar"])
589 call assert_equal([' bar'], split(execute('dp'), "\n"))
590 call setline(1, ['foo', "\tbar"])
591 call assert_equal([' bar'], split(execute('dep'), "\n"))
592 call setline(1, ['foo', "\tbar"])
593 call assert_equal([' bar'], split(execute('delp'), "\n"))
594 call setline(1, ['foo', "\tbar"])
595 call assert_equal([' bar'], split(execute('delep'), "\n"))
596 call setline(1, ['foo', "\tbar"])
597 call assert_equal([' bar'], split(execute('deletp'), "\n"))
598 call setline(1, ['foo', "\tbar"])
599 call assert_equal([' bar'], split(execute('deletep'), "\n"))
600 close!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100601endfunc
602
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200603" Test for commands that are blocked in a sandbox
604func Sandbox_tests()
605 call assert_fails("call histadd(':', 'ls')", 'E48:')
606 call assert_fails("call mkdir('Xdir')", 'E48:')
607 call assert_fails("call rename('a', 'b')", 'E48:')
608 call assert_fails("call setbufvar(1, 'myvar', 1)", 'E48:')
609 call assert_fails("call settabvar(1, 'myvar', 1)", 'E48:')
610 call assert_fails("call settabwinvar(1, 1, 'myvar', 1)", 'E48:')
611 call assert_fails("call setwinvar(1, 'myvar', 1)", 'E48:')
612 call assert_fails("call timer_start(100, '')", 'E48:')
613 if has('channel')
614 call assert_fails("call prompt_setcallback(1, '')", 'E48:')
615 call assert_fails("call prompt_setinterrupt(1, '')", 'E48:')
616 call assert_fails("call prompt_setprompt(1, '')", 'E48:')
617 endif
618 call assert_fails("let $TESTVAR=1", 'E48:')
619 call assert_fails("call feedkeys('ivim')", 'E48:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100620 call assert_fails("source! Xsomefile", 'E48:')
621 call assert_fails("call delete('Xthatfile')", 'E48:')
622 call assert_fails("call writefile([], 'Xanotherfile')", 'E48:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200623 call assert_fails('!ls', 'E48:')
624 call assert_fails('shell', 'E48:')
625 call assert_fails('stop', 'E48:')
626 call assert_fails('exe "normal \<C-Z>"', 'E48:')
627 set insertmode
628 call assert_fails('call feedkeys("\<C-Z>", "xt")', 'E48:')
629 set insertmode&
630 call assert_fails('suspend', 'E48:')
631 call assert_fails('call system("ls")', 'E48:')
632 call assert_fails('call systemlist("ls")', 'E48:')
633 if has('clientserver')
634 call assert_fails('let s=remote_expr("gvim", "2+2")', 'E48:')
635 if !has('win32')
Dominique Pelle923dce22021-11-21 11:36:04 +0000636 " remote_foreground() doesn't throw an error message on MS-Windows
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200637 call assert_fails('call remote_foreground("gvim")', 'E48:')
638 endif
639 call assert_fails('let s=remote_peek("gvim")', 'E48:')
640 call assert_fails('let s=remote_read("gvim")', 'E48:')
641 call assert_fails('let s=remote_send("gvim", "abc")', 'E48:')
642 call assert_fails('let s=server2client("gvim", "abc")', 'E48:')
643 endif
644 if has('terminal')
645 call assert_fails('terminal', 'E48:')
646 call assert_fails('call term_start("vim")', 'E48:')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100647 call assert_fails('call term_dumpwrite(1, "Xdumpfile")', 'E48:')
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200648 endif
649 if has('channel')
650 call assert_fails("call ch_logfile('chlog')", 'E48:')
651 call assert_fails("call ch_open('localhost:8765')", 'E48:')
652 endif
653 if has('job')
654 call assert_fails("call job_start('vim')", 'E48:')
655 endif
656 if has('unix') && has('libcall')
657 call assert_fails("echo libcall('libc.so', 'getenv', 'HOME')", 'E48:')
658 endif
659 if has('unix')
660 call assert_fails('cd `pwd`', 'E48:')
661 endif
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200662 " some options cannot be changed in a sandbox
663 call assert_fails('set exrc', 'E48:')
664 call assert_fails('set cdpath', 'E48:')
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200665 if has('xim') && has('gui_gtk')
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200666 call assert_fails('set imstyle', 'E48:')
667 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200668endfunc
669
670func Test_sandbox()
671 sandbox call Sandbox_tests()
672endfunc
673
Dominique Pelle6d37e8e2021-05-06 17:36:55 +0200674func Test_command_not_implemented_E319()
675 if !has('mzscheme')
676 call assert_fails('mzscheme', 'E319:')
677 endif
678endfunc
679
kuuote08d7b1c2021-10-04 22:17:36 +0100680func Test_not_break_expression_register()
681 call setreg('=', '1+1')
682 if 0
683 put =1
684 endif
685 call assert_equal('1+1', getreg('=', 1))
686endfunc
687
Bram Moolenaar03725c52021-11-24 12:17:53 +0000688func Test_address_line_overflow()
689 if v:sizeoflong < 8
690 throw 'Skipped: only works with 64 bit long ints'
691 endif
692 new
Bram Moolenaar3cf21b32022-01-11 19:34:16 +0000693 call setline(1, range(100))
Bram Moolenaar03725c52021-11-24 12:17:53 +0000694 call assert_fails('|.44444444444444444444444', 'E1247:')
695 call assert_fails('|.9223372036854775806', 'E1247:')
Bram Moolenaar3cf21b32022-01-11 19:34:16 +0000696
697 $
698 yank 77777777777777777777
699 call assert_equal("99\n", @")
700
Bram Moolenaar03725c52021-11-24 12:17:53 +0000701 bwipe!
702endfunc
703
Bram Moolenaar4d97a562022-05-28 14:25:35 +0100704" This was leaving the cursor in line zero
705func Test_using_zero_in_range()
706 new
707 norm o00
708 silent! 0;s/\%')
709 bwipe!
710endfunc
711
ii141f0dc5e2022-07-26 19:44:56 +0100712" Test :write after changing name with :file and loading it with :edit
713func Test_write_after_rename()
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100714 call writefile(['text'], 'Xafterfile', 'D')
ii141f0dc5e2022-07-26 19:44:56 +0100715
716 enew
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100717 file Xafterfile
ii141f0dc5e2022-07-26 19:44:56 +0100718 call assert_fails('write', 'E13: File exists (add ! to override)')
719
720 " works OK after ":edit"
721 edit
722 write
723
ii141f0dc5e2022-07-26 19:44:56 +0100724 bwipe!
725endfunc
726
Bram Moolenaar03725c52021-11-24 12:17:53 +0000727
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100728" vim: shiftwidth=2 sts=2 expandtab