blob: 95e3c135f2ea91b80f6284c06ac983345f950c15 [file] [log] [blame]
Bram Moolenaar1381d792016-08-18 22:11:42 +02001" Tests for tabpage
2
3function Test_tabpage()
4 bw!
5 " Simple test for opening and closing a tab page
6 tabnew
7 call assert_equal(2, tabpagenr())
8 quit
9
10 " Open three tab pages and use ":tabdo"
11 0tabnew
12 1tabnew
13 $tabnew
Bram Moolenaar3e8474d2016-10-12 17:52:42 +020014 %del
Bram Moolenaar1381d792016-08-18 22:11:42 +020015 tabdo call append(line('$'), tabpagenr())
16 tabclose! 2
17 tabrewind
18 let line1 = getline('$')
19 undo
20 q
21 tablast
22 let line2 = getline('$')
23 q!
24 call append(line('$'), line1)
25 call append(line('$'), line2)
26 unlet line1 line2
27 call assert_equal(['', '3', '1', '4'], getline(1, '$'))
28 "
29 " Test for settabvar() and gettabvar() functions. Open a new tab page and
30 " set 3 variables to a number, string and a list. Verify that the variables
31 " are correctly set.
32 tabnew
33 tabfirst
34 call settabvar(2, 'val_num', 100)
35 call settabvar(2, 'val_str', 'SetTabVar test')
36 call settabvar(2, 'val_list', ['red', 'blue', 'green'])
37 "
38 call assert_true(gettabvar(2, 'val_num') == 100 && gettabvar(2, 'val_str') == 'SetTabVar test' && gettabvar(2, 'val_list') == ['red', 'blue', 'green'])
39
40 tabnext 2
41 call assert_true(t:val_num == 100 && t:val_str == 'SetTabVar test' && t:val_list == ['red', 'blue', 'green'])
42 tabclose
43
44 if has('gui') || has('clientserver')
45 " Test for ":tab drop exist-file" to keep current window.
46 sp test1
47 tab drop test1
48 call assert_true(tabpagenr('$') == 1 && winnr('$') == 2 && winnr() == 1)
49 close
50 "
51 "
52 " Test for ":tab drop new-file" to keep current window of tabpage 1.
53 split
54 tab drop newfile
55 call assert_true(tabpagenr('$') == 2 && tabpagewinnr(1, '$') == 2 && tabpagewinnr(1) == 1)
56 tabclose
57 q
58 "
59 "
60 " Test for ":tab drop multi-opend-file" to keep current tabpage and window.
61 new test1
62 tabnew
63 new test1
64 tab drop test1
65 call assert_true(tabpagenr() == 2 && tabpagewinnr(2, '$') == 2 && tabpagewinnr(2) == 1)
66 tabclose
67 q
Bram Moolenaar5a030a52016-12-01 17:48:29 +010068 "
69 "
70 " Test for ":tab drop vertical-split-window" to jump test1 buffer
71 tabedit test1
72 vnew
73 tabfirst
74 tab drop test1
75 call assert_equal([2, 2, 2, 2], [tabpagenr('$'), tabpagenr(), tabpagewinnr(2, '$'), tabpagewinnr(2)])
76 1tabonly
Bram Moolenaar1381d792016-08-18 22:11:42 +020077 endif
78 "
79 "
80 for i in range(9) | tabnew | endfor
81 normal! 1gt
82 call assert_equal(1, tabpagenr())
83 tabmove 5
84 call assert_equal(5, tabpagenr())
85 .tabmove
86 call assert_equal(5, tabpagenr())
87 tabmove -
88 call assert_equal(4, tabpagenr())
89 tabmove +
90 call assert_equal(5, tabpagenr())
91 tabmove -2
92 call assert_equal(3, tabpagenr())
93 tabmove +4
94 call assert_equal(7, tabpagenr())
95 tabmove
96 call assert_equal(10, tabpagenr())
Bram Moolenaar1381d792016-08-18 22:11:42 +020097 0tabmove
98 call assert_equal(1, tabpagenr())
99 $tabmove
100 call assert_equal(10, tabpagenr())
101 tabmove 0
102 call assert_equal(1, tabpagenr())
103 tabmove $
104 call assert_equal(10, tabpagenr())
105 3tabmove
106 call assert_equal(4, tabpagenr())
107 7tabmove 5
108 call assert_equal(5, tabpagenr())
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100109 call assert_fails("99tabmove", 'E16:')
110 call assert_fails("+99tabmove", 'E16:')
111 call assert_fails("-99tabmove", 'E16:')
Bram Moolenaar1381d792016-08-18 22:11:42 +0200112 call assert_fails("tabmove foo", 'E474:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100113 call assert_fails("tabmove 99", 'E474:')
114 call assert_fails("tabmove +99", 'E474:')
115 call assert_fails("tabmove -99", 'E474:')
116 call assert_fails("tabmove -3+", 'E474:')
117 call assert_fails("tabmove $3", 'E474:')
118 1tabonly!
Bram Moolenaar1381d792016-08-18 22:11:42 +0200119endfunc
120
121" Test autocommands
122function Test_tabpage_with_autocmd()
123 if !has('autocmd')
124 return
125 endif
Bram Moolenaar1381d792016-08-18 22:11:42 +0200126 command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
127 augroup TestTabpageGroup
128 au!
129 autocmd TabEnter * call add(s:li, 'TabEnter')
130 autocmd WinEnter * call add(s:li, 'WinEnter')
131 autocmd BufEnter * call add(s:li, 'BufEnter')
132 autocmd TabLeave * call add(s:li, 'TabLeave')
133 autocmd WinLeave * call add(s:li, 'WinLeave')
134 autocmd BufLeave * call add(s:li, 'BufLeave')
135 augroup END
136
137 let s:li = []
138 let t:a='a'
139 C tab split
140 call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
141 let s:li = []
142 let t:a='b'
143 C tabnew
144 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
145 let t:a='c'
146 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
147 call assert_equal(['a', 'b', 'c'], s:li)
148
149 let s:li = []
150 C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
151 call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
152 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
153 call assert_equal(['2', '4', '6'], s:li)
154
155 let s:li = []
156 let w:a='a'
157 C vsplit
158 call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
159 let s:li = []
160 let w:a='a'
161 let tabn=tabpagenr()
162 let winr=range(1, winnr('$'))
163 C tabnext 1
164 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
165 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
166 call assert_equal(['a', 'a'], s:li)
167 let s:li = []
168 C call map(copy(winr), 'settabwinvar('.tabn.', v:val, ''a'', v:val*2)')
169 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
170 call assert_equal(['2', '4'], s:li)
171
172 augroup TabDestructive
173 autocmd TabEnter * :C tabnext 2 | C tabclose 3
174 augroup END
175 let s:li = []
176 C tabnext 3
177 call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
178 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
179
180 autocmd! TabDestructive TabEnter
181 let s:li = []
182 C tabnew
183 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
184 let s:li = []
185 C tabnext 1
186 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
187
188 autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
189 let s:li = []
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100190 call assert_equal(3, tabpagenr('$'))
191 C tabnext 2
192 call assert_equal(2, tabpagenr('$'))
193 call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
Bram Moolenaar1381d792016-08-18 22:11:42 +0200194 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
195
196 delcommand C
197 autocmd! TabDestructive
198 augroup! TabDestructive
199 autocmd! TestTabpageGroup
200 augroup! TestTabpageGroup
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100201 1tabonly!
Bram Moolenaar1381d792016-08-18 22:11:42 +0200202endfunction
203
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200204function Test_tabpage_with_tab_modifier()
205 for n in range(4)
206 tabedit
207 endfor
208
209 function s:check_tab(pre_nr, cmd, post_nr)
210 exec 'tabnext ' . a:pre_nr
211 exec a:cmd
212 call assert_equal(a:post_nr, tabpagenr())
Bram Moolenaar100f5c92016-09-06 21:33:52 +0200213 call assert_equal('help', &buftype)
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200214 helpclose
215 endfunc
216
217 call s:check_tab(1, 'tab help', 2)
218 call s:check_tab(1, '3tab help', 4)
219 call s:check_tab(1, '.tab help', 2)
220 call s:check_tab(1, '.+1tab help', 3)
221 call s:check_tab(1, '0tab help', 1)
222 call s:check_tab(2, '+tab help', 4)
223 call s:check_tab(2, '+2tab help', 5)
224 call s:check_tab(4, '-tab help', 4)
225 call s:check_tab(4, '-2tab help', 3)
226 call s:check_tab(3, '$tab help', 6)
227 call assert_fails('99tab help', 'E16:')
228 call assert_fails('+99tab help', 'E16:')
229 call assert_fails('-99tab help', 'E16:')
230
231 delfunction s:check_tab
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100232 1tabonly!
233endfunction
234
235function Check_tab_count(pre_nr, cmd, post_nr)
236 exec 'tabnext' a:pre_nr
237 normal! G
238 exec a:cmd
239 call assert_equal(a:post_nr, tabpagenr(), a:cmd)
240endfunc
241
242" Test for [count] of tabnext
243function Test_tabpage_with_tabnext()
244 for n in range(4)
245 tabedit
246 call setline(1, ['', '', '3'])
247 endfor
248
249 call Check_tab_count(1, 'tabnext', 2)
250 call Check_tab_count(1, '3tabnext', 3)
251 call Check_tab_count(1, '.tabnext', 1)
252 call Check_tab_count(1, '.+1tabnext', 2)
253 call Check_tab_count(2, '+tabnext', 3)
254 call Check_tab_count(2, '+2tabnext', 4)
255 call Check_tab_count(4, '-tabnext', 3)
256 call Check_tab_count(4, '-2tabnext', 2)
257 call Check_tab_count(3, '$tabnext', 5)
258 call assert_fails('0tabnext', 'E16:')
259 call assert_fails('99tabnext', 'E16:')
260 call assert_fails('+99tabnext', 'E16:')
261 call assert_fails('-99tabnext', 'E16:')
262 call Check_tab_count(1, 'tabnext 3', 3)
263 call Check_tab_count(2, 'tabnext +', 3)
264 call Check_tab_count(2, 'tabnext +2', 4)
265 call Check_tab_count(4, 'tabnext -', 3)
266 call Check_tab_count(4, 'tabnext -2', 2)
267 call Check_tab_count(3, 'tabnext $', 5)
268 call assert_fails('tabnext 0', 'E474:')
269 call assert_fails('tabnext .', 'E474:')
270 call assert_fails('tabnext -+', 'E474:')
271 call assert_fails('tabnext +2-', 'E474:')
272 call assert_fails('tabnext $3', 'E474:')
273 call assert_fails('tabnext 99', 'E474:')
274 call assert_fails('tabnext +99', 'E474:')
275 call assert_fails('tabnext -99', 'E474:')
276
277 1tabonly!
278endfunction
279
280" Test for [count] of tabprevious
281function Test_tabpage_with_tabprevious()
282 for n in range(5)
283 tabedit
284 call setline(1, ['', '', '3'])
285 endfor
286
287 for cmd in ['tabNext', 'tabprevious']
288 call Check_tab_count(6, cmd, 5)
289 call Check_tab_count(6, '3' . cmd, 3)
290 call Check_tab_count(6, '8' . cmd, 4)
291 call Check_tab_count(6, cmd . ' 3', 3)
292 call Check_tab_count(6, cmd . ' 8', 4)
293 for n in range(2)
294 for c in ['0', '.+3', '+', '+2' , '-', '-2' , '$', '+99', '-99']
295 if n == 0 " pre count
296 let entire_cmd = c . cmd
297 let err_code = 'E16:'
298 else
299 let entire_cmd = cmd . ' ' . c
300 let err_code = 'E474:'
301 endif
302 call assert_fails(entire_cmd, err_code)
303 endfor
304 endfor
305 endfor
306
307 1tabonly!
308endfunction
309
310function s:reconstruct_tabpage_for_test(nr)
311 let n = (a:nr > 2) ? a:nr - 2 : 1
312 1tabonly!
313 0tabedit n0
314 for n in range(1, n)
315 exec '$tabedit n' . n
316 if n == 1
317 call setline(1, ['', '', '3'])
318 endif
319 endfor
320endfunc
321
322" Test for [count] of tabclose
323function Test_tabpage_with_tabclose()
324
325 " pre count
326 call s:reconstruct_tabpage_for_test(6)
327 call Check_tab_count(3, 'tabclose!', 3)
328 call Check_tab_count(1, '3tabclose', 1)
329 call Check_tab_count(4, '4tabclose', 3)
330 call Check_tab_count(3, '1tabclose', 2)
331 call Check_tab_count(2, 'tabclose', 1)
332 call assert_equal(1, tabpagenr('$'))
333 call assert_equal('', bufname(''))
334
335 call s:reconstruct_tabpage_for_test(6)
336 call Check_tab_count(2, '$tabclose', 2)
337 call Check_tab_count(4, '.tabclose', 4)
338 call Check_tab_count(3, '.+tabclose', 3)
339 call Check_tab_count(3, '.-2tabclose', 2)
340 call Check_tab_count(1, '.+1tabclose!', 1)
341 call assert_equal(1, tabpagenr('$'))
342 call assert_equal('', bufname(''))
343
344 " post count
345 call s:reconstruct_tabpage_for_test(6)
346 call Check_tab_count(3, 'tabclose!', 3)
347 call Check_tab_count(1, 'tabclose 3', 1)
348 call Check_tab_count(4, 'tabclose 4', 3)
349 call Check_tab_count(3, 'tabclose 1', 2)
350 call Check_tab_count(2, 'tabclose', 1)
351 call assert_equal(1, tabpagenr('$'))
352 call assert_equal('', bufname(''))
353
354 call s:reconstruct_tabpage_for_test(6)
355 call Check_tab_count(2, 'tabclose $', 2)
356 call Check_tab_count(4, 'tabclose', 4)
357 call Check_tab_count(3, 'tabclose +', 3)
358 call Check_tab_count(3, 'tabclose -2', 2)
359 call Check_tab_count(1, 'tabclose! +1', 1)
360 call assert_equal(1, tabpagenr('$'))
361 call assert_equal('', bufname(''))
362
363 call s:reconstruct_tabpage_for_test(6)
364 for n in range(2)
365 for c in ['0', '$3', '99', '+99', '-99']
366 if n == 0 " pre count
367 let entire_cmd = c . 'tabclose'
368 let err_code = 'E16:'
369 else
370 let entire_cmd = 'tabclose ' . c
371 let err_code = 'E474:'
372 endif
373 call assert_fails(entire_cmd, err_code)
374 call assert_equal(6, tabpagenr('$'))
375 endfor
376 endfor
377
378 call assert_fails('3tabclose', 'E37:')
379 call assert_fails('tabclose 3', 'E37:')
380 call assert_fails('tabclose -+', 'E474:')
381 call assert_fails('tabclose +2-', 'E474:')
382 call assert_equal(6, tabpagenr('$'))
383
384 1tabonly!
385endfunction
386
387" Test for [count] of tabonly
388function Test_tabpage_with_tabonly()
389
390 " Test for the normal behavior (pre count only)
391 let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
392 for c in tc
393 call s:reconstruct_tabpage_for_test(6)
394 let entire_cmd = c[1] . 'tabonly' . c[2]
395 call Check_tab_count(c[0], entire_cmd, 1)
396 call assert_equal(1, tabpagenr('$'))
397 endfor
398
399 " Test for the normal behavior
400 let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
401 \ [2, '', '!'],
402 \ [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
403 \ ]
404 for n in range(2)
405 for c in tc2
406 call s:reconstruct_tabpage_for_test(6)
407 if n == 0 " pre count
408 let entire_cmd = c[1] . 'tabonly' . c[2]
409 else
410 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
411 endif
412 call Check_tab_count(c[0], entire_cmd, 1)
413 call assert_equal(1, tabpagenr('$'))
414 endfor
415 endfor
416
417 " Test for the error behavior
418 for n in range(2)
419 for c in ['0', '$3', '99', '+99', '-99']
420 call s:reconstruct_tabpage_for_test(6)
421 if n == 0 " pre count
422 let entire_cmd = c . 'tabonly'
423 let err_code = 'E16:'
424 else
425 let entire_cmd = 'tabonly ' . c
426 let err_code = 'E474:'
427 endif
428 call assert_fails(entire_cmd, err_code)
429 call assert_equal(6, tabpagenr('$'))
430 endfor
431 endfor
432
433 " Test for the error behavior (post count only)
434 for c in tc
435 call s:reconstruct_tabpage_for_test(6)
436 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
437 let err_code = 'E474:'
438 call assert_fails(entire_cmd, err_code)
439 call assert_equal(6, tabpagenr('$'))
440 endfor
441
442 call assert_fails('tabonly -+', 'E474:')
443 call assert_fails('tabonly +2-', 'E474:')
444 call assert_equal(6, tabpagenr('$'))
445
446 1tabonly!
447 new
448 only!
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200449endfunction
450
Bram Moolenaar5a497892016-09-03 16:29:04 +0200451func Test_tabnext_on_buf_unload1()
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200452 " This once caused a crash
453 new
454 tabedit
455 tabfirst
456 au BufUnload <buffer> tabnext
457 q
458
459 while tabpagenr('$') > 1
Bram Moolenaar5a497892016-09-03 16:29:04 +0200460 bwipe!
461 endwhile
462endfunc
463
464func Test_tabnext_on_buf_unload2()
465 " This once caused a crash
466 tabedit
467 autocmd BufUnload <buffer> tabnext
468 file x
469 edit y
470
471 while tabpagenr('$') > 1
472 bwipe!
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200473 endwhile
474endfunc
475
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200476func Test_close_on_quitpre()
477 " This once caused a crash
Bram Moolenaarce11de82017-10-26 22:00:00 +0200478 edit Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200479 new
480 only
481 set bufhidden=delete
482 au QuitPre <buffer> close
483 tabnew tab1
484 tabnew tab2
485 1tabn
486 q!
487 call assert_equal(1, tabpagenr())
488 call assert_equal(2, tabpagenr('$'))
489 " clean up
490 while tabpagenr('$') > 1
491 bwipe!
492 endwhile
Bram Moolenaarce11de82017-10-26 22:00:00 +0200493 buf Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200494endfunc
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200495
Bram Moolenaar1381d792016-08-18 22:11:42 +0200496" vim: shiftwidth=2 sts=2 expandtab