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