1" Tests for window cmd (:wincmd, :split, :vsplit, :resize and etc...)
2
3so check.vim
4
5func Test_window_cmd_ls0_with_split()
6  set ls=0
7  set splitbelow
8  split
9  quit
10  call assert_equal(0, &lines - &cmdheight - winheight(0))
11  new | only!
12  "
13  set splitbelow&vim
14  botright split
15  quit
16  call assert_equal(0, &lines - &cmdheight - winheight(0))
17  new | only!
18  set ls&vim
19endfunc
20
21func Test_window_cmd_cmdwin_with_vsp()
22  let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
23  for v in range(0, 2)
24    exec "set ls=" . v
25    vsplit
26    call feedkeys("q:\<CR>")
27    let ac = &lines - (&cmdheight + winheight(0) + !!v)
28    let emsg = printf(efmt, ac, v, 'left')
29    call assert_equal(0, ac, emsg)
30    wincmd w
31    let ac = &lines - (&cmdheight + winheight(0) + !!v)
32    let emsg = printf(efmt, ac, v, 'right')
33    call assert_equal(0, ac, emsg)
34    new | only!
35  endfor
36  set ls&vim
37endfunc
38
39function Test_window_cmd_wincmd_gf()
40  let fname = 'test_gf.txt'
41  let swp_fname = '.' . fname . '.swp'
42  call writefile([], fname)
43  call writefile([], swp_fname)
44  function s:swap_exists()
45    let v:swapchoice = s:swap_choice
46  endfunc
47  " Remove the catch-all that runtest.vim adds
48  au! SwapExists
49  augroup test_window_cmd_wincmd_gf
50    autocmd!
51    exec "autocmd SwapExists " . fname . " call s:swap_exists()"
52  augroup END
53
54  call setline(1, fname)
55  " (E)dit anyway
56  let s:swap_choice = 'e'
57  wincmd gf
58  call assert_equal(2, tabpagenr())
59  call assert_equal(fname, bufname("%"))
60  quit!
61
62  " (Q)uit
63  let s:swap_choice = 'q'
64  wincmd gf
65  call assert_equal(1, tabpagenr())
66  call assert_notequal(fname, bufname("%"))
67  new | only!
68
69  call delete(fname)
70  call delete(swp_fname)
71  augroup! test_window_cmd_wincmd_gf
72endfunc
73
74func Test_window_quit()
75  e Xa
76  split Xb
77  call assert_equal(2, '$'->winnr())
78  call assert_equal('Xb', bufname(winbufnr(1)))
79  call assert_equal('Xa', bufname(winbufnr(2)))
80
81  wincmd q
82  call assert_equal(1, winnr('$'))
83  call assert_equal('Xa', bufname(winbufnr(1)))
84
85  bw Xa Xb
86endfunc
87
88func Test_window_horizontal_split()
89  call assert_equal(1, winnr('$'))
90  3wincmd s
91  call assert_equal(2, winnr('$'))
92  call assert_equal(3, winheight(0))
93  call assert_equal(winwidth(1), 2->winwidth())
94
95  call assert_fails('botright topleft wincmd s', 'E442:')
96  bw
97endfunc
98
99func Test_window_vertical_split()
100  call assert_equal(1, winnr('$'))
101  3wincmd v
102  call assert_equal(2, winnr('$'))
103  call assert_equal(3, winwidth(0))
104  call assert_equal(winheight(1), winheight(2))
105
106  call assert_fails('botright topleft wincmd v', 'E442:')
107  bw
108endfunc
109
110" Test the ":wincmd ^" and "<C-W>^" commands.
111func Test_window_split_edit_alternate()
112
113  " Test for failure when the alternate buffer/file no longer exists.
114  edit Xfoo | %bw
115  call assert_fails(':wincmd ^', 'E23')
116
117  " Test for the expected behavior when we have two named buffers.
118  edit Xfoo | edit Xbar
119  wincmd ^
120  call assert_equal('Xfoo', bufname(winbufnr(1)))
121  call assert_equal('Xbar', bufname(winbufnr(2)))
122  only
123
124  " Test for the expected behavior when the alternate buffer is not named.
125  enew | let l:nr1 = bufnr('%')
126  edit Xfoo | let l:nr2 = bufnr('%')
127  wincmd ^
128  call assert_equal(l:nr1, winbufnr(1))
129  call assert_equal(l:nr2, winbufnr(2))
130  only
131
132  " FIXME: this currently fails on AppVeyor, but passes locally
133  if !has('win32')
134    " Test the Normal mode command.
135    call feedkeys("\<C-W>\<C-^>", 'tx')
136    call assert_equal(l:nr2, winbufnr(1))
137    call assert_equal(l:nr1, winbufnr(2))
138  endif
139
140  %bw!
141endfunc
142
143" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
144func Test_window_split_edit_bufnr()
145
146  %bwipeout
147  let l:nr = bufnr('%') + 1
148  call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92')
149  call assert_fails(':' . l:nr . 'wincmd ^', 'E16')
150  call assert_fails(':0wincmd ^', 'E16')
151
152  edit Xfoo | edit Xbar | edit Xbaz
153  let l:foo_nr = bufnr('Xfoo')
154  let l:bar_nr = bufnr('Xbar')
155  let l:baz_nr = bufnr('Xbaz')
156
157  " FIXME: this currently fails on AppVeyor, but passes locally
158  if !has('win32')
159    call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
160    call assert_equal('Xfoo', bufname(winbufnr(1)))
161    call assert_equal('Xbaz', bufname(winbufnr(2)))
162    only
163
164    call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
165    call assert_equal('Xbar', bufname(winbufnr(1)))
166    call assert_equal('Xfoo', bufname(winbufnr(2)))
167    only
168
169    execute l:baz_nr . 'wincmd ^'
170    call assert_equal('Xbaz', bufname(winbufnr(1)))
171    call assert_equal('Xbar', bufname(winbufnr(2)))
172  endif
173
174  %bw!
175endfunc
176
177func Test_window_split_no_room()
178  " N horizontal windows need >= 2*N + 1 lines:
179  " - 1 line + 1 status line in each window
180  " - 1 Ex command line
181  "
182  " 2*N + 1 <= &lines
183  " N <= (lines - 1)/2
184  "
185  " Beyond that number of windows, E36: Not enough room is expected.
186  let hor_win_count = (&lines - 1)/2
187  let hor_split_count = hor_win_count - 1
188  for s in range(1, hor_split_count) | split | endfor
189  call assert_fails('split', 'E36:')
190
191  " N vertical windows need >= 2*(N - 1) + 1 columns:
192  " - 1 column + 1 separator for each window (except last window)
193  " - 1 column for the last window which does not have separator
194  "
195  " 2*(N - 1) + 1 <= &columns
196  " 2*N - 1 <= &columns
197  " N <= (&columns + 1)/2
198  let ver_win_count = (&columns + 1)/2
199  let ver_split_count = ver_win_count - 1
200  for s in range(1, ver_split_count) | vsplit | endfor
201  call assert_fails('vsplit', 'E36:')
202
203  %bw!
204endfunc
205
206func Test_window_exchange()
207  e Xa
208
209  " Nothing happens with window exchange when there is 1 window
210  wincmd x
211  call assert_equal(1, winnr('$'))
212
213  split Xb
214  split Xc
215
216  call assert_equal('Xc', bufname(winbufnr(1)))
217  call assert_equal('Xb', bufname(winbufnr(2)))
218  call assert_equal('Xa', bufname(winbufnr(3)))
219
220  " Exchange current window 1 with window 3
221  3wincmd x
222  call assert_equal('Xa', bufname(winbufnr(1)))
223  call assert_equal('Xb', bufname(winbufnr(2)))
224  call assert_equal('Xc', bufname(winbufnr(3)))
225
226  " Exchange window with next when at the top window
227  wincmd x
228  call assert_equal('Xb', bufname(winbufnr(1)))
229  call assert_equal('Xa', bufname(winbufnr(2)))
230  call assert_equal('Xc', bufname(winbufnr(3)))
231
232  " Exchange window with next when at the middle window
233  wincmd j
234  wincmd x
235  call assert_equal('Xb', bufname(winbufnr(1)))
236  call assert_equal('Xc', bufname(winbufnr(2)))
237  call assert_equal('Xa', bufname(winbufnr(3)))
238
239  " Exchange window with next when at the bottom window.
240  " When there is no next window, it exchanges with the previous window.
241  wincmd j
242  wincmd x
243  call assert_equal('Xb', bufname(winbufnr(1)))
244  call assert_equal('Xa', bufname(winbufnr(2)))
245  call assert_equal('Xc', bufname(winbufnr(3)))
246
247  bw Xa Xb Xc
248endfunc
249
250func Test_window_rotate()
251  e Xa
252  split Xb
253  split Xc
254  call assert_equal('Xc', bufname(winbufnr(1)))
255  call assert_equal('Xb', bufname(winbufnr(2)))
256  call assert_equal('Xa', bufname(winbufnr(3)))
257
258  " Rotate downwards
259  wincmd r
260  call assert_equal('Xa', bufname(winbufnr(1)))
261  call assert_equal('Xc', bufname(winbufnr(2)))
262  call assert_equal('Xb', bufname(winbufnr(3)))
263
264  2wincmd r
265  call assert_equal('Xc', bufname(winbufnr(1)))
266  call assert_equal('Xb', bufname(winbufnr(2)))
267  call assert_equal('Xa', bufname(winbufnr(3)))
268
269  " Rotate upwards
270  wincmd R
271  call assert_equal('Xb', bufname(winbufnr(1)))
272  call assert_equal('Xa', bufname(winbufnr(2)))
273  call assert_equal('Xc', bufname(winbufnr(3)))
274
275  2wincmd R
276  call assert_equal('Xc', bufname(winbufnr(1)))
277  call assert_equal('Xb', bufname(winbufnr(2)))
278  call assert_equal('Xa', bufname(winbufnr(3)))
279
280  bot vsplit
281  call assert_fails('wincmd R', 'E443:')
282
283  bw Xa Xb Xc
284endfunc
285
286func Test_window_height()
287  e Xa
288  split Xb
289
290  let [wh1, wh2] = [winheight(1), winheight(2)]
291  " Active window (1) should have the same height or 1 more
292  " than the other window.
293  call assert_inrange(wh2, wh2 + 1, wh1)
294
295  wincmd -
296  call assert_equal(wh1 - 1, winheight(1))
297  call assert_equal(wh2 + 1, winheight(2))
298
299  wincmd +
300  call assert_equal(wh1, winheight(1))
301  call assert_equal(wh2, 2->winheight())
302
303  2wincmd _
304  call assert_equal(2, winheight(1))
305  call assert_equal(wh1 + wh2 - 2, winheight(2))
306
307  wincmd =
308  call assert_equal(wh1, winheight(1))
309  call assert_equal(wh2, winheight(2))
310
311  2wincmd _
312  set winfixheight
313  split Xc
314  let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
315  call assert_equal(2, winheight(2))
316  call assert_inrange(wh3, wh3 + 1, wh1)
317  3wincmd +
318  call assert_equal(2,       winheight(2))
319  call assert_equal(wh1 + 3, winheight(1))
320  call assert_equal(wh3 - 3, winheight(3))
321  wincmd =
322  call assert_equal(2,   winheight(2))
323  call assert_equal(wh1, winheight(1))
324  call assert_equal(wh3, winheight(3))
325
326  wincmd j
327  set winfixheight&
328
329  wincmd =
330  let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
331  " Current window (2) should have the same height or 1 more
332  " than the other windows.
333  call assert_inrange(wh1, wh1 + 1, wh2)
334  call assert_inrange(wh3, wh3 + 1, wh2)
335
336  bw Xa Xb Xc
337endfunc
338
339func Test_window_width()
340  e Xa
341  vsplit Xb
342
343  let [ww1, ww2] = [winwidth(1), winwidth(2)]
344  " Active window (1) should have the same width or 1 more
345  " than the other window.
346  call assert_inrange(ww2, ww2 + 1, ww1)
347
348  wincmd <
349  call assert_equal(ww1 - 1, winwidth(1))
350  call assert_equal(ww2 + 1, winwidth(2))
351
352  wincmd >
353  call assert_equal(ww1, winwidth(1))
354  call assert_equal(ww2, winwidth(2))
355
356  2wincmd |
357  call assert_equal(2, winwidth(1))
358  call assert_equal(ww1 + ww2 - 2, winwidth(2))
359
360  wincmd =
361  call assert_equal(ww1, winwidth(1))
362  call assert_equal(ww2, winwidth(2))
363
364  2wincmd |
365  set winfixwidth
366  vsplit Xc
367  let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
368  call assert_equal(2, winwidth(2))
369  call assert_inrange(ww3, ww3 + 1, ww1)
370  3wincmd >
371  call assert_equal(2,       winwidth(2))
372  call assert_equal(ww1 + 3, winwidth(1))
373  call assert_equal(ww3 - 3, winwidth(3))
374  wincmd =
375  call assert_equal(2,   winwidth(2))
376  call assert_equal(ww1, winwidth(1))
377  call assert_equal(ww3, winwidth(3))
378
379  wincmd l
380  set winfixwidth&
381
382  wincmd =
383  let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
384  " Current window (2) should have the same width or 1 more
385  " than the other windows.
386  call assert_inrange(ww1, ww1 + 1, ww2)
387  call assert_inrange(ww3, ww3 + 1, ww2)
388
389  bw Xa Xb Xc
390endfunc
391
392func Test_equalalways_on_close()
393  set equalalways
394  vsplit
395  windo split
396  split
397  wincmd J
398  " now we have a frame top-left with two windows, a frame top-right with two
399  " windows and a frame at the bottom, full-width.
400  let height_1 = winheight(1)
401  let height_2 = winheight(2)
402  let height_3 = winheight(3)
403  let height_4 = winheight(4)
404  " closing the bottom window causes all windows to be resized.
405  close
406  call assert_notequal(height_1, winheight(1))
407  call assert_notequal(height_2, winheight(2))
408  call assert_notequal(height_3, winheight(3))
409  call assert_notequal(height_4, winheight(4))
410  call assert_equal(winheight(1), winheight(3))
411  call assert_equal(winheight(2), winheight(4))
412
413  1wincmd w
414  split
415  4wincmd w
416  resize + 5
417  " left column has three windows, equalized heights.
418  " right column has two windows, top one a bit higher
419  let height_1 = winheight(1)
420  let height_2 = winheight(2)
421  let height_4 = winheight(4)
422  let height_5 = winheight(5)
423  3wincmd w
424  " closing window in left column equalizes heights in left column but not in
425  " the right column
426  close
427  call assert_notequal(height_1, winheight(1))
428  call assert_notequal(height_2, winheight(2))
429  call assert_equal(height_4, winheight(3))
430  call assert_equal(height_5, winheight(4))
431
432  only
433  set equalalways&
434endfunc
435
436func Test_win_screenpos()
437  CheckFeature quickfix
438
439  call assert_equal(1, winnr('$'))
440  split
441  vsplit
442  10wincmd _
443  30wincmd |
444  call assert_equal([1, 1], win_screenpos(1))
445  call assert_equal([1, 32], win_screenpos(2))
446  call assert_equal([12, 1], win_screenpos(3))
447  call assert_equal([0, 0], win_screenpos(4))
448  call assert_fails('let l = win_screenpos([])', 'E745:')
449  only
450endfunc
451
452func Test_window_jump_tag()
453  CheckFeature quickfix
454
455  help
456  /iccf
457  call assert_match('^|iccf|',  getline('.'))
458  call assert_equal(2, winnr('$'))
459  2wincmd }
460  call assert_equal(3, winnr('$'))
461  call assert_match('^|iccf|',  getline('.'))
462  wincmd k
463  call assert_match('\*iccf\*',  getline('.'))
464  call assert_equal(2, winheight(0))
465
466  wincmd z
467  set previewheight=4
468  help
469  /bugs
470  wincmd }
471  wincmd k
472  call assert_match('\*bugs\*',  getline('.'))
473  call assert_equal(4, winheight(0))
474  set previewheight&
475
476  %bw!
477endfunc
478
479func Test_window_newtab()
480  e Xa
481
482  call assert_equal(1, tabpagenr('$'))
483  call assert_equal("\nAlready only one window", execute('wincmd T'))
484
485  split Xb
486  split Xc
487
488  wincmd T
489  call assert_equal(2, tabpagenr('$'))
490  call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
491  call assert_equal(['Xc'      ], map(2->tabpagebuflist(), 'bufname(v:val)'))
492  call assert_equal(['Xc'      ], map(tabpagebuflist(), 'bufname(v:val)'))
493
494  %bw!
495endfunc
496
497func Test_next_split_all()
498  " This was causing an illegal memory access.
499  n x
500  norm axxx
501  split
502  split
503  s/x
504  s/x
505  all
506  bwipe!
507endfunc
508
509" Tests for adjusting window and contents
510func GetScreenStr(row)
511   let str = ""
512   for c in range(1,3)
513       let str .= nr2char(screenchar(a:row, c))
514   endfor
515   return str
516endfunc
517
518func Test_window_contents()
519  enew! | only | new
520  call setline(1, range(1,256))
521
522  exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
523  redraw
524  let s3 = GetScreenStr(1)
525  wincmd p
526  call assert_equal(1, line("w0"))
527  call assert_equal('1  ', s3)
528
529  exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
530  redraw
531  let s3 = GetScreenStr(1)
532  wincmd p
533  call assert_equal(50, line("w0"))
534  call assert_equal('50 ', s3)
535
536  exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
537  redraw
538  let s3 = GetScreenStr(1)
539  wincmd p
540  call assert_equal(59, line("w0"))
541  call assert_equal('59 ', s3)
542
543  %d
544  call setline(1, ['one', 'two', 'three'])
545  call assert_equal(1, line('w0'))
546  call assert_equal(3, line('w$'))
547
548  bwipeout!
549  call test_garbagecollect_now()
550endfunc
551
552func Test_window_colon_command()
553  " This was reading invalid memory.
554  exe "norm! v\<C-W>:\<C-U>echo v:version"
555endfunc
556
557func Test_access_freed_mem()
558  call assert_equal(&columns, winwidth(0))
559  " This was accessing freed memory
560  au * 0 vs xxx
561  arg 0
562  argadd
563  call assert_fails("all", "E249:")
564  au!
565  bwipe xxx
566  call assert_equal(&columns, winwidth(0))
567endfunc
568
569func Test_insert_cleared_on_switch_to_term()
570  CheckFeature terminal
571
572  set showmode
573  terminal
574  wincmd p
575
576  call feedkeys("i\<C-O>", 'ntx')
577  redraw
578
579  " The "-- (insert) --" indicator should be visible.
580  let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
581  let str = trim(join(chars, ''))
582  call assert_equal('-- (insert) --', str)
583
584  call feedkeys("\<C-W>p", 'ntx')
585  redraw
586
587  " The "-- (insert) --" indicator should have been cleared.
588  let chars = map(range(1, &columns), 'nr2char(screenchar(&lines, v:val))')
589  let str = trim(join(chars, ''))
590  call assert_equal('', str)
591
592  set showmode&
593  %bw!
594endfunc
595
596func Test_visual_cleared_after_window_split()
597  new | only!
598  let smd_save = &showmode
599  set showmode
600  let ls_save = &laststatus
601  set laststatus=1
602  call setline(1, ['a', 'b', 'c', 'd', ''])
603  norm! G
604  exe "norm! kkvk"
605  redraw
606  exe "norm! \<C-W>v"
607  redraw
608  " check if '-- VISUAL --' disappeared from command line
609  let columns = range(1, &columns)
610  let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
611  let cmdline = join(cmdlinechars, '')
612  let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
613  let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
614  call assert_equal('', mode_shown)
615  let &showmode = smd_save
616  let &laststatus = ls_save
617  bwipe!
618endfunc
619
620func Test_winrestcmd()
621  2split
622  3vsplit
623  let a = winrestcmd()
624  call assert_equal(2, winheight(0))
625  call assert_equal(3, winwidth(0))
626  wincmd =
627  call assert_notequal(2, winheight(0))
628  call assert_notequal(3, winwidth(0))
629  exe a
630  call assert_equal(2, winheight(0))
631  call assert_equal(3, winwidth(0))
632  only
633endfunc
634
635func Fun_RenewFile()
636  " Need to wait a bit for the timestamp to be older.
637  let old_ftime = getftime("tmp.txt")
638  while getftime("tmp.txt") == old_ftime
639    sleep 100m
640    silent execute '!echo "1" > tmp.txt'
641  endwhile
642  sp
643  wincmd p
644  edit! tmp.txt
645endfunc
646
647func Test_window_prevwin()
648  " Can we make this work on MS-Windows?
649  if !has('unix')
650    return
651  endif
652
653  set hidden autoread
654  call writefile(['2'], 'tmp.txt')
655  new tmp.txt
656  q
657  call Fun_RenewFile()
658  call assert_equal(2, winnr())
659  wincmd p
660  call assert_equal(1, winnr())
661  wincmd p
662  q
663  call Fun_RenewFile()
664  call assert_equal(2, winnr())
665  wincmd p
666  call assert_equal(1, winnr())
667  wincmd p
668  " reset
669  q
670  call delete('tmp.txt')
671  set hidden&vim autoread&vim
672  delfunc Fun_RenewFile
673endfunc
674
675func Test_relative_cursor_position_in_one_line_window()
676  new
677  only
678  call setline(1, range(1, 10000))
679  normal 50%
680  let lnum = getcurpos()[1]
681  split
682  split
683  " make third window take as many lines as possible, other windows will
684  " become one line
685  3wincmd w
686  for i in range(1, &lines - 6)
687    wincmd +
688    redraw!
689  endfor
690
691  " first and second window should show cursor line
692  let wininfo = getwininfo()
693  call assert_equal(lnum, wininfo[0].topline)
694  call assert_equal(lnum, wininfo[1].topline)
695
696  only!
697  bwipe!
698  call assert_fails('call winrestview(test_null_dict())', 'E474:')
699endfunc
700
701func Test_relative_cursor_position_after_move_and_resize()
702  let so_save = &so
703  set so=0
704  enew
705  call setline(1, range(1, 10000))
706  normal 50%
707  split
708  1wincmd w
709  " Move cursor to first line in window
710  normal H
711  redraw!
712  " Reduce window height to two lines
713  let height = winheight(0)
714  while winheight(0) > 2
715    wincmd -
716    redraw!
717  endwhile
718  " move cursor to second/last line in window
719  normal j
720  " restore previous height
721  while winheight(0) < height
722    wincmd +
723    redraw!
724  endwhile
725  " make window two lines again
726  while winheight(0) > 2
727    wincmd -
728    redraw!
729  endwhile
730
731  " cursor should be at bottom line
732  let info = getwininfo(win_getid())[0]
733  call assert_equal(info.topline + 1, getcurpos()[1])
734
735  only!
736  bwipe!
737  let &so = so_save
738endfunc
739
740func Test_relative_cursor_position_after_resize()
741  let so_save = &so
742  set so=0
743  enew
744  call setline(1, range(1, 10000))
745  normal 50%
746  split
747  1wincmd w
748  let winid1 = win_getid()
749  let info = getwininfo(winid1)[0]
750  " Move cursor to second line in window
751  exe "normal " . (info.topline + 1) . "G"
752  redraw!
753  let lnum = getcurpos()[1]
754
755  " Make the window only two lines high, cursor should end up in top line
756  2wincmd w
757  exe (info.height - 2) . "wincmd +"
758  redraw!
759  let info = getwininfo(winid1)[0]
760  call assert_equal(lnum, info.topline)
761
762  only!
763  bwipe!
764  let &so = so_save
765endfunc
766
767func Test_relative_cursor_second_line_after_resize()
768  let so_save = &so
769  set so=0
770  enew
771  call setline(1, range(1, 10000))
772  normal 50%
773  split
774  1wincmd w
775  let winid1 = win_getid()
776  let info = getwininfo(winid1)[0]
777
778  " Make the window only two lines high
779  2wincmd _
780
781  " Move cursor to second line in window
782  normal H
783  normal j
784
785  " Make window size bigger, then back to 2 lines
786  for i in range(1, 10)
787    wincmd +
788    redraw!
789  endfor
790  for i in range(1, 10)
791    wincmd -
792    redraw!
793  endfor
794
795  " cursor should end up in bottom line
796  let info = getwininfo(winid1)[0]
797  call assert_equal(info.topline + 1, getcurpos()[1])
798
799  only!
800  bwipe!
801  let &so = so_save
802endfunc
803
804func Test_split_noscroll()
805  let so_save = &so
806  enew
807  call setline(1, range(1, 8))
808  normal 100%
809  split
810
811  1wincmd w
812  let winid1 = win_getid()
813  let info1 = getwininfo(winid1)[0]
814
815  2wincmd w
816  let winid2 = win_getid()
817  let info2 = getwininfo(winid2)[0]
818
819  call assert_equal(1, info1.topline)
820  call assert_equal(1, info2.topline)
821
822  " window that fits all lines by itself, but not when split: closing other
823  " window should restore fraction.
824  only!
825  call setline(1, range(1, &lines - 10))
826  exe &lines / 4
827  let winid1 = win_getid()
828  let info1 = getwininfo(winid1)[0]
829  call assert_equal(1, info1.topline)
830  new
831  redraw
832  close
833  let info1 = getwininfo(winid1)[0]
834  call assert_equal(1, info1.topline)
835
836  bwipe!
837  let &so = so_save
838endfunc
839
840" Tests for the winnr() function
841func Test_winnr()
842  only | tabonly
843  call assert_equal(1, winnr('j'))
844  call assert_equal(1, winnr('k'))
845  call assert_equal(1, winnr('h'))
846  call assert_equal(1, winnr('l'))
847
848  " create a set of horizontally and vertically split windows
849  leftabove new | wincmd p
850  leftabove new | wincmd p
851  rightbelow new | wincmd p
852  rightbelow new | wincmd p
853  leftabove vnew | wincmd p
854  leftabove vnew | wincmd p
855  rightbelow vnew | wincmd p
856  rightbelow vnew | wincmd p
857
858  call assert_equal(8, winnr('j'))
859  call assert_equal(2, winnr('k'))
860  call assert_equal(4, winnr('h'))
861  call assert_equal(6, winnr('l'))
862  call assert_equal(9, winnr('2j'))
863  call assert_equal(1, winnr('2k'))
864  call assert_equal(3, winnr('2h'))
865  call assert_equal(7, winnr('2l'))
866
867  " Error cases
868  call assert_fails("echo winnr('0.2k')", 'E15:')
869  call assert_equal(2, winnr('-2k'))
870  call assert_fails("echo winnr('-2xj')", 'E15:')
871  call assert_fails("echo winnr('j2j')", 'E15:')
872  call assert_fails("echo winnr('ll')", 'E15:')
873  call assert_fails("echo winnr('5')", 'E15:')
874  call assert_equal(4, winnr('0h'))
875  call assert_fails("let w = winnr([])", 'E730:')
876  call assert_equal('unknown', win_gettype(-1))
877  call assert_equal(-1, winheight(-1))
878  call assert_equal(-1, winwidth(-1))
879
880  tabnew
881  call assert_equal(8, tabpagewinnr(1, 'j'))
882  call assert_equal(2, 1->tabpagewinnr('k'))
883  call assert_equal(4, tabpagewinnr(1, 'h'))
884  call assert_equal(6, tabpagewinnr(1, 'l'))
885
886  only | tabonly
887endfunc
888
889func Test_winrestview()
890  split runtest.vim
891  normal 50%
892  let view = winsaveview()
893  close
894  split runtest.vim
895  eval view->winrestview()
896  call assert_equal(view, winsaveview())
897
898  bwipe!
899  call assert_fails('call winrestview(test_null_dict())', 'E474:')
900endfunc
901
902func Test_win_splitmove()
903  CheckFeature quickfix
904
905  edit a
906  leftabove split b
907  leftabove vsplit c
908  leftabove split d
909  call assert_equal(0, win_splitmove(winnr(), winnr('l')))
910  call assert_equal(bufname(winbufnr(1)), 'c')
911  call assert_equal(bufname(winbufnr(2)), 'd')
912  call assert_equal(bufname(winbufnr(3)), 'b')
913  call assert_equal(bufname(winbufnr(4)), 'a')
914  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
915  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'vertical': 1}))
916  call assert_equal(bufname(winbufnr(1)), 'c')
917  call assert_equal(bufname(winbufnr(2)), 'b')
918  call assert_equal(bufname(winbufnr(3)), 'd')
919  call assert_equal(bufname(winbufnr(4)), 'a')
920  call assert_equal(0, win_splitmove(winnr(), winnr('k'), {'vertical': 1}))
921  call assert_equal(bufname(winbufnr(1)), 'd')
922  call assert_equal(bufname(winbufnr(2)), 'c')
923  call assert_equal(bufname(winbufnr(3)), 'b')
924  call assert_equal(bufname(winbufnr(4)), 'a')
925  call assert_equal(0, win_splitmove(winnr(), winnr('j'), {'rightbelow': v:true}))
926  call assert_equal(bufname(winbufnr(1)), 'c')
927  call assert_equal(bufname(winbufnr(2)), 'b')
928  call assert_equal(bufname(winbufnr(3)), 'a')
929  call assert_equal(bufname(winbufnr(4)), 'd')
930  call assert_fails('call win_splitmove(winnr(), winnr("k"), test_null_dict())', 'E474:')
931  only | bd
932
933  call assert_fails('call win_splitmove(winnr(), 123)', 'E957:')
934  call assert_fails('call win_splitmove(123, winnr())', 'E957:')
935  call assert_fails('call win_splitmove(winnr(), winnr())', 'E957:')
936
937  tabnew
938  call assert_fails('call win_splitmove(1, win_getid(1, 1))', 'E957:')
939  tabclose
940endfunc
941
942" Test for the :only command
943func Test_window_only()
944  new
945  set modified
946  new
947  call assert_fails('only', 'E445:')
948  only!
949  " Test for :only with a count
950  let wid = win_getid()
951  new
952  new
953  3only
954  call assert_equal(1, winnr('$'))
955  call assert_equal(wid, win_getid())
956  call assert_fails('close', 'E444:')
957  call assert_fails('%close', 'E16:')
958endfunc
959
960" Test for errors with :wincmd
961func Test_wincmd_errors()
962  call assert_fails('wincmd g', 'E474:')
963  call assert_fails('wincmd ab', 'E474:')
964endfunc
965
966" Test for errors with :winpos
967func Test_winpos_errors()
968  if !has("gui_running") && !has('win32')
969    call assert_fails('winpos', 'E188:')
970  endif
971  call assert_fails('winpos 10', 'E466:')
972endfunc
973
974" Test for +cmd in a :split command
975func Test_split_cmd()
976  split +set\ readonly
977  call assert_equal(1, &readonly)
978  call assert_equal(2, winnr('$'))
979  close
980endfunc
981
982" Create maximum number of horizontally or vertically split windows and then
983" run commands that create a new horizontally/vertically split window
984func Run_noroom_for_newwindow_test(dir_arg)
985  let dir = (a:dir_arg == 'v') ? 'vert ' : ''
986
987  " Open as many windows as possible
988  while v:true
989    try
990      exe dir . 'new'
991    catch /E36:/
992      break
993    endtry
994  endwhile
995
996  call writefile(['first', 'second', 'third'], 'Xfile1')
997  call writefile([], 'Xfile2')
998  call writefile([], 'Xfile3')
999
1000  " Argument list related commands
1001  args Xfile1 Xfile2 Xfile3
1002  next
1003  for cmd in ['sargument 2', 'snext', 'sprevious', 'sNext', 'srewind',
1004			\ 'sfirst', 'slast']
1005    call assert_fails(dir .. cmd, 'E36:')
1006  endfor
1007  %argdelete
1008
1009  " Buffer related commands
1010  set modified
1011  hide enew
1012  for cmd in ['sbuffer Xfile1', 'sbnext', 'sbprevious', 'sbNext', 'sbrewind',
1013		\ 'sbfirst', 'sblast', 'sball', 'sbmodified', 'sunhide']
1014    call assert_fails(dir .. cmd, 'E36:')
1015  endfor
1016
1017  " Window related commands
1018  for cmd in ['split', 'split Xfile2', 'new', 'new Xfile3', 'sview Xfile1',
1019		\ 'sfind runtest.vim']
1020    call assert_fails(dir .. cmd, 'E36:')
1021  endfor
1022
1023  " Help
1024  call assert_fails(dir .. 'help', 'E36:')
1025  call assert_fails(dir .. 'helpgrep window', 'E36:')
1026
1027  " Command-line window
1028  if a:dir_arg == 'h'
1029    " Cmd-line window is always a horizontally split window
1030    call assert_beeps('call feedkeys("q:\<CR>", "xt")')
1031  endif
1032
1033  " Quickfix and location list window
1034  if has('quickfix')
1035    cexpr ''
1036    call assert_fails(dir .. 'copen', 'E36:')
1037    lexpr ''
1038    call assert_fails(dir .. 'lopen', 'E36:')
1039
1040    " Preview window
1041    call assert_fails(dir .. 'pedit Xfile2', 'E36:')
1042    call setline(1, 'abc')
1043    call assert_fails(dir .. 'psearch abc', 'E36:')
1044  endif
1045
1046  " Window commands (CTRL-W ^ and CTRL-W f)
1047  if a:dir_arg == 'h'
1048    call assert_fails('call feedkeys("\<C-W>^", "xt")', 'E36:')
1049    call setline(1, 'Xfile1')
1050    call assert_fails('call feedkeys("gg\<C-W>f", "xt")', 'E36:')
1051  endif
1052  enew!
1053
1054  " Tag commands (:stag, :stselect and :stjump)
1055  call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
1056        \ "second\tXfile1\t2",
1057        \ "third\tXfile1\t3",],
1058        \ 'Xtags')
1059  set tags=Xtags
1060  call assert_fails(dir .. 'stag second', 'E36:')
1061  call assert_fails('call feedkeys(":" .. dir .. "stselect second\n1\n", "xt")', 'E36:')
1062  call assert_fails(dir .. 'stjump second', 'E36:')
1063  call assert_fails(dir .. 'ptag second', 'E36:')
1064  set tags&
1065  call delete('Xtags')
1066
1067  " :isplit and :dsplit
1068  call setline(1, ['#define FOO 1', 'FOO'])
1069  normal 2G
1070  call assert_fails(dir .. 'isplit FOO', 'E36:')
1071  call assert_fails(dir .. 'dsplit FOO', 'E36:')
1072
1073  " terminal
1074  if has('terminal')
1075    call assert_fails(dir .. 'terminal', 'E36:')
1076  endif
1077
1078  %bwipe!
1079  call delete('Xfile1')
1080  call delete('Xfile2')
1081  call delete('Xfile3')
1082  only
1083endfunc
1084
1085func Test_split_cmds_with_no_room()
1086  call Run_noroom_for_newwindow_test('h')
1087  call Run_noroom_for_newwindow_test('v')
1088endfunc
1089
1090" Test for various wincmd failures
1091func Test_wincmd_fails()
1092  only!
1093  call assert_beeps("normal \<C-W>w")
1094  call assert_beeps("normal \<C-W>p")
1095  call assert_beeps("normal \<C-W>gk")
1096  call assert_beeps("normal \<C-W>r")
1097  call assert_beeps("normal \<C-W>K")
1098  call assert_beeps("normal \<C-W>H")
1099  call assert_beeps("normal \<C-W>2gt")
1100endfunc
1101
1102" vim: shiftwidth=2 sts=2 expandtab
1103