xref: /vim-8.2.3635/src/testdir/test_tabpage.vim (revision bc93cebb)
1" Tests for tabpage
2
3source screendump.vim
4source check.vim
5
6function Test_tabpage()
7  CheckFeature quickfix
8
9  bw!
10  " Simple test for opening and closing a tab page
11  tabnew
12  call assert_equal(2, tabpagenr())
13  quit
14
15  " Open three tab pages and use ":tabdo"
16  0tabnew
17  1tabnew
18  $tabnew
19  %del
20  tabdo call append(line('$'), tabpagenr())
21  tabclose! 2
22  tabrewind
23  let line1 = getline('$')
24  undo
25  q
26  tablast
27  let line2 = getline('$')
28  q!
29  call append(line('$'), line1)
30  call append(line('$'), line2)
31  unlet line1 line2
32  call assert_equal(['', '3', '1', '4'], getline(1, '$'))
33  "
34  " Test for settabvar() and gettabvar() functions. Open a new tab page and
35  " set 3 variables to a number, string and a list. Verify that the variables
36  " are correctly set.
37  tabnew
38  tabfirst
39  call settabvar(2, 'val_num', 100)
40  eval 'SetTabVar test'->settabvar(2, 'val_str')
41  call settabvar(2, 'val_list', ['red', 'blue', 'green'])
42  "
43  call assert_true(gettabvar(2, 'val_num') == 100 && gettabvar(2, 'val_str') == 'SetTabVar test' && gettabvar(2, 'val_list') == ['red', 'blue', 'green'])
44
45  tabnext 2
46  call assert_true(t:val_num == 100 && t:val_str == 'SetTabVar test'  && t:val_list == ['red', 'blue', 'green'])
47  tabclose
48
49  " Test for ":tab drop exist-file" to keep current window.
50  sp test1
51  tab drop test1
52  call assert_true(tabpagenr('$') == 1 && winnr('$') == 2 && winnr() == 1)
53  close
54  "
55  "
56  " Test for ":tab drop new-file" to keep current window of tabpage 1.
57  split
58  tab drop newfile
59  call assert_true(tabpagenr('$') == 2 && tabpagewinnr(1, '$') == 2 && tabpagewinnr(1) == 1)
60  tabclose
61  q
62  "
63  "
64  " Test for ":tab drop multi-opened-file" to keep current tabpage and window.
65  new test1
66  tabnew
67  new test1
68  tab drop test1
69  call assert_true(tabpagenr() == 2 && tabpagewinnr(2, '$') == 2 && tabpagewinnr(2) == 1)
70  tabclose
71  q
72  "
73  "
74  " Test for ":tab drop vertical-split-window" to jump test1 buffer
75  tabedit test1
76  vnew
77  tabfirst
78  tab drop test1
79  call assert_equal([2, 2, 2, 2], [tabpagenr('$'), tabpagenr(), tabpagewinnr(2, '$'), tabpagewinnr(2)])
80  1tabonly
81  "
82  "
83  for i in range(9) | tabnew | endfor
84  normal! 1gt
85  call assert_equal(1, tabpagenr())
86  tabmove 5
87  call assert_equal(5, tabpagenr())
88  .tabmove
89  call assert_equal(5, tabpagenr())
90  tabmove -
91  call assert_equal(4, tabpagenr())
92  tabmove +
93  call assert_equal(5, tabpagenr())
94  tabmove -2
95  call assert_equal(3, tabpagenr())
96  tabmove +4
97  call assert_equal(7, tabpagenr())
98  tabmove
99  call assert_equal(10, tabpagenr())
100  0tabmove
101  call assert_equal(1, tabpagenr())
102  $tabmove
103  call assert_equal(10, tabpagenr())
104  tabmove 0
105  call assert_equal(1, tabpagenr())
106  tabmove $
107  call assert_equal(10, tabpagenr())
108  3tabmove
109  call assert_equal(4, tabpagenr())
110  7tabmove 5
111  call assert_equal(5, tabpagenr())
112  -tabmove
113  call assert_equal(4, tabpagenr())
114  +tabmove
115  call assert_equal(5, tabpagenr())
116  -2tabmove
117  call assert_equal(3, tabpagenr())
118  +3tabmove
119  call assert_equal(6, tabpagenr())
120
121  " The following are a no-op
122  norm! 2gt
123  call assert_equal(2, tabpagenr())
124  tabmove 2
125  call assert_equal(2, tabpagenr())
126  2tabmove
127  call assert_equal(2, tabpagenr())
128  tabmove 1
129  call assert_equal(2, tabpagenr())
130  1tabmove
131  call assert_equal(2, tabpagenr())
132
133  call assert_fails("99tabmove", 'E16:')
134  call assert_fails("+99tabmove", 'E16:')
135  call assert_fails("-99tabmove", 'E16:')
136  call assert_fails("tabmove foo", 'E474:')
137  call assert_fails("tabmove 99", 'E474:')
138  call assert_fails("tabmove +99", 'E474:')
139  call assert_fails("tabmove -99", 'E474:')
140  call assert_fails("tabmove -3+", 'E474:')
141  call assert_fails("tabmove $3", 'E474:')
142  call assert_fails("%tabonly", 'E16:')
143  1tabonly!
144  tabnew
145  call assert_fails("-2tabmove", 'E474:')
146  tabonly!
147endfunc
148
149" Test autocommands
150function Test_tabpage_with_autocmd()
151  command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
152  augroup TestTabpageGroup
153    au!
154    autocmd TabEnter * call add(s:li, 'TabEnter')
155    autocmd WinEnter * call add(s:li, 'WinEnter')
156    autocmd BufEnter * call add(s:li, 'BufEnter')
157    autocmd TabLeave * call add(s:li, 'TabLeave')
158    autocmd WinLeave * call add(s:li, 'WinLeave')
159    autocmd BufLeave * call add(s:li, 'BufLeave')
160  augroup END
161
162  let s:li = []
163  let t:a='a'
164  C tab split
165  call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
166  let s:li = []
167  let t:a='b'
168  C tabnew
169  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
170  let t:a='c'
171  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
172  call assert_equal(['a', 'b', 'c'], s:li)
173
174  let s:li = []
175  C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
176  call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
177  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
178  call assert_equal(['2', '4', '6'], s:li)
179
180  let s:li = []
181  let w:a='a'
182  C vsplit
183  call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
184  let s:li = []
185  let w:a='a'
186  let tabn=tabpagenr()
187  let winr=range(1, winnr('$'))
188  C tabnext 1
189  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
190  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
191  call assert_equal(['a', 'a'], s:li)
192  let s:li = []
193  C call map(copy(winr), '(v:val*2)->settabwinvar(' .. tabn .. ', v:val, ''a'')')
194  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
195  call assert_equal(['2', '4'], s:li)
196
197  augroup TabDestructive
198    autocmd TabEnter * :C tabnext 2 | C tabclose 3
199  augroup END
200  let s:li = []
201  C tabnext 3
202  call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
203  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
204
205  autocmd! TabDestructive TabEnter
206  let s:li = []
207  C tabnew
208  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
209  let s:li = []
210  C tabnext 1
211  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
212
213  autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
214  let s:li = []
215  call assert_equal(3, tabpagenr('$'))
216  C tabnext 2
217  call assert_equal(2, tabpagenr('$'))
218  call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
219  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
220
221  delcommand C
222  autocmd! TabDestructive
223  augroup! TabDestructive
224  autocmd! TestTabpageGroup
225  augroup! TestTabpageGroup
226  1tabonly!
227endfunction
228
229" Test autocommands on tab drop
230function Test_tabpage_with_autocmd_tab_drop()
231  augroup TestTabpageGroup
232    au!
233    autocmd TabEnter * call add(s:li, 'TabEnter')
234    autocmd WinEnter * call add(s:li, 'WinEnter')
235    autocmd BufEnter * call add(s:li, 'BufEnter')
236    autocmd TabLeave * call add(s:li, 'TabLeave')
237    autocmd WinLeave * call add(s:li, 'WinLeave')
238    autocmd BufLeave * call add(s:li, 'BufLeave')
239  augroup END
240
241  let s:li = []
242  tab drop test1
243  call assert_equal(['BufLeave', 'BufEnter'], s:li)
244
245  let s:li = []
246  tab drop test2 test3
247  call assert_equal([
248        \ 'TabLeave', 'TabEnter', 'TabLeave', 'TabEnter',
249        \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter',
250        \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
251
252  autocmd! TestTabpageGroup
253  augroup! TestTabpageGroup
254  1tabonly!
255endfunction
256
257function Test_tabpage_with_tab_modifier()
258  CheckFeature quickfix
259
260  for n in range(4)
261    tabedit
262  endfor
263
264  function s:check_tab(pre_nr, cmd, post_nr)
265    exec 'tabnext ' . a:pre_nr
266    exec a:cmd
267    call assert_equal(a:post_nr, tabpagenr())
268    call assert_equal('help', &buftype)
269    helpclose
270  endfunc
271
272  call s:check_tab(1, 'tab help', 2)
273  call s:check_tab(1, '3tab help', 4)
274  call s:check_tab(1, '.tab help', 2)
275  call s:check_tab(1, '.+1tab help', 3)
276  call s:check_tab(1, '0tab help', 1)
277  call s:check_tab(2, '+tab help', 4)
278  call s:check_tab(2, '+2tab help', 5)
279  call s:check_tab(4, '-tab help', 4)
280  call s:check_tab(4, '-2tab help', 3)
281  call s:check_tab(3, '$tab help', 6)
282  call assert_fails('99tab help', 'E16:')
283  call assert_fails('+99tab help', 'E16:')
284  call assert_fails('-99tab help', 'E16:')
285
286  delfunction s:check_tab
287  1tabonly!
288endfunction
289
290function Check_tab_count(pre_nr, cmd, post_nr)
291  exec 'tabnext' a:pre_nr
292  normal! G
293  exec a:cmd
294  call assert_equal(a:post_nr, tabpagenr(), a:cmd)
295endfunc
296
297" Test for [count] of tabnext
298function Test_tabpage_with_tabnext()
299  for n in range(4)
300    tabedit
301    call setline(1, ['', '', '3'])
302  endfor
303
304  call Check_tab_count(1, 'tabnext', 2)
305  call Check_tab_count(1, '3tabnext', 3)
306  call Check_tab_count(1, '.tabnext', 1)
307  call Check_tab_count(1, '.+1tabnext', 2)
308  call Check_tab_count(2, '+tabnext', 3)
309  call Check_tab_count(2, '+2tabnext', 4)
310  call Check_tab_count(4, '-tabnext', 3)
311  call Check_tab_count(4, '-2tabnext', 2)
312  call Check_tab_count(3, '$tabnext', 5)
313  call assert_fails('0tabnext', 'E16:')
314  call assert_fails('99tabnext', 'E16:')
315  call assert_fails('+99tabnext', 'E16:')
316  call assert_fails('-99tabnext', 'E16:')
317  call Check_tab_count(1, 'tabnext 3', 3)
318  call Check_tab_count(2, 'tabnext +', 3)
319  call Check_tab_count(2, 'tabnext +2', 4)
320  call Check_tab_count(4, 'tabnext -', 3)
321  call Check_tab_count(4, 'tabnext -2', 2)
322  call Check_tab_count(3, 'tabnext $', 5)
323  call assert_fails('tabnext 0', 'E474:')
324  call assert_fails('tabnext .', 'E474:')
325  call assert_fails('tabnext -+', 'E474:')
326  call assert_fails('tabnext +2-', 'E474:')
327  call assert_fails('tabnext $3', 'E474:')
328  call assert_fails('tabnext 99', 'E474:')
329  call assert_fails('tabnext +99', 'E474:')
330  call assert_fails('tabnext -99', 'E474:')
331
332  1tabonly!
333endfunction
334
335" Test for [count] of tabprevious
336function Test_tabpage_with_tabprevious()
337  for n in range(5)
338    tabedit
339    call setline(1, ['', '', '3'])
340  endfor
341
342  for cmd in ['tabNext', 'tabprevious']
343    call Check_tab_count(6, cmd, 5)
344    call Check_tab_count(6, '3' . cmd, 3)
345    call Check_tab_count(6, '8' . cmd, 4)
346    call Check_tab_count(6, cmd . ' 3', 3)
347    call Check_tab_count(6, cmd . ' 8', 4)
348    for n in range(2)
349      for c in ['0', '.+3', '+', '+2' , '-', '-2' , '$', '+99', '-99']
350        if n == 0 " pre count
351          let entire_cmd = c . cmd
352          let err_code = 'E16:'
353        else
354          let entire_cmd = cmd . ' ' . c
355          let err_code = 'E474:'
356        endif
357        call assert_fails(entire_cmd, err_code)
358      endfor
359    endfor
360  endfor
361
362  1tabonly!
363endfunction
364
365function s:reconstruct_tabpage_for_test(nr)
366  let n = (a:nr > 2) ? a:nr - 2 : 1
367  1tabonly!
368  0tabedit n0
369  for n in range(1, n)
370    exec '$tabedit n' . n
371    if n == 1
372      call setline(1, ['', '', '3'])
373    endif
374  endfor
375endfunc
376
377func Test_tabpage_ctrl_pgup_pgdown()
378  enew!
379  tabnew tab1
380  tabnew tab2
381
382  call assert_equal(3, tabpagenr())
383  exe "norm! \<C-PageUp>"
384  call assert_equal(2, tabpagenr())
385  exe "norm! \<C-PageDown>"
386  call assert_equal(3, tabpagenr())
387
388  " Check wrapping at last or first page.
389  exe "norm! \<C-PageDown>"
390  call assert_equal(1, tabpagenr())
391  exe "norm! \<C-PageUp>"
392  call assert_equal(3, tabpagenr())
393
394 " With a count, <C-PageUp> and <C-PageDown> are not symmetrical somehow:
395 " - {count}<C-PageUp> goes {count} pages downward (relative count)
396 " - {count}<C-PageDown> goes to page number {count} (absolute count)
397  exe "norm! 2\<C-PageUp>"
398  call assert_equal(1, tabpagenr())
399  exe "norm! 2\<C-PageDown>"
400  call assert_equal(2, tabpagenr())
401
402  1tabonly!
403endfunc
404
405" Test for [count] of tabclose
406function Test_tabpage_with_tabclose()
407
408  " pre count
409  call s:reconstruct_tabpage_for_test(6)
410  call Check_tab_count(3, 'tabclose!', 3)
411  call Check_tab_count(1, '3tabclose', 1)
412  call Check_tab_count(4, '4tabclose', 3)
413  call Check_tab_count(3, '1tabclose', 2)
414  call Check_tab_count(2, 'tabclose', 1)
415  call assert_equal(1, tabpagenr('$'))
416  call assert_equal('', bufname(''))
417
418  call s:reconstruct_tabpage_for_test(6)
419  call Check_tab_count(2, '$tabclose', 2)
420  call Check_tab_count(4, '.tabclose', 4)
421  call Check_tab_count(3, '.+tabclose', 3)
422  call Check_tab_count(3, '.-2tabclose', 2)
423  call Check_tab_count(1, '.+1tabclose!', 1)
424  call assert_equal(1, tabpagenr('$'))
425  call assert_equal('', bufname(''))
426
427  " post count
428  call s:reconstruct_tabpage_for_test(6)
429  call Check_tab_count(3, 'tabclose!', 3)
430  call Check_tab_count(1, 'tabclose 3', 1)
431  call Check_tab_count(4, 'tabclose 4', 3)
432  call Check_tab_count(3, 'tabclose 1', 2)
433  call Check_tab_count(2, 'tabclose', 1)
434  call assert_equal(1, tabpagenr('$'))
435  call assert_equal('', bufname(''))
436
437  call s:reconstruct_tabpage_for_test(6)
438  call Check_tab_count(2, 'tabclose $', 2)
439  call Check_tab_count(4, 'tabclose', 4)
440  call Check_tab_count(3, 'tabclose +', 3)
441  call Check_tab_count(3, 'tabclose -2', 2)
442  call Check_tab_count(1, 'tabclose! +1', 1)
443  call assert_equal(1, tabpagenr('$'))
444  call assert_equal('', bufname(''))
445
446  call s:reconstruct_tabpage_for_test(6)
447  for n in range(2)
448    for c in ['0', '$3', '99', '+99', '-99']
449      if n == 0 " pre count
450        let entire_cmd = c . 'tabclose'
451        let err_code = 'E16:'
452      else
453        let entire_cmd = 'tabclose ' . c
454        let err_code = 'E474:'
455      endif
456      call assert_fails(entire_cmd, err_code)
457      call assert_equal(6, tabpagenr('$'))
458    endfor
459  endfor
460
461  call assert_fails('3tabclose', 'E37:')
462  call assert_fails('tabclose 3', 'E37:')
463  call assert_fails('tabclose -+', 'E474:')
464  call assert_fails('tabclose +2-', 'E474:')
465  call assert_equal(6, tabpagenr('$'))
466
467  1tabonly!
468endfunction
469
470" Test for [count] of tabonly
471function Test_tabpage_with_tabonly()
472
473  " Test for the normal behavior (pre count only)
474  let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
475  for c in tc
476    call s:reconstruct_tabpage_for_test(6)
477    let entire_cmd = c[1] . 'tabonly' . c[2]
478    call Check_tab_count(c[0], entire_cmd, 1)
479    call assert_equal(1, tabpagenr('$'))
480  endfor
481
482  " Test for the normal behavior
483  let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
484        \    [2, '', '!'],
485        \    [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
486        \  ]
487  for n in range(2)
488    for c in tc2
489      call s:reconstruct_tabpage_for_test(6)
490      if n == 0 " pre count
491        let entire_cmd = c[1] . 'tabonly' . c[2]
492      else
493        let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
494      endif
495      call Check_tab_count(c[0], entire_cmd, 1)
496      call assert_equal(1, tabpagenr('$'))
497    endfor
498  endfor
499
500  " Test for the error behavior
501  for n in range(2)
502    for c in ['0', '$3', '99', '+99', '-99']
503      call s:reconstruct_tabpage_for_test(6)
504      if n == 0 " pre count
505        let entire_cmd = c . 'tabonly'
506        let err_code = 'E16:'
507      else
508        let entire_cmd = 'tabonly ' . c
509        let err_code = 'E474:'
510      endif
511      call assert_fails(entire_cmd, err_code)
512      call assert_equal(6, tabpagenr('$'))
513    endfor
514  endfor
515
516  " Test for the error behavior (post count only)
517  for c in tc
518    call s:reconstruct_tabpage_for_test(6)
519    let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
520    let err_code = 'E474:'
521    call assert_fails(entire_cmd, err_code)
522    call assert_equal(6, tabpagenr('$'))
523  endfor
524
525  call assert_fails('tabonly -+', 'E474:')
526  call assert_fails('tabonly +2-', 'E474:')
527  call assert_equal(6, tabpagenr('$'))
528
529  1tabonly!
530  new
531  only!
532endfunction
533
534func Test_tabnext_on_buf_unload1()
535  " This once caused a crash
536  new
537  tabedit
538  tabfirst
539  au BufUnload <buffer> tabnext
540  q
541
542  while tabpagenr('$') > 1
543    bwipe!
544  endwhile
545endfunc
546
547func Test_tabnext_on_buf_unload2()
548  " This once caused a crash
549  tabedit
550  autocmd BufUnload <buffer> tabnext
551  file x
552  edit y
553
554  while tabpagenr('$') > 1
555    bwipe!
556  endwhile
557endfunc
558
559func Test_close_on_quitpre()
560  " This once caused a crash
561  edit Xtest
562  new
563  only
564  set bufhidden=delete
565  au QuitPre <buffer> close
566  tabnew tab1
567  tabnew tab2
568  1tabn
569  q!
570  call assert_equal(1, tabpagenr())
571  call assert_equal(2, tabpagenr('$'))
572  " clean up
573  while tabpagenr('$') > 1
574    bwipe!
575  endwhile
576  buf Xtest
577endfunc
578
579func Test_tabs()
580  enew!
581  tabnew tab1
582  norm ixxx
583  let a=split(execute(':tabs'), "\n")
584  call assert_equal(['Tab page 1',
585      \              '    [No Name]',
586      \              'Tab page 2',
587      \              '> + tab1'], a)
588
589  1tabonly!
590  bw!
591endfunc
592
593func Test_tabpage_cmdheight()
594  if !CanRunVimInTerminal()
595    throw 'Skipped: cannot make screendumps'
596  endif
597  call writefile([
598        \ 'set laststatus=2',
599        \ 'set cmdheight=2',
600        \ 'tabnew',
601        \ 'set cmdheight=3',
602        \ 'tabnext',
603        \ 'redraw!',
604        \ 'echo "hello\nthere"',
605        \ 'tabnext',
606        \ 'redraw',
607	\ ], 'XTest_tabpage_cmdheight')
608  " Check that cursor line is concealed
609  let buf = RunVimInTerminal('-S XTest_tabpage_cmdheight', {'statusoff': 3})
610  call VerifyScreenDump(buf, 'Test_tabpage_cmdheight', {})
611
612  call StopVimInTerminal(buf)
613  call delete('XTest_tabpage_cmdheight')
614endfunc
615
616" Test for closing the tab page from a command window
617func Test_tabpage_close_cmdwin()
618  tabnew
619  call feedkeys("q/:tabclose\<CR>\<Esc>", 'xt')
620  call assert_equal(2, tabpagenr('$'))
621  call feedkeys("q/:tabonly\<CR>\<Esc>", 'xt')
622  call assert_equal(2, tabpagenr('$'))
623  tabonly
624endfunc
625
626" vim: shiftwidth=2 sts=2 expandtab
627