xref: /vim-8.2.3635/src/testdir/test_arglist.vim (revision cd5c8f82)
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())
191endfunc
192
193func Reset_arglist()
194  args a | %argd
195endfunc
196
197" Test for argc()
198func Test_argc()
199  call Reset_arglist()
200  call assert_equal(0, argc())
201  argadd a b
202  call assert_equal(2, argc())
203endfunc
204
205" Test for arglistid()
206func Test_arglistid()
207  call Reset_arglist()
208  arga a b
209  call assert_equal(0, arglistid())
210  split
211  arglocal
212  call assert_equal(1, arglistid())
213  tabnew | tabfirst
214  call assert_equal(0, arglistid(2))
215  call assert_equal(1, arglistid(1, 1))
216  call assert_equal(0, arglistid(2, 1))
217  call assert_equal(1, arglistid(1, 2))
218  tabonly | only | enew!
219  argglobal
220  call assert_equal(0, arglistid())
221endfunc
222
223" Test for argv()
224func Test_argv()
225  call Reset_arglist()
226  call assert_equal([], argv())
227  call assert_equal("", argv(2))
228  argadd a b c d
229  call assert_equal('c', argv(2))
230endfunc
231
232" Test for the :argedit command
233func Test_argedit()
234  call Reset_arglist()
235  argedit a
236  call assert_equal(['a'], argv())
237  call assert_equal('a', expand('%:t'))
238  argedit b
239  call assert_equal(['a', 'b'], argv())
240  call assert_equal('b', expand('%:t'))
241  argedit a
242  call assert_equal(['a', 'b'], argv())
243  call assert_equal('a', expand('%:t'))
244  if has('unix')
245    " on MS-Windows this would edit file "a b"
246    call assert_fails('argedit a b', 'E172:')
247  endif
248  argedit c
249  call assert_equal(['a', 'c', 'b'], argv())
250  0argedit x
251  call assert_equal(['x', 'a', 'c', 'b'], argv())
252  enew! | set modified
253  call assert_fails('argedit y', 'E37:')
254  argedit! y
255  call assert_equal(['x', 'y', 'a', 'c', 'b'], argv())
256  %argd
257endfunc
258
259" Test for the :argdelete command
260func Test_argdelete()
261  call Reset_arglist()
262  args aa a aaa b bb
263  argdelete a*
264  call assert_equal(['b', 'bb'], argv())
265  call assert_equal('aa', expand('%:t'))
266  last
267  argdelete %
268  call assert_equal(['b'], argv())
269  call assert_fails('argdelete', 'E471:')
270  call assert_fails('1,100argdelete', 'E16:')
271  %argd
272endfunc
273
274" Tests for the :next, :prev, :first, :last, :rewind commands
275func Test_argpos()
276  call Reset_arglist()
277  args a b c d
278  last
279  call assert_equal(3, argidx())
280  call assert_fails('next', 'E165:')
281  prev
282  call assert_equal(2, argidx())
283  Next
284  call assert_equal(1, argidx())
285  first
286  call assert_equal(0, argidx())
287  call assert_fails('prev', 'E164:')
288  3next
289  call assert_equal(3, argidx())
290  rewind
291  call assert_equal(0, argidx())
292  %argd
293endfunc
294
295" Test for autocommand that redefines the argument list, when doing ":all".
296func Test_arglist_autocmd()
297  autocmd BufReadPost Xxx2 next Xxx2 Xxx1
298  call writefile(['test file Xxx1'], 'Xxx1')
299  call writefile(['test file Xxx2'], 'Xxx2')
300  call writefile(['test file Xxx3'], 'Xxx3')
301
302  new
303  " redefine arglist; go to Xxx1
304  next! Xxx1 Xxx2 Xxx3
305  " open window for all args
306  all
307  call assert_equal('test file Xxx1', getline(1))
308  wincmd w
309  wincmd w
310  call assert_equal('test file Xxx1', getline(1))
311  " should now be in Xxx2
312  rewind
313  call assert_equal('test file Xxx2', getline(1))
314
315  autocmd! BufReadPost Xxx2
316  enew! | only
317  call delete('Xxx1')
318  call delete('Xxx2')
319  call delete('Xxx3')
320  argdelete Xxx*
321  bwipe! Xxx1 Xxx2 Xxx3
322endfunc
323
324func Test_arg_all_expand()
325  call writefile(['test file Xxx1'], 'Xx x')
326  next notexist Xx\ x runtest.vim
327  call assert_equal('notexist Xx\ x runtest.vim', expand('##'))
328  call delete('Xx x')
329endfunc
330