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  " Remove the catch-all that runtest.vim adds
46  au! SwapExists
47  augroup test_window_cmd_wincmd_gf
48    autocmd!
49    exec "autocmd SwapExists " . fname . " call s:swap_exists()"
50  augroup END
51
52  call setline(1, fname)
53  " (E)dit anyway
54  let s:swap_choice = 'e'
55  wincmd gf
56  call assert_equal(2, tabpagenr())
57  call assert_equal(fname, bufname("%"))
58  quit!
59
60  " (Q)uit
61  let s:swap_choice = 'q'
62  wincmd gf
63  call assert_equal(1, tabpagenr())
64  call assert_notequal(fname, bufname("%"))
65  new | only!
66
67  call delete(fname)
68  call delete(swp_fname)
69  augroup! test_window_cmd_wincmd_gf
70endfunc
71
72func Test_window_quit()
73  e Xa
74  split Xb
75  call assert_equal(2, winnr('$'))
76  call assert_equal('Xb', bufname(winbufnr(1)))
77  call assert_equal('Xa', bufname(winbufnr(2)))
78
79  wincmd q
80  call assert_equal(1, winnr('$'))
81  call assert_equal('Xa', bufname(winbufnr(1)))
82
83  bw Xa Xb
84endfunc
85
86func Test_window_horizontal_split()
87  call assert_equal(1, winnr('$'))
88  3wincmd s
89  call assert_equal(2, winnr('$'))
90  call assert_equal(3, winheight(0))
91  call assert_equal(winwidth(1), winwidth(2))
92
93  call assert_fails('botright topleft wincmd s', 'E442:')
94  bw
95endfunc
96
97func Test_window_vertical_split()
98  call assert_equal(1, winnr('$'))
99  3wincmd v
100  call assert_equal(2, winnr('$'))
101  call assert_equal(3, winwidth(0))
102  call assert_equal(winheight(1), winheight(2))
103
104  call assert_fails('botright topleft wincmd v', 'E442:')
105  bw
106endfunc
107
108" Test the ":wincmd ^" and "<C-W>^" commands.
109func Test_window_split_edit_alternate()
110
111  " Test for failure when the alternate buffer/file no longer exists.
112  edit Xfoo | %bw
113  call assert_fails(':wincmd ^', 'E23')
114
115  " Test for the expected behavior when we have two named buffers.
116  edit Xfoo | edit Xbar
117  wincmd ^
118  call assert_equal('Xfoo', bufname(winbufnr(1)))
119  call assert_equal('Xbar', bufname(winbufnr(2)))
120  only
121
122  " Test for the expected behavior when the alternate buffer is not named.
123  enew | let l:nr1 = bufnr('%')
124  edit Xfoo | let l:nr2 = bufnr('%')
125  wincmd ^
126  call assert_equal(l:nr1, winbufnr(1))
127  call assert_equal(l:nr2, winbufnr(2))
128  only
129
130  " FIXME: this currently fails on AppVeyor, but passes locally
131  if !has('win32')
132    " Test the Normal mode command.
133    call feedkeys("\<C-W>\<C-^>", 'tx')
134    call assert_equal(l:nr2, winbufnr(1))
135    call assert_equal(l:nr1, winbufnr(2))
136  endif
137
138  %bw!
139endfunc
140
141" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
142func Test_window_split_edit_bufnr()
143
144  %bwipeout
145  let l:nr = bufnr('%') + 1
146  call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92')
147  call assert_fails(':' . l:nr . 'wincmd ^', 'E16')
148  call assert_fails(':0wincmd ^', 'E16')
149
150  edit Xfoo | edit Xbar | edit Xbaz
151  let l:foo_nr = bufnr('Xfoo')
152  let l:bar_nr = bufnr('Xbar')
153  let l:baz_nr = bufnr('Xbaz')
154
155  " FIXME: this currently fails on AppVeyor, but passes locally
156  if !has('win32')
157    call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
158    call assert_equal('Xfoo', bufname(winbufnr(1)))
159    call assert_equal('Xbaz', bufname(winbufnr(2)))
160    only
161
162    call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
163    call assert_equal('Xbar', bufname(winbufnr(1)))
164    call assert_equal('Xfoo', bufname(winbufnr(2)))
165    only
166
167    execute l:baz_nr . 'wincmd ^'
168    call assert_equal('Xbaz', bufname(winbufnr(1)))
169    call assert_equal('Xbar', bufname(winbufnr(2)))
170  endif
171
172  %bw!
173endfunc
174
175func Test_window_exchange()
176  e Xa
177
178  " Nothing happens with window exchange when there is 1 window
179  wincmd x
180  call assert_equal(1, winnr('$'))
181
182  split Xb
183  split Xc
184
185  call assert_equal('Xc', bufname(winbufnr(1)))
186  call assert_equal('Xb', bufname(winbufnr(2)))
187  call assert_equal('Xa', bufname(winbufnr(3)))
188
189  " Exchange current window 1 with window 3
190  3wincmd x
191  call assert_equal('Xa', bufname(winbufnr(1)))
192  call assert_equal('Xb', bufname(winbufnr(2)))
193  call assert_equal('Xc', bufname(winbufnr(3)))
194
195  " Exchange window with next when at the top window
196  wincmd x
197  call assert_equal('Xb', bufname(winbufnr(1)))
198  call assert_equal('Xa', bufname(winbufnr(2)))
199  call assert_equal('Xc', bufname(winbufnr(3)))
200
201  " Exchange window with next when at the middle window
202  wincmd j
203  wincmd x
204  call assert_equal('Xb', bufname(winbufnr(1)))
205  call assert_equal('Xc', bufname(winbufnr(2)))
206  call assert_equal('Xa', bufname(winbufnr(3)))
207
208  " Exchange window with next when at the bottom window.
209  " When there is no next window, it exchanges with the previous window.
210  wincmd j
211  wincmd x
212  call assert_equal('Xb', bufname(winbufnr(1)))
213  call assert_equal('Xa', bufname(winbufnr(2)))
214  call assert_equal('Xc', bufname(winbufnr(3)))
215
216  bw Xa Xb Xc
217endfunc
218
219func Test_window_rotate()
220  e Xa
221  split Xb
222  split Xc
223  call assert_equal('Xc', bufname(winbufnr(1)))
224  call assert_equal('Xb', bufname(winbufnr(2)))
225  call assert_equal('Xa', bufname(winbufnr(3)))
226
227  " Rotate downwards
228  wincmd r
229  call assert_equal('Xa', bufname(winbufnr(1)))
230  call assert_equal('Xc', bufname(winbufnr(2)))
231  call assert_equal('Xb', bufname(winbufnr(3)))
232
233  2wincmd r
234  call assert_equal('Xc', bufname(winbufnr(1)))
235  call assert_equal('Xb', bufname(winbufnr(2)))
236  call assert_equal('Xa', bufname(winbufnr(3)))
237
238  " Rotate upwards
239  wincmd R
240  call assert_equal('Xb', bufname(winbufnr(1)))
241  call assert_equal('Xa', bufname(winbufnr(2)))
242  call assert_equal('Xc', bufname(winbufnr(3)))
243
244  2wincmd R
245  call assert_equal('Xc', bufname(winbufnr(1)))
246  call assert_equal('Xb', bufname(winbufnr(2)))
247  call assert_equal('Xa', bufname(winbufnr(3)))
248
249  bot vsplit
250  call assert_fails('wincmd R', 'E443:')
251
252  bw Xa Xb Xc
253endfunc
254
255func Test_window_height()
256  e Xa
257  split Xb
258
259  let [wh1, wh2] = [winheight(1), winheight(2)]
260  " Active window (1) should have the same height or 1 more
261  " than the other window.
262  call assert_inrange(wh2, wh2 + 1, wh1)
263
264  wincmd -
265  call assert_equal(wh1 - 1, winheight(1))
266  call assert_equal(wh2 + 1, winheight(2))
267
268  wincmd +
269  call assert_equal(wh1, winheight(1))
270  call assert_equal(wh2, winheight(2))
271
272  2wincmd _
273  call assert_equal(2, winheight(1))
274  call assert_equal(wh1 + wh2 - 2, winheight(2))
275
276  wincmd =
277  call assert_equal(wh1, winheight(1))
278  call assert_equal(wh2, winheight(2))
279
280  2wincmd _
281  set winfixheight
282  split Xc
283  let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
284  call assert_equal(2, winheight(2))
285  call assert_inrange(wh3, wh3 + 1, wh1)
286  3wincmd +
287  call assert_equal(2,       winheight(2))
288  call assert_equal(wh1 + 3, winheight(1))
289  call assert_equal(wh3 - 3, winheight(3))
290  wincmd =
291  call assert_equal(2,   winheight(2))
292  call assert_equal(wh1, winheight(1))
293  call assert_equal(wh3, winheight(3))
294
295  wincmd j
296  set winfixheight&
297
298  wincmd =
299  let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
300  " Current window (2) should have the same height or 1 more
301  " than the other windows.
302  call assert_inrange(wh1, wh1 + 1, wh2)
303  call assert_inrange(wh3, wh3 + 1, wh2)
304
305  bw Xa Xb Xc
306endfunc
307
308func Test_window_width()
309  e Xa
310  vsplit Xb
311
312  let [ww1, ww2] = [winwidth(1), winwidth(2)]
313  " Active window (1) should have the same width or 1 more
314  " than the other window.
315  call assert_inrange(ww2, ww2 + 1, ww1)
316
317  wincmd <
318  call assert_equal(ww1 - 1, winwidth(1))
319  call assert_equal(ww2 + 1, winwidth(2))
320
321  wincmd >
322  call assert_equal(ww1, winwidth(1))
323  call assert_equal(ww2, winwidth(2))
324
325  2wincmd |
326  call assert_equal(2, winwidth(1))
327  call assert_equal(ww1 + ww2 - 2, winwidth(2))
328
329  wincmd =
330  call assert_equal(ww1, winwidth(1))
331  call assert_equal(ww2, winwidth(2))
332
333  2wincmd |
334  set winfixwidth
335  vsplit Xc
336  let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
337  call assert_equal(2, winwidth(2))
338  call assert_inrange(ww3, ww3 + 1, ww1)
339  3wincmd >
340  call assert_equal(2,       winwidth(2))
341  call assert_equal(ww1 + 3, winwidth(1))
342  call assert_equal(ww3 - 3, winwidth(3))
343  wincmd =
344  call assert_equal(2,   winwidth(2))
345  call assert_equal(ww1, winwidth(1))
346  call assert_equal(ww3, winwidth(3))
347
348  wincmd l
349  set winfixwidth&
350
351  wincmd =
352  let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
353  " Current window (2) should have the same width or 1 more
354  " than the other windows.
355  call assert_inrange(ww1, ww1 + 1, ww2)
356  call assert_inrange(ww3, ww3 + 1, ww2)
357
358  bw Xa Xb Xc
359endfunc
360
361func Test_equalalways_on_close()
362  set equalalways
363  vsplit
364  windo split
365  split
366  wincmd J
367  " now we have a frame top-left with two windows, a frame top-right with two
368  " windows and a frame at the bottom, full-width.
369  let height_1 = winheight(1)
370  let height_2 = winheight(2)
371  let height_3 = winheight(3)
372  let height_4 = winheight(4)
373  " closing the bottom window causes all windows to be resized.
374  close
375  call assert_notequal(height_1, winheight(1))
376  call assert_notequal(height_2, winheight(2))
377  call assert_notequal(height_3, winheight(3))
378  call assert_notequal(height_4, winheight(4))
379  call assert_equal(winheight(1), winheight(3))
380  call assert_equal(winheight(2), winheight(4))
381
382  1wincmd w
383  split
384  4wincmd w
385  resize + 5
386  " left column has three windows, equalized heights.
387  " right column has two windows, top one a bit higher
388  let height_1 = winheight(1)
389  let height_2 = winheight(2)
390  let height_4 = winheight(4)
391  let height_5 = winheight(5)
392  3wincmd w
393  " closing window in left column equalizes heights in left column but not in
394  " the right column
395  close
396  call assert_notequal(height_1, winheight(1))
397  call assert_notequal(height_2, winheight(2))
398  call assert_equal(height_4, winheight(3))
399  call assert_equal(height_5, winheight(4))
400
401  only
402  set equalalways&
403endfunc
404
405func Test_win_screenpos()
406  call assert_equal(1, winnr('$'))
407  split
408  vsplit
409  10wincmd _
410  30wincmd |
411  call assert_equal([1, 1], win_screenpos(1))
412  call assert_equal([1, 32], win_screenpos(2))
413  call assert_equal([12, 1], win_screenpos(3))
414  call assert_equal([0, 0], win_screenpos(4))
415  only
416endfunc
417
418func Test_window_jump_tag()
419  help
420  /iccf
421  call assert_match('^|iccf|',  getline('.'))
422  call assert_equal(2, winnr('$'))
423  2wincmd }
424  call assert_equal(3, winnr('$'))
425  call assert_match('^|iccf|',  getline('.'))
426  wincmd k
427  call assert_match('\*iccf\*',  getline('.'))
428  call assert_equal(2, winheight(0))
429
430  wincmd z
431  set previewheight=4
432  help
433  /bugs
434  wincmd }
435  wincmd k
436  call assert_match('\*bugs\*',  getline('.'))
437  call assert_equal(4, winheight(0))
438  set previewheight&
439
440  %bw!
441endfunc
442
443func Test_window_newtab()
444  e Xa
445
446  call assert_equal(1, tabpagenr('$'))
447  call assert_equal("\nAlready only one window", execute('wincmd T'))
448
449  split Xb
450  split Xc
451
452  wincmd T
453  call assert_equal(2, tabpagenr('$'))
454  call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
455  call assert_equal(['Xc'      ], map(tabpagebuflist(2), 'bufname(v:val)'))
456
457  %bw!
458endfunc
459
460func Test_next_split_all()
461  " This was causing an illegal memory access.
462  n x
463  norm axxx
464  split
465  split
466  s/x
467  s/x
468  all
469  bwipe!
470endfunc
471
472" Tests for adjusting window and contents
473func GetScreenStr(row)
474   let str = ""
475   for c in range(1,3)
476       let str .= nr2char(screenchar(a:row, c))
477   endfor
478   return str
479endfunc
480
481func Test_window_contents()
482  enew! | only | new
483  call setline(1, range(1,256))
484
485  exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
486  redraw
487  let s3 = GetScreenStr(1)
488  wincmd p
489  call assert_equal(1, line("w0"))
490  call assert_equal('1  ', s3)
491
492  exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
493  redraw
494  let s3 = GetScreenStr(1)
495  wincmd p
496  call assert_equal(50, line("w0"))
497  call assert_equal('50 ', s3)
498
499  exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
500  redraw
501  let s3 = GetScreenStr(1)
502  wincmd p
503  call assert_equal(59, line("w0"))
504  call assert_equal('59 ', s3)
505
506  bwipeout!
507  call test_garbagecollect_now()
508endfunc
509
510func Test_window_colon_command()
511  " This was reading invalid memory.
512  exe "norm! v\<C-W>:\<C-U>echo v:version"
513endfunc
514
515func Test_access_freed_mem()
516  " This was accessing freed memory (but with what events?)
517  au BufEnter,BufLeave,WinEnter,WinLeave 0 vs xxx
518  arg 0
519  argadd
520  all
521  all
522  au!
523  bwipe xxx
524endfunc
525
526func Test_visual_cleared_after_window_split()
527  new | only!
528  let smd_save = &showmode
529  set showmode
530  let ls_save = &laststatus
531  set laststatus=1
532  call setline(1, ['a', 'b', 'c', 'd', ''])
533  norm! G
534  exe "norm! kkvk"
535  redraw
536  exe "norm! \<C-W>v"
537  redraw
538  " check if '-- VISUAL --' disappeared from command line
539  let columns = range(1, &columns)
540  let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
541  let cmdline = join(cmdlinechars, '')
542  let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
543  let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
544  call assert_equal('', mode_shown)
545  let &showmode = smd_save
546  let &laststatus = ls_save
547  bwipe!
548endfunc
549
550func Test_winrestcmd()
551  2split
552  3vsplit
553  let restcmd = winrestcmd()
554  call assert_equal(2, winheight(0))
555  call assert_equal(3, winwidth(0))
556  wincmd =
557  call assert_notequal(2, winheight(0))
558  call assert_notequal(3, winwidth(0))
559  exe restcmd
560  call assert_equal(2, winheight(0))
561  call assert_equal(3, winwidth(0))
562  only
563
564  wincmd v
565  wincmd s
566  wincmd v
567  redraw
568  let restcmd = winrestcmd()
569  wincmd _
570  wincmd |
571  exe restcmd
572  redraw
573  call assert_equal(restcmd, winrestcmd())
574
575  only
576endfunc
577
578function! Fun_RenewFile()
579  " Need to wait a bit for the timestamp to be older.
580  sleep 2
581  silent execute '!echo "1" > tmp.txt'
582  sp
583  wincmd p
584  edit! tmp.txt
585endfunction
586
587func Test_window_prevwin()
588  " Can we make this work on MS-Windows?
589  if !has('unix')
590    return
591  endif
592
593  set hidden autoread
594  call writefile(['2'], 'tmp.txt')
595  new tmp.txt
596  q
597  call Fun_RenewFile()
598  call assert_equal(2, winnr())
599  wincmd p
600  call assert_equal(1, winnr())
601  wincmd p
602  q
603  call Fun_RenewFile()
604  call assert_equal(2, winnr())
605  wincmd p
606  call assert_equal(1, winnr())
607  wincmd p
608  " reset
609  q
610  call delete('tmp.txt')
611  set nohidden autoread&vim
612  delfunc Fun_RenewFile
613endfunc
614
615func Test_relative_cursor_position_in_one_line_window()
616  new
617  only
618  call setline(1, range(1, 10000))
619  normal 50%
620  let lnum = getcurpos()[1]
621  split
622  split
623  " make third window take as many lines as possible, other windows will
624  " become one line
625  3wincmd w
626  for i in range(1, &lines - 6)
627    wincmd +
628    redraw!
629  endfor
630
631  " first and second window should show cursor line
632  let wininfo = getwininfo()
633  call assert_equal(lnum, wininfo[0].topline)
634  call assert_equal(lnum, wininfo[1].topline)
635
636  only!
637  bwipe!
638endfunc
639
640func Test_relative_cursor_position_after_move_and_resize()
641  let so_save = &so
642  set so=0
643  enew
644  call setline(1, range(1, 10000))
645  normal 50%
646  split
647  1wincmd w
648  " Move cursor to first line in window
649  normal H
650  redraw!
651  " Reduce window height to two lines
652  let height = winheight(0)
653  while winheight(0) > 2
654    wincmd -
655    redraw!
656  endwhile
657  " move cursor to second/last line in window
658  normal j
659  " restore previous height
660  while winheight(0) < height
661    wincmd +
662    redraw!
663  endwhile
664  " make window two lines again
665  while winheight(0) > 2
666    wincmd -
667    redraw!
668  endwhile
669
670  " cursor should be at bottom line
671  let info = getwininfo(win_getid())[0]
672  call assert_equal(info.topline + 1, getcurpos()[1])
673
674  only!
675  bwipe!
676  let &so = so_save
677endfunc
678
679func Test_relative_cursor_position_after_resize()
680  let so_save = &so
681  set so=0
682  enew
683  call setline(1, range(1, 10000))
684  normal 50%
685  split
686  1wincmd w
687  let winid1 = win_getid()
688  let info = getwininfo(winid1)[0]
689  " Move cursor to second line in window
690  exe "normal " . (info.topline + 1) . "G"
691  redraw!
692  let lnum = getcurpos()[1]
693
694  " Make the window only two lines high, cursor should end up in top line
695  2wincmd w
696  exe (info.height - 2) . "wincmd +"
697  redraw!
698  let info = getwininfo(winid1)[0]
699  call assert_equal(lnum, info.topline)
700
701  only!
702  bwipe!
703  let &so = so_save
704endfunc
705
706func Test_relative_cursor_second_line_after_resize()
707  let so_save = &so
708  set so=0
709  enew
710  call setline(1, range(1, 10000))
711  normal 50%
712  split
713  1wincmd w
714  let winid1 = win_getid()
715  let info = getwininfo(winid1)[0]
716
717  " Make the window only two lines high
718  2wincmd _
719
720  " Move cursor to second line in window
721  normal H
722  normal j
723
724  " Make window size bigger, then back to 2 lines
725  for i in range(1, 10)
726    wincmd +
727    redraw!
728  endfor
729  for i in range(1, 10)
730    wincmd -
731    redraw!
732  endfor
733
734  " cursor should end up in bottom line
735  let info = getwininfo(winid1)[0]
736  call assert_equal(info.topline + 1, getcurpos()[1])
737
738  only!
739  bwipe!
740  let &so = so_save
741endfunc
742
743func Test_split_noscroll()
744  let so_save = &so
745  enew
746  call setline(1, range(1, 8))
747  normal 100%
748  split
749
750  1wincmd w
751  let winid1 = win_getid()
752  let info1 = getwininfo(winid1)[0]
753
754  2wincmd w
755  let winid2 = win_getid()
756  let info2 = getwininfo(winid2)[0]
757
758  call assert_equal(1, info1.topline)
759  call assert_equal(1, info2.topline)
760
761  " window that fits all lines by itself, but not when split: closing other
762  " window should restore fraction.
763  only!
764  call setline(1, range(1, &lines - 10))
765  exe &lines / 4
766  let winid1 = win_getid()
767  let info1 = getwininfo(winid1)[0]
768  call assert_equal(1, info1.topline)
769  new
770  redraw
771  close
772  let info1 = getwininfo(winid1)[0]
773  call assert_equal(1, info1.topline)
774
775  bwipe!
776  let &so = so_save
777endfunc
778
779" Tests for the winnr() function
780func Test_winnr()
781  only | tabonly
782  call assert_equal(1, winnr('j'))
783  call assert_equal(1, winnr('k'))
784  call assert_equal(1, winnr('h'))
785  call assert_equal(1, winnr('l'))
786
787  " create a set of horizontally and vertically split windows
788  leftabove new | wincmd p
789  leftabove new | wincmd p
790  rightbelow new | wincmd p
791  rightbelow new | wincmd p
792  leftabove vnew | wincmd p
793  leftabove vnew | wincmd p
794  rightbelow vnew | wincmd p
795  rightbelow vnew | wincmd p
796
797  call assert_equal(8, winnr('j'))
798  call assert_equal(2, winnr('k'))
799  call assert_equal(4, winnr('h'))
800  call assert_equal(6, winnr('l'))
801  call assert_equal(9, winnr('2j'))
802  call assert_equal(1, winnr('2k'))
803  call assert_equal(3, winnr('2h'))
804  call assert_equal(7, winnr('2l'))
805
806  " Error cases
807  call assert_fails("echo winnr('0.2k')", 'E15:')
808  call assert_equal(2, winnr('-2k'))
809  call assert_fails("echo winnr('-2xj')", 'E15:')
810  call assert_fails("echo winnr('j2j')", 'E15:')
811  call assert_fails("echo winnr('ll')", 'E15:')
812  call assert_fails("echo winnr('5')", 'E15:')
813  call assert_equal(4, winnr('0h'))
814
815  tabnew
816  call assert_equal(8, tabpagewinnr(1, 'j'))
817  call assert_equal(2, tabpagewinnr(1, 'k'))
818  call assert_equal(4, tabpagewinnr(1, 'h'))
819  call assert_equal(6, tabpagewinnr(1, 'l'))
820
821  only | tabonly
822endfunc
823
824func Test_win_splitmove()
825  edit a
826  leftabove split b
827  leftabove vsplit c
828  leftabove split d
829  call assert_equal(0, win_splitmove(winnr(), winnr('l')))
830  call assert_equal(bufname(winbufnr(1)), 'c')
831  call assert_equal(bufname(winbufnr(2)), 'd')
832  call assert_equal(bufname(winbufnr(3)), 'b')
833  call assert_equal(bufname(winbufnr(4)), 'a')
834  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
835  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
836  call assert_equal(bufname(winbufnr(1)), 'c')
837  call assert_equal(bufname(winbufnr(2)), 'b')
838  call assert_equal(bufname(winbufnr(3)), 'd')
839  call assert_equal(bufname(winbufnr(4)), 'a')
840  call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
841  call assert_equal(bufname(winbufnr(1)), 'd')
842  call assert_equal(bufname(winbufnr(2)), 'c')
843  call assert_equal(bufname(winbufnr(3)), 'b')
844  call assert_equal(bufname(winbufnr(4)), 'a')
845  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
846  call assert_equal(bufname(winbufnr(1)), 'c')
847  call assert_equal(bufname(winbufnr(2)), 'b')
848  call assert_equal(bufname(winbufnr(3)), 'a')
849  call assert_equal(bufname(winbufnr(4)), 'd')
850  only | bd
851
852  call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
853  call assert_fails('call win_splitmove(123, winnr())', 'E957:')
854  call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
855
856  tabnew
857  call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
858  tabclose
859endfunc
860
861func Test_floatwin_splitmove()
862  vsplit
863  let win2 = win_getid()
864  let popup_winid = nvim_open_win(0, 0, {'relative': 'win',
865        \ 'row': 3, 'col': 3, 'width': 12, 'height': 3})
866  call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
867  call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
868
869  call nvim_win_close(popup_winid, 1)
870  bwipe
871endfunc
872
873func Test_window_resize()
874  " Vertical :resize (absolute, relative, min and max size).
875  vsplit
876  vert resize 8
877  call assert_equal(8, winwidth(0))
878  vert resize +2
879  call assert_equal(10, winwidth(0))
880  vert resize -2
881  call assert_equal(8, winwidth(0))
882  vert resize
883  call assert_equal(&columns - 2, winwidth(0))
884  vert resize 0
885  call assert_equal(1, winwidth(0))
886  vert resize 99999
887  call assert_equal(&columns - 2, winwidth(0))
888
889  %bwipe!
890
891  " Horizontal :resize (with absolute, relative size, min and max size).
892  split
893  resize 8
894  call assert_equal(8, winheight(0))
895  resize +2
896  call assert_equal(10, winheight(0))
897  resize -2
898  call assert_equal(8, winheight(0))
899  resize
900  call assert_equal(&lines - 4, winheight(0))
901  resize 0
902  call assert_equal(1, winheight(0))
903  resize 99999
904  call assert_equal(&lines - 4, winheight(0))
905
906  " :resize with explicit window number.
907  let other_winnr = winnr('j')
908  exe other_winnr .. 'resize 10'
909  call assert_equal(10, winheight(other_winnr))
910  call assert_equal(&lines - 10 - 3, winheight(0))
911  exe other_winnr .. 'resize +1'
912  exe other_winnr .. 'resize +1'
913  call assert_equal(12, winheight(other_winnr))
914  call assert_equal(&lines - 10 - 3 -2, winheight(0))
915  close
916
917  vsplit
918  wincmd l
919  let other_winnr = winnr('h')
920  call assert_notequal(winnr(), other_winnr)
921  exe 'vert ' .. other_winnr .. 'resize -' .. &columns
922  call assert_equal(0, winwidth(other_winnr))
923
924  %bwipe!
925endfunc
926
927" vim: shiftwidth=2 sts=2 expandtab
928