blob: 19d0cee47a0f8ad2b32b94434e6fbbb8c914a026 [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
83func Init_abc()
84 args a b c
85 next
86endfunc
87
88func Assert_argc(l)
89 call assert_equal(len(a:l), argc())
90 let i = 0
91 while i < len(a:l) && i < argc()
92 call assert_equal(a:l[i], argv(i))
93 let i += 1
94 endwhile
95endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +010096
97" Test for [count]argument and [count]argdelete commands
98" Ported from the test_argument_count.in test script
Bram Moolenaar8c34aa02017-03-16 22:52:32 +010099func Test_argument()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100100 " Clean the argument list
101 arga a | %argd
102
103 let save_hidden = &hidden
104 set hidden
105
106 let g:buffers = []
107 augroup TEST
108 au BufEnter * call add(buffers, expand('%:t'))
109 augroup END
110
111 argadd a b c d
112 $argu
113 $-argu
114 -argu
115 1argu
116 +2argu
117
118 augroup TEST
119 au!
120 augroup END
121
122 call assert_equal(['d', 'c', 'b', 'a', 'c'], g:buffers)
123
124 redir => result
125 ar
126 redir END
127 call assert_true(result =~# 'a b \[c] d')
128
129 .argd
130 call assert_equal(['a', 'b', 'd'], argv())
131
132 -argd
133 call assert_equal(['a', 'd'], argv())
134
135 $argd
136 call assert_equal(['a'], argv())
137
138 1arga c
139 1arga b
140 $argu
141 $arga x
142 call assert_equal(['a', 'b', 'c', 'x'], argv())
143
Bram Moolenaar30141702016-01-19 14:14:08 +0100144 0arga y
145 call assert_equal(['y', 'a', 'b', 'c', 'x'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100146
147 %argd
148 call assert_equal([], argv())
149
150 arga a b c d e f
151 2,$-argd
152 call assert_equal(['a', 'f'], argv())
153
154 let &hidden = save_hidden
155
156 " Setting argument list should fail when the current buffer has unsaved
157 " changes
158 %argd
159 enew!
160 set modified
161 call assert_fails('args x y z', 'E37:')
162 args! x y z
163 call assert_equal(['x', 'y', 'z'], argv())
164 call assert_equal('x', expand('%:t'))
165
166 last | enew | argu
167 call assert_equal('z', expand('%:t'))
168
169 %argdelete
170 call assert_fails('argument', 'E163:')
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100171endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100172
173" Test for 0argadd and 0argedit
174" Ported from the test_argument_0count.in test script
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100175func Test_zero_argadd()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100176 " Clean the argument list
177 arga a | %argd
178
179 arga a b c d
180 2argu
181 0arga added
182 call assert_equal(['added', 'a', 'b', 'c', 'd'], argv())
183
184 2argu
185 arga third
186 call assert_equal(['added', 'a', 'third', 'b', 'c', 'd'], argv())
187
188 %argd
189 arga a b c d
190 2argu
191 0arge edited
192 call assert_equal(['edited', 'a', 'b', 'c', 'd'], argv())
193
194 2argu
195 arga third
196 call assert_equal(['edited', 'a', 'third', 'b', 'c', 'd'], argv())
Bram Moolenaar90305c62017-07-16 15:31:17 +0200197
198 2argu
199 argedit file\ with\ spaces another file
200 call assert_equal(['edited', 'a', 'file with spaces', 'another', 'file', 'third', 'b', 'c', 'd'], argv())
201 call assert_equal('file with spaces', expand('%'))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100202endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100203
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100204func Reset_arglist()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100205 args a | %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100206endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100207
208" Test for argc()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100209func Test_argc()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100210 call Reset_arglist()
211 call assert_equal(0, argc())
212 argadd a b
213 call assert_equal(2, argc())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100214endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100215
216" Test for arglistid()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100217func Test_arglistid()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100218 call Reset_arglist()
219 arga a b
220 call assert_equal(0, arglistid())
221 split
222 arglocal
223 call assert_equal(1, arglistid())
224 tabnew | tabfirst
225 call assert_equal(0, arglistid(2))
226 call assert_equal(1, arglistid(1, 1))
227 call assert_equal(0, arglistid(2, 1))
228 call assert_equal(1, arglistid(1, 2))
229 tabonly | only | enew!
230 argglobal
231 call assert_equal(0, arglistid())
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100232endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100233
234" Test for argv()
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100235func Test_argv()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100236 call Reset_arglist()
237 call assert_equal([], argv())
238 call assert_equal("", argv(2))
239 argadd a b c d
240 call assert_equal('c', argv(2))
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100241endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100242
243" Test for the :argedit command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100244func Test_argedit()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100245 call Reset_arglist()
246 argedit a
247 call assert_equal(['a'], argv())
248 call assert_equal('a', expand('%:t'))
249 argedit b
250 call assert_equal(['a', 'b'], argv())
251 call assert_equal('b', expand('%:t'))
252 argedit a
Bram Moolenaar90305c62017-07-16 15:31:17 +0200253 call assert_equal(['a', 'b', 'a'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100254 call assert_equal('a', expand('%:t'))
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200255 " When file name case is ignored, an existing buffer with only case
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200256 " difference is re-used.
Bram Moolenaar90305c62017-07-16 15:31:17 +0200257 argedit C D
258 call assert_equal('C', expand('%:t'))
259 call assert_equal(['a', 'b', 'a', 'C', 'D'], argv())
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100260 argedit c
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200261 if has('fname_case')
262 call assert_equal(['a', 'b', 'a', 'C', 'c', 'D'], argv())
263 else
264 call assert_equal(['a', 'b', 'a', 'C', 'C', 'D'], argv())
265 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100266 0argedit x
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200267 if has('fname_case')
268 call assert_equal(['x', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
269 else
270 call assert_equal(['x', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
271 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100272 enew! | set modified
273 call assert_fails('argedit y', 'E37:')
274 argedit! y
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200275 if has('fname_case')
276 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'c', 'D'], argv())
277 else
278 call assert_equal(['x', 'y', 'y', 'a', 'b', 'a', 'C', 'C', 'D'], argv())
279 endif
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100280 %argd
Bram Moolenaar9b50bba2017-07-16 16:42:13 +0200281 bwipe! C
282 bwipe! D
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100283endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100284
285" Test for the :argdelete command
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100286func Test_argdelete()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100287 call Reset_arglist()
288 args aa a aaa b bb
289 argdelete a*
290 call assert_equal(['b', 'bb'], argv())
291 call assert_equal('aa', expand('%:t'))
292 last
293 argdelete %
294 call assert_equal(['b'], argv())
295 call assert_fails('argdelete', 'E471:')
296 call assert_fails('1,100argdelete', 'E16:')
297 %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100298endfunc
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100299
300" Tests for the :next, :prev, :first, :last, :rewind commands
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100301func Test_argpos()
Bram Moolenaar99dbe292016-01-19 13:07:23 +0100302 call Reset_arglist()
303 args a b c d
304 last
305 call assert_equal(3, argidx())
306 call assert_fails('next', 'E165:')
307 prev
308 call assert_equal(2, argidx())
309 Next
310 call assert_equal(1, argidx())
311 first
312 call assert_equal(0, argidx())
313 call assert_fails('prev', 'E164:')
314 3next
315 call assert_equal(3, argidx())
316 rewind
317 call assert_equal(0, argidx())
318 %argd
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100319endfunc
Bram Moolenaar53f16732016-09-07 20:46:39 +0200320
321" Test for autocommand that redefines the argument list, when doing ":all".
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100322func Test_arglist_autocmd()
Bram Moolenaar53f16732016-09-07 20:46:39 +0200323 autocmd BufReadPost Xxx2 next Xxx2 Xxx1
324 call writefile(['test file Xxx1'], 'Xxx1')
325 call writefile(['test file Xxx2'], 'Xxx2')
326 call writefile(['test file Xxx3'], 'Xxx3')
327
328 new
329 " redefine arglist; go to Xxx1
330 next! Xxx1 Xxx2 Xxx3
331 " open window for all args
332 all
333 call assert_equal('test file Xxx1', getline(1))
334 wincmd w
335 wincmd w
336 call assert_equal('test file Xxx1', getline(1))
337 " should now be in Xxx2
338 rewind
339 call assert_equal('test file Xxx2', getline(1))
340
341 autocmd! BufReadPost Xxx2
342 enew! | only
343 call delete('Xxx1')
344 call delete('Xxx2')
345 call delete('Xxx3')
346 argdelete Xxx*
347 bwipe! Xxx1 Xxx2 Xxx3
Bram Moolenaar8c34aa02017-03-16 22:52:32 +0100348endfunc
349
350func Test_arg_all_expand()
351 call writefile(['test file Xxx1'], 'Xx x')
352 next notexist Xx\ x runtest.vim
353 call assert_equal('notexist Xx\ x runtest.vim', expand('##'))
354 call delete('Xx x')
355endfunc