xref: /vim-8.2.3635/src/testdir/test_tabpage.vim (revision d7df2798)
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  1tabonly!
143endfunc
144
145" Test autocommands
146function Test_tabpage_with_autocmd()
147  command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
148  augroup TestTabpageGroup
149    au!
150    autocmd TabEnter * call add(s:li, 'TabEnter')
151    autocmd WinEnter * call add(s:li, 'WinEnter')
152    autocmd BufEnter * call add(s:li, 'BufEnter')
153    autocmd TabLeave * call add(s:li, 'TabLeave')
154    autocmd WinLeave * call add(s:li, 'WinLeave')
155    autocmd BufLeave * call add(s:li, 'BufLeave')
156  augroup END
157
158  let s:li = []
159  let t:a='a'
160  C tab split
161  call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
162  let s:li = []
163  let t:a='b'
164  C tabnew
165  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
166  let t:a='c'
167  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
168  call assert_equal(['a', 'b', 'c'], s:li)
169
170  let s:li = []
171  C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
172  call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
173  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
174  call assert_equal(['2', '4', '6'], s:li)
175
176  let s:li = []
177  let w:a='a'
178  C vsplit
179  call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
180  let s:li = []
181  let w:a='a'
182  let tabn=tabpagenr()
183  let winr=range(1, winnr('$'))
184  C tabnext 1
185  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
186  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
187  call assert_equal(['a', 'a'], s:li)
188  let s:li = []
189  C call map(copy(winr), '(v:val*2)->settabwinvar(' .. tabn .. ', v:val, ''a'')')
190  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
191  call assert_equal(['2', '4'], s:li)
192
193  augroup TabDestructive
194    autocmd TabEnter * :C tabnext 2 | C tabclose 3
195  augroup END
196  let s:li = []
197  C tabnext 3
198  call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
199  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
200
201  autocmd! TabDestructive TabEnter
202  let s:li = []
203  C tabnew
204  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
205  let s:li = []
206  C tabnext 1
207  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
208
209  autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
210  let s:li = []
211  call assert_equal(3, tabpagenr('$'))
212  C tabnext 2
213  call assert_equal(2, tabpagenr('$'))
214  call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
215  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
216
217  delcommand C
218  autocmd! TabDestructive
219  augroup! TabDestructive
220  autocmd! TestTabpageGroup
221  augroup! TestTabpageGroup
222  1tabonly!
223endfunction
224
225function Test_tabpage_with_tab_modifier()
226  CheckFeature quickfix
227
228  for n in range(4)
229    tabedit
230  endfor
231
232  function s:check_tab(pre_nr, cmd, post_nr)
233    exec 'tabnext ' . a:pre_nr
234    exec a:cmd
235    call assert_equal(a:post_nr, tabpagenr())
236    call assert_equal('help', &buftype)
237    helpclose
238  endfunc
239
240  call s:check_tab(1, 'tab help', 2)
241  call s:check_tab(1, '3tab help', 4)
242  call s:check_tab(1, '.tab help', 2)
243  call s:check_tab(1, '.+1tab help', 3)
244  call s:check_tab(1, '0tab help', 1)
245  call s:check_tab(2, '+tab help', 4)
246  call s:check_tab(2, '+2tab help', 5)
247  call s:check_tab(4, '-tab help', 4)
248  call s:check_tab(4, '-2tab help', 3)
249  call s:check_tab(3, '$tab help', 6)
250  call assert_fails('99tab help', 'E16:')
251  call assert_fails('+99tab help', 'E16:')
252  call assert_fails('-99tab help', 'E16:')
253
254  delfunction s:check_tab
255  1tabonly!
256endfunction
257
258function Check_tab_count(pre_nr, cmd, post_nr)
259  exec 'tabnext' a:pre_nr
260  normal! G
261  exec a:cmd
262  call assert_equal(a:post_nr, tabpagenr(), a:cmd)
263endfunc
264
265" Test for [count] of tabnext
266function Test_tabpage_with_tabnext()
267  for n in range(4)
268    tabedit
269    call setline(1, ['', '', '3'])
270  endfor
271
272  call Check_tab_count(1, 'tabnext', 2)
273  call Check_tab_count(1, '3tabnext', 3)
274  call Check_tab_count(1, '.tabnext', 1)
275  call Check_tab_count(1, '.+1tabnext', 2)
276  call Check_tab_count(2, '+tabnext', 3)
277  call Check_tab_count(2, '+2tabnext', 4)
278  call Check_tab_count(4, '-tabnext', 3)
279  call Check_tab_count(4, '-2tabnext', 2)
280  call Check_tab_count(3, '$tabnext', 5)
281  call assert_fails('0tabnext', 'E16:')
282  call assert_fails('99tabnext', 'E16:')
283  call assert_fails('+99tabnext', 'E16:')
284  call assert_fails('-99tabnext', 'E16:')
285  call Check_tab_count(1, 'tabnext 3', 3)
286  call Check_tab_count(2, 'tabnext +', 3)
287  call Check_tab_count(2, 'tabnext +2', 4)
288  call Check_tab_count(4, 'tabnext -', 3)
289  call Check_tab_count(4, 'tabnext -2', 2)
290  call Check_tab_count(3, 'tabnext $', 5)
291  call assert_fails('tabnext 0', 'E474:')
292  call assert_fails('tabnext .', 'E474:')
293  call assert_fails('tabnext -+', 'E474:')
294  call assert_fails('tabnext +2-', 'E474:')
295  call assert_fails('tabnext $3', 'E474:')
296  call assert_fails('tabnext 99', 'E474:')
297  call assert_fails('tabnext +99', 'E474:')
298  call assert_fails('tabnext -99', 'E474:')
299
300  1tabonly!
301endfunction
302
303" Test for [count] of tabprevious
304function Test_tabpage_with_tabprevious()
305  for n in range(5)
306    tabedit
307    call setline(1, ['', '', '3'])
308  endfor
309
310  for cmd in ['tabNext', 'tabprevious']
311    call Check_tab_count(6, cmd, 5)
312    call Check_tab_count(6, '3' . cmd, 3)
313    call Check_tab_count(6, '8' . cmd, 4)
314    call Check_tab_count(6, cmd . ' 3', 3)
315    call Check_tab_count(6, cmd . ' 8', 4)
316    for n in range(2)
317      for c in ['0', '.+3', '+', '+2' , '-', '-2' , '$', '+99', '-99']
318        if n == 0 " pre count
319          let entire_cmd = c . cmd
320          let err_code = 'E16:'
321        else
322          let entire_cmd = cmd . ' ' . c
323          let err_code = 'E474:'
324        endif
325        call assert_fails(entire_cmd, err_code)
326      endfor
327    endfor
328  endfor
329
330  1tabonly!
331endfunction
332
333function s:reconstruct_tabpage_for_test(nr)
334  let n = (a:nr > 2) ? a:nr - 2 : 1
335  1tabonly!
336  0tabedit n0
337  for n in range(1, n)
338    exec '$tabedit n' . n
339    if n == 1
340      call setline(1, ['', '', '3'])
341    endif
342  endfor
343endfunc
344
345func Test_tabpage_ctrl_pgup_pgdown()
346  enew!
347  tabnew tab1
348  tabnew tab2
349
350  call assert_equal(3, tabpagenr())
351  exe "norm! \<C-PageUp>"
352  call assert_equal(2, tabpagenr())
353  exe "norm! \<C-PageDown>"
354  call assert_equal(3, tabpagenr())
355
356  " Check wrapping at last or first page.
357  exe "norm! \<C-PageDown>"
358  call assert_equal(1, tabpagenr())
359  exe "norm! \<C-PageUp>"
360  call assert_equal(3, tabpagenr())
361
362 " With a count, <C-PageUp> and <C-PageDown> are not symmetrical somehow:
363 " - {count}<C-PageUp> goes {count} pages downward (relative count)
364 " - {count}<C-PageDown> goes to page number {count} (absolute count)
365  exe "norm! 2\<C-PageUp>"
366  call assert_equal(1, tabpagenr())
367  exe "norm! 2\<C-PageDown>"
368  call assert_equal(2, tabpagenr())
369
370  1tabonly!
371endfunc
372
373" Test for [count] of tabclose
374function Test_tabpage_with_tabclose()
375
376  " pre count
377  call s:reconstruct_tabpage_for_test(6)
378  call Check_tab_count(3, 'tabclose!', 3)
379  call Check_tab_count(1, '3tabclose', 1)
380  call Check_tab_count(4, '4tabclose', 3)
381  call Check_tab_count(3, '1tabclose', 2)
382  call Check_tab_count(2, 'tabclose', 1)
383  call assert_equal(1, tabpagenr('$'))
384  call assert_equal('', bufname(''))
385
386  call s:reconstruct_tabpage_for_test(6)
387  call Check_tab_count(2, '$tabclose', 2)
388  call Check_tab_count(4, '.tabclose', 4)
389  call Check_tab_count(3, '.+tabclose', 3)
390  call Check_tab_count(3, '.-2tabclose', 2)
391  call Check_tab_count(1, '.+1tabclose!', 1)
392  call assert_equal(1, tabpagenr('$'))
393  call assert_equal('', bufname(''))
394
395  " post count
396  call s:reconstruct_tabpage_for_test(6)
397  call Check_tab_count(3, 'tabclose!', 3)
398  call Check_tab_count(1, 'tabclose 3', 1)
399  call Check_tab_count(4, 'tabclose 4', 3)
400  call Check_tab_count(3, 'tabclose 1', 2)
401  call Check_tab_count(2, 'tabclose', 1)
402  call assert_equal(1, tabpagenr('$'))
403  call assert_equal('', bufname(''))
404
405  call s:reconstruct_tabpage_for_test(6)
406  call Check_tab_count(2, 'tabclose $', 2)
407  call Check_tab_count(4, 'tabclose', 4)
408  call Check_tab_count(3, 'tabclose +', 3)
409  call Check_tab_count(3, 'tabclose -2', 2)
410  call Check_tab_count(1, 'tabclose! +1', 1)
411  call assert_equal(1, tabpagenr('$'))
412  call assert_equal('', bufname(''))
413
414  call s:reconstruct_tabpage_for_test(6)
415  for n in range(2)
416    for c in ['0', '$3', '99', '+99', '-99']
417      if n == 0 " pre count
418        let entire_cmd = c . 'tabclose'
419        let err_code = 'E16:'
420      else
421        let entire_cmd = 'tabclose ' . c
422        let err_code = 'E474:'
423      endif
424      call assert_fails(entire_cmd, err_code)
425      call assert_equal(6, tabpagenr('$'))
426    endfor
427  endfor
428
429  call assert_fails('3tabclose', 'E37:')
430  call assert_fails('tabclose 3', 'E37:')
431  call assert_fails('tabclose -+', 'E474:')
432  call assert_fails('tabclose +2-', 'E474:')
433  call assert_equal(6, tabpagenr('$'))
434
435  1tabonly!
436endfunction
437
438" Test for [count] of tabonly
439function Test_tabpage_with_tabonly()
440
441  " Test for the normal behavior (pre count only)
442  let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
443  for c in tc
444    call s:reconstruct_tabpage_for_test(6)
445    let entire_cmd = c[1] . 'tabonly' . c[2]
446    call Check_tab_count(c[0], entire_cmd, 1)
447    call assert_equal(1, tabpagenr('$'))
448  endfor
449
450  " Test for the normal behavior
451  let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
452        \    [2, '', '!'],
453        \    [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
454        \  ]
455  for n in range(2)
456    for c in tc2
457      call s:reconstruct_tabpage_for_test(6)
458      if n == 0 " pre count
459        let entire_cmd = c[1] . 'tabonly' . c[2]
460      else
461        let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
462      endif
463      call Check_tab_count(c[0], entire_cmd, 1)
464      call assert_equal(1, tabpagenr('$'))
465    endfor
466  endfor
467
468  " Test for the error behavior
469  for n in range(2)
470    for c in ['0', '$3', '99', '+99', '-99']
471      call s:reconstruct_tabpage_for_test(6)
472      if n == 0 " pre count
473        let entire_cmd = c . 'tabonly'
474        let err_code = 'E16:'
475      else
476        let entire_cmd = 'tabonly ' . c
477        let err_code = 'E474:'
478      endif
479      call assert_fails(entire_cmd, err_code)
480      call assert_equal(6, tabpagenr('$'))
481    endfor
482  endfor
483
484  " Test for the error behavior (post count only)
485  for c in tc
486    call s:reconstruct_tabpage_for_test(6)
487    let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
488    let err_code = 'E474:'
489    call assert_fails(entire_cmd, err_code)
490    call assert_equal(6, tabpagenr('$'))
491  endfor
492
493  call assert_fails('tabonly -+', 'E474:')
494  call assert_fails('tabonly +2-', 'E474:')
495  call assert_equal(6, tabpagenr('$'))
496
497  1tabonly!
498  new
499  only!
500endfunction
501
502func Test_tabnext_on_buf_unload1()
503  " This once caused a crash
504  new
505  tabedit
506  tabfirst
507  au BufUnload <buffer> tabnext
508  q
509
510  while tabpagenr('$') > 1
511    bwipe!
512  endwhile
513endfunc
514
515func Test_tabnext_on_buf_unload2()
516  " This once caused a crash
517  tabedit
518  autocmd BufUnload <buffer> tabnext
519  file x
520  edit y
521
522  while tabpagenr('$') > 1
523    bwipe!
524  endwhile
525endfunc
526
527func Test_close_on_quitpre()
528  " This once caused a crash
529  edit Xtest
530  new
531  only
532  set bufhidden=delete
533  au QuitPre <buffer> close
534  tabnew tab1
535  tabnew tab2
536  1tabn
537  q!
538  call assert_equal(1, tabpagenr())
539  call assert_equal(2, tabpagenr('$'))
540  " clean up
541  while tabpagenr('$') > 1
542    bwipe!
543  endwhile
544  buf Xtest
545endfunc
546
547func Test_tabs()
548  enew!
549  tabnew tab1
550  norm ixxx
551  let a=split(execute(':tabs'), "\n")
552  call assert_equal(['Tab page 1',
553      \              '    [No Name]',
554      \              'Tab page 2',
555      \              '> + tab1'], a)
556
557  1tabonly!
558  bw!
559endfunc
560
561func Test_tabpage_cmdheight()
562  if !CanRunVimInTerminal()
563    throw 'Skipped: cannot make screendumps'
564  endif
565  call writefile([
566        \ 'set laststatus=2',
567        \ 'set cmdheight=2',
568        \ 'tabnew',
569        \ 'set cmdheight=3',
570        \ 'tabnext',
571        \ 'redraw!',
572        \ 'echo "hello\nthere"',
573        \ 'tabnext',
574        \ 'redraw',
575	\ ], 'XTest_tabpage_cmdheight')
576  " Check that cursor line is concealed
577  let buf = RunVimInTerminal('-S XTest_tabpage_cmdheight', {'statusoff': 3})
578  call VerifyScreenDump(buf, 'Test_tabpage_cmdheight', {})
579
580  call StopVimInTerminal(buf)
581  call delete('XTest_tabpage_cmdheight')
582endfunc
583
584" vim: shiftwidth=2 sts=2 expandtab
585