blob: 17e31e49d5a8a5b23e46ba8be69819098bd0af51 [file] [log] [blame]
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001" 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()
13 enew | only
14
Bram Moolenaardb4c9472022-10-15 22:06:06 +010015 call writefile(['testtext 1'], 'Xtest1', 'D')
16 call writefile(['testtext 2'], 'Xtest2', 'D')
17 call writefile(['testtext 3'], 'Xtest3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +020018
19 next! Xtest1 Xtest2
20 call setline(1, 'testtext 1 1')
21
22 " test for working :n when hidden set
23 set hidden
24 next
25 call assert_equal('Xtest2', bufname('%'))
26
27 " test for failing :rew when hidden not set
28 set nohidden
29 call setline(1, 'testtext 2 2')
Bram Moolenaare2e40752020-09-04 21:18:46 +020030 call assert_fails('rewind', 'E37:')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +020031 call assert_equal('Xtest2', bufname('%'))
32 call assert_equal('testtext 2 2', getline(1))
33
34 " test for working :rew when hidden set
35 set hidden
36 rewind
37 call assert_equal('Xtest1', bufname('%'))
38 call assert_equal('testtext 1 1', getline(1))
39
40 " test for :all keeping a buffer when it's modified
41 set nohidden
42 call setline(1, 'testtext 1 1 1')
43 split
44 next Xtest2 Xtest3
45 all
46 1wincmd w
47 call assert_equal('Xtest1', bufname('%'))
48 call assert_equal('testtext 1 1 1', getline(1))
49
50 " test abandoning changed buffer, should be unloaded even when 'hidden' set
51 set hidden
52 call setline(1, 'testtext 1 1 1 1')
53 quit!
54 call assert_equal('Xtest2', bufname('%'))
55 call assert_equal('testtext 2 2', getline(1))
56 unhide
57 call assert_equal('Xtest2', bufname('%'))
58 call assert_equal('testtext 2 2', getline(1))
59
60 " test ":hide" hides anyway when 'hidden' not set
61 set nohidden
62 call setline(1, 'testtext 2 2 2')
63 hide
64 call assert_equal('Xtest3', bufname('%'))
65 call assert_equal('testtext 3', getline(1))
66
67 " test ":edit" failing in modified buffer when 'hidden' not set
68 call setline(1, 'testtext 3 3')
Bram Moolenaare2e40752020-09-04 21:18:46 +020069 call assert_fails('edit Xtest1', 'E37:')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +020070 call assert_equal('Xtest3', bufname('%'))
71 call assert_equal('testtext 3 3', getline(1))
72
73 " test ":edit" working in modified buffer when 'hidden' set
74 set hidden
75 edit Xtest1
76 call assert_equal('Xtest1', bufname('%'))
77 call assert_equal('testtext 1', getline(1))
78
79 " test ":close" not hiding when 'hidden' not set in modified buffer
80 split Xtest3
81 set nohidden
82 call setline(1, 'testtext 3 3 3')
Bram Moolenaare2e40752020-09-04 21:18:46 +020083 call assert_fails('close', 'E37:')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +020084 call assert_equal('Xtest3', bufname('%'))
85 call assert_equal('testtext 3 3 3', getline(1))
86
87 " test ":close!" does hide when 'hidden' not set in modified buffer;
88 call setline(1, 'testtext 3 3 3 3')
89 close!
90 call assert_equal('Xtest1', bufname('%'))
91 call assert_equal('testtext 1', getline(1))
92
93 set nohidden
94
95 " test ":all!" hides changed buffer
96 split Xtest4
97 call setline(1, 'testtext 4')
98 all!
99 1wincmd w
100 call assert_equal('Xtest2', bufname('%'))
101 call assert_equal('testtext 2 2 2', getline(1))
102
103 " test ":q!" and hidden buffer.
104 bwipe! Xtest1 Xtest2 Xtest3 Xtest4
105 split Xtest1
106 wincmd w
107 bwipe!
108 set modified
109 bot split Xtest2
110 set modified
111 bot split Xtest3
112 set modified
113 wincmd t
114 hide
115 call assert_equal('Xtest2', bufname('%'))
116 quit!
117 call assert_equal('Xtest3', bufname('%'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200118 call assert_fails('silent! quit!', 'E37:')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200119 call assert_equal('Xtest1', bufname('%'))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200120endfunc
Bram Moolenaarc136af22018-05-04 20:15:38 +0200121
122" Test that ":close" will respect 'winfixheight' when possible.
123func Test_winfixheight_on_close()
124 set nosplitbelow nosplitright
125
126 split | split | vsplit
127
128 $wincmd w
129 setlocal winfixheight
130 let l:height = winheight(0)
131
132 3close
133
134 call assert_equal(l:height, winheight(0))
135
136 %bwipeout!
137 setlocal nowinfixheight splitbelow& splitright&
138endfunc
139
140" Test that ":close" will respect 'winfixwidth' when possible.
141func Test_winfixwidth_on_close()
142 set nosplitbelow nosplitright
143
144 vsplit | vsplit | split
145
146 $wincmd w
147 setlocal winfixwidth
148 let l:width = winwidth(0)
149
150 3close
151
152 call assert_equal(l:width, winwidth(0))
153
154 %bwipeout!
155 setlocal nowinfixwidth splitbelow& splitright&
156endfunction
Bram Moolenaar9e1e3582019-03-30 19:49:06 +0100157
158" Test that 'winfixheight' will be respected even there is non-leaf frame
Bram Moolenaara44b3ee2020-01-20 21:44:31 +0100159func Test_winfixheight_non_leaf_frame()
Bram Moolenaar9e1e3582019-03-30 19:49:06 +0100160 vsplit
161 botright 11new
162 let l:wid = win_getid()
163 setlocal winfixheight
164 call assert_equal(11, winheight(l:wid))
165 botright new
166 bwipe!
167 call assert_equal(11, winheight(l:wid))
168 %bwipe!
169endf
170
171" Test that 'winfixwidth' will be respected even there is non-leaf frame
Bram Moolenaara44b3ee2020-01-20 21:44:31 +0100172func Test_winfixwidth_non_leaf_frame()
Bram Moolenaar9e1e3582019-03-30 19:49:06 +0100173 split
174 topleft 11vnew
175 let l:wid = win_getid()
176 setlocal winfixwidth
177 call assert_equal(11, winwidth(l:wid))
178 topleft new
179 bwipe!
180 call assert_equal(11, winwidth(l:wid))
181 %bwipe!
182endf
Bram Moolenaara44b3ee2020-01-20 21:44:31 +0100183
184func Test_tabwin_close()
185 enew
186 let l:wid = win_getid()
187 tabedit
188 call win_execute(l:wid, 'close')
189 " Should not crash.
190 call assert_true(v:true)
Bram Moolenaarf3c51bb2020-09-26 19:11:39 +0200191
192 " This tests closing a window in another tab, while leaving the tab open
193 " i.e. two windows in another tab.
194 tabedit
195 let w:this_win = 42
196 new
197 let othertab_wid = win_getid()
198 tabprevious
199 call win_execute(othertab_wid, 'q')
200 " drawing the tabline helps check that the other tab's windows and buffers
201 " are still valid
202 redrawtabline
203 " but to be certain, ensure we can focus the other tab too
204 tabnext
205 call assert_equal(42, w:this_win)
206
207 bwipe!
Bram Moolenaara44b3ee2020-01-20 21:44:31 +0100208endfunc
Bram Moolenaaredd327c2020-04-15 20:05:47 +0200209
210" Test when closing a split window (above/below) restores space to the window
211" below when 'noequalalways' and 'splitright' are set.
212func Test_window_close_splitright_noequalalways()
213 set noequalalways
214 set splitright
215 new
216 let w1 = win_getid()
217 new
218 let w2 = win_getid()
219 execute "normal \<c-w>b"
220 let h = winheight(0)
221 let w = win_getid()
222 new
223 q
224 call assert_equal(h, winheight(0), "Window height does not match eight before opening and closing another window")
225 call assert_equal(w, win_getid(), "Did not return to original window after opening and closing a window")
226endfunc
227
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200228" vim: shiftwidth=2 sts=2 expandtab