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