blob: 53c251907ee71cec0be5591e0f6bf4ff910c5675 [file] [log] [blame]
Bram Moolenaar991dea32016-05-24 11:31:32 +02001" 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()
Bram Moolenaar72cf47a2018-05-10 18:23:29 +020020 let efmt = 'Expected 0 but got %d (in ls=%d, %s window)'
Bram Moolenaar991dea32016-05-24 11:31:32 +020021 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
Bram Moolenaar5d2ca042016-06-26 17:11:21 +020037function 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
Bram Moolenaar4520d442017-03-19 16:09:46 +010070func 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
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100106" Test the ":wincmd ^" and "<C-W>^" commands.
Bram Moolenaar4520d442017-03-19 16:09:46 +0100107func Test_window_split_edit_alternate()
Bram Moolenaar4520d442017-03-19 16:09:46 +0100108
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100109 " Test for failure when the alternate buffer/file no longer exists.
110 edit Xfoo | %bw
111 call assert_fails(':wincmd ^', 'E23')
112
113 " Test for the expected behavior when we have two named buffers.
114 edit Xfoo | edit Xbar
Bram Moolenaar4520d442017-03-19 16:09:46 +0100115 wincmd ^
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100116 call assert_equal('Xfoo', bufname(winbufnr(1)))
117 call assert_equal('Xbar', bufname(winbufnr(2)))
118 only
Bram Moolenaar4520d442017-03-19 16:09:46 +0100119
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100120 " Test for the expected behavior when the alternate buffer is not named.
121 enew | let l:nr1 = bufnr('%')
122 edit Xfoo | let l:nr2 = bufnr('%')
123 wincmd ^
124 call assert_equal(l:nr1, winbufnr(1))
125 call assert_equal(l:nr2, winbufnr(2))
126 only
127
Bram Moolenaard42333d2018-11-10 20:28:19 +0100128 " FIXME: this currently fails on AppVeyor, but passes locally
129 if !has('win32')
130 " Test the Normal mode command.
131 call feedkeys("\<C-W>\<C-^>", 'tx')
132 call assert_equal(l:nr2, winbufnr(1))
133 call assert_equal(l:nr1, winbufnr(2))
134 endif
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100135
136 %bw!
137endfunc
138
139" Test the ":[count]wincmd ^" and "[count]<C-W>^" commands.
140func Test_window_split_edit_bufnr()
141
142 %bwipeout
143 let l:nr = bufnr('%') + 1
144 call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92')
145 call assert_fails(':' . l:nr . 'wincmd ^', 'E16')
146 call assert_fails(':0wincmd ^', 'E16')
147
148 edit Xfoo | edit Xbar | edit Xbaz
149 let l:foo_nr = bufnr('Xfoo')
150 let l:bar_nr = bufnr('Xbar')
151 let l:baz_nr = bufnr('Xbaz')
152
153 call feedkeys(l:foo_nr . "\<C-W>\<C-^>", 'tx')
154 call assert_equal('Xfoo', bufname(winbufnr(1)))
155 call assert_equal('Xbaz', bufname(winbufnr(2)))
156 only
157
158 call feedkeys(l:bar_nr . "\<C-W>\<C-^>", 'tx')
159 call assert_equal('Xbar', bufname(winbufnr(1)))
160 call assert_equal('Xfoo', bufname(winbufnr(2)))
161 only
162
163 execute l:baz_nr . 'wincmd ^'
164 call assert_equal('Xbaz', bufname(winbufnr(1)))
165 call assert_equal('Xbar', bufname(winbufnr(2)))
166
167 %bw!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100168endfunc
169
170func Test_window_preview()
171 " Open a preview window
172 pedit Xa
173 call assert_equal(2, winnr('$'))
174 call assert_equal(0, &previewwindow)
175
176 " Go to the preview window
177 wincmd P
178 call assert_equal(1, &previewwindow)
179
180 " Close preview window
181 wincmd z
182 call assert_equal(1, winnr('$'))
183 call assert_equal(0, &previewwindow)
184
185 call assert_fails('wincmd P', 'E441:')
186endfunc
187
188func Test_window_exchange()
189 e Xa
190
191 " Nothing happens with window exchange when there is 1 window
192 wincmd x
193 call assert_equal(1, winnr('$'))
194
195 split Xb
196 split Xc
197
198 call assert_equal('Xc', bufname(winbufnr(1)))
199 call assert_equal('Xb', bufname(winbufnr(2)))
200 call assert_equal('Xa', bufname(winbufnr(3)))
201
202 " Exchange current window 1 with window 3
203 3wincmd x
204 call assert_equal('Xa', bufname(winbufnr(1)))
205 call assert_equal('Xb', bufname(winbufnr(2)))
206 call assert_equal('Xc', bufname(winbufnr(3)))
207
208 " Exchange window with next when at the top window
209 wincmd x
210 call assert_equal('Xb', bufname(winbufnr(1)))
211 call assert_equal('Xa', bufname(winbufnr(2)))
212 call assert_equal('Xc', bufname(winbufnr(3)))
213
214 " Exchange window with next when at the middle window
215 wincmd j
216 wincmd x
217 call assert_equal('Xb', bufname(winbufnr(1)))
218 call assert_equal('Xc', bufname(winbufnr(2)))
219 call assert_equal('Xa', bufname(winbufnr(3)))
220
221 " Exchange window with next when at the bottom window.
222 " When there is no next window, it exchanges with the previous window.
223 wincmd j
224 wincmd x
225 call assert_equal('Xb', bufname(winbufnr(1)))
226 call assert_equal('Xa', bufname(winbufnr(2)))
227 call assert_equal('Xc', bufname(winbufnr(3)))
228
229 bw Xa Xb Xc
230endfunc
231
232func Test_window_rotate()
233 e Xa
234 split Xb
235 split Xc
236 call assert_equal('Xc', bufname(winbufnr(1)))
237 call assert_equal('Xb', bufname(winbufnr(2)))
238 call assert_equal('Xa', bufname(winbufnr(3)))
239
240 " Rotate downwards
241 wincmd r
242 call assert_equal('Xa', bufname(winbufnr(1)))
243 call assert_equal('Xc', bufname(winbufnr(2)))
244 call assert_equal('Xb', bufname(winbufnr(3)))
245
246 2wincmd r
247 call assert_equal('Xc', bufname(winbufnr(1)))
248 call assert_equal('Xb', bufname(winbufnr(2)))
249 call assert_equal('Xa', bufname(winbufnr(3)))
250
251 " Rotate upwards
252 wincmd R
253 call assert_equal('Xb', bufname(winbufnr(1)))
254 call assert_equal('Xa', bufname(winbufnr(2)))
255 call assert_equal('Xc', bufname(winbufnr(3)))
256
257 2wincmd R
258 call assert_equal('Xc', bufname(winbufnr(1)))
259 call assert_equal('Xb', bufname(winbufnr(2)))
260 call assert_equal('Xa', bufname(winbufnr(3)))
261
262 bot vsplit
263 call assert_fails('wincmd R', 'E443:')
264
265 bw Xa Xb Xc
266endfunc
267
268func Test_window_height()
269 e Xa
270 split Xb
271
272 let [wh1, wh2] = [winheight(1), winheight(2)]
273 " Active window (1) should have the same height or 1 more
274 " than the other window.
275 call assert_inrange(wh2, wh2 + 1, wh1)
276
277 wincmd -
278 call assert_equal(wh1 - 1, winheight(1))
279 call assert_equal(wh2 + 1, winheight(2))
280
281 wincmd +
282 call assert_equal(wh1, winheight(1))
283 call assert_equal(wh2, winheight(2))
284
285 2wincmd _
286 call assert_equal(2, winheight(1))
287 call assert_equal(wh1 + wh2 - 2, winheight(2))
288
289 wincmd =
290 call assert_equal(wh1, winheight(1))
291 call assert_equal(wh2, winheight(2))
292
293 2wincmd _
294 set winfixheight
295 split Xc
296 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
297 call assert_equal(2, winheight(2))
298 call assert_inrange(wh3, wh3 + 1, wh1)
299 3wincmd +
300 call assert_equal(2, winheight(2))
301 call assert_equal(wh1 + 3, winheight(1))
302 call assert_equal(wh3 - 3, winheight(3))
303 wincmd =
304 call assert_equal(2, winheight(2))
305 call assert_equal(wh1, winheight(1))
306 call assert_equal(wh3, winheight(3))
307
308 wincmd j
309 set winfixheight&
310
311 wincmd =
312 let [wh1, wh2, wh3] = [winheight(1), winheight(2), winheight(3)]
313 " Current window (2) should have the same height or 1 more
314 " than the other windows.
315 call assert_inrange(wh1, wh1 + 1, wh2)
316 call assert_inrange(wh3, wh3 + 1, wh2)
317
318 bw Xa Xb Xc
319endfunc
320
321func Test_window_width()
322 e Xa
323 vsplit Xb
324
325 let [ww1, ww2] = [winwidth(1), winwidth(2)]
326 " Active window (1) should have the same width or 1 more
327 " than the other window.
328 call assert_inrange(ww2, ww2 + 1, ww1)
329
330 wincmd <
331 call assert_equal(ww1 - 1, winwidth(1))
332 call assert_equal(ww2 + 1, winwidth(2))
333
334 wincmd >
335 call assert_equal(ww1, winwidth(1))
336 call assert_equal(ww2, winwidth(2))
337
338 2wincmd |
339 call assert_equal(2, winwidth(1))
340 call assert_equal(ww1 + ww2 - 2, winwidth(2))
341
342 wincmd =
343 call assert_equal(ww1, winwidth(1))
344 call assert_equal(ww2, winwidth(2))
345
346 2wincmd |
347 set winfixwidth
348 vsplit Xc
349 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
Bram Moolenaar38e34832017-03-19 20:22:36 +0100350 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100351 call assert_inrange(ww3, ww3 + 1, ww1)
352 3wincmd >
Bram Moolenaar38e34832017-03-19 20:22:36 +0100353 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100354 call assert_equal(ww1 + 3, winwidth(1))
355 call assert_equal(ww3 - 3, winwidth(3))
356 wincmd =
Bram Moolenaar38e34832017-03-19 20:22:36 +0100357 call assert_equal(2, winwidth(2))
Bram Moolenaar4520d442017-03-19 16:09:46 +0100358 call assert_equal(ww1, winwidth(1))
359 call assert_equal(ww3, winwidth(3))
360
361 wincmd l
362 set winfixwidth&
363
364 wincmd =
365 let [ww1, ww2, ww3] = [winwidth(1), winwidth(2), winwidth(3)]
366 " Current window (2) should have the same width or 1 more
367 " than the other windows.
368 call assert_inrange(ww1, ww1 + 1, ww2)
369 call assert_inrange(ww3, ww3 + 1, ww2)
370
371 bw Xa Xb Xc
372endfunc
373
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200374func Test_equalalways_on_close()
375 set equalalways
376 vsplit
377 windo split
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100378 split
Bram Moolenaar8eeeba82017-06-25 22:45:39 +0200379 wincmd J
380 " now we have a frame top-left with two windows, a frame top-right with two
381 " windows and a frame at the bottom, full-width.
382 let height_1 = winheight(1)
383 let height_2 = winheight(2)
384 let height_3 = winheight(3)
385 let height_4 = winheight(4)
386 " closing the bottom window causes all windows to be resized.
387 close
388 call assert_notequal(height_1, winheight(1))
389 call assert_notequal(height_2, winheight(2))
390 call assert_notequal(height_3, winheight(3))
391 call assert_notequal(height_4, winheight(4))
392 call assert_equal(winheight(1), winheight(3))
393 call assert_equal(winheight(2), winheight(4))
394
395 1wincmd w
396 split
397 4wincmd w
398 resize + 5
399 " left column has three windows, equalized heights.
400 " right column has two windows, top one a bit higher
401 let height_1 = winheight(1)
402 let height_2 = winheight(2)
403 let height_4 = winheight(4)
404 let height_5 = winheight(5)
405 3wincmd w
406 " closing window in left column equalizes heights in left column but not in
407 " the right column
408 close
409 call assert_notequal(height_1, winheight(1))
410 call assert_notequal(height_2, winheight(2))
411 call assert_equal(height_4, winheight(3))
412 call assert_equal(height_5, winheight(4))
413
414 only
415 set equalalways&
416endfunc
417
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100418func Test_win_screenpos()
419 call assert_equal(1, winnr('$'))
420 split
421 vsplit
422 10wincmd _
423 30wincmd |
424 call assert_equal([1, 1], win_screenpos(1))
425 call assert_equal([1, 32], win_screenpos(2))
426 call assert_equal([12, 1], win_screenpos(3))
427 call assert_equal([0, 0], win_screenpos(4))
428 only
429endfunc
430
Bram Moolenaar4520d442017-03-19 16:09:46 +0100431func Test_window_jump_tag()
432 help
433 /iccf
434 call assert_match('^|iccf|', getline('.'))
435 call assert_equal(2, winnr('$'))
436 2wincmd }
437 call assert_equal(3, winnr('$'))
438 call assert_match('^|iccf|', getline('.'))
439 wincmd k
440 call assert_match('\*iccf\*', getline('.'))
441 call assert_equal(2, winheight(0))
442
443 wincmd z
444 set previewheight=4
445 help
446 /bugs
447 wincmd }
448 wincmd k
449 call assert_match('\*bugs\*', getline('.'))
450 call assert_equal(4, winheight(0))
451 set previewheight&
452
453 %bw!
454endfunc
455
456func Test_window_newtab()
457 e Xa
458
459 call assert_equal(1, tabpagenr('$'))
460 call assert_equal("\nAlready only one window", execute('wincmd T'))
461
462 split Xb
463 split Xc
464
465 wincmd T
466 call assert_equal(2, tabpagenr('$'))
467 call assert_equal(['Xb', 'Xa'], map(tabpagebuflist(1), 'bufname(v:val)'))
468 call assert_equal(['Xc' ], map(tabpagebuflist(2), 'bufname(v:val)'))
469
470 %bw!
471endfunc
472
Bram Moolenaarf79225e2017-03-18 23:11:04 +0100473func Test_next_split_all()
474 " This was causing an illegal memory access.
475 n x
476 norm axxx
477 split
478 split
479 s/x
480 s/x
481 all
482 bwipe!
483endfunc
484
Bram Moolenaar75373f32017-08-07 22:02:30 +0200485" Tests for adjusting window and contents
486func GetScreenStr(row)
487 let str = ""
488 for c in range(1,3)
489 let str .= nr2char(screenchar(a:row, c))
490 endfor
491 return str
492endfunc
493
494func Test_window_contents()
495 enew! | only | new
496 call setline(1, range(1,256))
497
498 exe "norm! \<C-W>t\<C-W>=1Gzt\<C-W>w\<C-W>+"
499 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200500 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200501 wincmd p
502 call assert_equal(1, line("w0"))
503 call assert_equal('1 ', s3)
504
505 exe "norm! \<C-W>t\<C-W>=50Gzt\<C-W>w\<C-W>+"
506 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200507 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200508 wincmd p
509 call assert_equal(50, line("w0"))
510 call assert_equal('50 ', s3)
511
512 exe "norm! \<C-W>t\<C-W>=59Gzt\<C-W>w\<C-W>+"
513 redraw
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200514 let s3 = GetScreenStr(1)
Bram Moolenaar75373f32017-08-07 22:02:30 +0200515 wincmd p
516 call assert_equal(59, line("w0"))
517 call assert_equal('59 ', s3)
518
519 bwipeout!
520 call test_garbagecollect_now()
521endfunc
522
Bram Moolenaar2efb3232017-12-19 12:27:23 +0100523func Test_window_colon_command()
524 " This was reading invalid memory.
525 exe "norm! v\<C-W>:\<C-U>echo v:version"
526endfunc
527
Bram Moolenaar6f361c92018-01-31 19:06:50 +0100528func Test_access_freed_mem()
529 " This was accessing freed memory
530 au * 0 vs xxx
531 arg 0
532 argadd
533 all
534 all
535 au!
536 bwipe xxx
537endfunc
538
Bram Moolenaar5bab5552018-04-13 20:41:29 +0200539func Test_visual_cleared_after_window_split()
540 new | only!
541 let smd_save = &showmode
542 set showmode
543 let ls_save = &laststatus
544 set laststatus=1
545 call setline(1, ['a', 'b', 'c', 'd', ''])
546 norm! G
547 exe "norm! kkvk"
548 redraw
549 exe "norm! \<C-W>v"
550 redraw
551 " check if '-- VISUAL --' disappeared from command line
552 let columns = range(1, &columns)
553 let cmdlinechars = map(columns, 'nr2char(screenchar(&lines, v:val))')
554 let cmdline = join(cmdlinechars, '')
555 let cmdline_ltrim = substitute(cmdline, '^\s*', "", "")
556 let mode_shown = substitute(cmdline_ltrim, '\s*$', "", "")
557 call assert_equal('', mode_shown)
558 let &showmode = smd_save
559 let &laststatus = ls_save
560 bwipe!
561endfunc
562
Bram Moolenaar72cf47a2018-05-10 18:23:29 +0200563func Test_winrestcmd()
564 2split
565 3vsplit
566 let a = winrestcmd()
567 call assert_equal(2, winheight(0))
568 call assert_equal(3, winwidth(0))
569 wincmd =
570 call assert_notequal(2, winheight(0))
571 call assert_notequal(3, winwidth(0))
572 exe a
573 call assert_equal(2, winheight(0))
574 call assert_equal(3, winwidth(0))
575 only
576endfunc
577
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200578" vim: shiftwidth=2 sts=2 expandtab