test_winbuf_close.vim 5.6 KB

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