blob: 9513ad334c9c02e7294a18a511a102a92b67144c [file] [log] [blame]
Bram Moolenaar72defda2016-01-17 18:04:33 +01001" Test argument list commands
2
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01003source shared.vim
4source term_util.vim
5
Bram Moolenaar72defda2016-01-17 18:04:33 +01006func Test_argidx()
7 args a b c
8 last
9 call assert_equal(2, argidx())
10 %argdelete
11 call assert_equal(0, argidx())
Bram Moolenaar69a92fb2017-03-09 15:58:30 +010012 " doing it again doesn't result in an error
13 %argdelete
14 call assert_equal(0, argidx())
15 call assert_fails('2argdelete', 'E16:')
Bram Moolenaar72defda2016-01-17 18:04:33 +010016
17 args a b c
18 call assert_equal(0, argidx())
19 next
20 call assert_equal(1, argidx())
21 next
22 call assert_equal(2, argidx())
23 1argdelete
24 call assert_equal(1, argidx())
25 1argdelete
26 call assert_equal(0, argidx())
27 1argdelete
28 call assert_equal(0, argidx())
29endfunc
Bram Moolenaara24f0a52016-01-17 19:39:00 +010030
31func Test_argadd()
32 %argdelete
33 argadd a b c
34 call assert_equal(0, argidx())
35
36 %argdelete
37 argadd a
38 call assert_equal(0, argidx())
39 argadd b c d
40 call assert_equal(0, argidx())
41
42 call Init_abc()
43 argadd x
44 call Assert_argc(['a', 'b', 'x', 'c'])
45 call assert_equal(1, argidx())
46
47 call Init_abc()
48 0argadd x
49 call Assert_argc(['x', 'a', 'b', 'c'])
50 call assert_equal(2, argidx())
51
52 call Init_abc()
53 1argadd x
54 call Assert_argc(['a', 'x', 'b', 'c'])
55 call assert_equal(2, argidx())
56
57 call Init_abc()
58 $argadd x
59 call Assert_argc(['a', 'b', 'c', 'x'])
60 call assert_equal(1, argidx())
61
62 call Init_abc()
63 $argadd x
64 +2argadd y
65 call Assert_argc(['a', 'b', 'c', 'x', 'y'])
66 call assert_equal(1, argidx())
Bram Moolenaar2faa29f2016-01-23 23:02:34 +010067
68 %argd
69 edit d
70 arga
Bram Moolenaar398ee732017-08-03 14:29:14 +020071 call assert_equal(1, len(argv()))
72 call assert_equal('d', get(argv(), 0, ''))
73
74 %argd
75 edit some\ file
76 arga
77 call assert_equal(1, len(argv()))
78 call assert_equal('some file', get(argv(), 0, ''))
Bram Moolenaar2faa29f2016-01-23 23:02:34 +010079
80 %argd
81 new
82 arga
Bram Moolenaar398ee732017-08-03 14:29:14 +020083 call assert_equal(0, len(argv()))
Bram Moolenaara24f0a52016-01-17 19:39:00 +010084endfunc
85
Bram Moolenaar32bbd002018-08-31 23:06:22 +020086func Test_argadd_empty_curbuf()
87 new
88 let curbuf = bufnr('%')
89 call writefile(['test', 'Xargadd'], 'Xargadd')
90 " must not re-use the current buffer.
91 argadd Xargadd
92 call assert_equal(curbuf, bufnr('%'))
93 call assert_equal('', bufname('%'))
Bram Moolenaar02b31112019-08-31 22:16:38 +020094 call assert_equal(1, '$'->line())
Bram Moolenaar32bbd002018-08-31 23:06:22 +020095 rew
Bram Moolenaar073e4b92019-08-18 23:01:56 +020096 call assert_notequal(curbuf, '%'->bufnr())
97 call assert_equal('Xargadd', '%'->bufname())
Bram Moolenaar32bbd002018-08-31 23:06:22 +020098 call assert_equal(2, line('$'))
99
Bram Moolenaard3398282018-09-24 21:32:11 +0200100 call delete('Xargadd')
Bram Moolenaar32bbd002018-08-31 23:06:22 +0200101 %argd
102 bwipe!
103endfunc
104
Bram Moolenaara24f0a52016-01-17 19:39:00 +0100105func Init_abc()
106 args a b c
107 next
108endfunc
109
110func Assert_argc(l)
111 call assert_equal(len(a:l), argc())
112 let i = 0
113 while i < len(a:l) && i < argc()
114 call assert_equal(a:l[i], argv(i))
115 let i += 1
116 endwhile
117endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100118
119" Test for [count]argument and [count]argdelete commands
120" Ported from the test_argument_count.in test script
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100121func Test_argument()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100122 " Clean the argument list
123 arga a | %argd
124
125 let save_hidden = &hidden
126 set hidden
127
128 let g:buffers = []
129 augroup TEST
130 au BufEnter * call add(buffers, expand('%:t'))
131 augroup END
132
133 argadd a b c d
134 $argu
135 $-argu
136 -argu
137 1argu
138 +2argu
139
140 augroup TEST
141 au!
142 augroup END
143
144 call assert_equal(['d', 'c', 'b', 'a', 'c'], g:buffers)
145
Bram Moolenaar949f1982019-07-23 23:00:08 +0200146 call assert_equal("\na b [c] d ", execute(':args'))
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100147
148 .argd
149 call assert_equal(['a', 'b', 'd'], argv())
150
151 -argd
152 call assert_equal(['a', 'd'], argv())
153
154 $argd
155 call assert_equal(['a'], argv())
156
157 1arga c
158 1arga b
159 $argu
160 $arga x
161 call assert_equal(['a', 'b', 'c', 'x'], argv())
162
Bram Moolenaar30141702016-01-19 14:14:08 +0100163 0arga y
164 call assert_equal(['y', 'a', 'b', 'c', 'x'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100165
166 %argd
167 call assert_equal([], argv())
168
169 arga a b c d e f
170 2,$-argd
171 call assert_equal(['a', 'f'], argv())
172
173 let &hidden = save_hidden
174
Bram Moolenaar9800bfe2019-07-27 21:23:45 +0200175 let save_columns = &columns
176 let &columns = 79
177 exe 'args ' .. join(range(1, 81))
178 call assert_equal(join([
179 \ '',
180 \ '[1] 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 ',
181 \ '2 7 12 17 22 27 32 37 42 47 52 57 62 67 72 77 ',
182 \ '3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 ',
183 \ '4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 ',
184 \ '5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 ',
185 \ ], "\n"),
186 \ execute('args'))
187
188 " No trailing newline with one item per row.
189 let long_arg = repeat('X', 81)
190 exe 'args ' .. long_arg
191 call assert_equal("\n[".long_arg.']', execute('args'))
192 let &columns = save_columns
193
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100194 " Setting argument list should fail when the current buffer has unsaved
195 " changes
196 %argd
197 enew!
198 set modified
199 call assert_fails('args x y z', 'E37:')
200 args! x y z
201 call assert_equal(['x', 'y', 'z'], argv())
202 call assert_equal('x', expand('%:t'))
203
204 last | enew | argu
205 call assert_equal('z', expand('%:t'))
206
207 %argdelete
208 call assert_fails('argument', 'E163:')
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100209endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100210
Bram Moolenaar5d69da42018-04-20 22:01:41 +0200211func Test_list_arguments()
212 " Clean the argument list
213 arga a | %argd
214
215 " four args half the screen width makes two lines with two columns
216 let aarg = repeat('a', &columns / 2 - 4)
217 let barg = repeat('b', &columns / 2 - 4)
218 let carg = repeat('c', &columns / 2 - 4)
219 let darg = repeat('d', &columns / 2 - 4)
220 exe 'argadd ' aarg barg carg darg
221
222 redir => result
223 args
224 redir END
225 call assert_match('\[' . aarg . '] \+' . carg . '\n' . barg . ' \+' . darg, trim(result))
226
227 " if one arg is longer than half the screen make one column
228 exe 'argdel' aarg
229 let aarg = repeat('a', &columns / 2 + 2)
230 exe '0argadd' aarg
231 redir => result
232 args
233 redir END
234 call assert_match(aarg . '\n\[' . barg . ']\n' . carg . '\n' . darg, trim(result))
235
236 %argdelete
237endfunc
238
Bram Moolenaar2ac372c2018-12-28 19:06:47 +0100239func Test_args_with_quote()
Bram Moolenaar3de8c2d2018-12-28 19:29:35 +0100240 " Only on Unix can a file name include a double quote.
241 if has('unix')
242 args \"foobar
243 call assert_equal('"foobar', argv(0))
244 %argdelete
245 endif
Bram Moolenaar2ac372c2018-12-28 19:06:47 +0100246endfunc
247
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100248" Test for 0argadd and 0argedit
249" Ported from the test_argument_0count.in test script
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100250func Test_zero_argadd()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100251 " Clean the argument list
252 arga a | %argd
253
254 arga a b c d
255 2argu
256 0arga added
257 call assert_equal(['added', 'a', 'b', 'c', 'd'], argv())
258
259 2argu
260 arga third
261 call assert_equal(['added', 'a', 'third', 'b', 'c', 'd'], argv())
262
263 %argd
264 arga a b c d
265 2argu
266 0arge edited
267 call assert_equal(['edited', 'a', 'b', 'c', 'd'], argv())
268
269 2argu
270 arga third
271 call assert_equal(['edited', 'a', 'third', 'b', 'c', 'd'], argv())
Bram Moolenaar90305c62017-07-16 15:31:17 +0200272
273 2argu
274 argedit file\ with\ spaces another file
275 call assert_equal(['edited', 'a', 'file with spaces', 'another', 'file', 'third', 'b', 'c', 'd'], argv())
276 call assert_equal('file with spaces', expand('%'))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100277endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100278
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100279func Reset_arglist()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100280 args a | %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100281endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100282
283" Test for argc()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100284func Test_argc()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100285 call Reset_arglist()
286 call assert_equal(0, argc())
287 argadd a b
288 call assert_equal(2, argc())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100289endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100290
291" Test for arglistid()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100292func Test_arglistid()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100293 call Reset_arglist()
294 arga a b
295 call assert_equal(0, arglistid())
296 split
297 arglocal
298 call assert_equal(1, arglistid())
299 tabnew | tabfirst
300 call assert_equal(0, arglistid(2))
301 call assert_equal(1, arglistid(1, 1))
302 call assert_equal(0, arglistid(2, 1))
303 call assert_equal(1, arglistid(1, 2))
304 tabonly | only | enew!
305 argglobal
306 call assert_equal(0, arglistid())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100307endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100308
Bram Moolenaare6e39892018-10-25 12:32:11 +0200309" Tests for argv() and argc()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100310func Test_argv()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100311 call Reset_arglist()
312 call assert_equal([], argv())
313 call assert_equal("", argv(2))
Bram Moolenaare6e39892018-10-25 12:32:11 +0200314 call assert_equal(0, argc())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100315 argadd a b c d
Bram Moolenaare6e39892018-10-25 12:32:11 +0200316 call assert_equal(4, argc())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100317 call assert_equal('c', argv(2))
Bram Moolenaare6e39892018-10-25 12:32:11 +0200318
319 let w1_id = win_getid()
320 split
321 let w2_id = win_getid()
322 arglocal
323 args e f g
324 tabnew
325 let w3_id = win_getid()
326 split
327 let w4_id = win_getid()
328 argglobal
329 tabfirst
330 call assert_equal(4, argc(w1_id))
331 call assert_equal('b', argv(1, w1_id))
332 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w1_id))
333
334 call assert_equal(3, argc(w2_id))
335 call assert_equal('f', argv(1, w2_id))
336 call assert_equal(['e', 'f', 'g'], argv(-1, w2_id))
337
338 call assert_equal(3, argc(w3_id))
339 call assert_equal('e', argv(0, w3_id))
340 call assert_equal(['e', 'f', 'g'], argv(-1, w3_id))
341
342 call assert_equal(4, argc(w4_id))
343 call assert_equal('c', argv(2, w4_id))
344 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w4_id))
345
346 call assert_equal(4, argc(-1))
347 call assert_equal(3, argc())
348 call assert_equal('d', argv(3, -1))
349 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, -1))
350 tabonly | only | enew!
351 " Negative test cases
352 call assert_equal(-1, argc(100))
353 call assert_equal('', argv(1, 100))
354 call assert_equal([], argv(-1, 100))
355 call assert_equal('', argv(10, -1))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100356endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100357
358" Test for the :argedit command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100359func Test_argedit()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100360 call Reset_arglist()
361 argedit a
362 call assert_equal(['a'], argv())
363 call assert_equal('a', expand('%:t'))
364 argedit b
365 call assert_equal(['a', 'b'], argv())
366 call assert_equal('b', expand('%:t'))
367 argedit a
Bram Moolenaar90305c62017-07-16 15:31:17 +0200368 call assert_equal(['a', 'b', 'a'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100369 call assert_equal('a', expand('%:t'))
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200370 " When file name case is ignored, an existing buffer with only case
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200371 " difference is re-used.
Bram Moolenaar90305c62017-07-16 15:31:17 +0200372 argedit C D
373 call assert_equal('C', expand('%:t'))
374 call assert_equal(['a', 'b', 'a', 'C', 'D'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100375 argedit c
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200376 if has('fname_case')
377 call assert_equal(['a', 'b', 'a', 'C', 'c', 'D'], argv())
378 else
379 call assert_equal(['a', 'b', 'a', 'C', 'C', 'D'], argv())
380 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100381 0argedit x
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200382 if has('fname_case')
383 call assert_equal(['x', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
384 else
385 call assert_equal(['x', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
386 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100387 enew! | set modified
388 call assert_fails('argedit y', 'E37:')
389 argedit! y
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200390 if has('fname_case')
391 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
392 else
393 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
394 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100395 %argd
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200396 bwipe! C
397 bwipe! D
Bram Moolenaar46a53df2018-04-24 21:58:51 +0200398
399 " :argedit reuses the current buffer if it is empty
400 %argd
401 " make sure to use a new buffer number for x when it is loaded
402 bw! x
403 new
Bram Moolenaara8eee212019-08-24 22:14:58 +0200404 let a = bufnr()
Bram Moolenaar46a53df2018-04-24 21:58:51 +0200405 argedit x
Bram Moolenaara8eee212019-08-24 22:14:58 +0200406 call assert_equal(a, bufnr())
407 call assert_equal('x', bufname())
Bram Moolenaar46a53df2018-04-24 21:58:51 +0200408 %argd
409 bw! x
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100410endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100411
412" Test for the :argdelete command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100413func Test_argdelete()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100414 call Reset_arglist()
415 args aa a aaa b bb
416 argdelete a*
417 call assert_equal(['b', 'bb'], argv())
418 call assert_equal('aa', expand('%:t'))
419 last
420 argdelete %
421 call assert_equal(['b'], argv())
422 call assert_fails('argdelete', 'E471:')
423 call assert_fails('1,100argdelete', 'E16:')
424 %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100425endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100426
Bram Moolenaarb513d302018-12-02 14:55:08 +0100427func Test_argdelete_completion()
428 args foo bar
429
430 call feedkeys(":argdelete \<C-A>\<C-B>\"\<CR>", 'tx')
431 call assert_equal('"argdelete bar foo', @:)
432
433 call feedkeys(":argdelete x \<C-A>\<C-B>\"\<CR>", 'tx')
434 call assert_equal('"argdelete x bar foo', @:)
435
436 %argd
437endfunc
438
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100439" Tests for the :next, :prev, :first, :last, :rewind commands
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100440func Test_argpos()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100441 call Reset_arglist()
442 args a b c d
443 last
444 call assert_equal(3, argidx())
445 call assert_fails('next', 'E165:')
446 prev
447 call assert_equal(2, argidx())
448 Next
449 call assert_equal(1, argidx())
450 first
451 call assert_equal(0, argidx())
452 call assert_fails('prev', 'E164:')
453 3next
454 call assert_equal(3, argidx())
455 rewind
456 call assert_equal(0, argidx())
457 %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100458endfunc
Bram Moolenaar53f16732016-09-07 20:46:39 +0200459
460" Test for autocommand that redefines the argument list, when doing ":all".
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100461func Test_arglist_autocmd()
Bram Moolenaar53f16732016-09-07 20:46:39 +0200462 autocmd BufReadPost Xxx2 next Xxx2 Xxx1
463 call writefile(['test file Xxx1'], 'Xxx1')
464 call writefile(['test file Xxx2'], 'Xxx2')
465 call writefile(['test file Xxx3'], 'Xxx3')
466
467 new
468 " redefine arglist; go to Xxx1
469 next! Xxx1 Xxx2 Xxx3
470 " open window for all args
471 all
472 call assert_equal('test file Xxx1', getline(1))
473 wincmd w
474 wincmd w
475 call assert_equal('test file Xxx1', getline(1))
476 " should now be in Xxx2
477 rewind
478 call assert_equal('test file Xxx2', getline(1))
479
480 autocmd! BufReadPost Xxx2
481 enew! | only
482 call delete('Xxx1')
483 call delete('Xxx2')
484 call delete('Xxx3')
485 argdelete Xxx*
486 bwipe! Xxx1 Xxx2 Xxx3
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100487endfunc
488
489func Test_arg_all_expand()
490 call writefile(['test file Xxx1'], 'Xx x')
491 next notexist Xx\ x runtest.vim
492 call assert_equal('notexist Xx\ x runtest.vim', expand('##'))
493 call delete('Xx x')
494endfunc
Bram Moolenaare961cba2018-09-18 21:51:47 +0200495
496func Test_large_arg()
497 " Argument longer or equal to the number of columns used to cause
498 " access to invalid memory.
499 exe 'argadd ' .repeat('x', &columns)
500 args
501endfunc
Bram Moolenaar72e1b392019-08-03 13:50:08 +0200502
503func Test_argdo()
504 next! Xa.c Xb.c Xc.c
505 new
506 let l = []
507 argdo call add(l, expand('%'))
508 call assert_equal(['Xa.c', 'Xb.c', 'Xc.c'], l)
509 bwipe Xa.c Xb.c Xc.c
510endfunc
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100511
512" Test for quiting Vim with unedited files in the argument list
513func Test_quit_with_arglist()
514 if !CanRunVimInTerminal()
515 throw 'Skipped: cannot run vim in terminal'
516 endif
517 let buf = RunVimInTerminal('', {'rows': 6})
518 call term_sendkeys(buf, ":args a b c\n")
519 call term_sendkeys(buf, ":quit\n")
520 call WaitForAssert({-> assert_match('^E173:', term_getline(buf, 6))})
521 call StopVimInTerminal(buf)
522endfunc
523
524" vim: shiftwidth=2 sts=2 expandtab