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