blob: 643c1068bd497f222bb809a9e8247ce12afa3074 [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
15 call writefile(['testtext 1'], 'Xtest1')
16 call writefile(['testtext 2'], 'Xtest2')
17 call writefile(['testtext 3'], 'Xtest3')
18
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')
30 call assert_fails('rewind', 'E37')
31 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')
69 call assert_fails('edit Xtest1', 'E37')
70 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')
83 call assert_fails('close', 'E37')
84 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 Moolenaar9b7bf9e2020-07-11 22:14:59 +0200118 call assert_fails('silent! quit!', 'E37')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200119 call assert_equal('Xtest1', bufname('%'))
120
121 call delete('Xtest1')
122 call delete('Xtest2')
123 call delete('Xtest3')
124endfunc
Bram Moolenaarc136af22018-05-04 20:15:38 +0200125
126" Test that ":close" will respect 'winfixheight' when possible.
127func Test_winfixheight_on_close()
128 set nosplitbelow nosplitright
129
130 split | split | vsplit
131
132 $wincmd w
133 setlocal winfixheight
134 let l:height = winheight(0)
135
136 3close
137
138 call assert_equal(l:height, winheight(0))
139
140 %bwipeout!
141 setlocal nowinfixheight splitbelow& splitright&
142endfunc
143
144" Test that ":close" will respect 'winfixwidth' when possible.
145func Test_winfixwidth_on_close()
146 set nosplitbelow nosplitright
147
148 vsplit | vsplit | split
149
150 $wincmd w
151 setlocal winfixwidth
152 let l:width = winwidth(0)
153
154 3close
155
156 call assert_equal(l:width, winwidth(0))
157
158 %bwipeout!
159 setlocal nowinfixwidth splitbelow& splitright&
160endfunction
Bram Moolenaar9e1e3582019-03-30 19:49:06 +0100161
162" Test that 'winfixheight' will be respected even there is non-leaf frame
Bram Moolenaara44b3ee2020-01-20 21:44:31 +0100163func Test_winfixheight_non_leaf_frame()
Bram Moolenaar9e1e3582019-03-30 19:49:06 +0100164 vsplit
165 botright 11new
166 let l:wid = win_getid()
167 setlocal winfixheight
168 call assert_equal(11, winheight(l:wid))
169 botright new
170 bwipe!
171 call assert_equal(11, winheight(l:wid))
172 %bwipe!
173endf
174
175" Test that 'winfixwidth' will be respected even there is non-leaf frame
Bram Moolenaara44b3ee2020-01-20 21:44:31 +0100176func Test_winfixwidth_non_leaf_frame()
Bram Moolenaar9e1e3582019-03-30 19:49:06 +0100177 split
178 topleft 11vnew
179 let l:wid = win_getid()
180 setlocal winfixwidth
181 call assert_equal(11, winwidth(l:wid))
182 topleft new
183 bwipe!
184 call assert_equal(11, winwidth(l:wid))
185 %bwipe!
186endf
Bram Moolenaara44b3ee2020-01-20 21:44:31 +0100187
188func Test_tabwin_close()
189 enew
190 let l:wid = win_getid()
191 tabedit
192 call win_execute(l:wid, 'close')
193 " Should not crash.
194 call assert_true(v:true)
195 %bwipe!
196endfunc
Bram Moolenaaredd327c2020-04-15 20:05:47 +0200197
198" Test when closing a split window (above/below) restores space to the window
199" below when 'noequalalways' and 'splitright' are set.
200func Test_window_close_splitright_noequalalways()
201 set noequalalways
202 set splitright
203 new
204 let w1 = win_getid()
205 new
206 let w2 = win_getid()
207 execute "normal \<c-w>b"
208 let h = winheight(0)
209 let w = win_getid()
210 new
211 q
212 call assert_equal(h, winheight(0), "Window height does not match eight before opening and closing another window")
213 call assert_equal(w, win_getid(), "Did not return to original window after opening and closing a window")
214endfunc
215