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