blob: 441373beae93b637eed5528251dab335cbd2fd99 [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
Bram Moolenaard3398282018-09-24 21:32:11 +020097 call delete('Xargadd')
Bram Moolenaar32bbd002018-08-31 23:06:22 +020098 %argd
99 bwipe!
100endfunc
101
Bram Moolenaara24f0a52016-01-17 19:39:00 +0100102func Init_abc()
103 args a b c
104 next
105endfunc
106
107func Assert_argc(l)
108 call assert_equal(len(a:l), argc())
109 let i = 0
110 while i < len(a:l) && i < argc()
111 call assert_equal(a:l[i], argv(i))
112 let i += 1
113 endwhile
114endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100115
116" Test for [count]argument and [count]argdelete commands
117" Ported from the test_argument_count.in test script
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100118func Test_argument()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100119 " Clean the argument list
120 arga a | %argd
121
122 let save_hidden = &hidden
123 set hidden
124
125 let g:buffers = []
126 augroup TEST
127 au BufEnter * call add(buffers, expand('%:t'))
128 augroup END
129
130 argadd a b c d
131 $argu
132 $-argu
133 -argu
134 1argu
135 +2argu
136
137 augroup TEST
138 au!
139 augroup END
140
141 call assert_equal(['d', 'c', 'b', 'a', 'c'], g:buffers)
142
Bram Moolenaar949f1982019-07-23 23:00:08 +0200143 call assert_equal("\na b [c] d ", execute(':args'))
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100144
145 .argd
146 call assert_equal(['a', 'b', 'd'], argv())
147
148 -argd
149 call assert_equal(['a', 'd'], argv())
150
151 $argd
152 call assert_equal(['a'], argv())
153
154 1arga c
155 1arga b
156 $argu
157 $arga x
158 call assert_equal(['a', 'b', 'c', 'x'], argv())
159
Bram Moolenaar30141702016-01-19 14:14:08 +0100160 0arga y
161 call assert_equal(['y', 'a', 'b', 'c', 'x'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100162
163 %argd
164 call assert_equal([], argv())
165
166 arga a b c d e f
167 2,$-argd
168 call assert_equal(['a', 'f'], argv())
169
170 let &hidden = save_hidden
171
172 " Setting argument list should fail when the current buffer has unsaved
173 " changes
174 %argd
175 enew!
176 set modified
177 call assert_fails('args x y z', 'E37:')
178 args! x y z
179 call assert_equal(['x', 'y', 'z'], argv())
180 call assert_equal('x', expand('%:t'))
181
182 last | enew | argu
183 call assert_equal('z', expand('%:t'))
184
185 %argdelete
186 call assert_fails('argument', 'E163:')
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100187endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100188
Bram Moolenaar5d69da42018-04-20 22:01:41 +0200189func Test_list_arguments()
190 " Clean the argument list
191 arga a | %argd
192
193 " four args half the screen width makes two lines with two columns
194 let aarg = repeat('a', &columns / 2 - 4)
195 let barg = repeat('b', &columns / 2 - 4)
196 let carg = repeat('c', &columns / 2 - 4)
197 let darg = repeat('d', &columns / 2 - 4)
198 exe 'argadd ' aarg barg carg darg
199
200 redir => result
201 args
202 redir END
203 call assert_match('\[' . aarg . '] \+' . carg . '\n' . barg . ' \+' . darg, trim(result))
204
205 " if one arg is longer than half the screen make one column
206 exe 'argdel' aarg
207 let aarg = repeat('a', &columns / 2 + 2)
208 exe '0argadd' aarg
209 redir => result
210 args
211 redir END
212 call assert_match(aarg . '\n\[' . barg . ']\n' . carg . '\n' . darg, trim(result))
213
214 %argdelete
215endfunc
216
Bram Moolenaar2ac372c2018-12-28 19:06:47 +0100217func Test_args_with_quote()
Bram Moolenaar3de8c2d2018-12-28 19:29:35 +0100218 " Only on Unix can a file name include a double quote.
219 if has('unix')
220 args \"foobar
221 call assert_equal('"foobar', argv(0))
222 %argdelete
223 endif
Bram Moolenaar2ac372c2018-12-28 19:06:47 +0100224endfunc
225
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100226" Test for 0argadd and 0argedit
227" Ported from the test_argument_0count.in test script
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100228func Test_zero_argadd()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100229 " Clean the argument list
230 arga a | %argd
231
232 arga a b c d
233 2argu
234 0arga added
235 call assert_equal(['added', 'a', 'b', 'c', 'd'], argv())
236
237 2argu
238 arga third
239 call assert_equal(['added', 'a', 'third', 'b', 'c', 'd'], argv())
240
241 %argd
242 arga a b c d
243 2argu
244 0arge edited
245 call assert_equal(['edited', 'a', 'b', 'c', 'd'], argv())
246
247 2argu
248 arga third
249 call assert_equal(['edited', 'a', 'third', 'b', 'c', 'd'], argv())
Bram Moolenaar90305c62017-07-16 15:31:17 +0200250
251 2argu
252 argedit file\ with\ spaces another file
253 call assert_equal(['edited', 'a', 'file with spaces', 'another', 'file', 'third', 'b', 'c', 'd'], argv())
254 call assert_equal('file with spaces', expand('%'))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100255endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100256
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100257func Reset_arglist()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100258 args a | %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100259endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100260
261" Test for argc()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100262func Test_argc()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100263 call Reset_arglist()
264 call assert_equal(0, argc())
265 argadd a b
266 call assert_equal(2, argc())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100267endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100268
269" Test for arglistid()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100270func Test_arglistid()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100271 call Reset_arglist()
272 arga a b
273 call assert_equal(0, arglistid())
274 split
275 arglocal
276 call assert_equal(1, arglistid())
277 tabnew | tabfirst
278 call assert_equal(0, arglistid(2))
279 call assert_equal(1, arglistid(1, 1))
280 call assert_equal(0, arglistid(2, 1))
281 call assert_equal(1, arglistid(1, 2))
282 tabonly | only | enew!
283 argglobal
284 call assert_equal(0, arglistid())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100285endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100286
Bram Moolenaare6e39892018-10-25 12:32:11 +0200287" Tests for argv() and argc()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100288func Test_argv()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100289 call Reset_arglist()
290 call assert_equal([], argv())
291 call assert_equal("", argv(2))
Bram Moolenaare6e39892018-10-25 12:32:11 +0200292 call assert_equal(0, argc())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100293 argadd a b c d
Bram Moolenaare6e39892018-10-25 12:32:11 +0200294 call assert_equal(4, argc())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100295 call assert_equal('c', argv(2))
Bram Moolenaare6e39892018-10-25 12:32:11 +0200296
297 let w1_id = win_getid()
298 split
299 let w2_id = win_getid()
300 arglocal
301 args e f g
302 tabnew
303 let w3_id = win_getid()
304 split
305 let w4_id = win_getid()
306 argglobal
307 tabfirst
308 call assert_equal(4, argc(w1_id))
309 call assert_equal('b', argv(1, w1_id))
310 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w1_id))
311
312 call assert_equal(3, argc(w2_id))
313 call assert_equal('f', argv(1, w2_id))
314 call assert_equal(['e', 'f', 'g'], argv(-1, w2_id))
315
316 call assert_equal(3, argc(w3_id))
317 call assert_equal('e', argv(0, w3_id))
318 call assert_equal(['e', 'f', 'g'], argv(-1, w3_id))
319
320 call assert_equal(4, argc(w4_id))
321 call assert_equal('c', argv(2, w4_id))
322 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w4_id))
323
324 call assert_equal(4, argc(-1))
325 call assert_equal(3, argc())
326 call assert_equal('d', argv(3, -1))
327 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, -1))
328 tabonly | only | enew!
329 " Negative test cases
330 call assert_equal(-1, argc(100))
331 call assert_equal('', argv(1, 100))
332 call assert_equal([], argv(-1, 100))
333 call assert_equal('', argv(10, -1))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100334endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100335
336" Test for the :argedit command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100337func Test_argedit()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100338 call Reset_arglist()
339 argedit a
340 call assert_equal(['a'], argv())
341 call assert_equal('a', expand('%:t'))
342 argedit b
343 call assert_equal(['a', 'b'], argv())
344 call assert_equal('b', expand('%:t'))
345 argedit a
Bram Moolenaar90305c62017-07-16 15:31:17 +0200346 call assert_equal(['a', 'b', 'a'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100347 call assert_equal('a', expand('%:t'))
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200348 " When file name case is ignored, an existing buffer with only case
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200349 " difference is re-used.
Bram Moolenaar90305c62017-07-16 15:31:17 +0200350 argedit C D
351 call assert_equal('C', expand('%:t'))
352 call assert_equal(['a', 'b', 'a', 'C', 'D'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100353 argedit c
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200354 if has('fname_case')
355 call assert_equal(['a', 'b', 'a', 'C', 'c', 'D'], argv())
356 else
357 call assert_equal(['a', 'b', 'a', 'C', 'C', 'D'], argv())
358 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100359 0argedit x
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200360 if has('fname_case')
361 call assert_equal(['x', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
362 else
363 call assert_equal(['x', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
364 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100365 enew! | set modified
366 call assert_fails('argedit y', 'E37:')
367 argedit! y
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200368 if has('fname_case')
369 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
370 else
371 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
372 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100373 %argd
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200374 bwipe! C
375 bwipe! D
Bram Moolenaar46a53df2018-04-24 21:58:51 +0200376
377 " :argedit reuses the current buffer if it is empty
378 %argd
379 " make sure to use a new buffer number for x when it is loaded
380 bw! x
381 new
382 let a = bufnr('')
383 argedit x
384 call assert_equal(a, bufnr(''))
385 call assert_equal('x', bufname(''))
386 %argd
387 bw! x
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100388endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100389
390" Test for the :argdelete command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100391func Test_argdelete()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100392 call Reset_arglist()
393 args aa a aaa b bb
394 argdelete a*
395 call assert_equal(['b', 'bb'], argv())
396 call assert_equal('aa', expand('%:t'))
397 last
398 argdelete %
399 call assert_equal(['b'], argv())
400 call assert_fails('argdelete', 'E471:')
401 call assert_fails('1,100argdelete', 'E16:')
402 %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100403endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100404
Bram Moolenaarb513d302018-12-02 14:55:08 +0100405func Test_argdelete_completion()
406 args foo bar
407
408 call feedkeys(":argdelete \<C-A>\<C-B>\"\<CR>", 'tx')
409 call assert_equal('"argdelete bar foo', @:)
410
411 call feedkeys(":argdelete x \<C-A>\<C-B>\"\<CR>", 'tx')
412 call assert_equal('"argdelete x bar foo', @:)
413
414 %argd
415endfunc
416
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100417" Tests for the :next, :prev, :first, :last, :rewind commands
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100418func Test_argpos()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100419 call Reset_arglist()
420 args a b c d
421 last
422 call assert_equal(3, argidx())
423 call assert_fails('next', 'E165:')
424 prev
425 call assert_equal(2, argidx())
426 Next
427 call assert_equal(1, argidx())
428 first
429 call assert_equal(0, argidx())
430 call assert_fails('prev', 'E164:')
431 3next
432 call assert_equal(3, argidx())
433 rewind
434 call assert_equal(0, argidx())
435 %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100436endfunc
Bram Moolenaar53f16732016-09-07 20:46:39 +0200437
438" Test for autocommand that redefines the argument list, when doing ":all".
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100439func Test_arglist_autocmd()
Bram Moolenaar53f16732016-09-07 20:46:39 +0200440 autocmd BufReadPost Xxx2 next Xxx2 Xxx1
441 call writefile(['test file Xxx1'], 'Xxx1')
442 call writefile(['test file Xxx2'], 'Xxx2')
443 call writefile(['test file Xxx3'], 'Xxx3')
444
445 new
446 " redefine arglist; go to Xxx1
447 next! Xxx1 Xxx2 Xxx3
448 " open window for all args
449 all
450 call assert_equal('test file Xxx1', getline(1))
451 wincmd w
452 wincmd w
453 call assert_equal('test file Xxx1', getline(1))
454 " should now be in Xxx2
455 rewind
456 call assert_equal('test file Xxx2', getline(1))
457
458 autocmd! BufReadPost Xxx2
459 enew! | only
460 call delete('Xxx1')
461 call delete('Xxx2')
462 call delete('Xxx3')
463 argdelete Xxx*
464 bwipe! Xxx1 Xxx2 Xxx3
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100465endfunc
466
467func Test_arg_all_expand()
468 call writefile(['test file Xxx1'], 'Xx x')
469 next notexist Xx\ x runtest.vim
470 call assert_equal('notexist Xx\ x runtest.vim', expand('##'))
471 call delete('Xx x')
472endfunc
Bram Moolenaare961cba2018-09-18 21:51:47 +0200473
474func Test_large_arg()
475 " Argument longer or equal to the number of columns used to cause
476 " access to invalid memory.
477 exe 'argadd ' .repeat('x', &columns)
478 args
479endfunc