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