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_win_screenpos()
366  call assert_equal(1, winnr('$'))
367  split
368  vsplit
369  10wincmd _
370  30wincmd |
371  call assert_equal([1, 1], win_screenpos(1))
372  call assert_equal([1, 32], win_screenpos(2))
373  call assert_equal([12, 1], win_screenpos(3))
374  call assert_equal([0, 0], win_screenpos(4))
375  only
376endfunc
377
378func Test_window_jump_tag()
379  help
380  /iccf
381  call assert_match('^|iccf|',  getline('.'))
382  call assert_equal(2, winnr('$'))
383  2wincmd }
384  call assert_equal(3, winnr('$'))
385  call assert_match('^|iccf|',  getline('.'))
386  wincmd k
387  call assert_match('\*iccf\*',  getline('.'))
388  call assert_equal(2, winheight(0))
389
390  wincmd z
391  set previewheight=4
392  help
393  /bugs
394  wincmd }
395  wincmd k
396  call assert_match('\*bugs\*',  getline('.'))
397  call assert_equal(4, winheight(0))
398  set previewheight&
399
400  %bw!
401endfunc
402
403func Test_window_newtab()
404  e Xa
405
406  call assert_equal(1, tabpagenr('$'))
407  call assert_equal("\nAlready only one window", execute('wincmd T'))
408
409  split Xb
410  split Xc
411
412  wincmd T
413  call assert_equal(2, tabpagenr('$'))
414  call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
415  call assert_equal(['Xc'      ], map(tabpagebuflist(2), 'bufname(v:val)'))
416
417  %bw!
418endfunc
419
420func Test_next_split_all()
421  " This was causing an illegal memory access.
422  n x
423  norm axxx
424  split
425  split
426  s/x
427  s/x
428  all
429  bwipe!
430endfunc
431
432" Tests for adjusting window and contents
433func GetScreenStr(row)
434   let str = ""
435   for c in range(1,3)
436       let str .= nr2char(screenchar(a:row, c))
437   endfor
438   return str
439endfunc
440
441func Test_window_contents()
442  enew! | only | new
443  call setline(1, range(1,256))
444
445  exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
446  redraw
447  let s3=GetScreenStr(1)
448  wincmd p
449  call assert_equal(1, line("w0"))
450  call assert_equal('1  ', s3)
451
452  exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
453  redraw
454  let s3=GetScreenStr(1)
455  wincmd p
456  call assert_equal(50, line("w0"))
457  call assert_equal('50 ', s3)
458
459  exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
460  redraw
461  let s3=GetScreenStr(1)
462  wincmd p
463  call assert_equal(59, line("w0"))
464  call assert_equal('59 ', s3)
465
466  bwipeout!
467  call test_garbagecollect_now()
468endfunc
469
470func Test_window_colon_command()
471  " This was reading invalid memory.
472  exe "norm! v\<C-W>:\<C-U>echo v:version"
473endfunc
474
475func Test_access_freed_mem()
476  " This was accessing freed memory
477  au * 0 vs xxx
478  arg 0
479  argadd
480  all
481  all
482  au!
483  bwipe xxx
484endfunc
485
486" vim: shiftwidth=2 sts=2 expandtab
487