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