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