blob: 6f648dd4e5c16d83ccf180b4dccfae752ffd6806 [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()
86 call writefile(['L1', 'L2'], 'Xfile')
87 enew | only
88 drop Xfile
89 call assert_equal('L2', getline(2))
90 " Test for switching to an existing window
91 below new
92 drop Xfile
93 call assert_equal(1, winnr())
94 " Test for splitting the current window
95 enew | only
96 set modified
97 drop Xfile
98 call assert_equal(2, winnr('$'))
99 " Check for setting the argument list
100 call assert_equal(['Xfile'], argv())
101 enew | only!
102 call delete('Xfile')
103endfunc
104
105" Test for the :append command
106func Test_append_cmd()
107 new
108 call setline(1, [' L1'])
109 call feedkeys(":append\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
110 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
111 %delete _
112 " append after a specific line
113 call setline(1, [' L1', ' L2', ' L3'])
114 call feedkeys(":2append\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
115 call assert_equal([' L1', ' L2', ' L4', ' L5', ' L3'], getline(1, '$'))
116 %delete _
117 " append with toggling 'autoindent'
118 call setline(1, [' L1'])
119 call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
120 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
121 call assert_false(&autoindent)
122 %delete _
123 " append with 'autoindent' set and toggling 'autoindent'
124 set autoindent
125 call setline(1, [' L1'])
126 call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
127 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
128 call assert_true(&autoindent)
129 set autoindent&
130 close!
131endfunc
132
zeertzjq1fa3de12021-12-31 12:19:22 +0000133func Test_append_cmd_empty_buf()
134 CheckRunVimInTerminal
135 let lines =<< trim END
136 func Timer(timer)
137 append
138 aaaaa
139 bbbbb
140 .
141 endfunc
142 call timer_start(10, 'Timer')
143 END
144 call writefile(lines, 'Xtest_append_cmd_empty_buf')
145 let buf = RunVimInTerminal('-S Xtest_append_cmd_empty_buf', {'rows': 6})
146 call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
147 call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
148
149 " clean up
150 call StopVimInTerminal(buf)
151 call delete('Xtest_append_cmd_empty_buf')
152endfunc
153
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100154" Test for the :insert command
155func Test_insert_cmd()
156 new
157 call setline(1, [' L1'])
158 call feedkeys(":insert\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
159 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
160 %delete _
161 " insert before a specific line
162 call setline(1, [' L1', ' L2', ' L3'])
163 call feedkeys(":2insert\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
164 call assert_equal([' L1', ' L4', ' L5', ' L2', ' L3'], getline(1, '$'))
165 %delete _
166 " insert with toggling 'autoindent'
167 call setline(1, [' L1'])
168 call feedkeys(":insert!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
169 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
170 call assert_false(&autoindent)
171 %delete _
172 " insert with 'autoindent' set and toggling 'autoindent'
173 set 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_true(&autoindent)
178 set autoindent&
179 close!
180endfunc
181
zeertzjq1fa3de12021-12-31 12:19:22 +0000182func Test_insert_cmd_empty_buf()
183 CheckRunVimInTerminal
184 let lines =<< trim END
185 func Timer(timer)
186 insert
187 aaaaa
188 bbbbb
189 .
190 endfunc
191 call timer_start(10, 'Timer')
192 END
193 call writefile(lines, 'Xtest_insert_cmd_empty_buf')
194 let buf = RunVimInTerminal('-S Xtest_insert_cmd_empty_buf', {'rows': 6})
195 call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
196 call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
197
198 " clean up
199 call StopVimInTerminal(buf)
200 call delete('Xtest_insert_cmd_empty_buf')
201endfunc
202
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100203" Test for the :change command
204func Test_change_cmd()
205 new
206 call setline(1, [' L1', 'L2', 'L3'])
207 call feedkeys(":change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
208 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
209 %delete _
210 " change a specific line
211 call setline(1, [' L1', ' L2', ' L3'])
212 call feedkeys(":2change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
213 call assert_equal([' L1', ' L4', ' L5', ' L3'], getline(1, '$'))
214 %delete _
215 " change with toggling 'autoindent'
216 call setline(1, [' L1', 'L2', 'L3'])
217 call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
218 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
219 call assert_false(&autoindent)
220 %delete _
221 " change with 'autoindent' set and toggling 'autoindent'
222 set autoindent
223 call setline(1, [' L1', 'L2', 'L3'])
224 call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
225 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
226 call assert_true(&autoindent)
227 set autoindent&
228 close!
229endfunc
230
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100231" Test for the :language command
232func Test_language_cmd()
233 CheckFeature multi_lang
234
235 call assert_fails('language ctype non_existing_lang', 'E197:')
236 call assert_fails('language time non_existing_lang', 'E197:')
237endfunc
238
239" Test for the :confirm command dialog
240func Test_confirm_cmd()
241 CheckNotGui
242 CheckRunVimInTerminal
243
Bram Moolenaar27321db2020-07-06 21:24:57 +0200244 call writefile(['foo1'], 'Xfoo')
245 call writefile(['bar1'], 'Xbar')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100246
247 " Test for saving all the modified buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200248 let lines =<< trim END
249 set nomore
250 new Xfoo
251 call setline(1, 'foo2')
252 new Xbar
253 call setline(1, 'bar2')
254 wincmd b
255 END
256 call writefile(lines, 'Xscript')
257 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100258 call term_sendkeys(buf, ":confirm qall\n")
259 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
260 call term_sendkeys(buf, "A")
261 call StopVimInTerminal(buf)
262
Bram Moolenaar27321db2020-07-06 21:24:57 +0200263 call assert_equal(['foo2'], readfile('Xfoo'))
264 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100265
266 " Test for discarding all the changes to modified buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200267 let lines =<< trim END
268 set nomore
269 new Xfoo
270 call setline(1, 'foo3')
271 new Xbar
272 call setline(1, 'bar3')
273 wincmd b
274 END
275 call writefile(lines, 'Xscript')
276 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100277 call term_sendkeys(buf, ":confirm qall\n")
278 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
279 call term_sendkeys(buf, "D")
280 call StopVimInTerminal(buf)
281
Bram Moolenaar27321db2020-07-06 21:24:57 +0200282 call assert_equal(['foo2'], readfile('Xfoo'))
283 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100284
285 " Test for saving and discarding changes to some buffers
Bram Moolenaar27321db2020-07-06 21:24:57 +0200286 let lines =<< trim END
287 set nomore
288 new Xfoo
289 call setline(1, 'foo4')
290 new Xbar
291 call setline(1, 'bar4')
292 wincmd b
293 END
294 call writefile(lines, 'Xscript')
295 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100296 call term_sendkeys(buf, ":confirm qall\n")
297 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
298 call term_sendkeys(buf, "N")
299 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, (C)ancel: ', term_getline(buf, 20))}, 1000)
300 call term_sendkeys(buf, "Y")
301 call StopVimInTerminal(buf)
302
Bram Moolenaar27321db2020-07-06 21:24:57 +0200303 call assert_equal(['foo4'], readfile('Xfoo'))
304 call assert_equal(['bar2'], readfile('Xbar'))
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100305
Bram Moolenaar27321db2020-07-06 21:24:57 +0200306 call delete('Xscript')
307 call delete('Xfoo')
308 call delete('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
321 call writefile(lines, 'Xscript')
322 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 +0200335 call delete('Xscript')
336endfunc
337
338" The ":confirm" prompt was sometimes used with the terminal in cooked mode.
339" This test verifies that a "\<CR>" character is NOT required to respond to a
340" prompt from the ":conf q" and ":conf wq" commands.
341func Test_confirm_q_wq()
342 CheckNotGui
343 CheckRunVimInTerminal
344
345 call writefile(['foo'], 'Xfoo')
346
347 let lines =<< trim END
348 set hidden nomore
349 call setline(1, 'abc')
350 edit Xfoo
351 END
352 call writefile(lines, 'Xscript')
353 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
354 call term_sendkeys(buf, ":confirm q\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)
360
361 call term_sendkeys(buf, ":edit Xfoo\n")
362 call term_sendkeys(buf, ":confirm wq\n")
363 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
364 \ term_getline(buf, 20))}, 1000)
365 call term_sendkeys(buf, 'C')
366 call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
367 \ term_getline(buf, 20))}, 1000)
368 call StopVimInTerminal(buf)
369
370 call delete('Xscript')
371 call delete('Xfoo')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100372endfunc
373
Dominique Pelle2bf60342021-05-02 20:16:24 +0200374func Test_confirm_write_ro()
375 CheckNotGui
376 CheckRunVimInTerminal
377
378 call writefile(['foo'], 'Xconfirm_write_ro')
379 let lines =<< trim END
380 set nobackup ff=unix cmdheight=2
381 edit Xconfirm_write_ro
382 norm Abar
383 END
384 call writefile(lines, 'Xscript')
385 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
386
387 " Try to write with 'ro' option.
388 call term_sendkeys(buf, ":set ro | confirm w\n")
389 call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
390 \ term_getline(buf, 18))}, 1000)
391 call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
392 \ term_getline(buf, 19))}, 1000)
393 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
394 call term_sendkeys(buf, 'N')
395 call WaitForAssert({-> assert_match('^ *$', term_getline(buf, 19))}, 1000)
396 call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
397 call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
398
399 call term_sendkeys(buf, ":confirm w\n")
400 call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
401 \ term_getline(buf, 18))}, 1000)
402 call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
403 \ term_getline(buf, 19))}, 1000)
404 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
405 call term_sendkeys(buf, 'Y')
406 call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 7B written$',
407 \ term_getline(buf, 19))}, 1000)
408 call assert_equal(['foobar'], readfile('Xconfirm_write_ro'))
409
410 " Try to write with read-only file permissions.
411 call setfperm('Xconfirm_write_ro', 'r--r--r--')
412 call term_sendkeys(buf, ":set noro | undo | confirm w\n")
413 call WaitForAssert({-> assert_match("^File permissions of \"Xconfirm_write_ro\" are read-only\. *$",
414 \ term_getline(buf, 17))}, 1000)
415 call WaitForAssert({-> assert_match('^It may still be possible to write it\. *$',
416 \ term_getline(buf, 18))}, 1000)
417 call WaitForAssert({-> assert_match('^Do you wish to try? *$', term_getline(buf, 19))}, 1000)
418 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
419 call term_sendkeys(buf, 'Y')
420 call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 4B written$',
421 \ term_getline(buf, 19))}, 1000)
422 call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
423
424 call StopVimInTerminal(buf)
425 call delete('Xscript')
426 call delete('Xconfirm_write_ro')
427endfunc
428
Dominique Pellebd9e7962021-08-09 21:04:44 +0200429func Test_confirm_write_partial_file()
430 CheckNotGui
431 CheckRunVimInTerminal
432
433 call writefile(['a', 'b', 'c', 'd'], 'Xwrite_partial')
434 call writefile(['set nobackup ff=unix cmdheight=2',
435 \ 'edit Xwrite_partial'], 'Xscript')
436 let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
437
438 call term_sendkeys(buf, ":confirm 2,3w\n")
439 call WaitForAssert({-> assert_match('^Write partial file? *$',
440 \ term_getline(buf, 19))}, 1000)
441 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
442 \ term_getline(buf, 20))}, 1000)
443 call term_sendkeys(buf, 'N')
444 call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
445 call assert_equal(['a', 'b', 'c', 'd'], readfile('Xwrite_partial'))
446 call delete('Xwrite_partial')
447
448 call term_sendkeys(buf, ":confirm 2,3w\n")
449 call WaitForAssert({-> assert_match('^Write partial file? *$',
450 \ term_getline(buf, 19))}, 1000)
451 call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
452 \ term_getline(buf, 20))}, 1000)
453 call term_sendkeys(buf, 'Y')
454 call WaitForAssert({-> assert_match('^"Xwrite_partial" \[New\] 2L, 4B written *$',
455 \ term_getline(buf, 19))}, 1000)
456 call WaitForAssert({-> assert_match('^Press ENTER or type command to continue *$',
457 \ term_getline(buf, 20))}, 1000)
458 call assert_equal(['b', 'c'], readfile('Xwrite_partial'))
459
460 call StopVimInTerminal(buf)
461 call delete('Xwrite_partial')
462 call delete('Xscript')
463endfunc
464
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100465" Test for the :print command
466func Test_print_cmd()
467 call assert_fails('print', 'E749:')
468endfunc
469
470" Test for the :winsize command
471func Test_winsize_cmd()
472 call assert_fails('winsize 1', 'E465:')
Bram Moolenaarf5a51162021-02-06 12:58:18 +0100473 call assert_fails('winsize 1 x', 'E465:')
474 call assert_fails('win_getid(1)', 'E475: Invalid argument: _getid(1)')
475 " Actually changing the window size would be flaky.
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100476endfunc
477
478" Test for the :redir command
Bram Moolenaarf9a65502021-03-05 20:47:44 +0100479" NOTE: if you run tests as root this will fail. Don't run tests as root!
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100480func Test_redir_cmd()
481 call assert_fails('redir @@', 'E475:')
482 call assert_fails('redir abc', 'E475:')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100483 call assert_fails('redir => 1abc', 'E474:')
484 call assert_fails('redir => a b', 'E488:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200485 call assert_fails('redir => abc[1]', 'E121:')
486 let b = 0zFF
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100487 call assert_fails('redir =>> b', 'E734:')
488 unlet b
489
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100490 if has('unix')
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100491 " Redirecting to a directory name
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100492 call mkdir('Xdir')
493 call assert_fails('redir > Xdir', 'E17:')
494 call delete('Xdir', 'd')
495 endif
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100496
497 " Test for redirecting to a register
498 redir @q> | echon 'clean ' | redir END
499 redir @q>> | echon 'water' | redir END
500 call assert_equal('clean water', @q)
501
502 " Test for redirecting to a variable
503 redir => color | echon 'blue ' | redir END
504 redir =>> color | echon 'sky' | redir END
505 call assert_equal('blue sky', color)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100506endfunc
507
Bram Moolenaar17709e22021-03-19 14:38:12 +0100508func Test_redir_cmd_readonly()
509 CheckNotRoot
Bram Moolenaar17709e22021-03-19 14:38:12 +0100510
511 " Redirecting to a read-only file
512 call writefile([], 'Xfile')
513 call setfperm('Xfile', 'r--r--r--')
514 call assert_fails('redir! > Xfile', 'E190:')
515 call delete('Xfile')
516endfunc
517
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100518" Test for the :filetype command
519func Test_filetype_cmd()
520 call assert_fails('filetype abc', 'E475:')
521endfunc
522
523" Test for the :mode command
524func Test_mode_cmd()
525 call assert_fails('mode abc', 'E359:')
526endfunc
527
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100528" Test for the :sleep command
529func Test_sleep_cmd()
530 call assert_fails('sleep x', 'E475:')
531endfunc
532
533" Test for the :read command
534func Test_read_cmd()
535 call writefile(['one'], 'Xfile')
536 new
537 call assert_fails('read', 'E32:')
538 edit Xfile
539 read
540 call assert_equal(['one', 'one'], getline(1, '$'))
541 close!
542 new
543 read Xfile
544 call assert_equal(['', 'one'], getline(1, '$'))
545 call deletebufline('', 1, '$')
546 call feedkeys("Qr Xfile\<CR>visual\<CR>", 'xt')
547 call assert_equal(['one'], getline(1, '$'))
548 close!
549 call delete('Xfile')
550endfunc
551
552" Test for running Ex commands when text is locked.
553" <C-\>e in the command line is used to lock the text
554func Test_run_excmd_with_text_locked()
555 " :quit
556 let cmd = ":\<C-\>eexecute('quit')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200557 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100558
559 " :qall
560 let cmd = ":\<C-\>eexecute('qall')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200561 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100562
563 " :exit
564 let cmd = ":\<C-\>eexecute('exit')\<CR>\<C-C>"
Bram Moolenaarff06f282020-04-21 22:01:14 +0200565 call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100566
567 " :close - should be ignored
568 new
569 let cmd = ":\<C-\>eexecute('close')\<CR>\<C-C>"
570 call assert_equal(2, winnr('$'))
571 close
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100572
Bram Moolenaarff06f282020-04-21 22:01:14 +0200573 call assert_fails("call feedkeys(\":\<C-R>=execute('bnext')\<CR>\", 'xt')", 'E565:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200574
575 " :tabfirst
576 tabnew
577 call assert_fails("call feedkeys(\":\<C-R>=execute('tabfirst')\<CR>\", 'xt')", 'E565:')
578 tabclose
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100579endfunc
580
581" Test for the :verbose command
582func Test_verbose_cmd()
583 call assert_equal([' verbose=1'], split(execute('verbose set vbs'), "\n"))
584 call assert_equal([' verbose=0'], split(execute('0verbose set vbs'), "\n"))
585 let l = execute("4verbose set verbose | set verbose")
586 call assert_equal([' verbose=4', ' verbose=0'], split(l, "\n"))
587endfunc
588
589" Test for the :delete command and the related abbreviated commands
590func Test_excmd_delete()
591 new
592 call setline(1, ['foo', "\tbar"])
593 call assert_equal(['^Ibar$'], split(execute('dl'), "\n"))
594 call setline(1, ['foo', "\tbar"])
595 call assert_equal(['^Ibar$'], split(execute('dell'), "\n"))
596 call setline(1, ['foo', "\tbar"])
597 call assert_equal(['^Ibar$'], split(execute('delel'), "\n"))
598 call setline(1, ['foo', "\tbar"])
599 call assert_equal(['^Ibar$'], split(execute('deletl'), "\n"))
600 call setline(1, ['foo', "\tbar"])
601 call assert_equal(['^Ibar$'], split(execute('deletel'), "\n"))
602 call setline(1, ['foo', "\tbar"])
603 call assert_equal([' bar'], split(execute('dp'), "\n"))
604 call setline(1, ['foo', "\tbar"])
605 call assert_equal([' bar'], split(execute('dep'), "\n"))
606 call setline(1, ['foo', "\tbar"])
607 call assert_equal([' bar'], split(execute('delp'), "\n"))
608 call setline(1, ['foo', "\tbar"])
609 call assert_equal([' bar'], split(execute('delep'), "\n"))
610 call setline(1, ['foo', "\tbar"])
611 call assert_equal([' bar'], split(execute('deletp'), "\n"))
612 call setline(1, ['foo', "\tbar"])
613 call assert_equal([' bar'], split(execute('deletep'), "\n"))
614 close!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100615endfunc
616
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200617" Test for commands that are blocked in a sandbox
618func Sandbox_tests()
619 call assert_fails("call histadd(':', 'ls')", 'E48:')
620 call assert_fails("call mkdir('Xdir')", 'E48:')
621 call assert_fails("call rename('a', 'b')", 'E48:')
622 call assert_fails("call setbufvar(1, 'myvar', 1)", 'E48:')
623 call assert_fails("call settabvar(1, 'myvar', 1)", 'E48:')
624 call assert_fails("call settabwinvar(1, 1, 'myvar', 1)", 'E48:')
625 call assert_fails("call setwinvar(1, 'myvar', 1)", 'E48:')
626 call assert_fails("call timer_start(100, '')", 'E48:')
627 if has('channel')
628 call assert_fails("call prompt_setcallback(1, '')", 'E48:')
629 call assert_fails("call prompt_setinterrupt(1, '')", 'E48:')
630 call assert_fails("call prompt_setprompt(1, '')", 'E48:')
631 endif
632 call assert_fails("let $TESTVAR=1", 'E48:')
633 call assert_fails("call feedkeys('ivim')", 'E48:')
634 call assert_fails("source! Xfile", 'E48:')
635 call assert_fails("call delete('Xfile')", 'E48:')
636 call assert_fails("call writefile([], 'Xfile')", 'E48:')
637 call assert_fails('!ls', 'E48:')
638 call assert_fails('shell', 'E48:')
639 call assert_fails('stop', 'E48:')
640 call assert_fails('exe "normal \<C-Z>"', 'E48:')
641 set insertmode
642 call assert_fails('call feedkeys("\<C-Z>", "xt")', 'E48:')
643 set insertmode&
644 call assert_fails('suspend', 'E48:')
645 call assert_fails('call system("ls")', 'E48:')
646 call assert_fails('call systemlist("ls")', 'E48:')
647 if has('clientserver')
648 call assert_fails('let s=remote_expr("gvim", "2+2")', 'E48:')
649 if !has('win32')
Dominique Pelle923dce22021-11-21 11:36:04 +0000650 " remote_foreground() doesn't throw an error message on MS-Windows
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200651 call assert_fails('call remote_foreground("gvim")', 'E48:')
652 endif
653 call assert_fails('let s=remote_peek("gvim")', 'E48:')
654 call assert_fails('let s=remote_read("gvim")', 'E48:')
655 call assert_fails('let s=remote_send("gvim", "abc")', 'E48:')
656 call assert_fails('let s=server2client("gvim", "abc")', 'E48:')
657 endif
658 if has('terminal')
659 call assert_fails('terminal', 'E48:')
660 call assert_fails('call term_start("vim")', 'E48:')
661 call assert_fails('call term_dumpwrite(1, "Xfile")', 'E48:')
662 endif
663 if has('channel')
664 call assert_fails("call ch_logfile('chlog')", 'E48:')
665 call assert_fails("call ch_open('localhost:8765')", 'E48:')
666 endif
667 if has('job')
668 call assert_fails("call job_start('vim')", 'E48:')
669 endif
670 if has('unix') && has('libcall')
671 call assert_fails("echo libcall('libc.so', 'getenv', 'HOME')", 'E48:')
672 endif
673 if has('unix')
674 call assert_fails('cd `pwd`', 'E48:')
675 endif
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200676 " some options cannot be changed in a sandbox
677 call assert_fails('set exrc', 'E48:')
678 call assert_fails('set cdpath', 'E48:')
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200679 if has('xim') && has('gui_gtk')
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200680 call assert_fails('set imstyle', 'E48:')
681 endif
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200682endfunc
683
684func Test_sandbox()
685 sandbox call Sandbox_tests()
686endfunc
687
Dominique Pelle6d37e8e2021-05-06 17:36:55 +0200688func Test_command_not_implemented_E319()
689 if !has('mzscheme')
690 call assert_fails('mzscheme', 'E319:')
691 endif
692endfunc
693
kuuote08d7b1c2021-10-04 22:17:36 +0100694func Test_not_break_expression_register()
695 call setreg('=', '1+1')
696 if 0
697 put =1
698 endif
699 call assert_equal('1+1', getreg('=', 1))
700endfunc
701
Bram Moolenaar03725c52021-11-24 12:17:53 +0000702func Test_address_line_overflow()
703 if v:sizeoflong < 8
704 throw 'Skipped: only works with 64 bit long ints'
705 endif
706 new
707 call setline(1, 'text')
708 call assert_fails('|.44444444444444444444444', 'E1247:')
709 call assert_fails('|.9223372036854775806', 'E1247:')
710 bwipe!
711endfunc
712
713
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100714" vim: shiftwidth=2 sts=2 expandtab