1" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...) 2 3so check.vim 4 5func Test_window_cmd_ls0_with_split() 6 set ls=0 7 set splitbelow 8 split 9 quit 10 call assert_equal(0, &lines - &cmdheight - winheight(0)) 11 new | only! 12 " 13 set splitbelow&vim 14 botright split 15 quit 16 call assert_equal(0, &lines - &cmdheight - winheight(0)) 17 new | only! 18 set ls&vim 19endfunc 20 21func Test_window_cmd_cmdwin_with_vsp() 22 let efmt = 'Expected 0 but got %d (in ls=%d, %s window)' 23 for v in range(0, 2) 24 exec "set ls=" . v 25 vsplit 26 call feedkeys("q:\<CR>") 27 let ac = &lines - (&cmdheight + winheight(0) + !!v) 28 let emsg = printf(efmt, ac, v, 'left') 29 call assert_equal(0, ac, emsg) 30 wincmd w 31 let ac = &lines - (&cmdheight + winheight(0) + !!v) 32 let emsg = printf(efmt, ac, v, 'right') 33 call assert_equal(0, ac, emsg) 34 new | only! 35 endfor 36 set ls&vim 37endfunc 38 39function Test_window_cmd_wincmd_gf() 40 let fname = 'test_gf.txt' 41 let swp_fname = '.' . fname . '.swp' 42 call writefile([], fname) 43 call writefile([], swp_fname) 44 function s:swap_exists() 45 let v:swapchoice = s:swap_choice 46 endfunc 47 " Remove the catch-all that runtest.vim adds 48 au! SwapExists 49 augroup test_window_cmd_wincmd_gf 50 autocmd! 51 exec "autocmd SwapExists " . fname . " call s:swap_exists()" 52 augroup END 53 54 call setline(1, fname) 55 " (E)dit anyway 56 let s:swap_choice = 'e' 57 wincmd gf 58 call assert_equal(2, tabpagenr()) 59 call assert_equal(fname, bufname("%")) 60 quit! 61 62 " (Q)uit 63 let s:swap_choice = 'q' 64 wincmd gf 65 call assert_equal(1, tabpagenr()) 66 call assert_notequal(fname, bufname("%")) 67 new | only! 68 69 call delete(fname) 70 call delete(swp_fname) 71 augroup! test_window_cmd_wincmd_gf 72endfunc 73 74func Test_window_quit() 75 e Xa 76 split Xb 77 call assert_equal(2, '$'->winnr()) 78 call assert_equal('Xb', bufname(winbufnr(1))) 79 call assert_equal('Xa', bufname(winbufnr(2))) 80 81 wincmd q 82 call assert_equal(1, winnr('$')) 83 call assert_equal('Xa', bufname(winbufnr(1))) 84 85 bw Xa Xb 86endfunc 87 88func Test_window_horizontal_split() 89 call assert_equal(1, winnr('$')) 90 3wincmd s 91 call assert_equal(2, winnr('$')) 92 call assert_equal(3, winheight(0)) 93 call assert_equal(winwidth(1), 2->winwidth()) 94 95 call assert_fails('botright topleft wincmd s', 'E442:') 96 bw 97endfunc 98 99func Test_window_vertical_split() 100 call assert_equal(1, winnr('$')) 101 3wincmd v 102 call assert_equal(2, winnr('$')) 103 call assert_equal(3, winwidth(0)) 104 call assert_equal(winheight(1), winheight(2)) 105 106 call assert_fails('botright topleft wincmd v', 'E442:') 107 bw 108endfunc 109 110" Test the ":wincmd ^" and "<C-W>^" commands. 111func Test_window_split_edit_alternate() 112 113 " Test for failure when the alternate buffer/file no longer exists. 114 edit Xfoo | %bw 115 call assert_fails(':wincmd ^', 'E23') 116 117 " Test for the expected behavior when we have two named buffers. 118 edit Xfoo | edit Xbar 119 wincmd ^ 120 call assert_equal('Xfoo', bufname(winbufnr(1))) 121 call assert_equal('Xbar', bufname(winbufnr(2))) 122 only 123 124 " Test for the expected behavior when the alternate buffer is not named. 125 enew | let l:nr1 = bufnr('%') 126 edit Xfoo | let l:nr2 = bufnr('%') 127 wincmd ^ 128 call assert_equal(l:nr1, winbufnr(1)) 129 call assert_equal(l:nr2, winbufnr(2)) 130 only 131 132 " FIXME: this currently fails on AppVeyor, but passes locally 133 if !has('win32') 134 " Test the Normal mode command. 135 call feedkeys("\<C-W>\<C-^>", 'tx') 136 call assert_equal(l:nr2, winbufnr(1)) 137 call assert_equal(l:nr1, winbufnr(2)) 138 endif 139 140 %bw! 141endfunc 142 143" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands. 144func Test_window_split_edit_bufnr() 145 146 %bwipeout 147 let l:nr = bufnr('%') + 1 148 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92') 149 call assert_fails(':' . l:nr . 'wincmd ^', 'E16') 150 call assert_fails(':0wincmd ^', 'E16') 151 152 edit Xfoo | edit Xbar | edit Xbaz 153 let l:foo_nr = bufnr('Xfoo') 154 let l:bar_nr = bufnr('Xbar') 155 let l:baz_nr = bufnr('Xbaz') 156 157 " FIXME: this currently fails on AppVeyor, but passes locally 158 if !has('win32') 159 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx') 160 call assert_equal('Xfoo', bufname(winbufnr(1))) 161 call assert_equal('Xbaz', bufname(winbufnr(2))) 162 only 163 164 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx') 165 call assert_equal('Xbar', bufname(winbufnr(1))) 166 call assert_equal('Xfoo', bufname(winbufnr(2))) 167 only 168 169 execute l:baz_nr . 'wincmd ^' 170 call assert_equal('Xbaz', bufname(winbufnr(1))) 171 call assert_equal('Xbar', bufname(winbufnr(2))) 172 endif 173 174 %bw! 175endfunc 176 177func Test_window_split_no_room() 178 " N horizontal windows need >= 2*N + 1 lines: 179 " - 1 line + 1 status line in each window 180 " - 1 Ex command line 181 " 182 " 2*N + 1 <= &lines 183 " N <= (lines - 1)/2 184 " 185 " Beyond that number of windows, E36: Not enough room is expected. 186 let hor_win_count = (&lines - 1)/2 187 let hor_split_count = hor_win_count - 1 188 for s in range(1, hor_split_count) | split | endfor 189 call assert_fails('split', 'E36:') 190 191 " N vertical windows need >= 2*(N - 1) + 1 columns: 192 " - 1 column + 1 separator for each window (except last window) 193 " - 1 column for the last window which does not have separator 194 " 195 " 2*(N - 1) + 1 <= &columns 196 " 2*N - 1 <= &columns 197 " N <= (&columns + 1)/2 198 let ver_win_count = (&columns + 1)/2 199 let ver_split_count = ver_win_count - 1 200 for s in range(1, ver_split_count) | vsplit | endfor 201 call assert_fails('vsplit', 'E36:') 202 203 %bw! 204endfunc 205 206func Test_window_preview() 207 CheckFeature quickfix 208 209 " Open a preview window 210 pedit Xa 211 call assert_equal(2, winnr('$')) 212 call assert_equal(0, &previewwindow) 213 214 " Go to the preview window 215 wincmd P 216 call assert_equal(1, &previewwindow) 217 218 " Close preview window 219 wincmd z 220 call assert_equal(1, winnr('$')) 221 call assert_equal(0, &previewwindow) 222 223 call assert_fails('wincmd P', 'E441:') 224endfunc 225 226func Test_window_preview_from_help() 227 CheckFeature quickfix 228 229 filetype on 230 call writefile(['/* some C code */'], 'Xpreview.c') 231 help 232 pedit Xpreview.c 233 wincmd P 234 call assert_equal(1, &previewwindow) 235 call assert_equal('c', &filetype) 236 wincmd z 237 238 filetype off 239 close 240 call delete('Xpreview.c') 241endfunc 242 243func Test_window_exchange() 244 e Xa 245 246 " Nothing happens with window exchange when there is 1 window 247 wincmd x 248 call assert_equal(1, winnr('$')) 249 250 split Xb 251 split Xc 252 253 call assert_equal('Xc', bufname(winbufnr(1))) 254 call assert_equal('Xb', bufname(winbufnr(2))) 255 call assert_equal('Xa', bufname(winbufnr(3))) 256 257 " Exchange current window 1 with window 3 258 3wincmd x 259 call assert_equal('Xa', bufname(winbufnr(1))) 260 call assert_equal('Xb', bufname(winbufnr(2))) 261 call assert_equal('Xc', bufname(winbufnr(3))) 262 263 " Exchange window with next when at the top window 264 wincmd x 265 call assert_equal('Xb', bufname(winbufnr(1))) 266 call assert_equal('Xa', bufname(winbufnr(2))) 267 call assert_equal('Xc', bufname(winbufnr(3))) 268 269 " Exchange window with next when at the middle window 270 wincmd j 271 wincmd x 272 call assert_equal('Xb', bufname(winbufnr(1))) 273 call assert_equal('Xc', bufname(winbufnr(2))) 274 call assert_equal('Xa', bufname(winbufnr(3))) 275 276 " Exchange window with next when at the bottom window. 277 " When there is no next window, it exchanges with the previous window. 278 wincmd j 279 wincmd x 280 call assert_equal('Xb', bufname(winbufnr(1))) 281 call assert_equal('Xa', bufname(winbufnr(2))) 282 call assert_equal('Xc', bufname(winbufnr(3))) 283 284 bw Xa Xb Xc 285endfunc 286 287func Test_window_rotate() 288 e Xa 289 split Xb 290 split Xc 291 call assert_equal('Xc', bufname(winbufnr(1))) 292 call assert_equal('Xb', bufname(winbufnr(2))) 293 call assert_equal('Xa', bufname(winbufnr(3))) 294 295 " Rotate downwards 296 wincmd r 297 call assert_equal('Xa', bufname(winbufnr(1))) 298 call assert_equal('Xc', bufname(winbufnr(2))) 299 call assert_equal('Xb', bufname(winbufnr(3))) 300 301 2wincmd r 302 call assert_equal('Xc', bufname(winbufnr(1))) 303 call assert_equal('Xb', bufname(winbufnr(2))) 304 call assert_equal('Xa', bufname(winbufnr(3))) 305 306 " Rotate upwards 307 wincmd R 308 call assert_equal('Xb', bufname(winbufnr(1))) 309 call assert_equal('Xa', bufname(winbufnr(2))) 310 call assert_equal('Xc', bufname(winbufnr(3))) 311 312 2wincmd R 313 call assert_equal('Xc', bufname(winbufnr(1))) 314 call assert_equal('Xb', bufname(winbufnr(2))) 315 call assert_equal('Xa', bufname(winbufnr(3))) 316 317 bot vsplit 318 call assert_fails('wincmd R', 'E443:') 319 320 bw Xa Xb Xc 321endfunc 322 323func Test_window_height() 324 e Xa 325 split Xb 326 327 let [wh1, wh2] = [winheight(1), winheight(2)] 328 " Active window (1) should have the same height or 1 more 329 " than the other window. 330 call assert_inrange(wh2, wh2 + 1, wh1) 331 332 wincmd - 333 call assert_equal(wh1 - 1, winheight(1)) 334 call assert_equal(wh2 + 1, winheight(2)) 335 336 wincmd + 337 call assert_equal(wh1, winheight(1)) 338 call assert_equal(wh2, 2->winheight()) 339 340 2wincmd _ 341 call assert_equal(2, winheight(1)) 342 call assert_equal(wh1 + wh2 - 2, winheight(2)) 343 344 wincmd = 345 call assert_equal(wh1, winheight(1)) 346 call assert_equal(wh2, winheight(2)) 347 348 2wincmd _ 349 set winfixheight 350 split Xc 351 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)] 352 call assert_equal(2, winheight(2)) 353 call assert_inrange(wh3, wh3 + 1, wh1) 354 3wincmd + 355 call assert_equal(2, winheight(2)) 356 call assert_equal(wh1 + 3, winheight(1)) 357 call assert_equal(wh3 - 3, winheight(3)) 358 wincmd = 359 call assert_equal(2, winheight(2)) 360 call assert_equal(wh1, winheight(1)) 361 call assert_equal(wh3, winheight(3)) 362 363 wincmd j 364 set winfixheight& 365 366 wincmd = 367 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)] 368 " Current window (2) should have the same height or 1 more 369 " than the other windows. 370 call assert_inrange(wh1, wh1 + 1, wh2) 371 call assert_inrange(wh3, wh3 + 1, wh2) 372 373 bw Xa Xb Xc 374endfunc 375 376func Test_window_width() 377 e Xa 378 vsplit Xb 379 380 let [ww1, ww2] = [winwidth(1), winwidth(2)] 381 " Active window (1) should have the same width or 1 more 382 " than the other window. 383 call assert_inrange(ww2, ww2 + 1, ww1) 384 385 wincmd < 386 call assert_equal(ww1 - 1, winwidth(1)) 387 call assert_equal(ww2 + 1, winwidth(2)) 388 389 wincmd > 390 call assert_equal(ww1, winwidth(1)) 391 call assert_equal(ww2, winwidth(2)) 392 393 2wincmd | 394 call assert_equal(2, winwidth(1)) 395 call assert_equal(ww1 + ww2 - 2, winwidth(2)) 396 397 wincmd = 398 call assert_equal(ww1, winwidth(1)) 399 call assert_equal(ww2, winwidth(2)) 400 401 2wincmd | 402 set winfixwidth 403 vsplit Xc 404 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)] 405 call assert_equal(2, winwidth(2)) 406 call assert_inrange(ww3, ww3 + 1, ww1) 407 3wincmd > 408 call assert_equal(2, winwidth(2)) 409 call assert_equal(ww1 + 3, winwidth(1)) 410 call assert_equal(ww3 - 3, winwidth(3)) 411 wincmd = 412 call assert_equal(2, winwidth(2)) 413 call assert_equal(ww1, winwidth(1)) 414 call assert_equal(ww3, winwidth(3)) 415 416 wincmd l 417 set winfixwidth& 418 419 wincmd = 420 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)] 421 " Current window (2) should have the same width or 1 more 422 " than the other windows. 423 call assert_inrange(ww1, ww1 + 1, ww2) 424 call assert_inrange(ww3, ww3 + 1, ww2) 425 426 bw Xa Xb Xc 427endfunc 428 429func Test_equalalways_on_close() 430 set equalalways 431 vsplit 432 windo split 433 split 434 wincmd J 435 " now we have a frame top-left with two windows, a frame top-right with two 436 " windows and a frame at the bottom, full-width. 437 let height_1 = winheight(1) 438 let height_2 = winheight(2) 439 let height_3 = winheight(3) 440 let height_4 = winheight(4) 441 " closing the bottom window causes all windows to be resized. 442 close 443 call assert_notequal(height_1, winheight(1)) 444 call assert_notequal(height_2, winheight(2)) 445 call assert_notequal(height_3, winheight(3)) 446 call assert_notequal(height_4, winheight(4)) 447 call assert_equal(winheight(1), winheight(3)) 448 call assert_equal(winheight(2), winheight(4)) 449 450 1wincmd w 451 split 452 4wincmd w 453 resize + 5 454 " left column has three windows, equalized heights. 455 " right column has two windows, top one a bit higher 456 let height_1 = winheight(1) 457 let height_2 = winheight(2) 458 let height_4 = winheight(4) 459 let height_5 = winheight(5) 460 3wincmd w 461 " closing window in left column equalizes heights in left column but not in 462 " the right column 463 close 464 call assert_notequal(height_1, winheight(1)) 465 call assert_notequal(height_2, winheight(2)) 466 call assert_equal(height_4, winheight(3)) 467 call assert_equal(height_5, winheight(4)) 468 469 only 470 set equalalways& 471endfunc 472 473func Test_win_screenpos() 474 CheckFeature quickfix 475 476 call assert_equal(1, winnr('$')) 477 split 478 vsplit 479 10wincmd _ 480 30wincmd | 481 call assert_equal([1, 1], win_screenpos(1)) 482 call assert_equal([1, 32], win_screenpos(2)) 483 call assert_equal([12, 1], win_screenpos(3)) 484 call assert_equal([0, 0], win_screenpos(4)) 485 only 486endfunc 487 488func Test_window_jump_tag() 489 CheckFeature quickfix 490 491 help 492 /iccf 493 call assert_match('^|iccf|', getline('.')) 494 call assert_equal(2, winnr('$')) 495 2wincmd } 496 call assert_equal(3, winnr('$')) 497 call assert_match('^|iccf|', getline('.')) 498 wincmd k 499 call assert_match('\*iccf\*', getline('.')) 500 call assert_equal(2, winheight(0)) 501 502 wincmd z 503 set previewheight=4 504 help 505 /bugs 506 wincmd } 507 wincmd k 508 call assert_match('\*bugs\*', getline('.')) 509 call assert_equal(4, winheight(0)) 510 set previewheight& 511 512 %bw! 513endfunc 514 515func Test_window_newtab() 516 e Xa 517 518 call assert_equal(1, tabpagenr('$')) 519 call assert_equal("\nAlready only one window", execute('wincmd T')) 520 521 split Xb 522 split Xc 523 524 wincmd T 525 call assert_equal(2, tabpagenr('$')) 526 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)')) 527 call assert_equal(['Xc' ], map(2->tabpagebuflist(), 'bufname(v:val)')) 528 529 %bw! 530endfunc 531 532func Test_next_split_all() 533 " This was causing an illegal memory access. 534 n x 535 norm axxx 536 split 537 split 538 s/x 539 s/x 540 all 541 bwipe! 542endfunc 543 544" Tests for adjusting window and contents 545func GetScreenStr(row) 546 let str = "" 547 for c in range(1,3) 548 let str .= nr2char(screenchar(a:row, c)) 549 endfor 550 return str 551endfunc 552 553func Test_window_contents() 554 enew! | only | new 555 call setline(1, range(1,256)) 556 557 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+" 558 redraw 559 let s3 = GetScreenStr(1) 560 wincmd p 561 call assert_equal(1, line("w0")) 562 call assert_equal('1 ', s3) 563 564 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+" 565 redraw 566 let s3 = GetScreenStr(1) 567 wincmd p 568 call assert_equal(50, line("w0")) 569 call assert_equal('50 ', s3) 570 571 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+" 572 redraw 573 let s3 = GetScreenStr(1) 574 wincmd p 575 call assert_equal(59, line("w0")) 576 call assert_equal('59 ', s3) 577 578 %d 579 call setline(1, ['one', 'two', 'three']) 580 call assert_equal(1, line('w0')) 581 call assert_equal(3, line('w$')) 582 583 bwipeout! 584 call test_garbagecollect_now() 585endfunc 586 587func Test_window_colon_command() 588 " This was reading invalid memory. 589 exe "norm! v\<C-W>:\<C-U>echo v:version" 590endfunc 591 592func Test_access_freed_mem() 593 call assert_equal(&columns, winwidth(0)) 594 " This was accessing freed memory 595 au * 0 vs xxx 596 arg 0 597 argadd 598 call assert_fails("all", "E249:") 599 au! 600 bwipe xxx 601 call assert_equal(&columns, winwidth(0)) 602endfunc 603 604func Test_insert_cleared_on_switch_to_term() 605 CheckFeature terminal 606 607 set showmode 608 terminal 609 wincmd p 610 611 call feedkeys("i\<C-O>", 'ntx') 612 redraw 613 614 " The "-- (insert) --" indicator should be visible. 615 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))') 616 let str = trim(join(chars, '')) 617 call assert_equal('-- (insert) --', str) 618 619 call feedkeys("\<C-W>p", 'ntx') 620 redraw 621 622 " The "-- (insert) --" indicator should have been cleared. 623 let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))') 624 let str = trim(join(chars, '')) 625 call assert_equal('', str) 626 627 set showmode& 628 %bw! 629endfunc 630 631func Test_visual_cleared_after_window_split() 632 new | only! 633 let smd_save = &showmode 634 set showmode 635 let ls_save = &laststatus 636 set laststatus=1 637 call setline(1, ['a', 'b', 'c', 'd', '']) 638 norm! G 639 exe "norm! kkvk" 640 redraw 641 exe "norm! \<C-W>v" 642 redraw 643 " check if '-- VISUAL --' disappeared from command line 644 let columns = range(1, &columns) 645 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))') 646 let cmdline = join(cmdlinechars, '') 647 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "") 648 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "") 649 call assert_equal('', mode_shown) 650 let &showmode = smd_save 651 let &laststatus = ls_save 652 bwipe! 653endfunc 654 655func Test_winrestcmd() 656 2split 657 3vsplit 658 let a = winrestcmd() 659 call assert_equal(2, winheight(0)) 660 call assert_equal(3, winwidth(0)) 661 wincmd = 662 call assert_notequal(2, winheight(0)) 663 call assert_notequal(3, winwidth(0)) 664 exe a 665 call assert_equal(2, winheight(0)) 666 call assert_equal(3, winwidth(0)) 667 only 668endfunc 669 670func Fun_RenewFile() 671 " Need to wait a bit for the timestamp to be older. 672 let old_ftime = getftime("tmp.txt") 673 while getftime("tmp.txt") == old_ftime 674 sleep 100m 675 silent execute '!echo "1" > tmp.txt' 676 endwhile 677 sp 678 wincmd p 679 edit! tmp.txt 680endfunc 681 682func Test_window_prevwin() 683 " Can we make this work on MS-Windows? 684 if !has('unix') 685 return 686 endif 687 688 set hidden autoread 689 call writefile(['2'], 'tmp.txt') 690 new tmp.txt 691 q 692 call Fun_RenewFile() 693 call assert_equal(2, winnr()) 694 wincmd p 695 call assert_equal(1, winnr()) 696 wincmd p 697 q 698 call Fun_RenewFile() 699 call assert_equal(2, winnr()) 700 wincmd p 701 call assert_equal(1, winnr()) 702 wincmd p 703 " reset 704 q 705 call delete('tmp.txt') 706 set hidden&vim autoread&vim 707 delfunc Fun_RenewFile 708endfunc 709 710func Test_relative_cursor_position_in_one_line_window() 711 new 712 only 713 call setline(1, range(1, 10000)) 714 normal 50% 715 let lnum = getcurpos()[1] 716 split 717 split 718 " make third window take as many lines as possible, other windows will 719 " become one line 720 3wincmd w 721 for i in range(1, &lines - 6) 722 wincmd + 723 redraw! 724 endfor 725 726 " first and second window should show cursor line 727 let wininfo = getwininfo() 728 call assert_equal(lnum, wininfo[0].topline) 729 call assert_equal(lnum, wininfo[1].topline) 730 731 only! 732 bwipe! 733endfunc 734 735func Test_relative_cursor_position_after_move_and_resize() 736 let so_save = &so 737 set so=0 738 enew 739 call setline(1, range(1, 10000)) 740 normal 50% 741 split 742 1wincmd w 743 " Move cursor to first line in window 744 normal H 745 redraw! 746 " Reduce window height to two lines 747 let height = winheight(0) 748 while winheight(0) > 2 749 wincmd - 750 redraw! 751 endwhile 752 " move cursor to second/last line in window 753 normal j 754 " restore previous height 755 while winheight(0) < height 756 wincmd + 757 redraw! 758 endwhile 759 " make window two lines again 760 while winheight(0) > 2 761 wincmd - 762 redraw! 763 endwhile 764 765 " cursor should be at bottom line 766 let info = getwininfo(win_getid())[0] 767 call assert_equal(info.topline + 1, getcurpos()[1]) 768 769 only! 770 bwipe! 771 let &so = so_save 772endfunc 773 774func Test_relative_cursor_position_after_resize() 775 let so_save = &so 776 set so=0 777 enew 778 call setline(1, range(1, 10000)) 779 normal 50% 780 split 781 1wincmd w 782 let winid1 = win_getid() 783 let info = getwininfo(winid1)[0] 784 " Move cursor to second line in window 785 exe "normal " . (info.topline + 1) . "G" 786 redraw! 787 let lnum = getcurpos()[1] 788 789 " Make the window only two lines high, cursor should end up in top line 790 2wincmd w 791 exe (info.height - 2) . "wincmd +" 792 redraw! 793 let info = getwininfo(winid1)[0] 794 call assert_equal(lnum, info.topline) 795 796 only! 797 bwipe! 798 let &so = so_save 799endfunc 800 801func Test_relative_cursor_second_line_after_resize() 802 let so_save = &so 803 set so=0 804 enew 805 call setline(1, range(1, 10000)) 806 normal 50% 807 split 808 1wincmd w 809 let winid1 = win_getid() 810 let info = getwininfo(winid1)[0] 811 812 " Make the window only two lines high 813 2wincmd _ 814 815 " Move cursor to second line in window 816 normal H 817 normal j 818 819 " Make window size bigger, then back to 2 lines 820 for i in range(1, 10) 821 wincmd + 822 redraw! 823 endfor 824 for i in range(1, 10) 825 wincmd - 826 redraw! 827 endfor 828 829 " cursor should end up in bottom line 830 let info = getwininfo(winid1)[0] 831 call assert_equal(info.topline + 1, getcurpos()[1]) 832 833 only! 834 bwipe! 835 let &so = so_save 836endfunc 837 838func Test_split_noscroll() 839 let so_save = &so 840 enew 841 call setline(1, range(1, 8)) 842 normal 100% 843 split 844 845 1wincmd w 846 let winid1 = win_getid() 847 let info1 = getwininfo(winid1)[0] 848 849 2wincmd w 850 let winid2 = win_getid() 851 let info2 = getwininfo(winid2)[0] 852 853 call assert_equal(1, info1.topline) 854 call assert_equal(1, info2.topline) 855 856 " window that fits all lines by itself, but not when split: closing other 857 " window should restore fraction. 858 only! 859 call setline(1, range(1, &lines - 10)) 860 exe &lines / 4 861 let winid1 = win_getid() 862 let info1 = getwininfo(winid1)[0] 863 call assert_equal(1, info1.topline) 864 new 865 redraw 866 close 867 let info1 = getwininfo(winid1)[0] 868 call assert_equal(1, info1.topline) 869 870 bwipe! 871 let &so = so_save 872endfunc 873 874" Tests for the winnr() function 875func Test_winnr() 876 only | tabonly 877 call assert_equal(1, winnr('j')) 878 call assert_equal(1, winnr('k')) 879 call assert_equal(1, winnr('h')) 880 call assert_equal(1, winnr('l')) 881 882 " create a set of horizontally and vertically split windows 883 leftabove new | wincmd p 884 leftabove new | wincmd p 885 rightbelow new | wincmd p 886 rightbelow new | wincmd p 887 leftabove vnew | wincmd p 888 leftabove vnew | wincmd p 889 rightbelow vnew | wincmd p 890 rightbelow vnew | wincmd p 891 892 call assert_equal(8, winnr('j')) 893 call assert_equal(2, winnr('k')) 894 call assert_equal(4, winnr('h')) 895 call assert_equal(6, winnr('l')) 896 call assert_equal(9, winnr('2j')) 897 call assert_equal(1, winnr('2k')) 898 call assert_equal(3, winnr('2h')) 899 call assert_equal(7, winnr('2l')) 900 901 " Error cases 902 call assert_fails("echo winnr('0.2k')", 'E15:') 903 call assert_equal(2, winnr('-2k')) 904 call assert_fails("echo winnr('-2xj')", 'E15:') 905 call assert_fails("echo winnr('j2j')", 'E15:') 906 call assert_fails("echo winnr('ll')", 'E15:') 907 call assert_fails("echo winnr('5')", 'E15:') 908 call assert_equal(4, winnr('0h')) 909 910 tabnew 911 call assert_equal(8, tabpagewinnr(1, 'j')) 912 call assert_equal(2, 1->tabpagewinnr('k')) 913 call assert_equal(4, tabpagewinnr(1, 'h')) 914 call assert_equal(6, tabpagewinnr(1, 'l')) 915 916 only | tabonly 917endfunc 918 919func Test_winrestview() 920 split runtest.vim 921 normal 50% 922 let view = winsaveview() 923 close 924 split runtest.vim 925 eval view->winrestview() 926 call assert_equal(view, winsaveview()) 927 928 bwipe! 929endfunc 930 931func Test_win_splitmove() 932 CheckFeature quickfix 933 934 edit a 935 leftabove split b 936 leftabove vsplit c 937 leftabove split d 938 call assert_equal(0, win_splitmove(winnr(), winnr('l'))) 939 call assert_equal(bufname(winbufnr(1)), 'c') 940 call assert_equal(bufname(winbufnr(2)), 'd') 941 call assert_equal(bufname(winbufnr(3)), 'b') 942 call assert_equal(bufname(winbufnr(4)), 'a') 943 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1})) 944 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1})) 945 call assert_equal(bufname(winbufnr(1)), 'c') 946 call assert_equal(bufname(winbufnr(2)), 'b') 947 call assert_equal(bufname(winbufnr(3)), 'd') 948 call assert_equal(bufname(winbufnr(4)), 'a') 949 call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1})) 950 call assert_equal(bufname(winbufnr(1)), 'd') 951 call assert_equal(bufname(winbufnr(2)), 'c') 952 call assert_equal(bufname(winbufnr(3)), 'b') 953 call assert_equal(bufname(winbufnr(4)), 'a') 954 call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true})) 955 call assert_equal(bufname(winbufnr(1)), 'c') 956 call assert_equal(bufname(winbufnr(2)), 'b') 957 call assert_equal(bufname(winbufnr(3)), 'a') 958 call assert_equal(bufname(winbufnr(4)), 'd') 959 only | bd 960 961 call assert_fails('call win_splitmove(winnr(), 123)', 'E957:') 962 call assert_fails('call win_splitmove(123, winnr())', 'E957:') 963 call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:') 964 965 tabnew 966 call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:') 967 tabclose 968endfunc 969 970" Test for the :only command 971func Test_window_only() 972 new 973 set modified 974 new 975 call assert_fails('only', 'E445:') 976 only! 977 " Test for :only with a count 978 let wid = win_getid() 979 new 980 new 981 3only 982 call assert_equal(1, winnr('$')) 983 call assert_equal(wid, win_getid()) 984 call assert_fails('close', 'E444:') 985 call assert_fails('%close', 'E16:') 986endfunc 987 988" Test for errors with :wincmd 989func Test_wincmd_errors() 990 call assert_fails('wincmd g', 'E474:') 991 call assert_fails('wincmd ab', 'E474:') 992endfunc 993 994" Test for errors with :winpos 995func Test_winpos_errors() 996 if !has("gui_running") && !has('win32') 997 call assert_fails('winpos', 'E188:') 998 endif 999 call assert_fails('winpos 10', 'E466:') 1000endfunc 1001 1002" Test for +cmd in a :split command 1003func Test_split_cmd() 1004 split +set\ readonly 1005 call assert_equal(1, &readonly) 1006 call assert_equal(2, winnr('$')) 1007 close 1008endfunc 1009 1010" vim: shiftwidth=2 sts=2 expandtab 1011