xref: /vim-8.2.3635/src/testdir/test_arglist.vim (revision 3cad4703)
1" Test argument list commands
2
3source check.vim
4source shared.vim
5source term_util.vim
6
7func Reset_arglist()
8  args a | %argd
9endfunc
10
11func Test_argidx()
12  args a b c
13  last
14  call assert_equal(2, argidx())
15  %argdelete
16  call assert_equal(0, argidx())
17  " doing it again doesn't result in an error
18  %argdelete
19  call assert_equal(0, argidx())
20  call assert_fails('2argdelete', 'E16:')
21
22  args a b c
23  call assert_equal(0, argidx())
24  next
25  call assert_equal(1, argidx())
26  next
27  call assert_equal(2, argidx())
28  1argdelete
29  call assert_equal(1, argidx())
30  1argdelete
31  call assert_equal(0, argidx())
32  1argdelete
33  call assert_equal(0, argidx())
34endfunc
35
36func Test_argadd()
37  call Reset_arglist()
38
39  %argdelete
40  argadd a b c
41  call assert_equal(0, argidx())
42
43  %argdelete
44  argadd a
45  call assert_equal(0, argidx())
46  argadd b c d
47  call assert_equal(0, argidx())
48
49  call Init_abc()
50  argadd x
51  call Assert_argc(['a', 'b', 'x', 'c'])
52  call assert_equal(1, argidx())
53
54  call Init_abc()
55  0argadd x
56  call Assert_argc(['x', 'a', 'b', 'c'])
57  call assert_equal(2, argidx())
58
59  call Init_abc()
60  1argadd x
61  call Assert_argc(['a', 'x', 'b', 'c'])
62  call assert_equal(2, argidx())
63
64  call Init_abc()
65  $argadd x
66  call Assert_argc(['a', 'b', 'c', 'x'])
67  call assert_equal(1, argidx())
68
69  call Init_abc()
70  $argadd x
71  +2argadd y
72  call Assert_argc(['a', 'b', 'c', 'x', 'y'])
73  call assert_equal(1, argidx())
74
75  %argd
76  edit d
77  arga
78  call assert_equal(1, len(argv()))
79  call assert_equal('d', get(argv(), 0, ''))
80
81  %argd
82  edit some\ file
83  arga
84  call assert_equal(1, len(argv()))
85  call assert_equal('some file', get(argv(), 0, ''))
86
87  %argd
88  new
89  arga
90  call assert_equal(0, len(argv()))
91
92  if has('unix')
93    call assert_fails('argadd `Xdoes_not_exist`', 'E479:')
94  endif
95endfunc
96
97func Test_argadd_empty_curbuf()
98  new
99  let curbuf = bufnr('%')
100  call writefile(['test', 'Xargadd'], 'Xargadd')
101  " must not re-use the current buffer.
102  argadd Xargadd
103  call assert_equal(curbuf, bufnr('%'))
104  call assert_equal('', bufname('%'))
105  call assert_equal(1, '$'->line())
106  rew
107  call assert_notequal(curbuf, '%'->bufnr())
108  call assert_equal('Xargadd', '%'->bufname())
109  call assert_equal(2, line('$'))
110
111  call delete('Xargadd')
112  %argd
113  bwipe!
114endfunc
115
116func Init_abc()
117  args a b c
118  next
119endfunc
120
121func Assert_argc(l)
122  call assert_equal(len(a:l), argc())
123  let i = 0
124  while i < len(a:l) && i < argc()
125    call assert_equal(a:l[i], argv(i))
126    let i += 1
127  endwhile
128endfunc
129
130" Test for [count]argument and [count]argdelete commands
131" Ported from the test_argument_count.in test script
132func Test_argument()
133  call Reset_arglist()
134
135  let save_hidden = &hidden
136  set hidden
137
138  let g:buffers = []
139  augroup TEST
140    au BufEnter * call add(buffers, expand('%:t'))
141  augroup END
142
143  argadd a b c d
144  $argu
145  $-argu
146  -argu
147  1argu
148  +2argu
149
150  augroup TEST
151    au!
152  augroup END
153
154  call assert_equal(['d', 'c', 'b', 'a', 'c'], g:buffers)
155
156  call assert_equal("\na   b   [c] d   ", execute(':args'))
157
158  .argd
159  call assert_equal(['a', 'b', 'd'], argv())
160
161  -argd
162  call assert_equal(['a', 'd'], argv())
163
164  $argd
165  call assert_equal(['a'], argv())
166
167  1arga c
168  1arga b
169  $argu
170  $arga x
171  call assert_equal(['a', 'b', 'c', 'x'], argv())
172
173  0arga y
174  call assert_equal(['y', 'a', 'b', 'c', 'x'], argv())
175
176  %argd
177  call assert_equal([], argv())
178
179  arga a b c d e f
180  2,$-argd
181  call assert_equal(['a', 'f'], argv())
182
183  let &hidden = save_hidden
184
185  let save_columns = &columns
186  let &columns = 79
187  try
188    exe 'args ' .. join(range(1, 81))
189    call assert_equal(join([
190          \ '',
191          \ '[1] 6   11  16  21  26  31  36  41  46  51  56  61  66  71  76  81  ',
192          \ '2   7   12  17  22  27  32  37  42  47  52  57  62  67  72  77  ',
193          \ '3   8   13  18  23  28  33  38  43  48  53  58  63  68  73  78  ',
194          \ '4   9   14  19  24  29  34  39  44  49  54  59  64  69  74  79  ',
195          \ '5   10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  ',
196          \ ], "\n"),
197          \ execute('args'))
198
199    " No trailing newline with one item per row.
200    let long_arg = repeat('X', 81)
201    exe 'args ' .. long_arg
202    call assert_equal("\n[".long_arg.']', execute('args'))
203  finally
204    let &columns = save_columns
205  endtry
206
207  " Setting argument list should fail when the current buffer has unsaved
208  " changes
209  %argd
210  enew!
211  set modified
212  call assert_fails('args x y z', 'E37:')
213  args! x y z
214  call assert_equal(['x', 'y', 'z'], argv())
215  call assert_equal('x', expand('%:t'))
216
217  last | enew | argu
218  call assert_equal('z', expand('%:t'))
219
220  %argdelete
221  call assert_fails('argument', 'E163:')
222endfunc
223
224func Test_list_arguments()
225  call Reset_arglist()
226
227  " four args half the screen width makes two lines with two columns
228  let aarg = repeat('a', &columns / 2 - 4)
229  let barg = repeat('b', &columns / 2 - 4)
230  let carg = repeat('c', &columns / 2 - 4)
231  let darg = repeat('d', &columns / 2 - 4)
232  exe 'argadd ' aarg barg carg darg
233
234  redir => result
235  args
236  redir END
237  call assert_match('\[' . aarg . '] \+' . carg . '\n' . barg . ' \+' . darg, trim(result))
238
239  " if one arg is longer than half the screen make one column
240  exe 'argdel' aarg
241  let aarg = repeat('a', &columns / 2 + 2)
242  exe '0argadd' aarg
243  redir => result
244  args
245  redir END
246  call assert_match(aarg . '\n\[' . barg . ']\n' . carg . '\n' . darg, trim(result))
247
248  %argdelete
249endfunc
250
251func Test_args_with_quote()
252  " Only on Unix can a file name include a double quote.
253  CheckUnix
254
255  args \"foobar
256  call assert_equal('"foobar', argv(0))
257  %argdelete
258endfunc
259
260" Test for 0argadd and 0argedit
261" Ported from the test_argument_0count.in test script
262func Test_zero_argadd()
263  call Reset_arglist()
264
265  arga a b c d
266  2argu
267  0arga added
268  call assert_equal(['added', 'a', 'b', 'c', 'd'], argv())
269
270  2argu
271  arga third
272  call assert_equal(['added', 'a', 'third', 'b', 'c', 'd'], argv())
273
274  %argd
275  arga a b c d
276  2argu
277  0arge edited
278  call assert_equal(['edited', 'a', 'b', 'c', 'd'], argv())
279
280  2argu
281  arga third
282  call assert_equal(['edited', 'a', 'third', 'b', 'c', 'd'], argv())
283
284  2argu
285  argedit file\ with\ spaces another file
286  call assert_equal(['edited', 'a', 'file with spaces', 'another', 'file', 'third', 'b', 'c', 'd'], argv())
287  call assert_equal('file with spaces', expand('%'))
288endfunc
289
290" Test for argc()
291func Test_argc()
292  call Reset_arglist()
293  call assert_equal(0, argc())
294  argadd a b
295  call assert_equal(2, argc())
296endfunc
297
298" Test for arglistid()
299func Test_arglistid()
300  call Reset_arglist()
301  arga a b
302  call assert_equal(0, arglistid())
303  split
304  arglocal
305  call assert_equal(1, arglistid())
306  tabnew | tabfirst
307  call assert_equal(0, arglistid(2))
308  call assert_equal(1, arglistid(1, 1))
309  call assert_equal(0, arglistid(2, 1))
310  call assert_equal(1, arglistid(1, 2))
311  tabonly | only | enew!
312  argglobal
313  call assert_equal(0, arglistid())
314endfunc
315
316" Tests for argv() and argc()
317func Test_argv()
318  call Reset_arglist()
319  call assert_equal([], argv())
320  call assert_equal("", argv(2))
321  call assert_equal(0, argc())
322  argadd a b c d
323  call assert_equal(4, argc())
324  call assert_equal('c', argv(2))
325
326  let w1_id = win_getid()
327  split
328  let w2_id = win_getid()
329  arglocal
330  args e f g
331  tabnew
332  let w3_id = win_getid()
333  split
334  let w4_id = win_getid()
335  argglobal
336  tabfirst
337  call assert_equal(4, argc(w1_id))
338  call assert_equal('b', argv(1, w1_id))
339  call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w1_id))
340
341  call assert_equal(3, argc(w2_id))
342  call assert_equal('f', argv(1, w2_id))
343  call assert_equal(['e', 'f', 'g'], argv(-1, w2_id))
344
345  call assert_equal(3, argc(w3_id))
346  call assert_equal('e', argv(0, w3_id))
347  call assert_equal(['e', 'f', 'g'], argv(-1, w3_id))
348
349  call assert_equal(4, argc(w4_id))
350  call assert_equal('c', argv(2, w4_id))
351  call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w4_id))
352
353  call assert_equal(4, argc(-1))
354  call assert_equal(3, argc())
355  call assert_equal('d', argv(3, -1))
356  call assert_equal(['a', 'b', 'c', 'd'], argv(-1, -1))
357  tabonly | only | enew!
358  " Negative test cases
359  call assert_equal(-1, argc(100))
360  call assert_equal('', argv(1, 100))
361  call assert_equal([], argv(-1, 100))
362  call assert_equal('', argv(10, -1))
363endfunc
364
365" Test for the :argedit command
366func Test_argedit()
367  call Reset_arglist()
368  argedit a
369  call assert_equal(['a'], argv())
370  call assert_equal('a', expand('%:t'))
371  argedit b
372  call assert_equal(['a', 'b'], argv())
373  call assert_equal('b', expand('%:t'))
374  argedit a
375  call assert_equal(['a', 'b', 'a'], argv())
376  call assert_equal('a', expand('%:t'))
377  " When file name case is ignored, an existing buffer with only case
378  " difference is re-used.
379  argedit C D
380  call assert_equal('C', expand('%:t'))
381  call assert_equal(['a', 'b', 'a', 'C', 'D'], argv())
382  argedit c
383  if has('fname_case')
384    call assert_equal(['a', 'b', 'a', 'C', 'c', 'D'], argv())
385  else
386    call assert_equal(['a', 'b', 'a', 'C', 'C', 'D'], argv())
387  endif
388  0argedit x
389  if has('fname_case')
390    call assert_equal(['x', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
391  else
392    call assert_equal(['x', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
393  endif
394  enew! | set modified
395  call assert_fails('argedit y', 'E37:')
396  argedit! y
397  if has('fname_case')
398    call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
399  else
400    call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
401  endif
402  %argd
403  bwipe! C
404  bwipe! D
405
406  " :argedit reuses the current buffer if it is empty
407  %argd
408  " make sure to use a new buffer number for x when it is loaded
409  bw! x
410  new
411  let a = bufnr()
412  argedit x
413  call assert_equal(a, bufnr())
414  call assert_equal('x', bufname())
415  %argd
416  bw! x
417endfunc
418
419" Test for the :argdelete command
420func Test_argdelete()
421  call Reset_arglist()
422  args aa a aaa b bb
423  argdelete a*
424  call assert_equal(['b', 'bb'], argv())
425  call assert_equal('aa', expand('%:t'))
426  last
427  argdelete %
428  call assert_equal(['b'], argv())
429  call assert_fails('argdelete', 'E610:')
430  call assert_fails('1,100argdelete', 'E16:')
431  call assert_fails('argdel /\)/', 'E55:')
432  call assert_fails('1argdel 1', 'E474:')
433
434  call Reset_arglist()
435  args a b c d
436  next
437  argdel
438  call Assert_argc(['a', 'c', 'd'])
439  %argdel
440
441  call assert_fails('argdel does_not_exist', 'E480:')
442endfunc
443
444func Test_argdelete_completion()
445  args foo bar
446
447  call feedkeys(":argdelete \<C-A>\<C-B>\"\<CR>", 'tx')
448  call assert_equal('"argdelete bar foo', @:)
449
450  call feedkeys(":argdelete x \<C-A>\<C-B>\"\<CR>", 'tx')
451  call assert_equal('"argdelete x bar foo', @:)
452
453  %argd
454endfunc
455
456" Tests for the :next, :prev, :first, :last, :rewind commands
457func Test_argpos()
458  call Reset_arglist()
459  args a b c d
460  last
461  call assert_equal(3, argidx())
462  call assert_fails('next', 'E165:')
463  prev
464  call assert_equal(2, argidx())
465  Next
466  call assert_equal(1, argidx())
467  first
468  call assert_equal(0, argidx())
469  call assert_fails('prev', 'E164:')
470  3next
471  call assert_equal(3, argidx())
472  rewind
473  call assert_equal(0, argidx())
474  %argd
475endfunc
476
477" Test for autocommand that redefines the argument list, when doing ":all".
478func Test_arglist_autocmd()
479  autocmd BufReadPost Xxx2 next Xxx2 Xxx1
480  call writefile(['test file Xxx1'], 'Xxx1')
481  call writefile(['test file Xxx2'], 'Xxx2')
482  call writefile(['test file Xxx3'], 'Xxx3')
483
484  new
485  " redefine arglist; go to Xxx1
486  next! Xxx1 Xxx2 Xxx3
487  " open window for all args; Reading Xxx2 will change the arglist and the
488  " third window will get Xxx1:
489  "   win 1: Xxx1
490  "   win 2: Xxx2
491  "   win 3: Xxx1
492  all
493  call assert_equal('test file Xxx1', getline(1))
494  wincmd w
495  wincmd w
496  call assert_equal('test file Xxx1', getline(1))
497  rewind
498  call assert_equal('test file Xxx2', getline(1))
499
500  autocmd! BufReadPost Xxx2
501  enew! | only
502  call delete('Xxx1')
503  call delete('Xxx2')
504  call delete('Xxx3')
505  argdelete Xxx*
506  bwipe! Xxx1 Xxx2 Xxx3
507endfunc
508
509func Test_arg_all_expand()
510  call writefile(['test file Xxx1'], 'Xx x')
511  next notexist Xx\ x runtest.vim
512  call assert_equal('notexist Xx\ x runtest.vim', expand('##'))
513  call delete('Xx x')
514endfunc
515
516func Test_large_arg()
517  " Argument longer or equal to the number of columns used to cause
518  " access to invalid memory.
519  exe 'argadd ' .repeat('x', &columns)
520  args
521endfunc
522
523func Test_argdo()
524  next! Xa.c Xb.c Xc.c
525  new
526  let l = []
527  argdo call add(l, expand('%'))
528  call assert_equal(['Xa.c', 'Xb.c', 'Xc.c'], l)
529  bwipe Xa.c Xb.c Xc.c
530endfunc
531
532" Test for quitting Vim with unedited files in the argument list
533func Test_quit_with_arglist()
534  CheckRunVimInTerminal
535
536  let buf = RunVimInTerminal('', {'rows': 6})
537  call term_sendkeys(buf, ":set nomore\n")
538  call term_sendkeys(buf, ":args a b c\n")
539  call term_sendkeys(buf, ":quit\n")
540  call TermWait(buf)
541  call WaitForAssert({-> assert_match('^E173:', term_getline(buf, 6))})
542  call StopVimInTerminal(buf)
543
544  " Try :confirm quit with unedited files in arglist
545  let buf = RunVimInTerminal('', {'rows': 6})
546  call term_sendkeys(buf, ":set nomore\n")
547  call term_sendkeys(buf, ":args a b c\n")
548  call term_sendkeys(buf, ":confirm quit\n")
549  call TermWait(buf)
550  call WaitForAssert({-> assert_match('^\[Y\]es, (N)o: *$',
551        \ term_getline(buf, 6))})
552  call term_sendkeys(buf, "N")
553  call TermWait(buf)
554  call term_sendkeys(buf, ":confirm quit\n")
555  call WaitForAssert({-> assert_match('^\[Y\]es, (N)o: *$',
556        \ term_getline(buf, 6))})
557  call term_sendkeys(buf, "Y")
558  call TermWait(buf)
559  call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})
560  only!
561  " When this test fails, swap files are left behind which breaks subsequent
562  " tests
563  call delete('.a.swp')
564  call delete('.b.swp')
565  call delete('.c.swp')
566endfunc
567
568" Test for ":all" not working when in the cmdline window
569func Test_all_not_allowed_from_cmdwin()
570  CheckFeature cmdwin
571
572  au BufEnter * all
573  next x
574  " Use try/catch here, somehow assert_fails() doesn't work on MS-Windows
575  " console.
576  let caught = 'no'
577  try
578    exe ":norm! 7q?apat\<CR>"
579  catch /E11:/
580    let caught = 'yes'
581  endtry
582  call assert_equal('yes', caught)
583  au! BufEnter
584endfunc
585
586" vim: shiftwidth=2 sts=2 expandtab
587