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