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