robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8634ec3827
master
1" Test for commands that close windows and/or buffers: 2" :quit 3" :close 4" :hide 5" :only 6" :sall 7" :all 8" :ball 9" :buf 10" :edit 11" 12func Test_winbuf_close () 13enew |only 14 15call writefile ([ 'testtext 1' ], 'Xtest1' ) 16call writefile ([ 'testtext 2' ], 'Xtest2' ) 17call writefile ([ 'testtext 3' ], 'Xtest3' ) 18 19next ! Xtest1 Xtest2 20call setline ( 1 , 'testtext 1 1' ) 21 22" test for working :n when hidden set 23set hidden 24next 25call assert_equal ( 'Xtest2' , bufname ( '%' )) 26 27" test for failing :rew when hidden not set 28set nohidden 29call setline ( 1 , 'testtext 2 2' ) 30call assert_fails ( 'rewind' , 'E37' ) 31call assert_equal ( 'Xtest2' , bufname ( '%' )) 32call assert_equal ( 'testtext 2 2' , getline ( 1 )) 33 34" test for working :rew when hidden set 35set hidden 36rewind 37call assert_equal ( 'Xtest1' , bufname ( '%' )) 38call assert_equal ( 'testtext 1 1' , getline ( 1 )) 39 40" test for :all keeping a buffer when it's modified 41set nohidden 42call setline ( 1 , 'testtext 1 1 1' ) 43split 44next Xtest2 Xtest3 45all 461 wincmd w 47call assert_equal ( 'Xtest1' , bufname ( '%' )) 48call assert_equal ( 'testtext 1 1 1' , getline ( 1 )) 49 50" test abandoning changed buffer, should be unloaded even when 'hidden' set 51set hidden 52call setline ( 1 , 'testtext 1 1 1 1' ) 53quit ! 54call assert_equal ( 'Xtest2' , bufname ( '%' )) 55call assert_equal ( 'testtext 2 2' , getline ( 1 )) 56unhide 57call assert_equal ( 'Xtest2' , bufname ( '%' )) 58call assert_equal ( 'testtext 2 2' , getline ( 1 )) 59 60" test ":hide" hides anyway when 'hidden' not set 61set nohidden 62call setline ( 1 , 'testtext 2 2 2' ) 63hide 64call assert_equal ( 'Xtest3' , bufname ( '%' )) 65call assert_equal ( 'testtext 3' , getline ( 1 )) 66 67" test ":edit" failing in modified buffer when 'hidden' not set 68call setline ( 1 , 'testtext 3 3' ) 69call assert_fails ( 'edit Xtest1' , 'E37' ) 70call assert_equal ( 'Xtest3' , bufname ( '%' )) 71call assert_equal ( 'testtext 3 3' , getline ( 1 )) 72 73" test ":edit" working in modified buffer when 'hidden' set 74set hidden 75edit Xtest1 76call assert_equal ( 'Xtest1' , bufname ( '%' )) 77call assert_equal ( 'testtext 1' , getline ( 1 )) 78 79" test ":close" not hiding when 'hidden' not set in modified buffer 80split Xtest3 81set nohidden 82call setline ( 1 , 'testtext 3 3 3' ) 83call assert_fails ( 'close' , 'E37' ) 84call assert_equal ( 'Xtest3' , bufname ( '%' )) 85call assert_equal ( 'testtext 3 3 3' , getline ( 1 )) 86 87" test ":close!" does hide when 'hidden' not set in modified buffer; 88call setline ( 1 , 'testtext 3 3 3 3' ) 89close ! 90call assert_equal ( 'Xtest1' , bufname ( '%' )) 91call assert_equal ( 'testtext 1' , getline ( 1 )) 92 93set nohidden 94 95" test ":all!" hides changed buffer 96split Xtest4 97call setline ( 1 , 'testtext 4' ) 98all ! 991 wincmd w 100call assert_equal ( 'Xtest2' , bufname ( '%' )) 101call assert_equal ( 'testtext 2 2 2' , getline ( 1 )) 102 103" test ":q!" and hidden buffer. 104bwipe ! Xtest1 Xtest2 Xtest3 Xtest4 105split Xtest1 106wincmd w 107bwipe ! 108set modified 109bot split Xtest2 110set modified 111bot split Xtest3 112set modified 113wincmd t 114hide 115call assert_equal ( 'Xtest2' , bufname ( '%' )) 116quit ! 117call assert_equal ( 'Xtest3' , bufname ( '%' )) 118call assert_fails ( 'silent! quit!' , 'E37' ) 119call assert_equal ( 'Xtest1' , bufname ( '%' )) 120 121call delete ( 'Xtest1' ) 122call delete ( 'Xtest2' ) 123call delete ( 'Xtest3' ) 124endfunc 125 126" Test that ":close" will respect 'winfixheight' when possible. 127func Test_winfixheight_on_close () 128set nosplitbelow nosplitright 129 130split |split |vsplit 131 132$ wincmd w 133setlocal winfixheight 134let l: height = winheight ( 0 ) 135 1363 close 137 138call assert_equal ( l: height , winheight ( 0 )) 139 140 %bwipeout ! 141setlocal nowinfixheight splitbelow & splitright & 142endfunc 143 144" Test that ":close" will respect 'winfixwidth' when possible. 145func Test_winfixwidth_on_close () 146set nosplitbelow nosplitright 147 148vsplit |vsplit |split 149 150$ wincmd w 151setlocal winfixwidth 152let l: width = winwidth ( 0 ) 153 1543 close 155 156call assert_equal ( l: width , winwidth ( 0 )) 157 158 %bwipeout ! 159setlocal nowinfixwidth splitbelow & splitright & 160endfunction 161 162" Test that 'winfixheight' will be respected even there is non-leaf frame 163func Test_winfixheight_non_leaf_frame () 164vsplit 165botright 11 new 166let l: wid = win_getid () 167setlocal winfixheight 168call assert_equal ( 11 , winheight ( l: wid )) 169botright new 170bwipe ! 171call assert_equal ( 11 , winheight ( l: wid )) 172 %bwipe ! 173endf 174 175" Test that 'winfixwidth' will be respected even there is non-leaf frame 176func Test_winfixwidth_non_leaf_frame () 177split 178topleft 11 vnew 179let l: wid = win_getid () 180setlocal winfixwidth 181call assert_equal ( 11 , winwidth ( l: wid )) 182topleft new 183bwipe ! 184call assert_equal ( 11 , winwidth ( l: wid )) 185 %bwipe ! 186endf 187 188func Test_tabwin_close () 189enew 190let l: wid = win_getid () 191tabedit 192call win_execute ( l: wid , 'close' ) 193" Should not crash. 194call assert_true ( v: true ) 195 196" This tests closing a window in another tab, while leaving the tab open 197" i.e. two windows in another tab. 198tabedit 199let w: this_win = 42 200new 201let othertab_wid = win_getid () 202tabprevious 203call win_execute ( othertab_wid , 'q' ) 204" drawing the tabline helps check that the other tab's windows and buffers 205" are still valid 206redrawtabline 207" but to be certain, ensure we can focus the other tab too 208tabnext 209call assert_equal ( 42 , w: this_win ) 210 211bwipe ! 212endfunc 213 214" Test when closing a split window (above/below) restores space to the window 215" below when 'noequalalways' and 'splitright' are set. 216func Test_window_close_splitright_noequalalways () 217set noequalalways 218set splitright 219new 220let w1 = win_getid () 221new 222let w2 = win_getid () 223execute "normal \<c-w>b" 224let h = winheight ( 0 ) 225let w = win_getid () 226new 227q 228call assert_equal ( h , winheight ( 0 ), "Window height does not match eight before opening and closing another window" ) 229call assert_equal ( w , win_getid (), "Did not return to original window after opening and closing a window" ) 230endfunc 231