test_tabpage.vim (29388B)
1 " Tests for tabpage 2 3 source screendump.vim 4 source check.vim 5 6 function Test_tabpage() 7 CheckFeature quickfix 8 9 bw! 10 " Simple test for opening and closing a tab page 11 tabnew 12 call assert_equal(2, tabpagenr()) 13 quit 14 15 " Open three tab pages and use ":tabdo" 16 0tabnew 17 1tabnew 18 $tabnew 19 %del 20 tabdo call append(line('$'), tabpagenr()) 21 tabclose! 2 22 tabrewind 23 let line1 = getline('$') 24 undo 25 q 26 tablast 27 let line2 = getline('$') 28 q! 29 call append(line('$'), line1) 30 call append(line('$'), line2) 31 unlet line1 line2 32 call assert_equal(['', '3', '1', '4'], getline(1, '$')) 33 " 34 " Test for settabvar() and gettabvar() functions. Open a new tab page and 35 " set 3 variables to a number, string and a list. Verify that the variables 36 " are correctly set. 37 tabnew 38 tabfirst 39 call settabvar(2, 'val_num', 100) 40 eval 'SetTabVar test'->settabvar(2, 'val_str') 41 call settabvar(2, 'val_list', ['red', 'blue', 'green']) 42 " 43 call assert_true(gettabvar(2, 'val_num') == 100 && gettabvar(2, 'val_str') == 'SetTabVar test' && gettabvar(2, 'val_list') == ['red', 'blue', 'green']) 44 45 tabnext 2 46 call assert_true(t:val_num == 100 && t:val_str == 'SetTabVar test' && t:val_list == ['red', 'blue', 'green']) 47 tabclose 48 49 " Test for ":tab drop exist-file" to keep current window. 50 sp test1 51 tab drop test1 52 call assert_true(tabpagenr('$') == 1 && winnr('$') == 2 && winnr() == 1) 53 close 54 " 55 " 56 " Test for ":tab drop new-file" to keep current window of tabpage 1. 57 split 58 tab drop newfile 59 call assert_true(tabpagenr('$') == 2 && tabpagewinnr(1, '$') == 2 && tabpagewinnr(1) == 1) 60 tabclose 61 q 62 " 63 " 64 " Test for ":tab drop multi-opened-file" to keep current tabpage and window. 65 new test1 66 tabnew 67 new test1 68 tab drop test1 69 call assert_true(tabpagenr() == 2 && tabpagewinnr(2, '$') == 2 && tabpagewinnr(2) == 1) 70 tabclose 71 q 72 " 73 " 74 " Test for ":tab drop vertical-split-window" to jump test1 buffer 75 tabedit test1 76 vnew 77 tabfirst 78 tab drop test1 79 call assert_equal([2, 2, 2, 2], [tabpagenr('$'), tabpagenr(), tabpagewinnr(2, '$'), tabpagewinnr(2)]) 80 1tabonly 81 " 82 " 83 for i in range(9) | tabnew | endfor 84 normal! 1gt 85 call assert_equal(1, tabpagenr()) 86 tabmove 5 87 call assert_equal(5, tabpagenr()) 88 .tabmove 89 call assert_equal(5, tabpagenr()) 90 tabmove - 91 call assert_equal(4, tabpagenr()) 92 tabmove + 93 call assert_equal(5, tabpagenr()) 94 tabmove -2 95 call assert_equal(3, tabpagenr()) 96 tabmove +4 97 call assert_equal(7, tabpagenr()) 98 tabmove 99 call assert_equal(10, tabpagenr()) 100 0tabmove 101 call assert_equal(1, tabpagenr()) 102 $tabmove 103 call assert_equal(10, tabpagenr()) 104 tabmove 0 105 call assert_equal(1, tabpagenr()) 106 tabmove $ 107 call assert_equal(10, tabpagenr()) 108 3tabmove 109 call assert_equal(4, tabpagenr()) 110 7tabmove 5 111 call assert_equal(5, tabpagenr()) 112 -tabmove 113 call assert_equal(4, tabpagenr()) 114 +tabmove 115 call assert_equal(5, tabpagenr()) 116 -2tabmove 117 call assert_equal(3, tabpagenr()) 118 +3tabmove 119 call assert_equal(6, tabpagenr()) 120 silent -tabmove 121 call assert_equal(5, tabpagenr()) 122 silent -2 tabmove 123 call assert_equal(3, tabpagenr()) 124 silent -2 tabmove 125 call assert_equal(1, tabpagenr()) 126 127 norm! 2gt 128 call assert_equal(2, tabpagenr()) 129 " The following are a no-op 130 tabmove 2 131 call assert_equal(2, tabpagenr()) 132 2tabmove 133 call assert_equal(2, tabpagenr()) 134 tabmove 1 135 call assert_equal(2, tabpagenr()) 136 1tabmove 137 call assert_equal(2, tabpagenr()) 138 139 call assert_fails('let t = tabpagenr("@")', 'E15:') 140 call assert_equal(0, tabpagewinnr(-1)) 141 call assert_fails("99tabmove", 'E16:') 142 call assert_fails("+99tabmove", 'E16:') 143 call assert_fails("-99tabmove", 'E16:') 144 call assert_fails("tabmove foo", 'E475:') 145 call assert_fails("tabmove 99", 'E475:') 146 call assert_fails("tabmove +99", 'E475:') 147 call assert_fails("tabmove -99", 'E475:') 148 call assert_fails("tabmove -3+", 'E475:') 149 call assert_fails("tabmove $3", 'E475:') 150 call assert_fails("%tabonly", 'E16:') 151 1tabonly! 152 tabmove 1 153 call assert_equal(1, tabpagenr()) 154 tabnew 155 call assert_fails("-2tabmove", 'E16:') 156 tabonly! 157 endfunc 158 159 func Test_tabpage_drop() 160 edit f1 161 tab split f2 162 tab split f3 163 normal! gt 164 call assert_equal(1, tabpagenr()) 165 tab drop f4 166 call assert_equal(1, tabpagenr('#')) 167 168 tab drop f3 169 call assert_equal(4, tabpagenr()) 170 call assert_equal(2, tabpagenr('#')) 171 bwipe! 172 bwipe! 173 bwipe! 174 bwipe! 175 call assert_equal(1, tabpagenr('$')) 176 177 call assert_equal(1, winnr('$')) 178 call assert_equal('', bufname('')) 179 call writefile(['L1', 'L2'], 'Xdropfile', 'D') 180 181 " Test for ':tab drop single-file': reuse current buffer 182 let expected_nr = bufnr() 183 tab drop Xdropfile 184 call assert_equal(1, tabpagenr('$')) 185 call assert_equal(expected_nr, bufnr()) 186 call assert_equal('L2', getline(2)) 187 bwipe! 188 189 " Test for ':tab drop single-file': not reuse modified buffer 190 set modified 191 let expected_nr = bufnr() + 1 192 tab drop Xdropfile 193 call assert_equal(2, tabpagenr()) 194 call assert_equal(2, tabpagenr('$')) 195 call assert_equal(expected_nr, bufnr()) 196 call assert_equal('L2', getline(2)) 197 bwipe! 198 199 " Test for ':tab drop single-file': multiple tabs already exist 200 tab split f2 201 tab split f3 202 let expected_nr = bufnr() + 1 203 tab drop Xdropfile 204 call assert_equal(4, tabpagenr()) 205 call assert_equal(4, tabpagenr('$')) 206 call assert_equal(expected_nr, bufnr()) 207 call assert_equal('L2', getline(2)) 208 %bwipe! 209 210 " Test for ':tab drop multi-files': reuse current buffer 211 let expected_nr = bufnr() 212 tab drop Xdropfile f1 f2 f3 213 call assert_equal(1, tabpagenr()) 214 call assert_equal(4, tabpagenr('$')) 215 call assert_equal(expected_nr, bufnr()) 216 call assert_equal('L2', getline(2)) 217 %bwipe! 218 219 " Test for ':tab drop multi-files': not reuse modified buffer 220 set modified 221 let expected_nr = bufnr() + 1 222 tab drop Xdropfile f1 f2 f3 223 call assert_equal(2, tabpagenr()) 224 call assert_equal(5, tabpagenr('$')) 225 call assert_equal(expected_nr, bufnr()) 226 call assert_equal('L2', getline(2)) 227 %bwipe! 228 229 " Test for ':tab drop multi-files': multiple tabs already exist 230 tab split f2 231 tab split f3 232 let expected_nr = bufnr() + 1 233 tab drop a b c 234 call assert_equal(4, tabpagenr()) 235 call assert_equal(6, tabpagenr('$')) 236 call assert_equal(expected_nr, bufnr()) 237 let expected_nr = bufnr() + 3 238 tab drop Xdropfile f1 f2 f3 239 call assert_equal(5, tabpagenr()) 240 call assert_equal(8, tabpagenr('$')) 241 call assert_equal(expected_nr, bufnr()) 242 call assert_equal('L2', getline(2)) 243 %bwipe! 244 endfunc 245 246 " Test autocommands 247 function Test_tabpage_with_autocmd() 248 command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args> 249 augroup TestTabpageGroup 250 au! 251 autocmd TabEnter * call add(s:li, 'TabEnter') 252 autocmd WinEnter * call add(s:li, 'WinEnter') 253 autocmd BufEnter * call add(s:li, 'BufEnter') 254 autocmd TabLeave * call add(s:li, 'TabLeave') 255 autocmd WinLeave * call add(s:li, 'WinLeave') 256 autocmd BufLeave * call add(s:li, 'BufLeave') 257 augroup END 258 259 let s:li = [] 260 let t:a='a' 261 C tab split 262 call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li) 263 let s:li = [] 264 let t:a='b' 265 C tabnew 266 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li) 267 let t:a='c' 268 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+') 269 call assert_equal(['a', 'b', 'c'], s:li) 270 271 let s:li = [] 272 C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') 273 call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li) 274 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+') 275 call assert_equal(['2', '4', '6'], s:li) 276 277 let s:li = [] 278 let w:a='a' 279 C vsplit 280 call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li) 281 let s:li = [] 282 let w:a='a' 283 let tabn=tabpagenr() 284 let winr=range(1, winnr('$')) 285 C tabnext 1 286 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li) 287 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+') 288 call assert_equal(['a', 'a'], s:li) 289 let s:li = [] 290 C call map(copy(winr), '(v:val*2)->settabwinvar(' .. tabn .. ', v:val, ''a'')') 291 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+') 292 call assert_equal(['2', '4'], s:li) 293 294 augroup TabDestructive 295 autocmd TabEnter * :C tabnext 2 | C tabclose 3 296 augroup END 297 let s:li = [] 298 C tabnext 3 299 call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li) 300 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')]) 301 302 autocmd! TabDestructive TabEnter 303 let s:li = [] 304 C tabnew 305 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li) 306 let s:li = [] 307 C tabnext 1 308 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li) 309 310 autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3 311 let s:li = [] 312 call assert_equal(3, tabpagenr('$')) 313 C tabnext 2 314 call assert_equal(2, tabpagenr('$')) 315 call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li) 316 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')]) 317 318 delcommand C 319 autocmd! TabDestructive 320 augroup! TabDestructive 321 autocmd! TestTabpageGroup 322 augroup! TestTabpageGroup 323 1tabonly! 324 endfunction 325 326 " Test autocommands on tab drop 327 function Test_tabpage_with_autocmd_tab_drop() 328 augroup TestTabpageGroup 329 au! 330 autocmd TabEnter * call add(s:li, 'TabEnter') 331 autocmd WinEnter * call add(s:li, 'WinEnter') 332 autocmd BufEnter * call add(s:li, 'BufEnter') 333 autocmd TabLeave * call add(s:li, 'TabLeave') 334 autocmd WinLeave * call add(s:li, 'WinLeave') 335 autocmd BufLeave * call add(s:li, 'BufLeave') 336 augroup END 337 338 let s:li = [] 339 tab drop test1 340 call assert_equal(['BufEnter'], s:li) 341 342 let s:li = [] 343 tab drop test2 test3 344 call assert_equal([ 345 \ 'TabLeave', 'TabEnter', 'TabLeave', 'TabEnter', 346 \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter', 347 \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter', 'BufEnter'], s:li) 348 349 autocmd! TestTabpageGroup 350 augroup! TestTabpageGroup 351 1tabonly! 352 endfunction 353 354 function Test_tabpage_with_tab_modifier() 355 CheckFeature quickfix 356 357 for n in range(4) 358 tabedit 359 endfor 360 361 function s:check_tab(pre_nr, cmd, post_nr) 362 exec 'tabnext ' . a:pre_nr 363 exec a:cmd 364 call assert_equal(a:post_nr, tabpagenr()) 365 call assert_equal('help', &buftype) 366 helpclose 367 endfunc 368 369 call s:check_tab(1, 'tab help', 2) 370 call s:check_tab(1, '3tab help', 4) 371 call s:check_tab(1, '.tab help', 2) 372 call s:check_tab(1, '.+1tab help', 3) 373 call s:check_tab(1, '0tab help', 1) 374 call s:check_tab(2, '+tab help', 4) 375 call s:check_tab(2, '+2tab help', 5) 376 call s:check_tab(4, '-tab help', 4) 377 call s:check_tab(4, '-2tab help', 3) 378 call s:check_tab(3, '$tab help', 6) 379 call assert_fails('99tab help', 'E16:') 380 call assert_fails('+99tab help', 'E16:') 381 call assert_fails('-99tab help', 'E16:') 382 383 delfunction s:check_tab 384 1tabonly! 385 endfunction 386 387 function Check_tab_count(pre_nr, cmd, post_nr) 388 exec 'tabnext' a:pre_nr 389 normal! G 390 exec a:cmd 391 call assert_equal(a:post_nr, tabpagenr(), a:cmd) 392 endfunc 393 394 " Test for [count] of tabnext 395 function Test_tabpage_with_tabnext() 396 for n in range(4) 397 tabedit 398 call setline(1, ['', '', '3']) 399 endfor 400 401 call Check_tab_count(1, 'tabnext', 2) 402 call Check_tab_count(1, '3tabnext', 3) 403 call Check_tab_count(1, '.tabnext', 1) 404 call Check_tab_count(1, '.+1tabnext', 2) 405 call Check_tab_count(2, '+tabnext', 3) 406 call Check_tab_count(2, '+2tabnext', 4) 407 call Check_tab_count(4, '-tabnext', 3) 408 call Check_tab_count(4, '-2tabnext', 2) 409 call Check_tab_count(3, '$tabnext', 5) 410 call assert_fails('0tabnext', 'E16:') 411 call assert_fails('99tabnext', 'E16:') 412 call assert_fails('+99tabnext', 'E16:') 413 call assert_fails('-99tabnext', 'E16:') 414 call Check_tab_count(1, 'tabnext 3', 3) 415 call Check_tab_count(2, 'tabnext +', 3) 416 call Check_tab_count(2, 'tabnext +2', 4) 417 call Check_tab_count(4, 'tabnext -', 3) 418 call Check_tab_count(4, 'tabnext -2', 2) 419 call Check_tab_count(3, 'tabnext $', 5) 420 call assert_fails('tabnext 0', 'E475:') 421 call assert_fails('tabnext .', 'E475:') 422 call assert_fails('tabnext -+', 'E475:') 423 call assert_fails('tabnext +2-', 'E475:') 424 call assert_fails('tabnext $3', 'E475:') 425 call assert_fails('tabnext 99', 'E475:') 426 call assert_fails('tabnext +99', 'E475:') 427 call assert_fails('tabnext -99', 'E475:') 428 429 1tabonly! 430 endfunction 431 432 " Test for [count] of tabprevious 433 function Test_tabpage_with_tabprevious() 434 for n in range(5) 435 tabedit 436 call setline(1, ['', '', '3']) 437 endfor 438 439 for cmd in ['tabNext', 'tabprevious'] 440 call Check_tab_count(6, cmd, 5) 441 call Check_tab_count(6, '3' . cmd, 3) 442 call Check_tab_count(6, '8' . cmd, 4) 443 call Check_tab_count(6, cmd . ' 3', 3) 444 call Check_tab_count(6, cmd . ' 8', 4) 445 for n in range(2) 446 for c in ['0', '.+3', '+', '+2' , '-', '-2' , '$', '+99', '-99'] 447 if n == 0 " pre count 448 let entire_cmd = c . cmd 449 let err_code = 'E16:' 450 else 451 let entire_cmd = cmd . ' ' . c 452 let err_code = 'E475:' 453 endif 454 call assert_fails(entire_cmd, err_code) 455 endfor 456 endfor 457 endfor 458 459 1tabonly! 460 endfunction 461 462 function s:reconstruct_tabpage_for_test(nr) 463 let n = (a:nr > 2) ? a:nr - 2 : 1 464 1tabonly! 465 0tabedit n0 466 for n in range(1, n) 467 exec '$tabedit n' . n 468 if n == 1 469 call setline(1, ['', '', '3']) 470 endif 471 endfor 472 endfunc 473 474 func Test_tabpage_ctrl_pgup_pgdown() 475 enew! 476 tabnew tab1 477 tabnew tab2 478 479 call assert_equal(3, tabpagenr()) 480 exe "norm! \<C-PageUp>" 481 call assert_equal(2, tabpagenr()) 482 exe "norm! \<C-PageDown>" 483 call assert_equal(3, tabpagenr()) 484 485 " Check wrapping at last or first page. 486 exe "norm! \<C-PageDown>" 487 call assert_equal(1, tabpagenr()) 488 exe "norm! \<C-PageUp>" 489 call assert_equal(3, tabpagenr()) 490 491 " With a count, <C-PageUp> and <C-PageDown> are not symmetrical somehow: 492 " - {count}<C-PageUp> goes {count} pages downward (relative count) 493 " - {count}<C-PageDown> goes to page number {count} (absolute count) 494 exe "norm! 2\<C-PageUp>" 495 call assert_equal(1, tabpagenr()) 496 exe "norm! 2\<C-PageDown>" 497 call assert_equal(2, tabpagenr()) 498 499 1tabonly! 500 endfunc 501 502 " Test for [count] of tabclose 503 function Test_tabpage_with_tabclose() 504 505 " pre count 506 call s:reconstruct_tabpage_for_test(6) 507 call Check_tab_count(3, 'tabclose!', 3) 508 call Check_tab_count(1, '3tabclose', 1) 509 call Check_tab_count(4, '4tabclose', 3) 510 call Check_tab_count(3, '1tabclose', 2) 511 call Check_tab_count(2, 'tabclose', 1) 512 call assert_equal(1, tabpagenr('$')) 513 call assert_equal('', bufname('')) 514 515 call s:reconstruct_tabpage_for_test(6) 516 call Check_tab_count(2, '$tabclose', 2) 517 call Check_tab_count(4, '.tabclose', 4) 518 call Check_tab_count(3, '.+tabclose', 3) 519 call Check_tab_count(3, '.-2tabclose', 2) 520 call Check_tab_count(1, '.+1tabclose!', 1) 521 call assert_equal(1, tabpagenr('$')) 522 call assert_equal('', bufname('')) 523 524 " post count 525 call s:reconstruct_tabpage_for_test(6) 526 call Check_tab_count(3, 'tabclose!', 3) 527 call Check_tab_count(1, 'tabclose 3', 1) 528 call Check_tab_count(4, 'tabclose 4', 3) 529 call Check_tab_count(3, 'tabclose 1', 2) 530 call Check_tab_count(2, 'tabclose', 1) 531 call assert_equal(1, tabpagenr('$')) 532 call assert_equal('', bufname('')) 533 534 call s:reconstruct_tabpage_for_test(6) 535 call Check_tab_count(2, 'tabclose $', 2) 536 call Check_tab_count(4, 'tabclose', 4) 537 call Check_tab_count(3, 'tabclose +', 3) 538 call Check_tab_count(3, 'tabclose -2', 2) 539 call Check_tab_count(1, 'tabclose! +1', 1) 540 call assert_equal(1, tabpagenr('$')) 541 call assert_equal('', bufname('')) 542 543 call s:reconstruct_tabpage_for_test(6) 544 for n in range(2) 545 for c in ['0', '$3', '99', '+99', '-99'] 546 if n == 0 " pre count 547 let entire_cmd = c . 'tabclose' 548 let err_code = 'E16:' 549 else 550 let entire_cmd = 'tabclose ' . c 551 let err_code = 'E475:' 552 endif 553 call assert_fails(entire_cmd, err_code) 554 call assert_equal(6, tabpagenr('$')) 555 endfor 556 endfor 557 558 call assert_fails('3tabclose', 'E37:') 559 call assert_fails('tabclose 3', 'E37:') 560 call assert_fails('tabclose -+', 'E475:') 561 call assert_fails('tabclose +2-', 'E475:') 562 call assert_equal(6, tabpagenr('$')) 563 564 1tabonly! 565 endfunction 566 567 " Test for [count] of tabonly 568 function Test_tabpage_with_tabonly() 569 570 " Test for the normal behavior (pre count only) 571 let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ] 572 for c in tc 573 call s:reconstruct_tabpage_for_test(6) 574 let entire_cmd = c[1] . 'tabonly' . c[2] 575 call Check_tab_count(c[0], entire_cmd, 1) 576 call assert_equal(1, tabpagenr('$')) 577 endfor 578 579 " Test for the normal behavior 580 let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'], 581 \ [2, '', '!'], 582 \ [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!'] 583 \ ] 584 for n in range(2) 585 for c in tc2 586 call s:reconstruct_tabpage_for_test(6) 587 if n == 0 " pre count 588 let entire_cmd = c[1] . 'tabonly' . c[2] 589 else 590 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1] 591 endif 592 call Check_tab_count(c[0], entire_cmd, 1) 593 call assert_equal(1, tabpagenr('$')) 594 endfor 595 endfor 596 597 " Test for the error behavior 598 for n in range(2) 599 for c in ['0', '$3', '99', '+99', '-99'] 600 call s:reconstruct_tabpage_for_test(6) 601 if n == 0 " pre count 602 let entire_cmd = c . 'tabonly' 603 let err_code = 'E16:' 604 else 605 let entire_cmd = 'tabonly ' . c 606 let err_code = 'E475:' 607 endif 608 call assert_fails(entire_cmd, err_code) 609 call assert_equal(6, tabpagenr('$')) 610 endfor 611 endfor 612 613 " Test for the error behavior (post count only) 614 for c in tc 615 call s:reconstruct_tabpage_for_test(6) 616 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1] 617 let err_code = 'E475:' 618 call assert_fails(entire_cmd, err_code) 619 call assert_equal(6, tabpagenr('$')) 620 endfor 621 622 call assert_fails('tabonly -+', 'E475:') 623 call assert_fails('tabonly +2-', 'E475:') 624 call assert_equal(6, tabpagenr('$')) 625 626 1tabonly! 627 new 628 only! 629 endfunction 630 631 func Test_tabnext_on_buf_unload1() 632 " This once caused a crash 633 new 634 tabedit 635 tabfirst 636 au BufUnload <buffer> tabnext 637 q 638 639 while tabpagenr('$') > 1 640 bwipe! 641 endwhile 642 endfunc 643 644 func Test_tabnext_on_buf_unload2() 645 " This once caused a crash 646 tabedit 647 autocmd BufUnload <buffer> tabnext 648 file x 649 edit y 650 651 while tabpagenr('$') > 1 652 bwipe! 653 endwhile 654 endfunc 655 656 func Test_close_on_quitpre() 657 " This once caused a crash 658 edit Xtest 659 new 660 only 661 set bufhidden=delete 662 au QuitPre <buffer> close 663 tabnew tab1 664 tabnew tab2 665 1tabn 666 q! 667 call assert_equal(1, tabpagenr()) 668 call assert_equal(2, tabpagenr('$')) 669 " clean up 670 while tabpagenr('$') > 1 671 bwipe! 672 endwhile 673 buf Xtest 674 endfunc 675 676 func Test_tabs() 677 enew! 678 tabnew tab1 679 norm ixxx 680 let a=split(execute(':tabs'), "\n") 681 call assert_equal(['Tab page 1', 682 \ '# [No Name]', 683 \ 'Tab page 2', 684 \ '> + tab1'], a) 685 686 1tabonly! 687 bw! 688 endfunc 689 690 func Test_tabpage_cmdheight() 691 CheckScreendump 692 CheckRunVimInTerminal 693 call writefile([ 694 \ 'set laststatus=2', 695 \ 'set cmdheight=2', 696 \ 'tabnew', 697 \ 'set cmdheight=3', 698 \ 'tabnext', 699 \ 'redraw!', 700 \ 'echo "hello\nthere"', 701 \ 'tabnext', 702 \ 'redraw', 703 \ ], 'XTest_tabpage_cmdheight') 704 " Check that cursor line is concealed 705 let buf = RunVimInTerminal('-S XTest_tabpage_cmdheight', {'statusoff': 3}) 706 call VerifyScreenDump(buf, 'Test_tabpage_cmdheight', {}) 707 708 call StopVimInTerminal(buf) 709 call delete('XTest_tabpage_cmdheight') 710 endfunc 711 712 " Test for closing the tab page from a command window 713 func Test_tabpage_close_cmdwin() 714 tabnew 715 call feedkeys("q/:tabclose\<CR>\<Esc>", 'xt') 716 call assert_equal(2, tabpagenr('$')) 717 call feedkeys("q/:tabonly\<CR>\<Esc>", 'xt') 718 call assert_equal(2, tabpagenr('$')) 719 tabonly 720 endfunc 721 722 " Pressing <C-PageUp> in insert mode should go to the previous tab page 723 " and <C-PageDown> should go to the next tab page 724 func Test_tabpage_Ctrl_Pageup() 725 tabnew 726 call feedkeys("i\<C-PageUp>", 'xt') 727 call assert_equal(1, tabpagenr()) 728 call feedkeys("i\<C-PageDown>", 'xt') 729 call assert_equal(2, tabpagenr()) 730 %bw! 731 endfunc 732 733 " Return the terminal key code for selecting a tab page from the tabline. This 734 " sequence contains the following codes: a CSI (0x9b), KS_TABLINE (0xf0), 735 " KS_FILLER (0x58) and then the tab page number. 736 func TabLineSelectPageCode(tabnr) 737 return "\x9b\xf0\x58" .. nr2char(a:tabnr) 738 endfunc 739 740 " Return the terminal key code for opening a new tabpage from the tabpage 741 " menu. This sequence consists of the following codes: a CSI (0x9b), 742 " KS_TABMENU (0xef), KS_FILLER (0x58), the tab page number and 743 " TABLINE_MENU_NEW (2). 744 func TabMenuNewItemCode(tabnr) 745 return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(2) 746 endfunc 747 748 " Return the terminal key code for closing a tabpage from the tabpage menu. 749 " This sequence consists of the following codes: a CSI (0x9b), KS_TABMENU 750 " (0xef), KS_FILLER (0x58), the tab page number and TABLINE_MENU_CLOSE (1). 751 func TabMenuCloseItemCode(tabnr) 752 return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(1) 753 endfunc 754 755 " Test for using the tabpage menu from the insert and normal modes 756 func Test_tabline_tabmenu() 757 " only works in GUI 758 CheckGui 759 760 %bw! 761 tabnew 762 tabnew 763 call assert_equal(3, tabpagenr()) 764 765 " go to tab page 2 in normal mode 766 call feedkeys(TabLineSelectPageCode(2), "Lx!") 767 call assert_equal(2, tabpagenr()) 768 769 " close tab page 3 in normal mode 770 call feedkeys(TabMenuCloseItemCode(3), "Lx!") 771 call assert_equal(2, tabpagenr('$')) 772 call assert_equal(2, tabpagenr()) 773 774 " open new tab page before tab page 1 in normal mode 775 call feedkeys(TabMenuNewItemCode(1), "Lx!") 776 call assert_equal(1, tabpagenr()) 777 call assert_equal(3, tabpagenr('$')) 778 779 " go to tab page 2 in operator-pending mode (should beep) 780 call assert_beeps('call feedkeys("c" .. TabLineSelectPageCode(2), "Lx!")') 781 call assert_equal(2, tabpagenr()) 782 call assert_equal(3, tabpagenr('$')) 783 784 " open new tab page before tab page 1 in operator-pending mode (should beep) 785 call assert_beeps('call feedkeys("c" .. TabMenuNewItemCode(1), "Lx!")') 786 call assert_equal(1, tabpagenr()) 787 call assert_equal(4, tabpagenr('$')) 788 789 " open new tab page after tab page 3 in normal mode 790 call feedkeys(TabMenuNewItemCode(4), "Lx!") 791 call assert_equal(4, tabpagenr()) 792 call assert_equal(5, tabpagenr('$')) 793 794 " go to tab page 2 in insert mode 795 call feedkeys("i" .. TabLineSelectPageCode(2) .. "\<C-C>", "Lx!") 796 call assert_equal(2, tabpagenr()) 797 798 " close tab page 2 in insert mode 799 call feedkeys("i" .. TabMenuCloseItemCode(2) .. "\<C-C>", "Lx!") 800 call assert_equal(4, tabpagenr('$')) 801 802 " open new tab page before tab page 3 in insert mode 803 call feedkeys("i" .. TabMenuNewItemCode(3) .. "\<C-C>", "Lx!") 804 call assert_equal(3, tabpagenr()) 805 call assert_equal(5, tabpagenr('$')) 806 807 " open new tab page after tab page 4 in insert mode 808 call feedkeys("i" .. TabMenuNewItemCode(5) .. "\<C-C>", "Lx!") 809 call assert_equal(5, tabpagenr()) 810 call assert_equal(6, tabpagenr('$')) 811 812 %bw! 813 endfunc 814 815 " Test for changing the current tab page from an autocmd when closing a tab 816 " page. 817 func Test_tabpage_switchtab_on_close() 818 only 819 tabnew 820 tabnew 821 " Test for BufLeave 822 augroup T1 823 au! 824 au BufLeave * tabfirst 825 augroup END 826 tabclose 827 call assert_equal(1, tabpagenr()) 828 augroup T1 829 au! 830 augroup END 831 832 " Test for WinLeave 833 $tabnew 834 augroup T1 835 au! 836 au WinLeave * tabfirst 837 augroup END 838 tabclose 839 call assert_equal(1, tabpagenr()) 840 augroup T1 841 au! 842 augroup END 843 844 " Test for TabLeave 845 $tabnew 846 augroup T1 847 au! 848 au TabLeave * tabfirst 849 augroup END 850 tabclose 851 call assert_equal(1, tabpagenr()) 852 augroup T1 853 au! 854 augroup END 855 augroup! T1 856 tabonly 857 endfunc 858 859 " Test for closing the destination tabpage when jumping from one to another. 860 func Test_tabpage_close_on_switch() 861 tabnew 862 tabnew 863 edit Xfile 864 augroup T2 865 au! 866 au BufLeave Xfile 1tabclose 867 augroup END 868 tabfirst 869 call assert_equal(2, tabpagenr()) 870 call assert_equal('Xfile', @%) 871 augroup T2 872 au! 873 augroup END 874 augroup! T2 875 %bw! 876 endfunc 877 878 " Test for jumping to last accessed tabpage 879 func Test_lastused_tabpage() 880 tabonly! 881 call assert_equal(0, tabpagenr('#')) 882 call assert_beeps('call feedkeys("g\<Tab>", "xt")') 883 call assert_beeps('call feedkeys("\<C-Tab>", "xt")') 884 call assert_beeps('call feedkeys("\<C-W>g\<Tab>", "xt")') 885 call assert_fails('tabnext #', 'E475:') 886 887 " open four tab pages 888 tabnew 889 tabnew 890 tabnew 891 892 2tabnext 893 894 " Test for g<Tab> 895 call assert_equal(4, tabpagenr('#')) 896 call feedkeys("g\<Tab>", "xt") 897 call assert_equal(4, tabpagenr()) 898 call assert_equal(2, tabpagenr('#')) 899 900 " Test for <C-Tab> 901 call feedkeys("\<C-Tab>", "xt") 902 call assert_equal(2, tabpagenr()) 903 call assert_equal(4, tabpagenr('#')) 904 905 " Test for <C-W>g<Tab> 906 call feedkeys("\<C-W>g\<Tab>", "xt") 907 call assert_equal(4, tabpagenr()) 908 call assert_equal(2, tabpagenr('#')) 909 910 " Test for :tabnext # 911 tabnext # 912 call assert_equal(2, tabpagenr()) 913 call assert_equal(4, tabpagenr('#')) 914 915 " Try to jump to a closed tab page 916 tabclose # 917 call assert_equal(0, tabpagenr('#')) 918 call feedkeys("g\<Tab>", "xt") 919 call assert_equal(2, tabpagenr()) 920 call feedkeys("\<C-Tab>", "xt") 921 call assert_equal(2, tabpagenr()) 922 call feedkeys("\<C-W>g\<Tab>", "xt") 923 call assert_equal(2, tabpagenr()) 924 call assert_fails('tabnext #', 'E475:') 925 call assert_equal(2, tabpagenr()) 926 927 " Test for :tabonly # 928 let wnum = win_getid() 929 $tabnew 930 tabonly # 931 call assert_equal(wnum, win_getid()) 932 call assert_equal(1, tabpagenr('$')) 933 934 " Test for :tabmove # 935 tabnew 936 let wnum = win_getid() 937 tabnew 938 tabnew 939 tabnext 2 940 tabmove # 941 call assert_equal(4, tabpagenr()) 942 call assert_equal(wnum, win_getid()) 943 944 tabonly! 945 endfunc 946 947 " Test for tabpage allocation failure 948 func Test_tabpage_alloc_failure() 949 CheckFunction test_alloc_fail 950 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0) 951 call assert_fails('tabnew', 'E342:') 952 953 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0) 954 edit Xfile1 955 call assert_fails('tabedit Xfile2', 'E342:') 956 call assert_equal(1, winnr('$')) 957 call assert_equal(1, tabpagenr('$')) 958 call assert_equal('Xfile1', @%) 959 960 new 961 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0) 962 call assert_fails('wincmd T', 'E342:') 963 bw! 964 965 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0) 966 call assert_fails('tab split', 'E342:') 967 call assert_equal(2, winnr('$')) 968 call assert_equal(1, tabpagenr('$')) 969 endfunc 970 971 func Test_tabpage_tabclose() 972 " Default behaviour, move to the right. 973 call s:reconstruct_tabpage_for_test(6) 974 norm! 4gt 975 setl tcl= 976 tabclose 977 call assert_equal("n3", bufname()) 978 979 " Move to the left. 980 call s:reconstruct_tabpage_for_test(6) 981 norm! 4gt 982 setl tcl=left 983 tabclose 984 call assert_equal("n1", bufname()) 985 986 " Move to the last used tab page. 987 call s:reconstruct_tabpage_for_test(6) 988 norm! 5gt 989 norm! 2gt 990 setl tcl=uselast 991 tabclose 992 call assert_equal("n3", bufname()) 993 994 " Same, but the last used tab page is invalid. Move to the right. 995 call s:reconstruct_tabpage_for_test(6) 996 norm! 5gt 997 norm! 3gt 998 setl tcl=uselast 999 tabclose 5 1000 tabclose! 1001 call assert_equal("n2", bufname()) 1002 1003 " Same, but the last used tab page is invalid. Move to the left. 1004 call s:reconstruct_tabpage_for_test(6) 1005 norm! 5gt 1006 norm! 3gt 1007 setl tcl=uselast,left 1008 tabclose 5 1009 tabclose! 1010 call assert_equal("n0", bufname()) 1011 1012 " Move left when moving right is not possible. 1013 call s:reconstruct_tabpage_for_test(6) 1014 setl tcl= 1015 norm! 6gt 1016 tabclose 1017 call assert_equal("n3", bufname()) 1018 1019 " Move right when moving left is not possible. 1020 call s:reconstruct_tabpage_for_test(6) 1021 setl tcl=left 1022 norm! 1gt 1023 tabclose 1024 call assert_equal("n0", bufname()) 1025 1026 setl tcl& 1027 endfunc 1028 1029 " this was giving ml_get errors 1030 func Test_tabpage_last_line() 1031 enew 1032 call setline(1, repeat(['a'], &lines + 5)) 1033 $ 1034 tabnew 1035 call setline(1, repeat(['b'], &lines + 20)) 1036 $ 1037 tabNext 1038 call assert_equal('a', getline('.')) 1039 1040 bwipe! 1041 bwipe! 1042 endfunc 1043 1044 " this was causing an endless loop 1045 func Test_tabpage_drop_tabmove() 1046 augroup TestTabpageTabmove 1047 au! 1048 autocmd! TabEnter * :if tabpagenr() > 1 | tabmove - | endif 1049 augroup end 1050 $tab drop XTab_99.log 1051 $tab drop XTab_98.log 1052 $tab drop XTab_97.log 1053 1054 autocmd! TestTabpageTabmove 1055 augroup! TestTabpageTabmove 1056 1057 " clean up 1058 bwipe! 1059 bwipe! 1060 bwipe! 1061 endfunc 1062 1063 " Test that settabvar() shouldn't change the last accessed tabpage. 1064 func Test_lastused_tabpage_settabvar() 1065 tabonly! 1066 tabnew 1067 tabnew 1068 tabnew 1069 call assert_equal(3, tabpagenr('#')) 1070 1071 call settabvar(2, 'myvar', 'tabval') 1072 call assert_equal('tabval', gettabvar(2, 'myvar')) 1073 call assert_equal(3, tabpagenr('#')) 1074 1075 bwipe! 1076 bwipe! 1077 bwipe! 1078 endfunc 1079 1080 " vim: shiftwidth=2 sts=2 expandtab