xref: /vim-8.2.3635/src/testdir/test_arglist.vim (revision acca8df9)
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  %argd
98  bwipe!
99endfunc
100
101func Init_abc()
102  args a b c
103  next
104endfunc
105
106func Assert_argc(l)
107  call assert_equal(len(a:l), argc())
108  let i = 0
109  while i < len(a:l) && i < argc()
110    call assert_equal(a:l[i], argv(i))
111    let i += 1
112  endwhile
113endfunc
114
115" Test for [count]argument and [count]argdelete commands
116" Ported from the test_argument_count.in test script
117func Test_argument()
118  " Clean the argument list
119  arga a | %argd
120
121  let save_hidden = &hidden
122  set hidden
123
124  let g:buffers = []
125  augroup TEST
126    au BufEnter * call add(buffers, expand('%:t'))
127  augroup END
128
129  argadd a b c d
130  $argu
131  $-argu
132  -argu
133  1argu
134  +2argu
135
136  augroup TEST
137    au!
138  augroup END
139
140  call assert_equal(['d', 'c', 'b', 'a', 'c'], g:buffers)
141
142  redir => result
143  args
144  redir END
145  call assert_equal('a   b   [c] d', trim(result))
146
147  .argd
148  call assert_equal(['a', 'b', 'd'], argv())
149
150  -argd
151  call assert_equal(['a', 'd'], argv())
152
153  $argd
154  call assert_equal(['a'], argv())
155
156  1arga c
157  1arga b
158  $argu
159  $arga x
160  call assert_equal(['a', 'b', 'c', 'x'], argv())
161
162  0arga y
163  call assert_equal(['y', 'a', 'b', 'c', 'x'], argv())
164
165  %argd
166  call assert_equal([], argv())
167
168  arga a b c d e f
169  2,$-argd
170  call assert_equal(['a', 'f'], argv())
171
172  let &hidden = save_hidden
173
174  " Setting argument list should fail when the current buffer has unsaved
175  " changes
176  %argd
177  enew!
178  set modified
179  call assert_fails('args x y z', 'E37:')
180  args! x y z
181  call assert_equal(['x', 'y', 'z'], argv())
182  call assert_equal('x', expand('%:t'))
183
184  last | enew | argu
185  call assert_equal('z', expand('%:t'))
186
187  %argdelete
188  call assert_fails('argument', 'E163:')
189endfunc
190
191func Test_list_arguments()
192  " Clean the argument list
193  arga a | %argd
194
195  " four args half the screen width makes two lines with two columns
196  let aarg = repeat('a', &columns / 2 - 4)
197  let barg = repeat('b', &columns / 2 - 4)
198  let carg = repeat('c', &columns / 2 - 4)
199  let darg = repeat('d', &columns / 2 - 4)
200  exe 'argadd ' aarg barg carg darg
201
202  redir => result
203  args
204  redir END
205  call assert_match('\[' . aarg . '] \+' . carg . '\n' . barg . ' \+' . darg, trim(result))
206
207  " if one arg is longer than half the screen make one column
208  exe 'argdel' aarg
209  let aarg = repeat('a', &columns / 2 + 2)
210  exe '0argadd' aarg
211  redir => result
212  args
213  redir END
214  call assert_match(aarg . '\n\[' . barg . ']\n' . carg . '\n' . darg, trim(result))
215
216  %argdelete
217endfunc
218
219" Test for 0argadd and 0argedit
220" Ported from the test_argument_0count.in test script
221func Test_zero_argadd()
222  " Clean the argument list
223  arga a | %argd
224
225  arga a b c d
226  2argu
227  0arga added
228  call assert_equal(['added', 'a', 'b', 'c', 'd'], argv())
229
230  2argu
231  arga third
232  call assert_equal(['added', 'a', 'third', 'b', 'c', 'd'], argv())
233
234  %argd
235  arga a b c d
236  2argu
237  0arge edited
238  call assert_equal(['edited', 'a', 'b', 'c', 'd'], argv())
239
240  2argu
241  arga third
242  call assert_equal(['edited', 'a', 'third', 'b', 'c', 'd'], argv())
243
244  2argu
245  argedit file\ with\ spaces another file
246  call assert_equal(['edited', 'a', 'file with spaces', 'another', 'file', 'third', 'b', 'c', 'd'], argv())
247  call assert_equal('file with spaces', expand('%'))
248endfunc
249
250func Reset_arglist()
251  args a | %argd
252endfunc
253
254" Test for argc()
255func Test_argc()
256  call Reset_arglist()
257  call assert_equal(0, argc())
258  argadd a b
259  call assert_equal(2, argc())
260endfunc
261
262" Test for arglistid()
263func Test_arglistid()
264  call Reset_arglist()
265  arga a b
266  call assert_equal(0, arglistid())
267  split
268  arglocal
269  call assert_equal(1, arglistid())
270  tabnew | tabfirst
271  call assert_equal(0, arglistid(2))
272  call assert_equal(1, arglistid(1, 1))
273  call assert_equal(0, arglistid(2, 1))
274  call assert_equal(1, arglistid(1, 2))
275  tabonly | only | enew!
276  argglobal
277  call assert_equal(0, arglistid())
278endfunc
279
280" Test for argv()
281func Test_argv()
282  call Reset_arglist()
283  call assert_equal([], argv())
284  call assert_equal("", argv(2))
285  argadd a b c d
286  call assert_equal('c', argv(2))
287endfunc
288
289" Test for the :argedit command
290func Test_argedit()
291  call Reset_arglist()
292  argedit a
293  call assert_equal(['a'], argv())
294  call assert_equal('a', expand('%:t'))
295  argedit b
296  call assert_equal(['a', 'b'], argv())
297  call assert_equal('b', expand('%:t'))
298  argedit a
299  call assert_equal(['a', 'b', 'a'], argv())
300  call assert_equal('a', expand('%:t'))
301  " When file name case is ignored, an existing buffer with only case
302  " difference is re-used.
303  argedit C D
304  call assert_equal('C', expand('%:t'))
305  call assert_equal(['a', 'b', 'a', 'C', 'D'], argv())
306  argedit c
307  if has('fname_case')
308    call assert_equal(['a', 'b', 'a', 'C', 'c', 'D'], argv())
309  else
310    call assert_equal(['a', 'b', 'a', 'C', 'C', 'D'], argv())
311  endif
312  0argedit x
313  if has('fname_case')
314    call assert_equal(['x', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
315  else
316    call assert_equal(['x', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
317  endif
318  enew! | set modified
319  call assert_fails('argedit y', 'E37:')
320  argedit! y
321  if has('fname_case')
322    call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
323  else
324    call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
325  endif
326  %argd
327  bwipe! C
328  bwipe! D
329
330  " :argedit reuses the current buffer if it is empty
331  %argd
332  " make sure to use a new buffer number for x when it is loaded
333  bw! x
334  new
335  let a = bufnr('')
336  argedit x
337  call assert_equal(a, bufnr(''))
338  call assert_equal('x', bufname(''))
339  %argd
340  bw! x
341endfunc
342
343" Test for the :argdelete command
344func Test_argdelete()
345  call Reset_arglist()
346  args aa a aaa b bb
347  argdelete a*
348  call assert_equal(['b', 'bb'], argv())
349  call assert_equal('aa', expand('%:t'))
350  last
351  argdelete %
352  call assert_equal(['b'], argv())
353  call assert_fails('argdelete', 'E471:')
354  call assert_fails('1,100argdelete', 'E16:')
355  %argd
356endfunc
357
358" Tests for the :next, :prev, :first, :last, :rewind commands
359func Test_argpos()
360  call Reset_arglist()
361  args a b c d
362  last
363  call assert_equal(3, argidx())
364  call assert_fails('next', 'E165:')
365  prev
366  call assert_equal(2, argidx())
367  Next
368  call assert_equal(1, argidx())
369  first
370  call assert_equal(0, argidx())
371  call assert_fails('prev', 'E164:')
372  3next
373  call assert_equal(3, argidx())
374  rewind
375  call assert_equal(0, argidx())
376  %argd
377endfunc
378
379" Test for autocommand that redefines the argument list, when doing ":all".
380func Test_arglist_autocmd()
381  autocmd BufReadPost Xxx2 next Xxx2 Xxx1
382  call writefile(['test file Xxx1'], 'Xxx1')
383  call writefile(['test file Xxx2'], 'Xxx2')
384  call writefile(['test file Xxx3'], 'Xxx3')
385
386  new
387  " redefine arglist; go to Xxx1
388  next! Xxx1 Xxx2 Xxx3
389  " open window for all args
390  all
391  call assert_equal('test file Xxx1', getline(1))
392  wincmd w
393  wincmd w
394  call assert_equal('test file Xxx1', getline(1))
395  " should now be in Xxx2
396  rewind
397  call assert_equal('test file Xxx2', getline(1))
398
399  autocmd! BufReadPost Xxx2
400  enew! | only
401  call delete('Xxx1')
402  call delete('Xxx2')
403  call delete('Xxx3')
404  argdelete Xxx*
405  bwipe! Xxx1 Xxx2 Xxx3
406endfunc
407
408func Test_arg_all_expand()
409  call writefile(['test file Xxx1'], 'Xx x')
410  next notexist Xx\ x runtest.vim
411  call assert_equal('notexist Xx\ x runtest.vim', expand('##'))
412  call delete('Xx x')
413endfunc
414