3 function Test_tabpage()
5 " Simple test for opening and closing a tab page
7 call assert_equal(2, tabpagenr())
10 " Open three tab pages and use ":tabdo"
15 tabdo call append(line('$'), tabpagenr())
18 let line1 = getline('$')
22 let line2 = getline('$')
24 call append(line('$'), line1)
25 call append(line('$'), line2)
27 call assert_equal(['', '3', '1', '4'], getline(1, '$'))
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
34 call settabvar(2, 'val_num', 100)
35 call settabvar(2, 'val_str', 'SetTabVar test')
36 call settabvar(2, 'val_list', ['red', 'blue', 'green'])
38 call assert_true(gettabvar(2, 'val_num') == 100 && gettabvar(2, 'val_str') == 'SetTabVar test' && gettabvar(2, 'val_list') == ['red', 'blue', 'green'])
41 call assert_true(t:val_num == 100 && t:val_str == 'SetTabVar test' && t:val_list == ['red', 'blue', 'green'])
44 if has('gui') || has('clientserver')
45 " Test for ":tab drop exist-file" to keep current window.
48 call assert_true(tabpagenr('$') == 1 && winnr('$') == 2 && winnr() == 1)
52 " Test for ":tab drop new-file" to keep current window of tabpage 1.
55 call assert_true(tabpagenr('$') == 2 && tabpagewinnr(1, '$') == 2 && tabpagewinnr(1) == 1)
60 " Test for ":tab drop multi-opend-file" to keep current tabpage and window.
65 call assert_true(tabpagenr() == 2 && tabpagewinnr(2, '$') == 2 && tabpagewinnr(2) == 1)
70 " Test for ":tab drop vertical-split-window" to jump test1 buffer
75 call assert_equal([2, 2, 2, 2], [tabpagenr('$'), tabpagenr(), tabpagewinnr(2, '$'), tabpagewinnr(2)])
80 for i in range(9) | tabnew | endfor
82 call assert_equal(1, tabpagenr())
84 call assert_equal(5, tabpagenr())
86 call assert_equal(5, tabpagenr())
88 call assert_equal(4, tabpagenr())
90 call assert_equal(5, tabpagenr())
92 call assert_equal(3, tabpagenr())
94 call assert_equal(7, tabpagenr())
96 call assert_equal(10, tabpagenr())
98 call assert_equal(1, tabpagenr())
100 call assert_equal(10, tabpagenr())
102 call assert_equal(1, tabpagenr())
104 call assert_equal(10, tabpagenr())
106 call assert_equal(4, tabpagenr())
108 call assert_equal(5, tabpagenr())
109 call assert_fails("99tabmove", 'E16:')
110 call assert_fails("+99tabmove", 'E16:')
111 call assert_fails("-99tabmove", 'E16:')
112 call assert_fails("tabmove foo", 'E474:')
113 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:')
122 function Test_tabpage_with_autocmd()
126 command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
127 augroup TestTabpageGroup
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')
140 call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
144 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
146 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
147 call assert_equal(['a', 'b', 'c'], 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)
158 call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
162 let winr=range(1, winnr('$'))
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)
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)
172 augroup TabDestructive
173 autocmd TabEnter * :C tabnext 2 | C tabclose 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('$')])
180 autocmd! TabDestructive TabEnter
183 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
186 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
188 autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
190 call assert_equal(3, tabpagenr('$'))
192 call assert_equal(2, tabpagenr('$'))
193 call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
194 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
197 autocmd! TabDestructive
198 augroup! TabDestructive
199 autocmd! TestTabpageGroup
200 augroup! TestTabpageGroup
204 function Test_tabpage_with_tab_modifier()
209 function s:check_tab(pre_nr, cmd, post_nr)
210 exec 'tabnext ' . a:pre_nr
212 call assert_equal(a:post_nr, tabpagenr())
213 call assert_equal('help', &buftype)
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:')
231 delfunction s:check_tab
235 function Check_tab_count(pre_nr, cmd, post_nr)
236 exec 'tabnext' a:pre_nr
239 call assert_equal(a:post_nr, tabpagenr(), a:cmd)
242 " Test for [count] of tabnext
243 function Test_tabpage_with_tabnext()
246 call setline(1, ['', '', '3'])
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:')
280 " Test for [count] of tabprevious
281 function Test_tabpage_with_tabprevious()
284 call setline(1, ['', '', '3'])
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)
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:'
299 let entire_cmd = cmd . ' ' . c
300 let err_code = 'E474:'
302 call assert_fails(entire_cmd, err_code)
310 function s:reconstruct_tabpage_for_test(nr)
311 let n = (a:nr > 2) ? a:nr - 2 : 1
315 exec '$tabedit n' . n
317 call setline(1, ['', '', '3'])
322 " Test for [count] of tabclose
323 function Test_tabpage_with_tabclose()
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(''))
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(''))
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(''))
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(''))
363 call s:reconstruct_tabpage_for_test(6)
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:'
370 let entire_cmd = 'tabclose ' . c
371 let err_code = 'E474:'
373 call assert_fails(entire_cmd, err_code)
374 call assert_equal(6, tabpagenr('$'))
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('$'))
387 " Test for [count] of tabonly
388 function Test_tabpage_with_tabonly()
390 " Test for the normal behavior (pre count only)
391 let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
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('$'))
399 " Test for the normal behavior
400 let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
402 \ [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
406 call s:reconstruct_tabpage_for_test(6)
407 if n == 0 " pre count
408 let entire_cmd = c[1] . 'tabonly' . c[2]
410 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
412 call Check_tab_count(c[0], entire_cmd, 1)
413 call assert_equal(1, tabpagenr('$'))
417 " Test for the error behavior
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:'
425 let entire_cmd = 'tabonly ' . c
426 let err_code = 'E474:'
428 call assert_fails(entire_cmd, err_code)
429 call assert_equal(6, tabpagenr('$'))
433 " Test for the error behavior (post count only)
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('$'))
442 call assert_fails('tabonly -+', 'E474:')
443 call assert_fails('tabonly +2-', 'E474:')
444 call assert_equal(6, tabpagenr('$'))
451 func Test_tabnext_on_buf_unload1()
452 " This once caused a crash
456 au BufUnload <buffer> tabnext
459 while tabpagenr('$') > 1
464 func Test_tabnext_on_buf_unload2()
465 " This once caused a crash
467 autocmd BufUnload <buffer> tabnext
471 while tabpagenr('$') > 1
477 " vim: shiftwidth=2 sts=2 expandtab