blob: a6c71c9f5f4d80014501085e78faf156cd90b5ea [file] [log] [blame]
Bram Moolenaar72defda2016-01-17 18:04:33 +01001" 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())
Bram Moolenaar69a92fb2017-03-09 15:58:30 +01009 " doing it again doesn't result in an error
10 %argdelete
11 call assert_equal(0, argidx())
12 call assert_fails('2argdelete', 'E16:')
Bram Moolenaar72defda2016-01-17 18:04:33 +010013
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
Bram Moolenaara24f0a52016-01-17 19:39:00 +010027
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())
Bram Moolenaar2faa29f2016-01-23 23:02:34 +010064
65 %argd
66 edit d
67 arga
Bram Moolenaar398ee732017-08-03 14:29:14 +020068 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, ''))
Bram Moolenaar2faa29f2016-01-23 23:02:34 +010076
77 %argd
78 new
79 arga
Bram Moolenaar398ee732017-08-03 14:29:14 +020080 call assert_equal(0, len(argv()))
Bram Moolenaara24f0a52016-01-17 19:39:00 +010081endfunc
82
Bram Moolenaar32bbd002018-08-31 23:06:22 +020083func 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 %argd
98 bwipe!
99endfunc
100
Bram Moolenaara24f0a52016-01-17 19:39:00 +0100101func Init_abc()
102 args a b c
103 next
104endfunc
105
106func Assert_argc(l)
107 call assert_equal(len(a:l), argc())
108 let i = 0
109 while i < len(a:l) && i < argc()
110 call assert_equal(a:l[i], argv(i))
111 let i += 1
112 endwhile
113endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100114
115" Test for [count]argument and [count]argdelete commands
116" Ported from the test_argument_count.in test script
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100117func Test_argument()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100118 " Clean the argument list
119 arga a | %argd
120
121 let save_hidden = &hidden
122 set hidden
123
124 let g:buffers = []
125 augroup TEST
126 au BufEnter * call add(buffers, expand('%:t'))
127 augroup END
128
129 argadd a b c d
130 $argu
131 $-argu
132 -argu
133 1argu
134 +2argu
135
136 augroup TEST
137 au!
138 augroup END
139
140 call assert_equal(['d', 'c', 'b', 'a', 'c'], g:buffers)
141
142 redir => result
Bram Moolenaar5d69da42018-04-20 22:01:41 +0200143 args
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100144 redir END
Bram Moolenaar5d69da42018-04-20 22:01:41 +0200145 call assert_equal('a b [c] d', trim(result))
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100146
147 .argd
148 call assert_equal(['a', 'b', 'd'], argv())
149
150 -argd
151 call assert_equal(['a', 'd'], argv())
152
153 $argd
154 call assert_equal(['a'], argv())
155
156 1arga c
157 1arga b
158 $argu
159 $arga x
160 call assert_equal(['a', 'b', 'c', 'x'], argv())
161
Bram Moolenaar30141702016-01-19 14:14:08 +0100162 0arga y
163 call assert_equal(['y', 'a', 'b', 'c', 'x'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100164
165 %argd
166 call assert_equal([], argv())
167
168 arga a b c d e f
169 2,$-argd
170 call assert_equal(['a', 'f'], argv())
171
172 let &hidden = save_hidden
173
174 " Setting argument list should fail when the current buffer has unsaved
175 " changes
176 %argd
177 enew!
178 set modified
179 call assert_fails('args x y z', 'E37:')
180 args! x y z
181 call assert_equal(['x', 'y', 'z'], argv())
182 call assert_equal('x', expand('%:t'))
183
184 last | enew | argu
185 call assert_equal('z', expand('%:t'))
186
187 %argdelete
188 call assert_fails('argument', 'E163:')
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100189endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100190
Bram Moolenaar5d69da42018-04-20 22:01:41 +0200191func Test_list_arguments()
192 " Clean the argument list
193 arga a | %argd
194
195 " four args half the screen width makes two lines with two columns
196 let aarg = repeat('a', &columns / 2 - 4)
197 let barg = repeat('b', &columns / 2 - 4)
198 let carg = repeat('c', &columns / 2 - 4)
199 let darg = repeat('d', &columns / 2 - 4)
200 exe 'argadd ' aarg barg carg darg
201
202 redir => result
203 args
204 redir END
205 call assert_match('\[' . aarg . '] \+' . carg . '\n' . barg . ' \+' . darg, trim(result))
206
207 " if one arg is longer than half the screen make one column
208 exe 'argdel' aarg
209 let aarg = repeat('a', &columns / 2 + 2)
210 exe '0argadd' aarg
211 redir => result
212 args
213 redir END
214 call assert_match(aarg . '\n\[' . barg . ']\n' . carg . '\n' . darg, trim(result))
215
216 %argdelete
217endfunc
218
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100219" Test for 0argadd and 0argedit
220" Ported from the test_argument_0count.in test script
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100221func Test_zero_argadd()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100222 " Clean the argument list
223 arga a | %argd
224
225 arga a b c d
226 2argu
227 0arga added
228 call assert_equal(['added', 'a', 'b', 'c', 'd'], argv())
229
230 2argu
231 arga third
232 call assert_equal(['added', 'a', 'third', 'b', 'c', 'd'], argv())
233
234 %argd
235 arga a b c d
236 2argu
237 0arge edited
238 call assert_equal(['edited', 'a', 'b', 'c', 'd'], argv())
239
240 2argu
241 arga third
242 call assert_equal(['edited', 'a', 'third', 'b', 'c', 'd'], argv())
Bram Moolenaar90305c62017-07-16 15:31:17 +0200243
244 2argu
245 argedit file\ with\ spaces another file
246 call assert_equal(['edited', 'a', 'file with spaces', 'another', 'file', 'third', 'b', 'c', 'd'], argv())
247 call assert_equal('file with spaces', expand('%'))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100248endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100249
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100250func Reset_arglist()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100251 args a | %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100252endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100253
254" Test for argc()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100255func Test_argc()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100256 call Reset_arglist()
257 call assert_equal(0, argc())
258 argadd a b
259 call assert_equal(2, argc())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100260endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100261
262" Test for arglistid()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100263func Test_arglistid()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100264 call Reset_arglist()
265 arga a b
266 call assert_equal(0, arglistid())
267 split
268 arglocal
269 call assert_equal(1, arglistid())
270 tabnew | tabfirst
271 call assert_equal(0, arglistid(2))
272 call assert_equal(1, arglistid(1, 1))
273 call assert_equal(0, arglistid(2, 1))
274 call assert_equal(1, arglistid(1, 2))
275 tabonly | only | enew!
276 argglobal
277 call assert_equal(0, arglistid())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100278endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100279
280" Test for argv()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100281func Test_argv()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100282 call Reset_arglist()
283 call assert_equal([], argv())
284 call assert_equal("", argv(2))
285 argadd a b c d
286 call assert_equal('c', argv(2))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100287endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100288
289" Test for the :argedit command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100290func Test_argedit()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100291 call Reset_arglist()
292 argedit a
293 call assert_equal(['a'], argv())
294 call assert_equal('a', expand('%:t'))
295 argedit b
296 call assert_equal(['a', 'b'], argv())
297 call assert_equal('b', expand('%:t'))
298 argedit a
Bram Moolenaar90305c62017-07-16 15:31:17 +0200299 call assert_equal(['a', 'b', 'a'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100300 call assert_equal('a', expand('%:t'))
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200301 " When file name case is ignored, an existing buffer with only case
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200302 " difference is re-used.
Bram Moolenaar90305c62017-07-16 15:31:17 +0200303 argedit C D
304 call assert_equal('C', expand('%:t'))
305 call assert_equal(['a', 'b', 'a', 'C', 'D'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100306 argedit c
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200307 if has('fname_case')
308 call assert_equal(['a', 'b', 'a', 'C', 'c', 'D'], argv())
309 else
310 call assert_equal(['a', 'b', 'a', 'C', 'C', 'D'], argv())
311 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100312 0argedit x
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200313 if has('fname_case')
314 call assert_equal(['x', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
315 else
316 call assert_equal(['x', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
317 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100318 enew! | set modified
319 call assert_fails('argedit y', 'E37:')
320 argedit! y
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200321 if has('fname_case')
322 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
323 else
324 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
325 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100326 %argd
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200327 bwipe! C
328 bwipe! D
Bram Moolenaar46a53df2018-04-24 21:58:51 +0200329
330 " :argedit reuses the current buffer if it is empty
331 %argd
332 " make sure to use a new buffer number for x when it is loaded
333 bw! x
334 new
335 let a = bufnr('')
336 argedit x
337 call assert_equal(a, bufnr(''))
338 call assert_equal('x', bufname(''))
339 %argd
340 bw! x
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100341endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100342
343" Test for the :argdelete command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100344func Test_argdelete()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100345 call Reset_arglist()
346 args aa a aaa b bb
347 argdelete a*
348 call assert_equal(['b', 'bb'], argv())
349 call assert_equal('aa', expand('%:t'))
350 last
351 argdelete %
352 call assert_equal(['b'], argv())
353 call assert_fails('argdelete', 'E471:')
354 call assert_fails('1,100argdelete', 'E16:')
355 %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100356endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100357
358" Tests for the :next, :prev, :first, :last, :rewind commands
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100359func Test_argpos()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100360 call Reset_arglist()
361 args a b c d
362 last
363 call assert_equal(3, argidx())
364 call assert_fails('next', 'E165:')
365 prev
366 call assert_equal(2, argidx())
367 Next
368 call assert_equal(1, argidx())
369 first
370 call assert_equal(0, argidx())
371 call assert_fails('prev', 'E164:')
372 3next
373 call assert_equal(3, argidx())
374 rewind
375 call assert_equal(0, argidx())
376 %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100377endfunc
Bram Moolenaar53f16732016-09-07 20:46:39 +0200378
379" Test for autocommand that redefines the argument list, when doing ":all".
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100380func Test_arglist_autocmd()
Bram Moolenaar53f16732016-09-07 20:46:39 +0200381 autocmd BufReadPost Xxx2 next Xxx2 Xxx1
382 call writefile(['test file Xxx1'], 'Xxx1')
383 call writefile(['test file Xxx2'], 'Xxx2')
384 call writefile(['test file Xxx3'], 'Xxx3')
385
386 new
387 " redefine arglist; go to Xxx1
388 next! Xxx1 Xxx2 Xxx3
389 " open window for all args
390 all
391 call assert_equal('test file Xxx1', getline(1))
392 wincmd w
393 wincmd w
394 call assert_equal('test file Xxx1', getline(1))
395 " should now be in Xxx2
396 rewind
397 call assert_equal('test file Xxx2', getline(1))
398
399 autocmd! BufReadPost Xxx2
400 enew! | only
401 call delete('Xxx1')
402 call delete('Xxx2')
403 call delete('Xxx3')
404 argdelete Xxx*
405 bwipe! Xxx1 Xxx2 Xxx3
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100406endfunc
407
408func Test_arg_all_expand()
409 call writefile(['test file Xxx1'], 'Xx x')
410 next notexist Xx\ x runtest.vim
411 call assert_equal('notexist Xx\ x runtest.vim', expand('##'))
412 call delete('Xx x')
413endfunc