blob: 2a76ca6515bd062b1b725b4e9299eba60edfc0a5 [file] [log] [blame]
Bram Moolenaar72defda2016-01-17 18:04:33 +01001" Test argument list commands
2
Shougo Matsushita3cad4702021-11-18 15:37:29 +00003func Reset_arglist()
4 args a | %argd
5endfunc
6
Bram Moolenaar72defda2016-01-17 18:04:33 +01007func Test_argidx()
8 args a b c
9 last
10 call assert_equal(2, argidx())
11 %argdelete
12 call assert_equal(0, argidx())
Bram Moolenaar69a92fb2017-03-09 15:58:30 +010013 " doing it again doesn't result in an error
14 %argdelete
15 call assert_equal(0, argidx())
16 call assert_fails('2argdelete', 'E16:')
Bram Moolenaar72defda2016-01-17 18:04:33 +010017
18 args a b c
19 call assert_equal(0, argidx())
20 next
21 call assert_equal(1, argidx())
22 next
23 call assert_equal(2, argidx())
24 1argdelete
25 call assert_equal(1, argidx())
26 1argdelete
27 call assert_equal(0, argidx())
28 1argdelete
29 call assert_equal(0, argidx())
30endfunc
Bram Moolenaara24f0a52016-01-17 19:39:00 +010031
32func Test_argadd()
Shougo Matsushita3cad4702021-11-18 15:37:29 +000033 call Reset_arglist()
34
Bram Moolenaara24f0a52016-01-17 19:39:00 +010035 %argdelete
36 argadd a b c
37 call assert_equal(0, argidx())
38
39 %argdelete
40 argadd a
41 call assert_equal(0, argidx())
42 argadd b c d
43 call assert_equal(0, argidx())
44
45 call Init_abc()
46 argadd x
47 call Assert_argc(['a', 'b', 'x', 'c'])
48 call assert_equal(1, argidx())
49
50 call Init_abc()
51 0argadd x
52 call Assert_argc(['x', 'a', 'b', 'c'])
53 call assert_equal(2, argidx())
54
55 call Init_abc()
56 1argadd x
57 call Assert_argc(['a', 'x', 'b', 'c'])
58 call assert_equal(2, argidx())
59
60 call Init_abc()
61 $argadd x
62 call Assert_argc(['a', 'b', 'c', 'x'])
63 call assert_equal(1, argidx())
64
65 call Init_abc()
66 $argadd x
67 +2argadd y
68 call Assert_argc(['a', 'b', 'c', 'x', 'y'])
69 call assert_equal(1, argidx())
Bram Moolenaar2faa29f2016-01-23 23:02:34 +010070
71 %argd
72 edit d
73 arga
Bram Moolenaar398ee732017-08-03 14:29:14 +020074 call assert_equal(1, len(argv()))
75 call assert_equal('d', get(argv(), 0, ''))
76
77 %argd
78 edit some\ file
79 arga
80 call assert_equal(1, len(argv()))
81 call assert_equal('some file', get(argv(), 0, ''))
Bram Moolenaar2faa29f2016-01-23 23:02:34 +010082
83 %argd
84 new
85 arga
Bram Moolenaar398ee732017-08-03 14:29:14 +020086 call assert_equal(0, len(argv()))
Dominique Pellebfb2bb12021-08-14 21:11:51 +020087
88 if has('unix')
89 call assert_fails('argadd `Xdoes_not_exist`', 'E479:')
90 endif
Bram Moolenaara24f0a52016-01-17 19:39:00 +010091endfunc
92
Bram Moolenaar32bbd002018-08-31 23:06:22 +020093func Test_argadd_empty_curbuf()
94 new
95 let curbuf = bufnr('%')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010096 call writefile(['test', 'Xargadd'], 'Xargadd', 'D')
Bram Moolenaar32bbd002018-08-31 23:06:22 +020097 " must not re-use the current buffer.
98 argadd Xargadd
99 call assert_equal(curbuf, bufnr('%'))
100 call assert_equal('', bufname('%'))
Bram Moolenaar02b31112019-08-31 22:16:38 +0200101 call assert_equal(1, '$'->line())
Bram Moolenaar32bbd002018-08-31 23:06:22 +0200102 rew
Bram Moolenaar073e4b92019-08-18 23:01:56 +0200103 call assert_notequal(curbuf, '%'->bufnr())
104 call assert_equal('Xargadd', '%'->bufname())
Bram Moolenaar32bbd002018-08-31 23:06:22 +0200105 call assert_equal(2, line('$'))
106
107 %argd
108 bwipe!
109endfunc
110
Bram Moolenaara24f0a52016-01-17 19:39:00 +0100111func Init_abc()
112 args a b c
113 next
114endfunc
115
116func Assert_argc(l)
117 call assert_equal(len(a:l), argc())
118 let i = 0
119 while i < len(a:l) && i < argc()
120 call assert_equal(a:l[i], argv(i))
121 let i += 1
122 endwhile
123endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100124
125" Test for [count]argument and [count]argdelete commands
126" Ported from the test_argument_count.in test script
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100127func Test_argument()
Shougo Matsushita3cad4702021-11-18 15:37:29 +0000128 call Reset_arglist()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100129
130 let save_hidden = &hidden
131 set hidden
132
133 let g:buffers = []
134 augroup TEST
135 au BufEnter * call add(buffers, expand('%:t'))
136 augroup END
137
138 argadd a b c d
139 $argu
140 $-argu
141 -argu
142 1argu
143 +2argu
144
145 augroup TEST
146 au!
147 augroup END
148
149 call assert_equal(['d', 'c', 'b', 'a', 'c'], g:buffers)
150
Bram Moolenaar949f1982019-07-23 23:00:08 +0200151 call assert_equal("\na b [c] d ", execute(':args'))
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100152
153 .argd
154 call assert_equal(['a', 'b', 'd'], argv())
155
156 -argd
157 call assert_equal(['a', 'd'], argv())
158
159 $argd
160 call assert_equal(['a'], argv())
161
162 1arga c
163 1arga b
164 $argu
165 $arga x
166 call assert_equal(['a', 'b', 'c', 'x'], argv())
167
Bram Moolenaar30141702016-01-19 14:14:08 +0100168 0arga y
169 call assert_equal(['y', 'a', 'b', 'c', 'x'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100170
171 %argd
172 call assert_equal([], argv())
173
174 arga a b c d e f
175 2,$-argd
176 call assert_equal(['a', 'f'], argv())
177
178 let &hidden = save_hidden
179
Bram Moolenaar9800bfe2019-07-27 21:23:45 +0200180 let save_columns = &columns
181 let &columns = 79
Bram Moolenaar71478202020-06-26 22:46:27 +0200182 try
183 exe 'args ' .. join(range(1, 81))
184 call assert_equal(join([
185 \ '',
186 \ '[1] 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 ',
187 \ '2 7 12 17 22 27 32 37 42 47 52 57 62 67 72 77 ',
188 \ '3 8 13 18 23 28 33 38 43 48 53 58 63 68 73 78 ',
189 \ '4 9 14 19 24 29 34 39 44 49 54 59 64 69 74 79 ',
190 \ '5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 ',
191 \ ], "\n"),
192 \ execute('args'))
Bram Moolenaar9800bfe2019-07-27 21:23:45 +0200193
Bram Moolenaar71478202020-06-26 22:46:27 +0200194 " No trailing newline with one item per row.
195 let long_arg = repeat('X', 81)
196 exe 'args ' .. long_arg
197 call assert_equal("\n[".long_arg.']', execute('args'))
198 finally
199 let &columns = save_columns
200 endtry
Bram Moolenaar9800bfe2019-07-27 21:23:45 +0200201
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100202 " Setting argument list should fail when the current buffer has unsaved
203 " changes
204 %argd
205 enew!
206 set modified
207 call assert_fails('args x y z', 'E37:')
208 args! x y z
209 call assert_equal(['x', 'y', 'z'], argv())
210 call assert_equal('x', expand('%:t'))
211
212 last | enew | argu
213 call assert_equal('z', expand('%:t'))
214
215 %argdelete
216 call assert_fails('argument', 'E163:')
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100217endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100218
Bram Moolenaar5d69da42018-04-20 22:01:41 +0200219func Test_list_arguments()
Shougo Matsushita3cad4702021-11-18 15:37:29 +0000220 call Reset_arglist()
Bram Moolenaar5d69da42018-04-20 22:01:41 +0200221
222 " four args half the screen width makes two lines with two columns
223 let aarg = repeat('a', &columns / 2 - 4)
224 let barg = repeat('b', &columns / 2 - 4)
225 let carg = repeat('c', &columns / 2 - 4)
226 let darg = repeat('d', &columns / 2 - 4)
227 exe 'argadd ' aarg barg carg darg
228
229 redir => result
230 args
231 redir END
232 call assert_match('\[' . aarg . '] \+' . carg . '\n' . barg . ' \+' . darg, trim(result))
233
234 " if one arg is longer than half the screen make one column
235 exe 'argdel' aarg
236 let aarg = repeat('a', &columns / 2 + 2)
237 exe '0argadd' aarg
238 redir => result
239 args
240 redir END
241 call assert_match(aarg . '\n\[' . barg . ']\n' . carg . '\n' . darg, trim(result))
242
243 %argdelete
244endfunc
245
Bram Moolenaar2ac372c2018-12-28 19:06:47 +0100246func Test_args_with_quote()
Bram Moolenaar3de8c2d2018-12-28 19:29:35 +0100247 " Only on Unix can a file name include a double quote.
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200248 CheckUnix
249
250 args \"foobar
251 call assert_equal('"foobar', argv(0))
252 %argdelete
Bram Moolenaar2ac372c2018-12-28 19:06:47 +0100253endfunc
254
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100255" Test for 0argadd and 0argedit
256" Ported from the test_argument_0count.in test script
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100257func Test_zero_argadd()
Shougo Matsushita3cad4702021-11-18 15:37:29 +0000258 call Reset_arglist()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100259
260 arga a b c d
261 2argu
262 0arga added
263 call assert_equal(['added', 'a', 'b', 'c', 'd'], argv())
264
265 2argu
266 arga third
267 call assert_equal(['added', 'a', 'third', 'b', 'c', 'd'], argv())
268
269 %argd
270 arga a b c d
271 2argu
272 0arge edited
273 call assert_equal(['edited', 'a', 'b', 'c', 'd'], argv())
274
275 2argu
276 arga third
277 call assert_equal(['edited', 'a', 'third', 'b', 'c', 'd'], argv())
Bram Moolenaar90305c62017-07-16 15:31:17 +0200278
279 2argu
280 argedit file\ with\ spaces another file
281 call assert_equal(['edited', 'a', 'file with spaces', 'another', 'file', 'third', 'b', 'c', 'd'], argv())
282 call assert_equal('file with spaces', expand('%'))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100283endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100284
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100285" Test for argc()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100286func Test_argc()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100287 call Reset_arglist()
288 call assert_equal(0, argc())
289 argadd a b
290 call assert_equal(2, argc())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100291endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100292
293" Test for arglistid()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100294func Test_arglistid()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100295 call Reset_arglist()
296 arga a b
297 call assert_equal(0, arglistid())
298 split
299 arglocal
300 call assert_equal(1, arglistid())
301 tabnew | tabfirst
302 call assert_equal(0, arglistid(2))
303 call assert_equal(1, arglistid(1, 1))
304 call assert_equal(0, arglistid(2, 1))
305 call assert_equal(1, arglistid(1, 2))
306 tabonly | only | enew!
307 argglobal
308 call assert_equal(0, arglistid())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100309endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100310
Bram Moolenaare6e39892018-10-25 12:32:11 +0200311" Tests for argv() and argc()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100312func Test_argv()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100313 call Reset_arglist()
314 call assert_equal([], argv())
315 call assert_equal("", argv(2))
Bram Moolenaare6e39892018-10-25 12:32:11 +0200316 call assert_equal(0, argc())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100317 argadd a b c d
Bram Moolenaare6e39892018-10-25 12:32:11 +0200318 call assert_equal(4, argc())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100319 call assert_equal('c', argv(2))
Bram Moolenaare6e39892018-10-25 12:32:11 +0200320
321 let w1_id = win_getid()
322 split
323 let w2_id = win_getid()
324 arglocal
325 args e f g
326 tabnew
327 let w3_id = win_getid()
328 split
329 let w4_id = win_getid()
330 argglobal
331 tabfirst
332 call assert_equal(4, argc(w1_id))
333 call assert_equal('b', argv(1, w1_id))
334 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w1_id))
335
336 call assert_equal(3, argc(w2_id))
337 call assert_equal('f', argv(1, w2_id))
338 call assert_equal(['e', 'f', 'g'], argv(-1, w2_id))
339
340 call assert_equal(3, argc(w3_id))
341 call assert_equal('e', argv(0, w3_id))
342 call assert_equal(['e', 'f', 'g'], argv(-1, w3_id))
343
344 call assert_equal(4, argc(w4_id))
345 call assert_equal('c', argv(2, w4_id))
346 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, w4_id))
347
348 call assert_equal(4, argc(-1))
349 call assert_equal(3, argc())
350 call assert_equal('d', argv(3, -1))
351 call assert_equal(['a', 'b', 'c', 'd'], argv(-1, -1))
352 tabonly | only | enew!
353 " Negative test cases
354 call assert_equal(-1, argc(100))
355 call assert_equal('', argv(1, 100))
356 call assert_equal([], argv(-1, 100))
357 call assert_equal('', argv(10, -1))
Christian Brabandt0a6e57b2024-08-15 22:15:28 +0200358 %argdelete
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100359endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100360
361" Test for the :argedit command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100362func Test_argedit()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100363 call Reset_arglist()
364 argedit a
365 call assert_equal(['a'], argv())
366 call assert_equal('a', expand('%:t'))
367 argedit b
368 call assert_equal(['a', 'b'], argv())
369 call assert_equal('b', expand('%:t'))
370 argedit a
Bram Moolenaar90305c62017-07-16 15:31:17 +0200371 call assert_equal(['a', 'b', 'a'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100372 call assert_equal('a', expand('%:t'))
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200373 " When file name case is ignored, an existing buffer with only case
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200374 " difference is re-used.
Bram Moolenaar90305c62017-07-16 15:31:17 +0200375 argedit C D
376 call assert_equal('C', expand('%:t'))
377 call assert_equal(['a', 'b', 'a', 'C', 'D'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100378 argedit c
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200379 if has('fname_case')
380 call assert_equal(['a', 'b', 'a', 'C', 'c', 'D'], argv())
381 else
382 call assert_equal(['a', 'b', 'a', 'C', 'C', 'D'], argv())
383 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100384 0argedit x
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200385 if has('fname_case')
386 call assert_equal(['x', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
387 else
388 call assert_equal(['x', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
389 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100390 enew! | set modified
391 call assert_fails('argedit y', 'E37:')
392 argedit! y
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200393 if has('fname_case')
394 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
395 else
396 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
397 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100398 %argd
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200399 bwipe! C
400 bwipe! D
Bram Moolenaar46a53df2018-04-24 21:58:51 +0200401
402 " :argedit reuses the current buffer if it is empty
403 %argd
404 " make sure to use a new buffer number for x when it is loaded
405 bw! x
406 new
Bram Moolenaara8eee212019-08-24 22:14:58 +0200407 let a = bufnr()
Bram Moolenaar46a53df2018-04-24 21:58:51 +0200408 argedit x
Bram Moolenaara8eee212019-08-24 22:14:58 +0200409 call assert_equal(a, bufnr())
410 call assert_equal('x', bufname())
Bram Moolenaar46a53df2018-04-24 21:58:51 +0200411 %argd
412 bw! x
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100413endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100414
Nir Lichtman73a02422021-12-24 20:28:03 +0000415" Test for the :argdedupe command
416func Test_argdedupe()
417 call Reset_arglist()
418 argdedupe
419 call assert_equal([], argv())
Nir Lichtmanb3052aa2022-11-12 17:00:31 +0000420
Nir Lichtman73a02422021-12-24 20:28:03 +0000421 args a a a aa b b a b aa
422 argdedupe
423 call assert_equal(['a', 'aa', 'b'], argv())
Nir Lichtmanb3052aa2022-11-12 17:00:31 +0000424
Nir Lichtman73a02422021-12-24 20:28:03 +0000425 args a b c
426 argdedupe
427 call assert_equal(['a', 'b', 'c'], argv())
Nir Lichtmanb3052aa2022-11-12 17:00:31 +0000428
Nir Lichtman73a02422021-12-24 20:28:03 +0000429 args a
430 argdedupe
431 call assert_equal(['a'], argv())
Nir Lichtmanb3052aa2022-11-12 17:00:31 +0000432
Nir Lichtman73a02422021-12-24 20:28:03 +0000433 args a A b B
434 argdedupe
435 if has('fname_case')
436 call assert_equal(['a', 'A', 'b', 'B'], argv())
437 else
438 call assert_equal(['a', 'b'], argv())
439 endif
Nir Lichtmanb3052aa2022-11-12 17:00:31 +0000440
Nir Lichtman73a02422021-12-24 20:28:03 +0000441 args a b a c a b
442 last
443 argdedupe
444 next
445 call assert_equal('c', expand('%:t'))
Nir Lichtmanb3052aa2022-11-12 17:00:31 +0000446
447 args a ./a
448 argdedupe
449 call assert_equal(['a'], argv())
450
Nir Lichtman73a02422021-12-24 20:28:03 +0000451 %argd
452endfunc
453
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100454" Test for the :argdelete command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100455func Test_argdelete()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100456 call Reset_arglist()
457 args aa a aaa b bb
458 argdelete a*
459 call assert_equal(['b', 'bb'], argv())
460 call assert_equal('aa', expand('%:t'))
461 last
462 argdelete %
463 call assert_equal(['b'], argv())
Bram Moolenaar7b221172020-08-17 19:34:10 +0200464 call assert_fails('argdelete', 'E610:')
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100465 call assert_fails('1,100argdelete', 'E16:')
Bram Moolenaar531be472020-09-23 22:38:05 +0200466 call assert_fails('argdel /\)/', 'E55:')
Bram Moolenaarafe8cf62020-10-05 20:07:18 +0200467 call assert_fails('1argdel 1', 'E474:')
Bram Moolenaar7b221172020-08-17 19:34:10 +0200468
469 call Reset_arglist()
470 args a b c d
471 next
472 argdel
473 call Assert_argc(['a', 'c', 'd'])
474 %argdel
Dominique Pellebfb2bb12021-08-14 21:11:51 +0200475
476 call assert_fails('argdel does_not_exist', 'E480:')
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100477endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100478
Bram Moolenaarb513d302018-12-02 14:55:08 +0100479func Test_argdelete_completion()
480 args foo bar
481
482 call feedkeys(":argdelete \<C-A>\<C-B>\"\<CR>", 'tx')
483 call assert_equal('"argdelete bar foo', @:)
484
485 call feedkeys(":argdelete x \<C-A>\<C-B>\"\<CR>", 'tx')
486 call assert_equal('"argdelete x bar foo', @:)
487
488 %argd
489endfunc
490
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100491" Tests for the :next, :prev, :first, :last, :rewind commands
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100492func Test_argpos()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100493 call Reset_arglist()
494 args a b c d
495 last
496 call assert_equal(3, argidx())
497 call assert_fails('next', 'E165:')
498 prev
499 call assert_equal(2, argidx())
500 Next
501 call assert_equal(1, argidx())
502 first
503 call assert_equal(0, argidx())
504 call assert_fails('prev', 'E164:')
505 3next
506 call assert_equal(3, argidx())
507 rewind
508 call assert_equal(0, argidx())
509 %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100510endfunc
Bram Moolenaar53f16732016-09-07 20:46:39 +0200511
512" Test for autocommand that redefines the argument list, when doing ":all".
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100513func Test_arglist_autocmd()
Bram Moolenaar53f16732016-09-07 20:46:39 +0200514 autocmd BufReadPost Xxx2 next Xxx2 Xxx1
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100515 call writefile(['test file Xxx1'], 'Xxx1', 'D')
516 call writefile(['test file Xxx2'], 'Xxx2', 'D')
517 call writefile(['test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar53f16732016-09-07 20:46:39 +0200518
519 new
520 " redefine arglist; go to Xxx1
521 next! Xxx1 Xxx2 Xxx3
Bram Moolenaar679140c2021-12-24 18:58:46 +0000522 " open window for all args; Reading Xxx2 will try to change the arglist and
523 " that will fail
524 call assert_fails("all", "E1156:")
Bram Moolenaar53f16732016-09-07 20:46:39 +0200525 call assert_equal('test file Xxx1', getline(1))
526 wincmd w
Bram Moolenaar53f16732016-09-07 20:46:39 +0200527 call assert_equal('test file Xxx2', getline(1))
Bram Moolenaar679140c2021-12-24 18:58:46 +0000528 wincmd w
529 call assert_equal('test file Xxx3', getline(1))
Bram Moolenaar53f16732016-09-07 20:46:39 +0200530
531 autocmd! BufReadPost Xxx2
532 enew! | only
Bram Moolenaar53f16732016-09-07 20:46:39 +0200533 argdelete Xxx*
534 bwipe! Xxx1 Xxx2 Xxx3
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100535endfunc
536
537func Test_arg_all_expand()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100538 call writefile(['test file Xxx1'], 'Xx x', 'D')
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100539 next notexist Xx\ x runtest.vim
540 call assert_equal('notexist Xx\ x runtest.vim', expand('##'))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100541endfunc
Bram Moolenaare961cba2018-09-18 21:51:47 +0200542
543func Test_large_arg()
544 " Argument longer or equal to the number of columns used to cause
545 " access to invalid memory.
546 exe 'argadd ' .repeat('x', &columns)
547 args
548endfunc
Bram Moolenaar72e1b392019-08-03 13:50:08 +0200549
550func Test_argdo()
551 next! Xa.c Xb.c Xc.c
552 new
zeertzjq969e11a2025-03-10 21:15:19 +0100553
554 let g:bufenter = 0
555 let g:bufleave = 0
556 autocmd BufEnter * let g:bufenter += 1
557 autocmd BufLeave * let g:bufleave += 1
558
Bram Moolenaar72e1b392019-08-03 13:50:08 +0200559 let l = []
560 argdo call add(l, expand('%'))
561 call assert_equal(['Xa.c', 'Xb.c', 'Xc.c'], l)
zeertzjq969e11a2025-03-10 21:15:19 +0100562 call assert_equal(3, g:bufenter)
563 call assert_equal(3, g:bufleave)
564
565 let g:bufenter = 0
566 let g:bufleave = 0
567
568 set eventignore=BufEnter,BufLeave
569 let l = []
570 argdo call add(l, expand('%'))
571 call assert_equal(['Xa.c', 'Xb.c', 'Xc.c'], l)
572 call assert_equal(0, g:bufenter)
573 call assert_equal(0, g:bufleave)
574 call assert_equal('BufEnter,BufLeave', &eventignore)
575 set eventignore&
576
577 autocmd! BufEnter
578 autocmd! BufLeave
579 unlet g:bufenter
580 unlet g:bufleave
Bram Moolenaar72e1b392019-08-03 13:50:08 +0200581 bwipe Xa.c Xb.c Xc.c
582endfunc
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100583
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100584" Test for quitting Vim with unedited files in the argument list
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100585func Test_quit_with_arglist()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200586 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +0100587
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100588 let buf = RunVimInTerminal('', {'rows': 6})
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100589 call term_sendkeys(buf, ":set nomore\n")
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100590 call term_sendkeys(buf, ":args a b c\n")
591 call term_sendkeys(buf, ":quit\n")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200592 call TermWait(buf)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100593 call WaitForAssert({-> assert_match('^E173:', term_getline(buf, 6))})
594 call StopVimInTerminal(buf)
Bram Moolenaar406cd902020-02-18 21:54:41 +0100595
596 " Try :confirm quit with unedited files in arglist
597 let buf = RunVimInTerminal('', {'rows': 6})
598 call term_sendkeys(buf, ":set nomore\n")
599 call term_sendkeys(buf, ":args a b c\n")
600 call term_sendkeys(buf, ":confirm quit\n")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200601 call TermWait(buf)
Bram Moolenaar406cd902020-02-18 21:54:41 +0100602 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o: *$',
603 \ term_getline(buf, 6))})
604 call term_sendkeys(buf, "N")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200605 call TermWait(buf)
Bram Moolenaar406cd902020-02-18 21:54:41 +0100606 call term_sendkeys(buf, ":confirm quit\n")
607 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o: *$',
608 \ term_getline(buf, 6))})
609 call term_sendkeys(buf, "Y")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200610 call TermWait(buf)
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100611 call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})
612 only!
Bram Moolenaar1671f442020-03-10 07:48:13 +0100613 " When this test fails, swap files are left behind which breaks subsequent
614 " tests
615 call delete('.a.swp')
616 call delete('.b.swp')
617 call delete('.c.swp')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100618endfunc
619
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +0100620" Test for ":all" not working when in the cmdline window
621func Test_all_not_allowed_from_cmdwin()
622 au BufEnter * all
623 next x
Bram Moolenaarbed72df2021-01-27 20:34:29 +0100624 " Use try/catch here, somehow assert_fails() doesn't work on MS-Windows
625 " console.
626 let caught = 'no'
627 try
628 exe ":norm! 7q?apat\<CR>"
629 catch /E11:/
630 let caught = 'yes'
631 endtry
632 call assert_equal('yes', caught)
Bram Moolenaarbb4b93e2021-01-26 21:35:08 +0100633 au! BufEnter
634endfunc
635
Bram Moolenaar6f983712021-12-24 18:11:27 +0000636func Test_clear_arglist_in_all()
637 n 0 00 000 0000 00000 000000
Bram Moolenaarb6db1462021-12-24 19:24:47 +0000638 au WinNew 0 n 0
zeertzjq67fe77d2025-04-20 10:21:18 +0200639 call assert_fails("all", "E1156:")
Bram Moolenaar6f983712021-12-24 18:11:27 +0000640 au! *
641endfunc
642
Yegappan Lakshmanan0dc2fd32022-09-09 11:27:59 +0100643" Test for the :all command
644func Test_all_command()
645 %argdelete
646
647 " :all command should not close windows with files in the argument list,
648 " but can rearrange the windows.
649 args Xargnew1 Xargnew2
650 %bw!
651 edit Xargold1
652 split Xargnew1
653 let Xargnew1_winid = win_getid()
654 split Xargold2
655 split Xargnew2
656 let Xargnew2_winid = win_getid()
657 split Xargold3
658 all
659 call assert_equal(2, winnr('$'))
660 call assert_equal([Xargnew1_winid, Xargnew2_winid],
661 \ [win_getid(1), win_getid(2)])
662 call assert_equal([bufnr('Xargnew1'), bufnr('Xargnew2')],
663 \ [winbufnr(1), winbufnr(2)])
664
665 " :all command should close windows for files which are not in the
666 " argument list in the current tab page.
667 %bw!
668 edit Xargold1
669 split Xargold2
670 tabedit Xargold3
671 split Xargold4
672 tabedit Xargold5
673 tabfirst
674 all
675 call assert_equal(3, tabpagenr('$'))
676 call assert_equal([bufnr('Xargnew1'), bufnr('Xargnew2')], tabpagebuflist(1))
677 call assert_equal([bufnr('Xargold4'), bufnr('Xargold3')], tabpagebuflist(2))
678 call assert_equal([bufnr('Xargold5')], tabpagebuflist(3))
679
680 " :tab all command should close windows for files which are not in the
681 " argument list across all the tab pages.
682 %bw!
683 edit Xargold1
684 split Xargold2
685 tabedit Xargold3
686 split Xargold4
687 tabedit Xargold5
688 tabfirst
689 args Xargnew1 Xargnew2
690 tab all
691 call assert_equal(2, tabpagenr('$'))
692 call assert_equal([bufnr('Xargnew1')], tabpagebuflist(1))
693 call assert_equal([bufnr('Xargnew2')], tabpagebuflist(2))
694
695 " If a count is specified, then :all should open only that many windows.
696 %bw!
697 args Xargnew1 Xargnew2 Xargnew3 Xargnew4 Xargnew5
698 all 3
699 call assert_equal(3, winnr('$'))
700 call assert_equal([bufnr('Xargnew1'), bufnr('Xargnew2'), bufnr('Xargnew3')],
701 \ [winbufnr(1), winbufnr(2), winbufnr(3)])
702
703 " The :all command should not open more than 'tabpagemax' tab pages.
704 " If there are more files, then they should be opened in the last tab page.
705 %bw!
706 set tabpagemax=3
707 tab all
708 call assert_equal(3, tabpagenr('$'))
709 call assert_equal([bufnr('Xargnew1')], tabpagebuflist(1))
710 call assert_equal([bufnr('Xargnew2')], tabpagebuflist(2))
711 call assert_equal([bufnr('Xargnew3'), bufnr('Xargnew4'), bufnr('Xargnew5')],
712 \ tabpagebuflist(3))
713 set tabpagemax&
714
715 " Without the 'hidden' option, modified buffers should not be closed.
716 args Xargnew1 Xargnew2
717 %bw!
718 edit Xargtemp1
719 call setline(1, 'temp buffer 1')
720 split Xargtemp2
721 call setline(1, 'temp buffer 2')
722 all
723 call assert_equal(4, winnr('$'))
724 call assert_equal([bufnr('Xargtemp2'), bufnr('Xargtemp1'), bufnr('Xargnew1'),
725 \ bufnr('Xargnew2')],
726 \ [winbufnr(1), winbufnr(2), winbufnr(3), winbufnr(4)])
727
728 " With the 'hidden' option set, both modified and unmodified buffers in
729 " closed windows should be hidden.
730 set hidden
731 all
732 call assert_equal(2, winnr('$'))
733 call assert_equal([bufnr('Xargnew1'), bufnr('Xargnew2')],
734 \ [winbufnr(1), winbufnr(2)])
735 call assert_equal([1, 1, 0, 0], [getbufinfo('Xargtemp1')[0].hidden,
736 \ getbufinfo('Xargtemp2')[0].hidden,
737 \ getbufinfo('Xargnew1')[0].hidden,
738 \ getbufinfo('Xargnew2')[0].hidden])
739 set nohidden
740
741 " When 'winheight' is set to a large value, :all should open only one
742 " window.
743 args Xargnew1 Xargnew2 Xargnew3 Xargnew4 Xargnew5
744 %bw!
745 set winheight=9999
746 call assert_fails('all', 'E36:')
747 call assert_equal([1, bufnr('Xargnew1')], [winnr('$'), winbufnr(1)])
748 set winheight&
749
750 " When 'winwidth' is set to a large value, :vert all should open only one
751 " window.
752 %bw!
753 set winwidth=9999
754 call assert_fails('vert all', 'E36:')
755 call assert_equal([1, bufnr('Xargnew1')], [winnr('$'), winbufnr(1)])
756 set winwidth&
757
758 " empty argument list tests
759 %bw!
760 %argdelete
761 call assert_equal('', execute('args'))
762 all
763 call assert_equal(1, winnr('$'))
764
765 %argdelete
766 %bw!
767endfunc
768
Christian Brabandt0a6e57b2024-08-15 22:15:28 +0200769" Test for deleting buffer when creating an arglist. This was accessing freed
770" memory
771func Test_crash_arglist_uaf()
772 "%argdelete
773 new one
774 au BufAdd XUAFlocal :bw
775 "call assert_fails(':arglocal XUAFlocal', 'E163:')
776 arglocal XUAFlocal
777 au! BufAdd
778 bw! XUAFlocal
779
780 au BufAdd XUAFlocal2 :bw
781 new two
782 new three
783 arglocal
784 argadd XUAFlocal2 Xfoobar
785 bw! XUAFlocal2
786 bw! two
787
788 au! BufAdd
789endfunc
790
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100791" vim: shiftwidth=2 sts=2 expandtab