xref: /vim-8.2.3635/src/testdir/test_arglist.vim (revision bb76f24a)
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
10  args a b c
11  call assert_equal(0, argidx())
12  next
13  call assert_equal(1, argidx())
14  next
15  call assert_equal(2, argidx())
16  1argdelete
17  call assert_equal(1, argidx())
18  1argdelete
19  call assert_equal(0, argidx())
20  1argdelete
21  call assert_equal(0, argidx())
22endfunc
23
24func Test_argadd()
25  %argdelete
26  argadd a b c
27  call assert_equal(0, argidx())
28
29  %argdelete
30  argadd a
31  call assert_equal(0, argidx())
32  argadd b c d
33  call assert_equal(0, argidx())
34
35  call Init_abc()
36  argadd x
37  call Assert_argc(['a', 'b', 'x', 'c'])
38  call assert_equal(1, argidx())
39
40  call Init_abc()
41  0argadd x
42  call Assert_argc(['x', 'a', 'b', 'c'])
43  call assert_equal(2, argidx())
44
45  call Init_abc()
46  1argadd x
47  call Assert_argc(['a', 'x', 'b', 'c'])
48  call assert_equal(2, argidx())
49
50  call Init_abc()
51  $argadd x
52  call Assert_argc(['a', 'b', 'c', 'x'])
53  call assert_equal(1, argidx())
54
55  call Init_abc()
56  $argadd x
57  +2argadd y
58  call Assert_argc(['a', 'b', 'c', 'x', 'y'])
59  call assert_equal(1, argidx())
60
61  %argd
62  edit d
63  arga
64  call assert_equal(len(argv()), 1)
65  call assert_equal(get(argv(), 0, ''), 'd')
66
67  %argd
68  new
69  arga
70  call assert_equal(len(argv()), 0)
71endfunc
72
73func Init_abc()
74  args a b c
75  next
76endfunc
77
78func Assert_argc(l)
79  call assert_equal(len(a:l), argc())
80  let i = 0
81  while i < len(a:l) && i < argc()
82    call assert_equal(a:l[i], argv(i))
83    let i += 1
84  endwhile
85endfunc
86
87" Test for [count]argument and [count]argdelete commands
88" Ported from the test_argument_count.in test script
89function Test_argument()
90  " Clean the argument list
91  arga a | %argd
92
93  let save_hidden = &hidden
94  set hidden
95
96  let g:buffers = []
97  augroup TEST
98    au BufEnter * call add(buffers, expand('%:t'))
99  augroup END
100
101  argadd a b c d
102  $argu
103  $-argu
104  -argu
105  1argu
106  +2argu
107
108  augroup TEST
109    au!
110  augroup END
111
112  call assert_equal(['d', 'c', 'b', 'a', 'c'], g:buffers)
113
114  redir => result
115  ar
116  redir END
117  call assert_true(result =~# 'a b \[c] d')
118
119  .argd
120  call assert_equal(['a', 'b', 'd'], argv())
121
122  -argd
123  call assert_equal(['a', 'd'], argv())
124
125  $argd
126  call assert_equal(['a'], argv())
127
128  1arga c
129  1arga b
130  $argu
131  $arga x
132  call assert_equal(['a', 'b', 'c', 'x'], argv())
133
134  0arga y
135  call assert_equal(['y', 'a', 'b', 'c', 'x'], argv())
136
137  %argd
138  call assert_equal([], argv())
139
140  arga a b c d e f
141  2,$-argd
142  call assert_equal(['a', 'f'], argv())
143
144  let &hidden = save_hidden
145
146  " Setting argument list should fail when the current buffer has unsaved
147  " changes
148  %argd
149  enew!
150  set modified
151  call assert_fails('args x y z', 'E37:')
152  args! x y z
153  call assert_equal(['x', 'y', 'z'], argv())
154  call assert_equal('x', expand('%:t'))
155
156  last | enew | argu
157  call assert_equal('z', expand('%:t'))
158
159  %argdelete
160  call assert_fails('argument', 'E163:')
161endfunction
162
163" Test for 0argadd and 0argedit
164" Ported from the test_argument_0count.in test script
165function Test_zero_argadd()
166  " Clean the argument list
167  arga a | %argd
168
169  arga a b c d
170  2argu
171  0arga added
172  call assert_equal(['added', 'a', 'b', 'c', 'd'], argv())
173
174  2argu
175  arga third
176  call assert_equal(['added', 'a', 'third', 'b', 'c', 'd'], argv())
177
178  %argd
179  arga a b c d
180  2argu
181  0arge edited
182  call assert_equal(['edited', 'a', 'b', 'c', 'd'], argv())
183
184  2argu
185  arga third
186  call assert_equal(['edited', 'a', 'third', 'b', 'c', 'd'], argv())
187endfunction
188
189function Reset_arglist()
190  args a | %argd
191endfunction
192
193" Test for argc()
194function Test_argc()
195  call Reset_arglist()
196  call assert_equal(0, argc())
197  argadd a b
198  call assert_equal(2, argc())
199endfunction
200
201" Test for arglistid()
202function Test_arglistid()
203  call Reset_arglist()
204  arga a b
205  call assert_equal(0, arglistid())
206  split
207  arglocal
208  call assert_equal(1, arglistid())
209  tabnew | tabfirst
210  call assert_equal(0, arglistid(2))
211  call assert_equal(1, arglistid(1, 1))
212  call assert_equal(0, arglistid(2, 1))
213  call assert_equal(1, arglistid(1, 2))
214  tabonly | only | enew!
215  argglobal
216  call assert_equal(0, arglistid())
217endfunction
218
219" Test for argv()
220function Test_argv()
221  call Reset_arglist()
222  call assert_equal([], argv())
223  call assert_equal("", argv(2))
224  argadd a b c d
225  call assert_equal('c', argv(2))
226endfunction
227
228" Test for the :argedit command
229function Test_argedit()
230  call Reset_arglist()
231  argedit a
232  call assert_equal(['a'], argv())
233  call assert_equal('a', expand('%:t'))
234  argedit b
235  call assert_equal(['a', 'b'], argv())
236  call assert_equal('b', expand('%:t'))
237  argedit a
238  call assert_equal(['a', 'b'], argv())
239  call assert_equal('a', expand('%:t'))
240  if has('unix')
241    " on MS-Windows this would edit file "a b"
242    call assert_fails('argedit a b', 'E172:')
243  endif
244  argedit c
245  call assert_equal(['a', 'c', 'b'], argv())
246  0argedit x
247  call assert_equal(['x', 'a', 'c', 'b'], argv())
248  enew! | set modified
249  call assert_fails('argedit y', 'E37:')
250  argedit! y
251  call assert_equal(['x', 'y', 'a', 'c', 'b'], argv())
252  %argd
253endfunction
254
255" Test for the :argdelete command
256function Test_argdelete()
257  call Reset_arglist()
258  args aa a aaa b bb
259  argdelete a*
260  call assert_equal(['b', 'bb'], argv())
261  call assert_equal('aa', expand('%:t'))
262  last
263  argdelete %
264  call assert_equal(['b'], argv())
265  call assert_fails('argdelete', 'E471:')
266  call assert_fails('1,100argdelete', 'E16:')
267  %argd
268endfunction
269
270" Tests for the :next, :prev, :first, :last, :rewind commands
271function Test_argpos()
272  call Reset_arglist()
273  args a b c d
274  last
275  call assert_equal(3, argidx())
276  call assert_fails('next', 'E165:')
277  prev
278  call assert_equal(2, argidx())
279  Next
280  call assert_equal(1, argidx())
281  first
282  call assert_equal(0, argidx())
283  call assert_fails('prev', 'E164:')
284  3next
285  call assert_equal(3, argidx())
286  rewind
287  call assert_equal(0, argidx())
288  %argd
289endfunction
290
291" Test for autocommand that redefines the argument list, when doing ":all".
292function Test_arglist_autocmd()
293  autocmd BufReadPost Xxx2 next Xxx2 Xxx1
294  call writefile(['test file Xxx1'], 'Xxx1')
295  call writefile(['test file Xxx2'], 'Xxx2')
296  call writefile(['test file Xxx3'], 'Xxx3')
297
298  new
299  " redefine arglist; go to Xxx1
300  next! Xxx1 Xxx2 Xxx3
301  " open window for all args
302  all
303  call assert_equal('test file Xxx1', getline(1))
304  wincmd w
305  wincmd w
306  call assert_equal('test file Xxx1', getline(1))
307  " should now be in Xxx2
308  rewind
309  call assert_equal('test file Xxx2', getline(1))
310
311  autocmd! BufReadPost Xxx2
312  enew! | only
313  call delete('Xxx1')
314  call delete('Xxx2')
315  call delete('Xxx3')
316  argdelete Xxx*
317  bwipe! Xxx1 Xxx2 Xxx3
318endfunction
319