xref: /vim-8.2.3635/src/testdir/test_arglist.vim (revision 577fadfc)
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  redir => result
144  args
145  redir END
146  call assert_equal('a   b   [c] d', trim(result))
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  " Setting argument list should fail when the current buffer has unsaved
176  " changes
177  %argd
178  enew!
179  set modified
180  call assert_fails('args x y z', 'E37:')
181  args! x y z
182  call assert_equal(['x', 'y', 'z'], argv())
183  call assert_equal('x', expand('%:t'))
184
185  last | enew | argu
186  call assert_equal('z', expand('%:t'))
187
188  %argdelete
189  call assert_fails('argument', 'E163:')
190endfunc
191
192func Test_list_arguments()
193  " Clean the argument list
194  arga a | %argd
195
196  " four args half the screen width makes two lines with two columns
197  let aarg = repeat('a', &columns / 2 - 4)
198  let barg = repeat('b', &columns / 2 - 4)
199  let carg = repeat('c', &columns / 2 - 4)
200  let darg = repeat('d', &columns / 2 - 4)
201  exe 'argadd ' aarg barg carg darg
202
203  redir => result
204  args
205  redir END
206  call assert_match('\[' . aarg . '] \+' . carg . '\n' . barg . ' \+' . darg, trim(result))
207
208  " if one arg is longer than half the screen make one column
209  exe 'argdel' aarg
210  let aarg = repeat('a', &columns / 2 + 2)
211  exe '0argadd' aarg
212  redir => result
213  args
214  redir END
215  call assert_match(aarg . '\n\[' . barg . ']\n' . carg . '\n' . darg, trim(result))
216
217  %argdelete
218endfunc
219
220func Test_args_with_quote()
221  " Only on Unix can a file name include a double quote.
222  if has('unix')
223    args \"foobar
224    call assert_equal('"foobar', argv(0))
225    %argdelete
226  endif
227endfunc
228
229" Test for 0argadd and 0argedit
230" Ported from the test_argument_0count.in test script
231func Test_zero_argadd()
232  " Clean the argument list
233  arga a | %argd
234
235  arga a b c d
236  2argu
237  0arga added
238  call assert_equal(['added', 'a', 'b', 'c', 'd'], argv())
239
240  2argu
241  arga third
242  call assert_equal(['added', 'a', 'third', 'b', 'c', 'd'], argv())
243
244  %argd
245  arga a b c d
246  2argu
247  0arge edited
248  call assert_equal(['edited', 'a', 'b', 'c', 'd'], argv())
249
250  2argu
251  arga third
252  call assert_equal(['edited', 'a', 'third', 'b', 'c', 'd'], argv())
253
254  2argu
255  argedit file\ with\ spaces another file
256  call assert_equal(['edited', 'a', 'file with spaces', 'another', 'file', 'third', 'b', 'c', 'd'], argv())
257  call assert_equal('file with spaces', expand('%'))
258endfunc
259
260func Reset_arglist()
261  args a | %argd
262endfunc
263
264" Test for argc()
265func Test_argc()
266  call Reset_arglist()
267  call assert_equal(0, argc())
268  argadd a b
269  call assert_equal(2, argc())
270endfunc
271
272" Test for arglistid()
273func Test_arglistid()
274  call Reset_arglist()
275  arga a b
276  call assert_equal(0, arglistid())
277  split
278  arglocal
279  call assert_equal(1, arglistid())
280  tabnew | tabfirst
281  call assert_equal(0, arglistid(2))
282  call assert_equal(1, arglistid(1, 1))
283  call assert_equal(0, arglistid(2, 1))
284  call assert_equal(1, arglistid(1, 2))
285  tabonly | only | enew!
286  argglobal
287  call assert_equal(0, arglistid())
288endfunc
289
290" Tests for argv() and argc()
291func Test_argv()
292  call Reset_arglist()
293  call assert_equal([], argv())
294  call assert_equal("", argv(2))
295  call assert_equal(0, argc())
296  argadd a b c d
297  call assert_equal(4, argc())
298  call assert_equal('c', argv(2))
299
300  let w1_id = win_getid()
301  split
302  let w2_id = win_getid()
303  arglocal
304  args e f g
305  tabnew
306  let w3_id = win_getid()
307  split
308  let w4_id = win_getid()
309  argglobal
310  tabfirst
311  call assert_equal(4, argc(w1_id))
312  call assert_equal('b', argv(1, w1_id))
313  call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w1_id))
314
315  call assert_equal(3, argc(w2_id))
316  call assert_equal('f', argv(1, w2_id))
317  call assert_equal(['e', 'f', 'g'], argv(-1, w2_id))
318
319  call assert_equal(3, argc(w3_id))
320  call assert_equal('e', argv(0, w3_id))
321  call assert_equal(['e', 'f', 'g'], argv(-1, w3_id))
322
323  call assert_equal(4, argc(w4_id))
324  call assert_equal('c', argv(2, w4_id))
325  call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w4_id))
326
327  call assert_equal(4, argc(-1))
328  call assert_equal(3, argc())
329  call assert_equal('d', argv(3, -1))
330  call assert_equal(['a', 'b', 'c', 'd'], argv(-1, -1))
331  tabonly | only | enew!
332  " Negative test cases
333  call assert_equal(-1, argc(100))
334  call assert_equal('', argv(1, 100))
335  call assert_equal([], argv(-1, 100))
336  call assert_equal('', argv(10, -1))
337endfunc
338
339" Test for the :argedit command
340func Test_argedit()
341  call Reset_arglist()
342  argedit a
343  call assert_equal(['a'], argv())
344  call assert_equal('a', expand('%:t'))
345  argedit b
346  call assert_equal(['a', 'b'], argv())
347  call assert_equal('b', expand('%:t'))
348  argedit a
349  call assert_equal(['a', 'b', 'a'], argv())
350  call assert_equal('a', expand('%:t'))
351  " When file name case is ignored, an existing buffer with only case
352  " difference is re-used.
353  argedit C D
354  call assert_equal('C', expand('%:t'))
355  call assert_equal(['a', 'b', 'a', 'C', 'D'], argv())
356  argedit c
357  if has('fname_case')
358    call assert_equal(['a', 'b', 'a', 'C', 'c', 'D'], argv())
359  else
360    call assert_equal(['a', 'b', 'a', 'C', 'C', 'D'], argv())
361  endif
362  0argedit x
363  if has('fname_case')
364    call assert_equal(['x', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
365  else
366    call assert_equal(['x', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
367  endif
368  enew! | set modified
369  call assert_fails('argedit y', 'E37:')
370  argedit! y
371  if has('fname_case')
372    call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
373  else
374    call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
375  endif
376  %argd
377  bwipe! C
378  bwipe! D
379
380  " :argedit reuses the current buffer if it is empty
381  %argd
382  " make sure to use a new buffer number for x when it is loaded
383  bw! x
384  new
385  let a = bufnr('')
386  argedit x
387  call assert_equal(a, bufnr(''))
388  call assert_equal('x', bufname(''))
389  %argd
390  bw! x
391endfunc
392
393" Test for the :argdelete command
394func Test_argdelete()
395  call Reset_arglist()
396  args aa a aaa b bb
397  argdelete a*
398  call assert_equal(['b', 'bb'], argv())
399  call assert_equal('aa', expand('%:t'))
400  last
401  argdelete %
402  call assert_equal(['b'], argv())
403  call assert_fails('argdelete', 'E471:')
404  call assert_fails('1,100argdelete', 'E16:')
405  %argd
406endfunc
407
408func Test_argdelete_completion()
409  args foo bar
410
411  call feedkeys(":argdelete \<C-A>\<C-B>\"\<CR>", 'tx')
412  call assert_equal('"argdelete bar foo', @:)
413
414  call feedkeys(":argdelete x \<C-A>\<C-B>\"\<CR>", 'tx')
415  call assert_equal('"argdelete x bar foo', @:)
416
417  %argd
418endfunc
419
420" Tests for the :next, :prev, :first, :last, :rewind commands
421func Test_argpos()
422  call Reset_arglist()
423  args a b c d
424  last
425  call assert_equal(3, argidx())
426  call assert_fails('next', 'E165:')
427  prev
428  call assert_equal(2, argidx())
429  Next
430  call assert_equal(1, argidx())
431  first
432  call assert_equal(0, argidx())
433  call assert_fails('prev', 'E164:')
434  3next
435  call assert_equal(3, argidx())
436  rewind
437  call assert_equal(0, argidx())
438  %argd
439endfunc
440
441" Test for autocommand that redefines the argument list, when doing ":all".
442func Test_arglist_autocmd()
443  autocmd BufReadPost Xxx2 next Xxx2 Xxx1
444  call writefile(['test file Xxx1'], 'Xxx1')
445  call writefile(['test file Xxx2'], 'Xxx2')
446  call writefile(['test file Xxx3'], 'Xxx3')
447
448  new
449  " redefine arglist; go to Xxx1
450  next! Xxx1 Xxx2 Xxx3
451  " open window for all args
452  all
453  call assert_equal('test file Xxx1', getline(1))
454  wincmd w
455  wincmd w
456  call assert_equal('test file Xxx1', getline(1))
457  " should now be in Xxx2
458  rewind
459  call assert_equal('test file Xxx2', getline(1))
460
461  autocmd! BufReadPost Xxx2
462  enew! | only
463  call delete('Xxx1')
464  call delete('Xxx2')
465  call delete('Xxx3')
466  argdelete Xxx*
467  bwipe! Xxx1 Xxx2 Xxx3
468endfunc
469
470func Test_arg_all_expand()
471  call writefile(['test file Xxx1'], 'Xx x')
472  next notexist Xx\ x runtest.vim
473  call assert_equal('notexist Xx\ x runtest.vim', expand('##'))
474  call delete('Xx x')
475endfunc
476
477func Test_large_arg()
478  " Argument longer or equal to the number of columns used to cause
479  " access to invalid memory.
480  exe 'argadd ' .repeat('x', &columns)
481  args
482endfunc
483