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