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