blob: daed3d42184de6ee429d17c557093b90639436fe [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
Bram Moolenaar073e4b92019-08-18 23:01:56 +020093 call assert_notequal(curbuf, '%'->bufnr())
94 call assert_equal('Xargadd', '%'->bufname())
Bram Moolenaar32bbd002018-08-31 23:06:22 +020095 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
Bram Moolenaar9800bfe2019-07-27 21:23:45 +0200172 let save_columns = &columns
173 let &columns = 79
174 exe 'args ' .. join(range(1, 81))
175 call assert_equal(join([
176 \ '',
177 \ '[1] 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 ',
178 \ '2 7 12 17 22 27 32 37 42 47 52 57 62 67 72 77 ',
179 \ '3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 ',
180 \ '4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 ',
181 \ '5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 ',
182 \ ], "\n"),
183 \ execute('args'))
184
185 " No trailing newline with one item per row.
186 let long_arg = repeat('X', 81)
187 exe 'args ' .. long_arg
188 call assert_equal("\n[".long_arg.']', execute('args'))
189 let &columns = save_columns
190
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100191 " Setting argument list should fail when the current buffer has unsaved
192 " changes
193 %argd
194 enew!
195 set modified
196 call assert_fails('args x y z', 'E37:')
197 args! x y z
198 call assert_equal(['x', 'y', 'z'], argv())
199 call assert_equal('x', expand('%:t'))
200
201 last | enew | argu
202 call assert_equal('z', expand('%:t'))
203
204 %argdelete
205 call assert_fails('argument', 'E163:')
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100206endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100207
Bram Moolenaar5d69da42018-04-20 22:01:41 +0200208func Test_list_arguments()
209 " Clean the argument list
210 arga a | %argd
211
212 " four args half the screen width makes two lines with two columns
213 let aarg = repeat('a', &columns / 2 - 4)
214 let barg = repeat('b', &columns / 2 - 4)
215 let carg = repeat('c', &columns / 2 - 4)
216 let darg = repeat('d', &columns / 2 - 4)
217 exe 'argadd ' aarg barg carg darg
218
219 redir => result
220 args
221 redir END
222 call assert_match('\[' . aarg . '] \+' . carg . '\n' . barg . ' \+' . darg, trim(result))
223
224 " if one arg is longer than half the screen make one column
225 exe 'argdel' aarg
226 let aarg = repeat('a', &columns / 2 + 2)
227 exe '0argadd' aarg
228 redir => result
229 args
230 redir END
231 call assert_match(aarg . '\n\[' . barg . ']\n' . carg . '\n' . darg, trim(result))
232
233 %argdelete
234endfunc
235
Bram Moolenaar2ac372c2018-12-28 19:06:47 +0100236func Test_args_with_quote()
Bram Moolenaar3de8c2d2018-12-28 19:29:35 +0100237 " Only on Unix can a file name include a double quote.
238 if has('unix')
239 args \"foobar
240 call assert_equal('"foobar', argv(0))
241 %argdelete
242 endif
Bram Moolenaar2ac372c2018-12-28 19:06:47 +0100243endfunc
244
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100245" Test for 0argadd and 0argedit
246" Ported from the test_argument_0count.in test script
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100247func Test_zero_argadd()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100248 " Clean the argument list
249 arga a | %argd
250
251 arga a b c d
252 2argu
253 0arga added
254 call assert_equal(['added', 'a', 'b', 'c', 'd'], argv())
255
256 2argu
257 arga third
258 call assert_equal(['added', 'a', 'third', 'b', 'c', 'd'], argv())
259
260 %argd
261 arga a b c d
262 2argu
263 0arge edited
264 call assert_equal(['edited', 'a', 'b', 'c', 'd'], argv())
265
266 2argu
267 arga third
268 call assert_equal(['edited', 'a', 'third', 'b', 'c', 'd'], argv())
Bram Moolenaar90305c62017-07-16 15:31:17 +0200269
270 2argu
271 argedit file\ with\ spaces another file
272 call assert_equal(['edited', 'a', 'file with spaces', 'another', 'file', 'third', 'b', 'c', 'd'], argv())
273 call assert_equal('file with spaces', expand('%'))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100274endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100275
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100276func Reset_arglist()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100277 args a | %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100278endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100279
280" Test for argc()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100281func Test_argc()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100282 call Reset_arglist()
283 call assert_equal(0, argc())
284 argadd a b
285 call assert_equal(2, argc())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100286endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100287
288" Test for arglistid()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100289func Test_arglistid()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100290 call Reset_arglist()
291 arga a b
292 call assert_equal(0, arglistid())
293 split
294 arglocal
295 call assert_equal(1, arglistid())
296 tabnew | tabfirst
297 call assert_equal(0, arglistid(2))
298 call assert_equal(1, arglistid(1, 1))
299 call assert_equal(0, arglistid(2, 1))
300 call assert_equal(1, arglistid(1, 2))
301 tabonly | only | enew!
302 argglobal
303 call assert_equal(0, arglistid())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100304endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100305
Bram Moolenaare6e39892018-10-25 12:32:11 +0200306" Tests for argv() and argc()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100307func Test_argv()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100308 call Reset_arglist()
309 call assert_equal([], argv())
310 call assert_equal("", argv(2))
Bram Moolenaare6e39892018-10-25 12:32:11 +0200311 call assert_equal(0, argc())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100312 argadd a b c d
Bram Moolenaare6e39892018-10-25 12:32:11 +0200313 call assert_equal(4, argc())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100314 call assert_equal('c', argv(2))
Bram Moolenaare6e39892018-10-25 12:32:11 +0200315
316 let w1_id = win_getid()
317 split
318 let w2_id = win_getid()
319 arglocal
320 args e f g
321 tabnew
322 let w3_id = win_getid()
323 split
324 let w4_id = win_getid()
325 argglobal
326 tabfirst
327 call assert_equal(4, argc(w1_id))
328 call assert_equal('b', argv(1, w1_id))
329 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w1_id))
330
331 call assert_equal(3, argc(w2_id))
332 call assert_equal('f', argv(1, w2_id))
333 call assert_equal(['e', 'f', 'g'], argv(-1, w2_id))
334
335 call assert_equal(3, argc(w3_id))
336 call assert_equal('e', argv(0, w3_id))
337 call assert_equal(['e', 'f', 'g'], argv(-1, w3_id))
338
339 call assert_equal(4, argc(w4_id))
340 call assert_equal('c', argv(2, w4_id))
341 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w4_id))
342
343 call assert_equal(4, argc(-1))
344 call assert_equal(3, argc())
345 call assert_equal('d', argv(3, -1))
346 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, -1))
347 tabonly | only | enew!
348 " Negative test cases
349 call assert_equal(-1, argc(100))
350 call assert_equal('', argv(1, 100))
351 call assert_equal([], argv(-1, 100))
352 call assert_equal('', argv(10, -1))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100353endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100354
355" Test for the :argedit command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100356func Test_argedit()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100357 call Reset_arglist()
358 argedit a
359 call assert_equal(['a'], argv())
360 call assert_equal('a', expand('%:t'))
361 argedit b
362 call assert_equal(['a', 'b'], argv())
363 call assert_equal('b', expand('%:t'))
364 argedit a
Bram Moolenaar90305c62017-07-16 15:31:17 +0200365 call assert_equal(['a', 'b', 'a'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100366 call assert_equal('a', expand('%:t'))
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200367 " When file name case is ignored, an existing buffer with only case
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200368 " difference is re-used.
Bram Moolenaar90305c62017-07-16 15:31:17 +0200369 argedit C D
370 call assert_equal('C', expand('%:t'))
371 call assert_equal(['a', 'b', 'a', 'C', 'D'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100372 argedit c
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200373 if has('fname_case')
374 call assert_equal(['a', 'b', 'a', 'C', 'c', 'D'], argv())
375 else
376 call assert_equal(['a', 'b', 'a', 'C', 'C', 'D'], argv())
377 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100378 0argedit x
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200379 if has('fname_case')
380 call assert_equal(['x', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
381 else
382 call assert_equal(['x', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
383 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100384 enew! | set modified
385 call assert_fails('argedit y', 'E37:')
386 argedit! y
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200387 if has('fname_case')
388 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
389 else
390 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
391 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100392 %argd
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200393 bwipe! C
394 bwipe! D
Bram Moolenaar46a53df2018-04-24 21:58:51 +0200395
396 " :argedit reuses the current buffer if it is empty
397 %argd
398 " make sure to use a new buffer number for x when it is loaded
399 bw! x
400 new
Bram Moolenaara8eee212019-08-24 22:14:58 +0200401 let a = bufnr()
Bram Moolenaar46a53df2018-04-24 21:58:51 +0200402 argedit x
Bram Moolenaara8eee212019-08-24 22:14:58 +0200403 call assert_equal(a, bufnr())
404 call assert_equal('x', bufname())
Bram Moolenaar46a53df2018-04-24 21:58:51 +0200405 %argd
406 bw! x
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100407endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100408
409" Test for the :argdelete command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100410func Test_argdelete()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100411 call Reset_arglist()
412 args aa a aaa b bb
413 argdelete a*
414 call assert_equal(['b', 'bb'], argv())
415 call assert_equal('aa', expand('%:t'))
416 last
417 argdelete %
418 call assert_equal(['b'], argv())
419 call assert_fails('argdelete', 'E471:')
420 call assert_fails('1,100argdelete', 'E16:')
421 %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100422endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100423
Bram Moolenaarb513d302018-12-02 14:55:08 +0100424func Test_argdelete_completion()
425 args foo bar
426
427 call feedkeys(":argdelete \<C-A>\<C-B>\"\<CR>", 'tx')
428 call assert_equal('"argdelete bar foo', @:)
429
430 call feedkeys(":argdelete x \<C-A>\<C-B>\"\<CR>", 'tx')
431 call assert_equal('"argdelete x bar foo', @:)
432
433 %argd
434endfunc
435
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100436" Tests for the :next, :prev, :first, :last, :rewind commands
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100437func Test_argpos()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100438 call Reset_arglist()
439 args a b c d
440 last
441 call assert_equal(3, argidx())
442 call assert_fails('next', 'E165:')
443 prev
444 call assert_equal(2, argidx())
445 Next
446 call assert_equal(1, argidx())
447 first
448 call assert_equal(0, argidx())
449 call assert_fails('prev', 'E164:')
450 3next
451 call assert_equal(3, argidx())
452 rewind
453 call assert_equal(0, argidx())
454 %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100455endfunc
Bram Moolenaar53f16732016-09-07 20:46:39 +0200456
457" Test for autocommand that redefines the argument list, when doing ":all".
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100458func Test_arglist_autocmd()
Bram Moolenaar53f16732016-09-07 20:46:39 +0200459 autocmd BufReadPost Xxx2 next Xxx2 Xxx1
460 call writefile(['test file Xxx1'], 'Xxx1')
461 call writefile(['test file Xxx2'], 'Xxx2')
462 call writefile(['test file Xxx3'], 'Xxx3')
463
464 new
465 " redefine arglist; go to Xxx1
466 next! Xxx1 Xxx2 Xxx3
467 " open window for all args
468 all
469 call assert_equal('test file Xxx1', getline(1))
470 wincmd w
471 wincmd w
472 call assert_equal('test file Xxx1', getline(1))
473 " should now be in Xxx2
474 rewind
475 call assert_equal('test file Xxx2', getline(1))
476
477 autocmd! BufReadPost Xxx2
478 enew! | only
479 call delete('Xxx1')
480 call delete('Xxx2')
481 call delete('Xxx3')
482 argdelete Xxx*
483 bwipe! Xxx1 Xxx2 Xxx3
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100484endfunc
485
486func Test_arg_all_expand()
487 call writefile(['test file Xxx1'], 'Xx x')
488 next notexist Xx\ x runtest.vim
489 call assert_equal('notexist Xx\ x runtest.vim', expand('##'))
490 call delete('Xx x')
491endfunc
Bram Moolenaare961cba2018-09-18 21:51:47 +0200492
493func Test_large_arg()
494 " Argument longer or equal to the number of columns used to cause
495 " access to invalid memory.
496 exe 'argadd ' .repeat('x', &columns)
497 args
498endfunc
Bram Moolenaar72e1b392019-08-03 13:50:08 +0200499
500func Test_argdo()
501 next! Xa.c Xb.c Xc.c
502 new
503 let l = []
504 argdo call add(l, expand('%'))
505 call assert_equal(['Xa.c', 'Xb.c', 'Xc.c'], l)
506 bwipe Xa.c Xb.c Xc.c
507endfunc