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