xref: /vim-8.2.3635/src/testdir/test_arglist.vim (revision bf9679ae)
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" Test for argv()
282func Test_argv()
283  call Reset_arglist()
284  call assert_equal([], argv())
285  call assert_equal("", argv(2))
286  argadd a b c d
287  call assert_equal('c', argv(2))
288endfunc
289
290" Test for the :argedit command
291func Test_argedit()
292  call Reset_arglist()
293  argedit a
294  call assert_equal(['a'], argv())
295  call assert_equal('a', expand('%:t'))
296  argedit b
297  call assert_equal(['a', 'b'], argv())
298  call assert_equal('b', expand('%:t'))
299  argedit a
300  call assert_equal(['a', 'b', 'a'], argv())
301  call assert_equal('a', expand('%:t'))
302  " When file name case is ignored, an existing buffer with only case
303  " difference is re-used.
304  argedit C D
305  call assert_equal('C', expand('%:t'))
306  call assert_equal(['a', 'b', 'a', 'C', 'D'], argv())
307  argedit c
308  if has('fname_case')
309    call assert_equal(['a', 'b', 'a', 'C', 'c', 'D'], argv())
310  else
311    call assert_equal(['a', 'b', 'a', 'C', 'C', 'D'], argv())
312  endif
313  0argedit x
314  if has('fname_case')
315    call assert_equal(['x', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
316  else
317    call assert_equal(['x', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
318  endif
319  enew! | set modified
320  call assert_fails('argedit y', 'E37:')
321  argedit! y
322  if has('fname_case')
323    call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
324  else
325    call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
326  endif
327  %argd
328  bwipe! C
329  bwipe! D
330
331  " :argedit reuses the current buffer if it is empty
332  %argd
333  " make sure to use a new buffer number for x when it is loaded
334  bw! x
335  new
336  let a = bufnr('')
337  argedit x
338  call assert_equal(a, bufnr(''))
339  call assert_equal('x', bufname(''))
340  %argd
341  bw! x
342endfunc
343
344" Test for the :argdelete command
345func Test_argdelete()
346  call Reset_arglist()
347  args aa a aaa b bb
348  argdelete a*
349  call assert_equal(['b', 'bb'], argv())
350  call assert_equal('aa', expand('%:t'))
351  last
352  argdelete %
353  call assert_equal(['b'], argv())
354  call assert_fails('argdelete', 'E471:')
355  call assert_fails('1,100argdelete', 'E16:')
356  %argd
357endfunc
358
359" Tests for the :next, :prev, :first, :last, :rewind commands
360func Test_argpos()
361  call Reset_arglist()
362  args a b c d
363  last
364  call assert_equal(3, argidx())
365  call assert_fails('next', 'E165:')
366  prev
367  call assert_equal(2, argidx())
368  Next
369  call assert_equal(1, argidx())
370  first
371  call assert_equal(0, argidx())
372  call assert_fails('prev', 'E164:')
373  3next
374  call assert_equal(3, argidx())
375  rewind
376  call assert_equal(0, argidx())
377  %argd
378endfunc
379
380" Test for autocommand that redefines the argument list, when doing ":all".
381func Test_arglist_autocmd()
382  autocmd BufReadPost Xxx2 next Xxx2 Xxx1
383  call writefile(['test file Xxx1'], 'Xxx1')
384  call writefile(['test file Xxx2'], 'Xxx2')
385  call writefile(['test file Xxx3'], 'Xxx3')
386
387  new
388  " redefine arglist; go to Xxx1
389  next! Xxx1 Xxx2 Xxx3
390  " open window for all args
391  all
392  call assert_equal('test file Xxx1', getline(1))
393  wincmd w
394  wincmd w
395  call assert_equal('test file Xxx1', getline(1))
396  " should now be in Xxx2
397  rewind
398  call assert_equal('test file Xxx2', getline(1))
399
400  autocmd! BufReadPost Xxx2
401  enew! | only
402  call delete('Xxx1')
403  call delete('Xxx2')
404  call delete('Xxx3')
405  argdelete Xxx*
406  bwipe! Xxx1 Xxx2 Xxx3
407endfunc
408
409func Test_arg_all_expand()
410  call writefile(['test file Xxx1'], 'Xx x')
411  next notexist Xx\ x runtest.vim
412  call assert_equal('notexist Xx\ x runtest.vim', expand('##'))
413  call delete('Xx x')
414endfunc
415
416func Test_large_arg()
417  " Argument longer or equal to the number of columns used to cause
418  " access to invalid memory.
419  exe 'argadd ' .repeat('x', &columns)
420  args
421endfunc
422