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