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