robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
26.8 KiB863 linesraw
1" Tests for various Ex commands.
2
3source check.vim
4source shared.vim
5source term_util.vim
6source screendump.vim
7
8func Test_ex_delete()
9  new
10
11  call setline(1, ['a', 'b', 'c'])
12  2
13  " :dl is :delete with the "l" flag, not :dlist
14  .dl
15  call assert_equal(['a', 'c'], getline(1, 2))
16  %delete _
17
18  " :delete # used to clobber "0
19  call setreg('#', '')
20  call setreg('0', '')
21  call setline(1, ['a', 'b', 'c'])
22  call assert_fails("1delete #", 'E488:')
23  call assert_equal(['a', 'b', 'c'], getline(1, '$'))
24  call assert_equal('', getreg('#'))
25  call assert_equal('', getreg('0'))
26
27  bw!
28endfunc
29
30func Test_range_error()
31  call assert_fails(':.echo 1', 'E481:')
32  call assert_fails(':$echo 1', 'E481:')
33  call assert_fails(':1,2echo 1', 'E481:')
34  call assert_fails(':+1echo 1', 'E481:')
35  call assert_fails(':/1/echo 1', 'E481:')
36  call assert_fails(':\/echo 1', 'E481:')
37  normal vv
38  call assert_fails(":'<,'>echo 1", 'E481:')
39  call assert_fails(":\\xcenter", 'E10:')
40endfunc
41
42func Test_buffers_lastused()
43  edit bufc " oldest
44
45  sleep 1200m
46  edit bufa " middle
47
48  sleep 1200m
49  edit bufb " newest
50
51  enew
52
53  let ls = split(execute('buffers t', 'silent!'), '\n')
54  let bufs = []
55  for line in ls
56    let bufs += [split(line, '"\s*')[1:2]]
57  endfor
58
59  let names = []
60  for buf in bufs
61    if buf[0] !=# '[No Name]'
62      let names += [buf[0]]
63    endif
64  endfor
65
66  call assert_equal(['bufb', 'bufa', 'bufc'], names)
67  call assert_match('[0-2] seconds\= ago', bufs[1][1])
68
69  bwipeout bufa
70  bwipeout bufb
71  bwipeout bufc
72endfunc
73
74" Test for the :copy command
75func Test_copy()
76  new
77
78  call setline(1, ['L1', 'L2', 'L3', 'L4'])
79  " copy lines in a range to inside the range
80  1,3copy 2
81  call assert_equal(['L1', 'L2', 'L1', 'L2', 'L3', 'L3', 'L4'], getline(1, 7))
82
83  " Specifying a count before using : to run an ex-command
84  exe "normal! gg4:yank\<CR>"
85  call assert_equal("L1\nL2\nL1\nL2\n", @")
86
87  bw!
88endfunc
89
90" Test for the :file command
91func Test_file_cmd()
92  call assert_fails('3file', 'E474:')
93  call assert_fails('0,0file', 'E474:')
94  call assert_fails('0file abc', 'E474:')
95  if !has('win32')
96    " Change the name of the buffer to the same name
97    new Xfile1
98    file Xfile1
99    call assert_equal('Xfile1', @%)
100    call assert_equal('Xfile1', @#)
101    bw!
102  endif
103endfunc
104
105" Test for the :drop command
106func Test_drop_cmd()
107  call writefile(['L1', 'L2'], 'Xdropfile', 'D')
108  " Test for reusing the current buffer
109  enew | only
110  let expected_nr = bufnr()
111  drop Xdropfile
112  call assert_equal(expected_nr, bufnr())
113  call assert_equal('L2', getline(2))
114  " Test for switching to an existing window
115  below new
116  drop Xdropfile
117  call assert_equal(1, winnr())
118  " Test for splitting the current window (set nohidden)
119  enew | only
120  set modified
121  drop Xdropfile
122  call assert_equal(2, winnr('$'))
123  " Not splitting the current window even if modified (set hidden)
124  set hidden
125  enew | only
126  set modified
127  drop Xdropfile
128  call assert_equal(1, winnr('$'))
129  " Check for setting the argument list
130  call assert_equal(['Xdropfile'], argv())
131  enew | only! | bw! Xdropfile
132endfunc
133
134" Test for the :append command
135func Test_append_cmd()
136  new
137  call setline(1, ['  L1'])
138  call feedkeys(":append\<CR>  L2\<CR>  L3\<CR>.\<CR>", 'xt')
139  call assert_equal(['  L1', '  L2', '  L3'], getline(1, '$'))
140  %delete _
141  " append after a specific line
142  call setline(1, ['  L1', '  L2', '  L3'])
143  call feedkeys(":2append\<CR>  L4\<CR>  L5\<CR>.\<CR>", 'xt')
144  call assert_equal(['  L1', '  L2', '  L4', '  L5', '  L3'], getline(1, '$'))
145  %delete _
146  " append with toggling 'autoindent'
147  call setline(1, ['  L1'])
148  call feedkeys(":append!\<CR>  L2\<CR>  L3\<CR>.\<CR>", 'xt')
149  call assert_equal(['  L1', '    L2', '      L3'], getline(1, '$'))
150  call assert_false(&autoindent)
151  %delete _
152  " append with 'autoindent' set and toggling 'autoindent'
153  set autoindent
154  call setline(1, ['  L1'])
155  call feedkeys(":append!\<CR>  L2\<CR>  L3\<CR>.\<CR>", 'xt')
156  call assert_equal(['  L1', '  L2', '  L3'], getline(1, '$'))
157  call assert_true(&autoindent)
158  set autoindent&
159  bw!
160endfunc
161
162func Test_append_cmd_empty_buf()
163  CheckRunVimInTerminal
164  let lines =<< trim END
165    func Timer(timer)
166      append
167    aaaaa
168    bbbbb
169    .
170    endfunc
171    call timer_start(10, 'Timer')
172  END
173  call writefile(lines, 'Xtest_append_cmd_empty_buf', 'D')
174  let buf = RunVimInTerminal('-S Xtest_append_cmd_empty_buf', {'rows': 6})
175  call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
176  call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
177
178  " clean up
179  call StopVimInTerminal(buf)
180endfunc
181
182" Test for :append, :insert and :change in a block that is not executed: the
183" lines up to the "." are skipped, not executed as commands.
184func Test_append_cmd_skipped()
185  let lines =<< trim END
186    if 0
187      append
188    endif
189    .
190    endif
191    let g:skipped = 'append'
192    if 0
193      insert
194    else
195    .
196    endif
197    let g:skipped ..= ' insert'
198    if 0
199      1change
200    endif
201    .
202    endif
203    let g:skipped ..= ' change'
204    while 0
205      append
206    endwhile
207    .
208    endwhile
209    let g:skipped ..= ' while'
210  END
211  call writefile(lines, 'Xappend_skipped', 'D')
212  new
213  call setline(1, 'text')
214  source Xappend_skipped
215  call assert_equal('append insert change while', g:skipped)
216  call assert_equal(['text'], getline(1, '$'))
217
218  " In Vim9 script :append is not allowed, but it is not executed here.
219  let lines =<< trim END
220    vim9script
221    if false
222      append
223    enddef
224    .
225    endif
226    g:skipped = 'vim9'
227  END
228  call writefile(lines, 'Xappend_skipped_vim9', 'D')
229  "source Xappend_skipped_vim9
230  "call assert_equal('vim9', g:skipped)
231  call assert_equal(['text'], getline(1, '$'))
232
233  unlet g:skipped
234  bwipe!
235endfunc
236
237" Test for the :insert command
238func Test_insert_cmd()
239  new
240  call setline(1, ['  L1'])
241  call feedkeys(":insert\<CR>  L2\<CR>  L3\<CR>.\<CR>", 'xt')
242  call assert_equal(['  L2', '  L3', '  L1'], getline(1, '$'))
243  %delete _
244  " insert before a specific line
245  call setline(1, ['  L1', '  L2', '  L3'])
246  call feedkeys(":2insert\<CR>  L4\<CR>  L5\<CR>.\<CR>", 'xt')
247  call assert_equal(['  L1', '  L4', '  L5', '  L2', '  L3'], getline(1, '$'))
248  %delete _
249  " insert with toggling 'autoindent'
250  call setline(1, ['  L1'])
251  call feedkeys(":insert!\<CR>  L2\<CR>  L3\<CR>.\<CR>", 'xt')
252  call assert_equal(['    L2', '      L3', '  L1'], getline(1, '$'))
253  call assert_false(&autoindent)
254  %delete _
255  " insert with 'autoindent' set and toggling 'autoindent'
256  set autoindent
257  call setline(1, ['  L1'])
258  call feedkeys(":insert!\<CR>  L2\<CR>  L3\<CR>.\<CR>", 'xt')
259  call assert_equal(['  L2', '  L3', '  L1'], getline(1, '$'))
260  call assert_true(&autoindent)
261  set autoindent&
262  bw!
263endfunc
264
265func Test_insert_cmd_empty_buf()
266  CheckRunVimInTerminal
267  let lines =<< trim END
268    func Timer(timer)
269      insert
270    aaaaa
271    bbbbb
272    .
273    endfunc
274    call timer_start(10, 'Timer')
275  END
276  call writefile(lines, 'Xtest_insert_cmd_empty_buf', 'D')
277  let buf = RunVimInTerminal('-S Xtest_insert_cmd_empty_buf', {'rows': 6})
278  call WaitForAssert({-> assert_equal('bbbbb', term_getline(buf, 2))})
279  call WaitForAssert({-> assert_equal('aaaaa', term_getline(buf, 1))})
280
281  " clean up
282  call StopVimInTerminal(buf)
283endfunc
284
285" Test for the :change command
286func Test_change_cmd()
287  new
288  call setline(1, ['  L1', 'L2', 'L3'])
289  call feedkeys(":change\<CR>  L4\<CR>  L5\<CR>.\<CR>", 'xt')
290  call assert_equal(['  L4', '  L5', 'L2', 'L3'], getline(1, '$'))
291  %delete _
292  " change a specific line
293  call setline(1, ['  L1', '  L2', '  L3'])
294  call feedkeys(":2change\<CR>  L4\<CR>  L5\<CR>.\<CR>", 'xt')
295  call assert_equal(['  L1', '  L4', '  L5', '  L3'], getline(1, '$'))
296  %delete _
297  " change with toggling 'autoindent'
298  call setline(1, ['  L1', 'L2', 'L3'])
299  call feedkeys(":change!\<CR>  L4\<CR>  L5\<CR>.\<CR>", 'xt')
300  call assert_equal(['    L4', '      L5', 'L2', 'L3'], getline(1, '$'))
301  call assert_false(&autoindent)
302  %delete _
303  " change with 'autoindent' set and toggling 'autoindent'
304  set autoindent
305  call setline(1, ['  L1', 'L2', 'L3'])
306  call feedkeys(":change!\<CR>  L4\<CR>  L5\<CR>.\<CR>", 'xt')
307  call assert_equal(['  L4', '  L5', 'L2', 'L3'], getline(1, '$'))
308  call assert_true(&autoindent)
309  set autoindent&
310  bw!
311endfunc
312
313" Test for the :language command
314func Test_language_cmd()
315  CheckFeature multi_lang
316
317  " OpenBSD allows nearly arbitrary locale names, since it largely ignores them
318  " (see setlocale(3)). One useful exception for this test is that in doesn't
319  " allow names containing dots unless they end in '.UTF-8'.
320  "
321  " Windows also allows nonsensical locale names, though it seems to reject
322  " names with multiple underscores (possibly expecting 'language_region', but
323  " not 'language_region_additional').
324  call assert_fails('language ctype non_existing_lang.bad', 'E197:')
325  call assert_fails('language time non_existing_lang.bad', 'E197:')
326endfunc
327
328" Test for the :confirm command dialog
329func Test_confirm_cmd()
330  CheckNotGui
331  CheckRunVimInTerminal
332
333  call writefile(['foo1'], 'Xfoo')
334  call writefile(['bar1'], 'Xbar')
335
336  " Test for saving all the modified buffers
337  let lines =<< trim END
338    set nomore
339    new Xfoo
340    call setline(1, 'foo2')
341    new Xbar
342    call setline(1, 'bar2')
343    wincmd b
344  END
345  call writefile(lines, 'Xscript')
346  let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
347  call term_sendkeys(buf, ":confirm qall\n")
348  call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
349  call term_sendkeys(buf, "A")
350  call StopVimInTerminal(buf)
351
352  call assert_equal(['foo2'], readfile('Xfoo'))
353  call assert_equal(['bar2'], readfile('Xbar'))
354
355  " Test for discarding all the changes to modified buffers
356  let lines =<< trim END
357    set nomore
358    new Xfoo
359    call setline(1, 'foo3')
360    new Xbar
361    call setline(1, 'bar3')
362    wincmd b
363  END
364  call writefile(lines, 'Xscript')
365  let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
366  call term_sendkeys(buf, ":confirm qall\n")
367  call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
368  call term_sendkeys(buf, "D")
369  call StopVimInTerminal(buf)
370
371  call assert_equal(['foo2'], readfile('Xfoo'))
372  call assert_equal(['bar2'], readfile('Xbar'))
373
374  " Test for saving and discarding changes to some buffers
375  let lines =<< trim END
376    set nomore
377    new Xfoo
378    call setline(1, 'foo4')
379    new Xbar
380    call setline(1, 'bar4')
381    wincmd b
382  END
383  call writefile(lines, 'Xscript')
384  let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
385  call term_sendkeys(buf, ":confirm qall\n")
386  call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
387  call term_sendkeys(buf, "N")
388  call WaitForAssert({-> assert_match('\[Y\]es, (N)o, (C)ancel: ', term_getline(buf, 20))}, 1000)
389  call term_sendkeys(buf, "Y")
390  call StopVimInTerminal(buf)
391
392  call assert_equal(['foo4'], readfile('Xfoo'))
393  call assert_equal(['bar2'], readfile('Xbar'))
394
395  call delete('Xscript')
396  call delete('Xfoo')
397  call delete('Xbar')
398endfunc
399
400func Test_confirm_cmd_cancel()
401  CheckNotGui
402  CheckRunVimInTerminal
403
404  " Test for closing a window with a modified buffer
405  let lines =<< trim END
406    set nomore
407    new
408    call setline(1, 'abc')
409  END
410  call writefile(lines, 'Xscript')
411  let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
412  call term_sendkeys(buf, ":confirm close\n")
413  call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
414        \ term_getline(buf, 20))}, 1000)
415  call term_sendkeys(buf, "C")
416  call WaitForAssert({-> assert_equal('', term_getline(buf, 20))}, 1000)
417  call term_sendkeys(buf, ":confirm close\n")
418  call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
419        \ term_getline(buf, 20))}, 1000)
420  call term_sendkeys(buf, "N")
421  call WaitForAssert({-> assert_match('^ *0,0-1         All$',
422        \ term_getline(buf, 20))}, 1000)
423  call StopVimInTerminal(buf)
424  call delete('Xscript')
425endfunc
426
427" The ":confirm" prompt was sometimes used with the terminal in cooked mode.
428" This test verifies that a "\<CR>" character is NOT required to respond to a
429" prompt from the ":conf q" and ":conf wq" commands.
430func Test_confirm_q_wq()
431  CheckNotGui
432  CheckRunVimInTerminal
433
434  call writefile(['foo'], 'Xfoo')
435
436  let lines =<< trim END
437    set hidden nomore
438    call setline(1, 'abc')
439    edit Xfoo
440  END
441  call writefile(lines, 'Xscript')
442  let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
443  call term_sendkeys(buf, ":confirm q\n")
444  call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
445        \ term_getline(buf, 20))}, 1000)
446  call term_sendkeys(buf, 'C')
447  call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
448        \ term_getline(buf, 20))}, 1000)
449
450  call term_sendkeys(buf, ":edit Xfoo\n")
451  call term_sendkeys(buf, ":confirm wq\n")
452  call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
453        \ term_getline(buf, 20))}, 1000)
454  call term_sendkeys(buf, 'C')
455  call WaitForAssert({-> assert_notmatch('^\[Y\]es, (N)o, (C)ancel: C*$',
456        \ term_getline(buf, 20))}, 1000)
457  call StopVimInTerminal(buf)
458
459  call delete('Xscript')
460  call delete('Xfoo')
461endfunc
462
463func Test_confirm_write_ro()
464  CheckNotGui
465  CheckRunVimInTerminal
466
467  call writefile(['foo'], 'Xconfirm_write_ro')
468  let lines =<< trim END
469    set nobackup ff=unix cmdheight=2
470    edit Xconfirm_write_ro
471    norm Abar
472  END
473  call writefile(lines, 'Xscript')
474  let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
475
476  " Try to write with 'ro' option.
477  call term_sendkeys(buf, ":set ro | confirm w\n")
478  call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
479        \            term_getline(buf, 18))}, 1000)
480  call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
481        \            term_getline(buf, 19))}, 1000)
482  call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
483  call term_sendkeys(buf, 'N')
484  call WaitForAssert({-> assert_match('^ *$', term_getline(buf, 19))}, 1000)
485  call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
486  call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
487
488  call term_sendkeys(buf, ":confirm w\n")
489  call WaitForAssert({-> assert_match("^'readonly' option is set for \"Xconfirm_write_ro\"\. *$",
490        \            term_getline(buf, 18))}, 1000)
491  call WaitForAssert({-> assert_match('^Do you wish to write anyway? *$',
492        \            term_getline(buf, 19))}, 1000)
493  call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
494  call term_sendkeys(buf, 'Y')
495  call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 7B written$',
496        \            term_getline(buf, 19))}, 1000)
497  call assert_equal(['foobar'], readfile('Xconfirm_write_ro'))
498
499  " Try to write with read-only file permissions.
500  call setfperm('Xconfirm_write_ro', 'r--r--r--')
501  call term_sendkeys(buf, ":set noro | undo | confirm w\n")
502  call WaitForAssert({-> assert_match("^File permissions of \"Xconfirm_write_ro\" are read-only\. *$",
503        \            term_getline(buf, 17))}, 1000)
504  call WaitForAssert({-> assert_match('^It may still be possible to write it\. *$',
505        \            term_getline(buf, 18))}, 1000)
506  call WaitForAssert({-> assert_match('^Do you wish to try? *$', term_getline(buf, 19))}, 1000)
507  call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$', term_getline(buf, 20))}, 1000)
508  call term_sendkeys(buf, 'Y')
509  call WaitForAssert({-> assert_match('^"Xconfirm_write_ro" 1L, 4B written$',
510        \            term_getline(buf, 19))}, 1000)
511  call assert_equal(['foo'], readfile('Xconfirm_write_ro'))
512
513  call StopVimInTerminal(buf)
514  call delete('Xscript')
515  call delete('Xconfirm_write_ro')
516endfunc
517
518func Test_confirm_write_partial_file()
519  CheckNotGui
520  CheckRunVimInTerminal
521
522  call writefile(['a', 'b', 'c', 'd'], 'Xwrite_partial')
523  call writefile(['set nobackup ff=unix cmdheight=2',
524        \         'edit Xwrite_partial'], 'Xscript')
525  let buf = RunVimInTerminal('-S Xscript', {'rows': 20})
526
527  call term_sendkeys(buf, ":confirm 2,3w\n")
528  call WaitForAssert({-> assert_match('^Write partial file? *$',
529        \            term_getline(buf, 19))}, 1000)
530  call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
531        \            term_getline(buf, 20))}, 1000)
532  call term_sendkeys(buf, 'N')
533  call WaitForAssert({-> assert_match('.* All$', term_getline(buf, 20))}, 1000)
534  call assert_equal(['a', 'b', 'c', 'd'], readfile('Xwrite_partial'))
535  call delete('Xwrite_partial')
536
537  call term_sendkeys(buf, ":confirm 2,3w\n")
538  call WaitForAssert({-> assert_match('^Write partial file? *$',
539        \            term_getline(buf, 19))}, 1000)
540  call WaitForAssert({-> assert_match('^(Y)es, \[N\]o: *$',
541        \            term_getline(buf, 20))}, 1000)
542  call term_sendkeys(buf, 'Y')
543  call WaitForAssert({-> assert_match('^"Xwrite_partial" \[New\] 2L, 4B written *$',
544        \            term_getline(buf, 19))}, 1000)
545  call WaitForAssert({-> assert_match('^Press ENTER or type command to continue *$',
546        \            term_getline(buf, 20))}, 1000)
547  call assert_equal(['b', 'c'], readfile('Xwrite_partial'))
548
549  call StopVimInTerminal(buf)
550  call delete('Xwrite_partial')
551  call delete('Xscript')
552endfunc
553
554" Test for the :print command
555func Test_print_cmd()
556  call assert_fails('print', 'E749:')
557endfunc
558
559" Test for the :winsize command
560func Test_winsize_cmd()
561  call assert_fails('winsize 1', 'E465:')
562  call assert_fails('winsize 1 x', 'E465:')
563  call assert_fails('win_getid(1)', 'E475: Invalid argument: _getid(1)')
564  " Actually changing the window size would be flaky.
565endfunc
566
567" Test for the :redir command
568" NOTE: if you run tests as root this will fail.  Don't run tests as root!
569func Test_redir_cmd()
570  call assert_fails('redir @@', 'E475:')
571  call assert_fails('redir abc', 'E475:')
572  call assert_fails('redir => 1abc', 'E474:')
573  call assert_fails('redir => a b', 'E488:')
574  call assert_fails('redir => abc[1]', 'E121:')
575  let b = 0zFF
576  call assert_fails('redir =>> b', 'E734:')
577  unlet b
578
579  if has('unix')
580    " Redirecting to a directory name
581    call mkdir('Xdir')
582    call assert_fails('redir > Xdir', 'E17:')
583    call delete('Xdir', 'd')
584  endif
585
586  " Test for redirecting to a register
587  redir @q> | echon 'clean ' | redir END
588  redir @q>> | echon 'water' | redir END
589  call assert_equal('clean water', @q)
590
591  " Test for redirecting to a variable
592  redir => color | echon 'blue ' | redir END
593  redir =>> color | echon 'sky' | redir END
594  call assert_equal('blue sky', color)
595endfunc
596
597func Test_redir_cmd_readonly()
598  CheckNotRoot
599
600  " Redirecting to a read-only file
601  call writefile([], 'Xfile')
602  call setfperm('Xfile', 'r--r--r--')
603  call assert_fails('redir! > Xfile', 'E190:')
604  call delete('Xfile')
605endfunc
606
607" Test for the :filetype command
608func Test_filetype_cmd()
609  call assert_fails('filetype abc', 'E475:')
610endfunc
611
612" Test for the :mode command
613func Test_mode_cmd()
614  call assert_fails('mode abc', 'E359:')
615endfunc
616
617" Test for the :sleep command
618func Test_sleep_cmd()
619  call assert_fails('sleep x', 'E475:')
620endfunc
621
622" Test for the :read command
623func Test_read_cmd()
624  call writefile(['one'], 'Xcmdfile', 'D')
625  new
626  call assert_fails('read', 'E32:')
627  edit Xcmdfile
628  read
629  call assert_equal(['one', 'one'], getline(1, '$'))
630  bw!
631  new
632  read Xcmdfile
633  call assert_equal(['', 'one'], getline(1, '$'))
634  " Nvim: Ex mode is a cmdwin wrapper; cannot run inside feedkeys('x'), and
635  " ":read" no longer deletes the empty first line in Ex mode.
636  "call deletebufline('', 1, '$')
637  "call feedkeys("Qr Xcmdfile\<CR>visual\<CR>", 'xt')
638  "call assert_equal(['one'], getline(1, '$'))
639  bw!
640endfunc
641
642" Test for running Ex commands when text is locked.
643" <C-\>e in the command line is used to lock the text
644func Test_run_excmd_with_text_locked()
645  " :quit
646  let cmd = ":\<C-\>eexecute('quit')\<CR>\<C-C>"
647  call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
648
649  " :qall
650  let cmd = ":\<C-\>eexecute('qall')\<CR>\<C-C>"
651  call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
652
653  " :exit
654  let cmd = ":\<C-\>eexecute('exit')\<CR>\<C-C>"
655  call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
656
657  " :close - should be ignored
658  new
659  let cmd = ":\<C-\>eexecute('close')\<CR>\<C-C>"
660  call assert_equal(2, winnr('$'))
661  close
662
663  call assert_fails("call feedkeys(\":\<C-R>=execute('bnext')\<CR>\", 'xt')", 'E565:')
664
665  " :tabfirst
666  tabnew
667  call assert_fails("call feedkeys(\":\<C-R>=execute('tabfirst')\<CR>\", 'xt')", 'E565:')
668  tabclose
669endfunc
670
671" Test for the :verbose command
672func Test_verbose_cmd()
673  set verbose=3
674  call assert_match('  verbose=1\n\s*Last set from ', execute('verbose set vbs'), "\n")
675  call assert_equal(['  verbose=0'], split(execute('0verbose set vbs'), "\n"))
676  set verbose=0
677  call assert_match('  verbose=4\n\s*Last set from .*\n  verbose=0',
678        \ execute("4verbose set verbose | set verbose"))
679endfunc
680
681" Test for the :delete command and the related abbreviated commands
682func Test_excmd_delete()
683  new
684  call setline(1, ['foo', "\tbar"])
685  call assert_equal(['^Ibar$'], split(execute('dl'), "\n"))
686  call setline(1, ['foo', "\tbar"])
687  call assert_equal(['^Ibar$'], split(execute('dell'), "\n"))
688  call setline(1, ['foo', "\tbar"])
689  call assert_equal(['^Ibar$'], split(execute('delel'), "\n"))
690  call setline(1, ['foo', "\tbar"])
691  call assert_equal(['^Ibar$'], split(execute('deletl'), "\n"))
692  call setline(1, ['foo', "\tbar"])
693  call assert_equal(['^Ibar$'], split(execute('deletel'), "\n"))
694  call setline(1, ['foo', "\tbar"])
695  call assert_equal(['        bar'], split(execute('dp'), "\n"))
696  call setline(1, ['foo', "\tbar"])
697  call assert_equal(['        bar'], split(execute('dep'), "\n"))
698  call setline(1, ['foo', "\tbar"])
699  call assert_equal(['        bar'], split(execute('delp'), "\n"))
700  call setline(1, ['foo', "\tbar"])
701  call assert_equal(['        bar'], split(execute('delep'), "\n"))
702  call setline(1, ['foo', "\tbar"])
703  call assert_equal(['        bar'], split(execute('deletp'), "\n"))
704  call setline(1, ['foo', "\tbar"])
705  call assert_equal(['        bar'], split(execute('deletep'), "\n"))
706  bw!
707endfunc
708
709" Test for commands that are blocked in a sandbox
710func Sandbox_tests()
711  call assert_fails("call histadd(':', 'ls')", 'E48:')
712  call assert_fails("call mkdir('Xdir')", 'E48:')
713  call assert_fails("call rename('a', 'b')", 'E48:')
714  call assert_fails("call setbufvar(1, 'myvar', 1)", 'E48:')
715  call assert_fails("call settabvar(1, 'myvar', 1)", 'E48:')
716  call assert_fails("call settabwinvar(1, 1, 'myvar', 1)", 'E48:')
717  call assert_fails("call setwinvar(1, 'myvar', 1)", 'E48:')
718  call assert_fails("call timer_start(100, '')", 'E48:')
719  if has('channel')
720    call assert_fails("call prompt_setcallback(1, '')", 'E48:')
721    call assert_fails("call prompt_setinterrupt(1, '')", 'E48:')
722    call assert_fails("call prompt_setprompt(1, '')", 'E48:')
723  endif
724  call assert_fails("let $TESTVAR=1", 'E48:')
725  call assert_fails("call feedkeys('ivim')", 'E48:')
726  call assert_fails("source! Xfile", 'E48:')
727  call assert_fails("call delete('Xfile')", 'E48:')
728  call assert_fails("call writefile([], 'Xfile')", 'E48:')
729  call assert_fails('!ls', 'E48:')
730  " call assert_fails('shell', 'E48:')
731  call assert_fails('stop', 'E48:')
732  call assert_fails('exe "normal \<C-Z>"', 'E48:')
733  " set insertmode
734  " call assert_fails('call feedkeys("\<C-Z>", "xt")', 'E48:')
735  " set insertmode&
736  call assert_fails('suspend', 'E48:')
737  call assert_fails('call system("ls")', 'E48:')
738  call assert_fails('call systemlist("ls")', 'E48:')
739  if has('clientserver')
740    call assert_fails('let s=remote_expr("gvim", "2+2")', 'E48:')
741    if !has('win32')
742      " remote_foreground() doesn't throw an error message on MS-Windows
743      call assert_fails('call remote_foreground("gvim")', 'E48:')
744    endif
745    call assert_fails('let s=remote_peek("gvim")', 'E48:')
746    call assert_fails('let s=remote_read("gvim")', 'E48:')
747    call assert_fails('let s=remote_send("gvim", "abc")', 'E48:')
748    call assert_fails('let s=server2client("gvim", "abc")', 'E48:')
749  endif
750  if has('terminal')
751    call assert_fails('terminal', 'E48:')
752    call assert_fails('call term_start("vim")', 'E48:')
753    call assert_fails('call term_dumpwrite(1, "Xfile")', 'E48:')
754  endif
755  if has('channel')
756    call assert_fails("call ch_logfile('chlog')", 'E48:')
757    call assert_fails("call ch_open('localhost:8765')", 'E48:')
758  endif
759  if has('job')
760    call assert_fails("call job_start('vim')", 'E48:')
761  endif
762  if has('unix') && has('libcall')
763    call assert_fails("echo libcall('libc.so', 'getenv', 'HOME')", 'E48:')
764  endif
765  if has('unix')
766    call assert_fails('cd `pwd`', 'E48:')
767  endif
768  "call assert_fails("call echoraw('test')", 'E48:')
769  "call assert_fails("echoconsole 'test'", 'E48:')
770  call assert_fails("call readfile('Xsomefile')", 'E48:')
771  call assert_fails("call readblob('Xsomefile')", 'E48:')
772  call assert_fails("call readdir('.')", 'E48:')
773  "call assert_fails("call readdirex('.')", 'E48:')
774  call assert_fails("call chdir('.')", 'E48:')
775  " some options cannot be changed in a sandbox
776  call assert_fails('set exrc', 'E48:')
777  call assert_fails('set cdpath', 'E48:')
778  if has('xim') && has('gui_gtk')
779    call assert_fails('set imstyle', 'E48:')
780  endif
781endfunc
782
783func Test_sandbox()
784  sandbox call Sandbox_tests()
785endfunc
786
787func Test_command_not_implemented_E319()
788  if !has('mzscheme')
789    call assert_fails('mzscheme', 'E319:')
790  endif
791endfunc
792
793func Test_not_break_expression_register()
794  call setreg('=', '1+1')
795  if 0
796    put =1
797  endif
798  call assert_equal('1+1', getreg('=', 1))
799endfunc
800
801func Test_address_line_overflow()
802  if !has('nvim') && v:sizeoflong < 8
803    throw 'Skipped: only works with 64 bit long ints'
804  endif
805  new
806  call setline(1, range(100))
807  call assert_fails('|.44444444444444444444444', 'E1247:')
808  call assert_fails('|.9223372036854775806', 'E1247:')
809  call assert_fails('.44444444444444444444444d', 'E1247:')
810  call assert_equal(range(100)->map('string(v:val)'), getline(1, '$'))
811
812  $
813  yank 77777777777777777777
814  call assert_equal("99\n", @")
815
816  bwipe!
817endfunc
818
819" This was leaving the cursor in line zero
820func Test_using_zero_in_range()
821  new
822  norm o00
823  silent!  0;s/\%')
824  bwipe!
825endfunc
826
827" Test :write after changing name with :file and loading it with :edit
828func Test_write_after_rename()
829  call writefile(['text'], 'Xfile')
830
831  enew
832  file Xfile
833  call assert_fails('write', 'E13: File exists (add ! to override)')
834
835  " works OK after ":edit"
836  edit
837  write
838
839  call delete('Xfile')
840  bwipe!
841endfunc
842
843" catch address lines overflow
844func Test_ex_address_range_overflow()
845  call assert_fails(':--+foobar', 'E492:')
846endfunc
847
848func Test_drop_modified_file()
849  CheckScreendump
850  let lines =<< trim END
851  call setline(1, 'The quick brown fox jumped over the lazy dogs')
852  END
853  call writefile([''], 'Xdrop_modified.txt', 'D')
854  call writefile(lines, 'Xtest_drop_modified', 'D')
855  let buf = RunVimInTerminal('-S Xtest_drop_modified Xdrop_modified.txt', {'rows': 10,'columns': 40})
856  call term_sendkeys(buf, ":drop Xdrop_modified.txt\<CR>")
857  call VerifyScreenDump(buf, 'Test_drop_modified_1', {})
858
859  " clean up
860  call StopVimInTerminal(buf)
861endfunc
862
863" vim: shiftwidth=2 sts=2 expandtab