1" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...)
2
3func Test_window_cmd_ls0_with_split()
4  set ls=0
5  set splitbelow
6  split
7  quit
8  call assert_equal(0, &lines - &cmdheight - winheight(0))
9  new | only!
10  "
11  set splitbelow&vim
12  botright split
13  quit
14  call assert_equal(0, &lines - &cmdheight - winheight(0))
15  new | only!
16  set ls&vim
17endfunc
18
19func Test_window_cmd_cmdwin_with_vsp()
20  let efmt='Expected 0 but got %d (in ls=%d, %s window)'
21  for v in range(0, 2)
22    exec "set ls=" . v
23    vsplit
24    call feedkeys("q:\<CR>")
25    let ac = &lines - (&cmdheight + winheight(0) + !!v)
26    let emsg = printf(efmt, ac, v, 'left')
27    call assert_equal(0, ac, emsg)
28    wincmd w
29    let ac = &lines - (&cmdheight + winheight(0) + !!v)
30    let emsg = printf(efmt, ac, v, 'right')
31    call assert_equal(0, ac, emsg)
32    new | only!
33  endfor
34  set ls&vim
35endfunc
36
37function Test_window_cmd_wincmd_gf()
38  let fname = 'test_gf.txt'
39  let swp_fname = '.' . fname . '.swp'
40  call writefile([], fname)
41  call writefile([], swp_fname)
42  function s:swap_exists()
43    let v:swapchoice = s:swap_choice
44  endfunc
45  augroup test_window_cmd_wincmd_gf
46    autocmd!
47    exec "autocmd SwapExists " . fname . " call s:swap_exists()"
48  augroup END
49
50  call setline(1, fname)
51  " (E)dit anyway
52  let s:swap_choice = 'e'
53  wincmd gf
54  call assert_equal(2, tabpagenr())
55  call assert_equal(fname, bufname("%"))
56  quit!
57
58  " (Q)uit
59  let s:swap_choice = 'q'
60  wincmd gf
61  call assert_equal(1, tabpagenr())
62  call assert_notequal(fname, bufname("%"))
63  new | only!
64
65  call delete(fname)
66  call delete(swp_fname)
67  augroup! test_window_cmd_wincmd_gf
68endfunc
69
70func Test_window_quit()
71  e Xa
72  split Xb
73  call assert_equal(2, winnr('$'))
74  call assert_equal('Xb', bufname(winbufnr(1)))
75  call assert_equal('Xa', bufname(winbufnr(2)))
76
77  wincmd q
78  call assert_equal(1, winnr('$'))
79  call assert_equal('Xa', bufname(winbufnr(1)))
80
81  bw Xa Xb
82endfunc
83
84func Test_window_horizontal_split()
85  call assert_equal(1, winnr('$'))
86  3wincmd s
87  call assert_equal(2, winnr('$'))
88  call assert_equal(3, winheight(0))
89  call assert_equal(winwidth(1), winwidth(2))
90
91  call assert_fails('botright topleft wincmd s', 'E442:')
92  bw
93endfunc
94
95func Test_window_vertical_split()
96  call assert_equal(1, winnr('$'))
97  3wincmd v
98  call assert_equal(2, winnr('$'))
99  call assert_equal(3, winwidth(0))
100  call assert_equal(winheight(1), winheight(2))
101
102  call assert_fails('botright topleft wincmd v', 'E442:')
103  bw
104endfunc
105
106func Test_window_split_edit_alternate()
107  e Xa
108  e Xb
109
110  wincmd ^
111  call assert_equal('Xa', bufname(winbufnr(1)))
112  call assert_equal('Xb', bufname(winbufnr(2)))
113
114  bw Xa Xb
115endfunc
116
117func Test_window_preview()
118  " Open a preview window
119  pedit Xa
120  call assert_equal(2, winnr('$'))
121  call assert_equal(0, &previewwindow)
122
123  " Go to the preview window
124  wincmd P
125  call assert_equal(1, &previewwindow)
126
127  " Close preview window
128  wincmd z
129  call assert_equal(1, winnr('$'))
130  call assert_equal(0, &previewwindow)
131
132  call assert_fails('wincmd P', 'E441:')
133endfunc
134
135func Test_window_exchange()
136  e Xa
137
138  " Nothing happens with window exchange when there is 1 window
139  wincmd x
140  call assert_equal(1, winnr('$'))
141
142  split Xb
143  split Xc
144
145  call assert_equal('Xc', bufname(winbufnr(1)))
146  call assert_equal('Xb', bufname(winbufnr(2)))
147  call assert_equal('Xa', bufname(winbufnr(3)))
148
149  " Exchange current window 1 with window 3
150  3wincmd x
151  call assert_equal('Xa', bufname(winbufnr(1)))
152  call assert_equal('Xb', bufname(winbufnr(2)))
153  call assert_equal('Xc', bufname(winbufnr(3)))
154
155  " Exchange window with next when at the top window
156  wincmd x
157  call assert_equal('Xb', bufname(winbufnr(1)))
158  call assert_equal('Xa', bufname(winbufnr(2)))
159  call assert_equal('Xc', bufname(winbufnr(3)))
160
161  " Exchange window with next when at the middle window
162  wincmd j
163  wincmd x
164  call assert_equal('Xb', bufname(winbufnr(1)))
165  call assert_equal('Xc', bufname(winbufnr(2)))
166  call assert_equal('Xa', bufname(winbufnr(3)))
167
168  " Exchange window with next when at the bottom window.
169  " When there is no next window, it exchanges with the previous window.
170  wincmd j
171  wincmd x
172  call assert_equal('Xb', bufname(winbufnr(1)))
173  call assert_equal('Xa', bufname(winbufnr(2)))
174  call assert_equal('Xc', bufname(winbufnr(3)))
175
176  bw Xa Xb Xc
177endfunc
178
179func Test_window_rotate()
180  e Xa
181  split Xb
182  split Xc
183  call assert_equal('Xc', bufname(winbufnr(1)))
184  call assert_equal('Xb', bufname(winbufnr(2)))
185  call assert_equal('Xa', bufname(winbufnr(3)))
186
187  " Rotate downwards
188  wincmd r
189  call assert_equal('Xa', bufname(winbufnr(1)))
190  call assert_equal('Xc', bufname(winbufnr(2)))
191  call assert_equal('Xb', bufname(winbufnr(3)))
192
193  2wincmd r
194  call assert_equal('Xc', bufname(winbufnr(1)))
195  call assert_equal('Xb', bufname(winbufnr(2)))
196  call assert_equal('Xa', bufname(winbufnr(3)))
197
198  " Rotate upwards
199  wincmd R
200  call assert_equal('Xb', bufname(winbufnr(1)))
201  call assert_equal('Xa', bufname(winbufnr(2)))
202  call assert_equal('Xc', bufname(winbufnr(3)))
203
204  2wincmd R
205  call assert_equal('Xc', bufname(winbufnr(1)))
206  call assert_equal('Xb', bufname(winbufnr(2)))
207  call assert_equal('Xa', bufname(winbufnr(3)))
208
209  bot vsplit
210  call assert_fails('wincmd R', 'E443:')
211
212  bw Xa Xb Xc
213endfunc
214
215func Test_window_height()
216  e Xa
217  split Xb
218
219  let [wh1, wh2] = [winheight(1), winheight(2)]
220  " Active window (1) should have the same height or 1 more
221  " than the other window.
222  call assert_inrange(wh2, wh2 + 1, wh1)
223
224  wincmd -
225  call assert_equal(wh1 - 1, winheight(1))
226  call assert_equal(wh2 + 1, winheight(2))
227
228  wincmd +
229  call assert_equal(wh1, winheight(1))
230  call assert_equal(wh2, winheight(2))
231
232  2wincmd _
233  call assert_equal(2, winheight(1))
234  call assert_equal(wh1 + wh2 - 2, winheight(2))
235
236  wincmd =
237  call assert_equal(wh1, winheight(1))
238  call assert_equal(wh2, winheight(2))
239
240  2wincmd _
241  set winfixheight
242  split Xc
243  let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
244  call assert_equal(2, winheight(2))
245  call assert_inrange(wh3, wh3 + 1, wh1)
246  3wincmd +
247  call assert_equal(2,       winheight(2))
248  call assert_equal(wh1 + 3, winheight(1))
249  call assert_equal(wh3 - 3, winheight(3))
250  wincmd =
251  call assert_equal(2,   winheight(2))
252  call assert_equal(wh1, winheight(1))
253  call assert_equal(wh3, winheight(3))
254
255  wincmd j
256  set winfixheight&
257
258  wincmd =
259  let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
260  " Current window (2) should have the same height or 1 more
261  " than the other windows.
262  call assert_inrange(wh1, wh1 + 1, wh2)
263  call assert_inrange(wh3, wh3 + 1, wh2)
264
265  bw Xa Xb Xc
266endfunc
267
268func Test_window_width()
269  e Xa
270  vsplit Xb
271
272  let [ww1, ww2] = [winwidth(1), winwidth(2)]
273  " Active window (1) should have the same width or 1 more
274  " than the other window.
275  call assert_inrange(ww2, ww2 + 1, ww1)
276
277  wincmd <
278  call assert_equal(ww1 - 1, winwidth(1))
279  call assert_equal(ww2 + 1, winwidth(2))
280
281  wincmd >
282  call assert_equal(ww1, winwidth(1))
283  call assert_equal(ww2, winwidth(2))
284
285  2wincmd |
286  call assert_equal(2, winwidth(1))
287  call assert_equal(ww1 + ww2 - 2, winwidth(2))
288
289  wincmd =
290  call assert_equal(ww1, winwidth(1))
291  call assert_equal(ww2, winwidth(2))
292
293  2wincmd |
294  set winfixwidth
295  vsplit Xc
296  let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
297  call assert_equal(2, winwidth(2))
298  call assert_inrange(ww3, ww3 + 1, ww1)
299  3wincmd >
300  call assert_equal(2,       winwidth(2))
301  call assert_equal(ww1 + 3, winwidth(1))
302  call assert_equal(ww3 - 3, winwidth(3))
303  wincmd =
304  call assert_equal(2,   winwidth(2))
305  call assert_equal(ww1, winwidth(1))
306  call assert_equal(ww3, winwidth(3))
307
308  wincmd l
309  set winfixwidth&
310
311  wincmd =
312  let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
313  " Current window (2) should have the same width or 1 more
314  " than the other windows.
315  call assert_inrange(ww1, ww1 + 1, ww2)
316  call assert_inrange(ww3, ww3 + 1, ww2)
317
318  bw Xa Xb Xc
319endfunc
320
321func Test_equalalways_on_close()
322  set equalalways
323  vsplit
324  windo split
325  split
326  wincmd J
327  " now we have a frame top-left with two windows, a frame top-right with two
328  " windows and a frame at the bottom, full-width.
329  let height_1 = winheight(1)
330  let height_2 = winheight(2)
331  let height_3 = winheight(3)
332  let height_4 = winheight(4)
333  " closing the bottom window causes all windows to be resized.
334  close
335  call assert_notequal(height_1, winheight(1))
336  call assert_notequal(height_2, winheight(2))
337  call assert_notequal(height_3, winheight(3))
338  call assert_notequal(height_4, winheight(4))
339  call assert_equal(winheight(1), winheight(3))
340  call assert_equal(winheight(2), winheight(4))
341
342  1wincmd w
343  split
344  4wincmd w
345  resize + 5
346  " left column has three windows, equalized heights.
347  " right column has two windows, top one a bit higher
348  let height_1 = winheight(1)
349  let height_2 = winheight(2)
350  let height_4 = winheight(4)
351  let height_5 = winheight(5)
352  3wincmd w
353  " closing window in left column equalizes heights in left column but not in
354  " the right column
355  close
356  call assert_notequal(height_1, winheight(1))
357  call assert_notequal(height_2, winheight(2))
358  call assert_equal(height_4, winheight(3))
359  call assert_equal(height_5, winheight(4))
360
361  only
362  set equalalways&
363endfunc
364
365func Test_window_jump_tag()
366  help
367  /iccf
368  call assert_match('^|iccf|',  getline('.'))
369  call assert_equal(2, winnr('$'))
370  2wincmd }
371  call assert_equal(3, winnr('$'))
372  call assert_match('^|iccf|',  getline('.'))
373  wincmd k
374  call assert_match('\*iccf\*',  getline('.'))
375  call assert_equal(2, winheight(0))
376
377  wincmd z
378  set previewheight=4
379  help
380  /bugs
381  wincmd }
382  wincmd k
383  call assert_match('\*bugs\*',  getline('.'))
384  call assert_equal(4, winheight(0))
385  set previewheight&
386
387  %bw!
388endfunc
389
390func Test_window_newtab()
391  e Xa
392
393  call assert_equal(1, tabpagenr('$'))
394  call assert_equal("\nAlready only one window", execute('wincmd T'))
395
396  split Xb
397  split Xc
398
399  wincmd T
400  call assert_equal(2, tabpagenr('$'))
401  call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
402  call assert_equal(['Xc'      ], map(tabpagebuflist(2), 'bufname(v:val)'))
403
404  %bw!
405endfunc
406
407func Test_next_split_all()
408  " This was causing an illegal memory access.
409  n x
410  norm axxx
411  split
412  split
413  s/x
414  s/x
415  all
416  bwipe!
417endfunc
418
419" vim: shiftwidth=2 sts=2 expandtab
420