robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqvim-patch:9.2.0923: tabpage: closing a tab page loses the alternate tab page (#41229)5d5b8e3e7d

master
29.1 KiB1098 linesraw
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  silent -tabmove
121  call assert_equal(5, tabpagenr())
122  silent -2 tabmove
123  call assert_equal(3, tabpagenr())
124  silent	-2	tabmove
125  call assert_equal(1, tabpagenr())
126
127  norm! 2gt
128  call assert_equal(2, tabpagenr())
129  " The following are a no-op
130  tabmove 2
131  call assert_equal(2, tabpagenr())
132  2tabmove
133  call assert_equal(2, tabpagenr())
134  tabmove 1
135  call assert_equal(2, tabpagenr())
136  1tabmove
137  call assert_equal(2, tabpagenr())
138
139  call assert_fails('let t = tabpagenr("@")', 'E15:')
140  call assert_equal(0, tabpagewinnr(-1))
141  call assert_fails("99tabmove", 'E16:')
142  call assert_fails("+99tabmove", 'E16:')
143  call assert_fails("-99tabmove", 'E16:')
144  call assert_fails("tabmove foo", 'E475:')
145  call assert_fails("tabmove 99", 'E475:')
146  call assert_fails("tabmove +99", 'E475:')
147  call assert_fails("tabmove -99", 'E475:')
148  call assert_fails("tabmove -3+", 'E475:')
149  call assert_fails("tabmove $3", 'E475:')
150  call assert_fails("%tabonly", 'E16:')
151  1tabonly!
152  tabmove 1
153  call assert_equal(1, tabpagenr())
154  tabnew
155  call assert_fails("-2tabmove", 'E16:')
156  tabonly!
157endfunc
158
159func Test_tabpage_drop()
160  edit f1
161  tab split f2
162  tab split f3
163  normal! gt
164  call assert_equal(1, tabpagenr())
165  tab drop f4
166  call assert_equal(1, tabpagenr('#'))
167
168  tab drop f3
169  call assert_equal(4, tabpagenr())
170  call assert_equal(2, tabpagenr('#'))
171  bwipe!
172  bwipe!
173  bwipe!
174  bwipe!
175  call assert_equal(1, tabpagenr('$'))
176
177  call assert_equal(1, winnr('$'))
178  call assert_equal('', bufname(''))
179  call writefile(['L1', 'L2'], 'Xdropfile', 'D')
180
181  " Test for ':tab drop single-file': reuse current buffer
182  let expected_nr = bufnr()
183  tab drop Xdropfile
184  call assert_equal(1, tabpagenr('$'))
185  call assert_equal(expected_nr, bufnr())
186  call assert_equal('L2', getline(2))
187  bwipe!
188
189  " Test for ':tab drop single-file': not reuse modified buffer
190  set modified
191  let expected_nr = bufnr() + 1
192  tab drop Xdropfile
193  call assert_equal(2, tabpagenr())
194  call assert_equal(2, tabpagenr('$'))
195  call assert_equal(expected_nr, bufnr())
196  call assert_equal('L2', getline(2))
197  bwipe!
198
199  " Test for ':tab drop single-file': multiple tabs already exist
200  tab split f2
201  tab split f3
202  let expected_nr = bufnr() + 1
203  tab drop Xdropfile
204  call assert_equal(4, tabpagenr())
205  call assert_equal(4, tabpagenr('$'))
206  call assert_equal(expected_nr, bufnr())
207  call assert_equal('L2', getline(2))
208  %bwipe!
209
210  " Test for ':tab drop multi-files': reuse current buffer
211  let expected_nr = bufnr()
212  tab drop Xdropfile f1 f2 f3
213  call assert_equal(1, tabpagenr())
214  call assert_equal(4, tabpagenr('$'))
215  call assert_equal(expected_nr, bufnr())
216  call assert_equal('L2', getline(2))
217  %bwipe!
218
219  " Test for ':tab drop multi-files': not reuse modified buffer
220  set modified
221  let expected_nr = bufnr() + 1
222  tab drop Xdropfile f1 f2 f3
223  call assert_equal(2, tabpagenr())
224  call assert_equal(5, tabpagenr('$'))
225  call assert_equal(expected_nr, bufnr())
226  call assert_equal('L2', getline(2))
227  %bwipe!
228
229  " Test for ':tab drop multi-files': multiple tabs already exist
230  tab split f2
231  tab split f3
232  let expected_nr = bufnr() + 1
233  tab drop a b c
234  call assert_equal(4, tabpagenr())
235  call assert_equal(6, tabpagenr('$'))
236  call assert_equal(expected_nr, bufnr())
237  let expected_nr = bufnr() + 3
238  tab drop Xdropfile f1 f2 f3
239  call assert_equal(5, tabpagenr())
240  call assert_equal(8, tabpagenr('$'))
241  call assert_equal(expected_nr, bufnr())
242  call assert_equal('L2', getline(2))
243  %bwipe!
244endfunc
245
246" Test autocommands
247function Test_tabpage_with_autocmd()
248  command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
249  augroup TestTabpageGroup
250    au!
251    autocmd TabEnter * call add(s:li, 'TabEnter')
252    autocmd WinEnter * call add(s:li, 'WinEnter')
253    autocmd BufEnter * call add(s:li, 'BufEnter')
254    autocmd TabLeave * call add(s:li, 'TabLeave')
255    autocmd WinLeave * call add(s:li, 'WinLeave')
256    autocmd BufLeave * call add(s:li, 'BufLeave')
257  augroup END
258
259  let s:li = []
260  let t:a='a'
261  C tab split
262  call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
263  let s:li = []
264  let t:a='b'
265  C tabnew
266  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
267  let t:a='c'
268  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
269  call assert_equal(['a', 'b', 'c'], s:li)
270
271  let s:li = []
272  C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
273  call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
274  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
275  call assert_equal(['2', '4', '6'], s:li)
276
277  let s:li = []
278  let w:a='a'
279  C vsplit
280  call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
281  let s:li = []
282  let w:a='a'
283  let tabn=tabpagenr()
284  let winr=range(1, winnr('$'))
285  C tabnext 1
286  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
287  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
288  call assert_equal(['a', 'a'], s:li)
289  let s:li = []
290  C call map(copy(winr), '(v:val*2)->settabwinvar(' .. tabn .. ', v:val, ''a'')')
291  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
292  call assert_equal(['2', '4'], s:li)
293
294  augroup TabDestructive
295    autocmd TabEnter * :C tabnext 2 | C tabclose 3
296  augroup END
297  let s:li = []
298  C tabnext 3
299  call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
300  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
301
302  autocmd! TabDestructive TabEnter
303  let s:li = []
304  C tabnew
305  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
306  let s:li = []
307  C tabnext 1
308  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
309
310  autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
311  let s:li = []
312  call assert_equal(3, tabpagenr('$'))
313  C tabnext 2
314  call assert_equal(2, tabpagenr('$'))
315  call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
316  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
317
318  delcommand C
319  autocmd! TabDestructive
320  augroup! TabDestructive
321  autocmd! TestTabpageGroup
322  augroup! TestTabpageGroup
323  1tabonly!
324endfunction
325
326" Test autocommands on tab drop
327function Test_tabpage_with_autocmd_tab_drop()
328  augroup TestTabpageGroup
329    au!
330    autocmd TabEnter * call add(s:li, 'TabEnter')
331    autocmd WinEnter * call add(s:li, 'WinEnter')
332    autocmd BufEnter * call add(s:li, 'BufEnter')
333    autocmd TabLeave * call add(s:li, 'TabLeave')
334    autocmd WinLeave * call add(s:li, 'WinLeave')
335    autocmd BufLeave * call add(s:li, 'BufLeave')
336  augroup END
337
338  let s:li = []
339  tab drop test1
340  call assert_equal(['BufEnter'], s:li)
341
342  let s:li = []
343  tab drop test2 test3
344  call assert_equal([
345        \ 'TabLeave', 'TabEnter', 'TabLeave', 'TabEnter',
346        \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter',
347        \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter', 'BufEnter'], s:li)
348
349  autocmd! TestTabpageGroup
350  augroup! TestTabpageGroup
351  1tabonly!
352endfunction
353
354function Test_tabpage_with_tab_modifier()
355  CheckFeature quickfix
356
357  for n in range(4)
358    tabedit
359  endfor
360
361  function s:check_tab(pre_nr, cmd, post_nr)
362    exec 'tabnext ' . a:pre_nr
363    exec a:cmd
364    call assert_equal(a:post_nr, tabpagenr())
365    call assert_equal('help', &buftype)
366    helpclose
367  endfunc
368
369  call s:check_tab(1, 'tab help', 2)
370  call s:check_tab(1, '3tab help', 4)
371  call s:check_tab(1, '.tab help', 2)
372  call s:check_tab(1, '.+1tab help', 3)
373  call s:check_tab(1, '0tab help', 1)
374  call s:check_tab(2, '+tab help', 4)
375  call s:check_tab(2, '+2tab help', 5)
376  call s:check_tab(4, '-tab help', 4)
377  call s:check_tab(4, '-2tab help', 3)
378  call s:check_tab(3, '$tab help', 6)
379  call assert_fails('99tab help', 'E16:')
380  call assert_fails('+99tab help', 'E16:')
381  call assert_fails('-99tab help', 'E16:')
382
383  delfunction s:check_tab
384  1tabonly!
385endfunction
386
387function Check_tab_count(pre_nr, cmd, post_nr)
388  exec 'tabnext' a:pre_nr
389  normal! G
390  exec a:cmd
391  call assert_equal(a:post_nr, tabpagenr(), a:cmd)
392endfunc
393
394" Test for [count] of tabnext
395function Test_tabpage_with_tabnext()
396  for n in range(4)
397    tabedit
398    call setline(1, ['', '', '3'])
399  endfor
400
401  call Check_tab_count(1, 'tabnext', 2)
402  call Check_tab_count(1, '3tabnext', 3)
403  call Check_tab_count(1, '.tabnext', 1)
404  call Check_tab_count(1, '.+1tabnext', 2)
405  call Check_tab_count(2, '+tabnext', 3)
406  call Check_tab_count(2, '+2tabnext', 4)
407  call Check_tab_count(4, '-tabnext', 3)
408  call Check_tab_count(4, '-2tabnext', 2)
409  call Check_tab_count(3, '$tabnext', 5)
410  call assert_fails('0tabnext', 'E16:')
411  call assert_fails('99tabnext', 'E16:')
412  call assert_fails('+99tabnext', 'E16:')
413  call assert_fails('-99tabnext', 'E16:')
414  call Check_tab_count(1, 'tabnext 3', 3)
415  call Check_tab_count(2, 'tabnext +', 3)
416  call Check_tab_count(2, 'tabnext +2', 4)
417  call Check_tab_count(4, 'tabnext -', 3)
418  call Check_tab_count(4, 'tabnext -2', 2)
419  call Check_tab_count(3, 'tabnext $', 5)
420  call assert_fails('tabnext 0', 'E475:')
421  call assert_fails('tabnext .', 'E475:')
422  call assert_fails('tabnext -+', 'E475:')
423  call assert_fails('tabnext +2-', 'E475:')
424  call assert_fails('tabnext $3', 'E475:')
425  call assert_fails('tabnext 99', 'E475:')
426  call assert_fails('tabnext +99', 'E475:')
427  call assert_fails('tabnext -99', 'E475:')
428
429  1tabonly!
430endfunction
431
432" Test for [count] of tabprevious
433function Test_tabpage_with_tabprevious()
434  for n in range(5)
435    tabedit
436    call setline(1, ['', '', '3'])
437  endfor
438
439  for cmd in ['tabNext', 'tabprevious']
440    call Check_tab_count(6, cmd, 5)
441    call Check_tab_count(6, '3' . cmd, 3)
442    call Check_tab_count(6, '8' . cmd, 4)
443    call Check_tab_count(6, cmd . ' 3', 3)
444    call Check_tab_count(6, cmd . ' 8', 4)
445    for n in range(2)
446      for c in ['0', '.+3', '+', '+2' , '-', '-2' , '$', '+99', '-99']
447        if n == 0 " pre count
448          let entire_cmd = c . cmd
449          let err_code = 'E16:'
450        else
451          let entire_cmd = cmd . ' ' . c
452          let err_code = 'E475:'
453        endif
454        call assert_fails(entire_cmd, err_code)
455      endfor
456    endfor
457  endfor
458
459  1tabonly!
460endfunction
461
462function s:reconstruct_tabpage_for_test(nr)
463  let n = (a:nr > 2) ? a:nr - 2 : 1
464  1tabonly!
465  0tabedit n0
466  for n in range(1, n)
467    exec '$tabedit n' . n
468    if n == 1
469      call setline(1, ['', '', '3'])
470    endif
471  endfor
472endfunc
473
474func Test_tabpage_ctrl_pgup_pgdown()
475  enew!
476  tabnew tab1
477  tabnew tab2
478
479  call assert_equal(3, tabpagenr())
480  exe "norm! \<C-PageUp>"
481  call assert_equal(2, tabpagenr())
482  exe "norm! \<C-PageDown>"
483  call assert_equal(3, tabpagenr())
484
485  " Check wrapping at last or first page.
486  exe "norm! \<C-PageDown>"
487  call assert_equal(1, tabpagenr())
488  exe "norm! \<C-PageUp>"
489  call assert_equal(3, tabpagenr())
490
491 " With a count, <C-PageUp> and <C-PageDown> are not symmetrical somehow:
492 " - {count}<C-PageUp> goes {count} pages downward (relative count)
493 " - {count}<C-PageDown> goes to page number {count} (absolute count)
494  exe "norm! 2\<C-PageUp>"
495  call assert_equal(1, tabpagenr())
496  exe "norm! 2\<C-PageDown>"
497  call assert_equal(2, tabpagenr())
498
499  1tabonly!
500endfunc
501
502" Test for [count] of tabclose
503function Test_tabpage_with_tabclose()
504
505  " pre count
506  call s:reconstruct_tabpage_for_test(6)
507  call Check_tab_count(3, 'tabclose!', 3)
508  call Check_tab_count(1, '3tabclose', 1)
509  call Check_tab_count(4, '4tabclose', 3)
510  call Check_tab_count(3, '1tabclose', 2)
511  call Check_tab_count(2, 'tabclose', 1)
512  call assert_equal(1, tabpagenr('$'))
513  call assert_equal('', bufname(''))
514
515  call s:reconstruct_tabpage_for_test(6)
516  call Check_tab_count(2, '$tabclose', 2)
517  call Check_tab_count(4, '.tabclose', 4)
518  call Check_tab_count(3, '.+tabclose', 3)
519  call Check_tab_count(3, '.-2tabclose', 2)
520  call Check_tab_count(1, '.+1tabclose!', 1)
521  call assert_equal(1, tabpagenr('$'))
522  call assert_equal('', bufname(''))
523
524  " post count
525  call s:reconstruct_tabpage_for_test(6)
526  call Check_tab_count(3, 'tabclose!', 3)
527  call Check_tab_count(1, 'tabclose 3', 1)
528  call Check_tab_count(4, 'tabclose 4', 3)
529  call Check_tab_count(3, 'tabclose 1', 2)
530  call Check_tab_count(2, 'tabclose', 1)
531  call assert_equal(1, tabpagenr('$'))
532  call assert_equal('', bufname(''))
533
534  call s:reconstruct_tabpage_for_test(6)
535  call Check_tab_count(2, 'tabclose $', 2)
536  call Check_tab_count(4, 'tabclose', 4)
537  call Check_tab_count(3, 'tabclose +', 3)
538  call Check_tab_count(3, 'tabclose -2', 2)
539  call Check_tab_count(1, 'tabclose! +1', 1)
540  call assert_equal(1, tabpagenr('$'))
541  call assert_equal('', bufname(''))
542
543  call s:reconstruct_tabpage_for_test(6)
544  for n in range(2)
545    for c in ['0', '$3', '99', '+99', '-99']
546      if n == 0 " pre count
547        let entire_cmd = c . 'tabclose'
548        let err_code = 'E16:'
549      else
550        let entire_cmd = 'tabclose ' . c
551        let err_code = 'E475:'
552      endif
553      call assert_fails(entire_cmd, err_code)
554      call assert_equal(6, tabpagenr('$'))
555    endfor
556  endfor
557
558  call assert_fails('3tabclose', 'E37:')
559  call assert_fails('tabclose 3', 'E37:')
560  call assert_fails('tabclose -+', 'E475:')
561  call assert_fails('tabclose +2-', 'E475:')
562  call assert_equal(6, tabpagenr('$'))
563
564  1tabonly!
565endfunction
566
567" Test for [count] of tabonly
568function Test_tabpage_with_tabonly()
569
570  " Test for the normal behavior (pre count only)
571  let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
572  for c in tc
573    call s:reconstruct_tabpage_for_test(6)
574    let entire_cmd = c[1] . 'tabonly' . c[2]
575    call Check_tab_count(c[0], entire_cmd, 1)
576    call assert_equal(1, tabpagenr('$'))
577  endfor
578
579  " Test for the normal behavior
580  let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
581        \    [2, '', '!'],
582        \    [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
583        \  ]
584  for n in range(2)
585    for c in tc2
586      call s:reconstruct_tabpage_for_test(6)
587      if n == 0 " pre count
588        let entire_cmd = c[1] . 'tabonly' . c[2]
589      else
590        let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
591      endif
592      call Check_tab_count(c[0], entire_cmd, 1)
593      call assert_equal(1, tabpagenr('$'))
594    endfor
595  endfor
596
597  " Test for the error behavior
598  for n in range(2)
599    for c in ['0', '$3', '99', '+99', '-99']
600      call s:reconstruct_tabpage_for_test(6)
601      if n == 0 " pre count
602        let entire_cmd = c . 'tabonly'
603        let err_code = 'E16:'
604      else
605        let entire_cmd = 'tabonly ' . c
606        let err_code = 'E475:'
607      endif
608      call assert_fails(entire_cmd, err_code)
609      call assert_equal(6, tabpagenr('$'))
610    endfor
611  endfor
612
613  " Test for the error behavior (post count only)
614  for c in tc
615    call s:reconstruct_tabpage_for_test(6)
616    let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
617    let err_code = 'E475:'
618    call assert_fails(entire_cmd, err_code)
619    call assert_equal(6, tabpagenr('$'))
620  endfor
621
622  call assert_fails('tabonly -+', 'E475:')
623  call assert_fails('tabonly +2-', 'E475:')
624  call assert_equal(6, tabpagenr('$'))
625
626  1tabonly!
627  new
628  only!
629endfunction
630
631func Test_tabnext_on_buf_unload1()
632  " This once caused a crash
633  new
634  tabedit
635  tabfirst
636  au BufUnload <buffer> tabnext
637  q
638
639  while tabpagenr('$') > 1
640    bwipe!
641  endwhile
642endfunc
643
644func Test_tabnext_on_buf_unload2()
645  " This once caused a crash
646  tabedit
647  autocmd BufUnload <buffer> tabnext
648  file x
649  edit y
650
651  while tabpagenr('$') > 1
652    bwipe!
653  endwhile
654endfunc
655
656func Test_close_on_quitpre()
657  " This once caused a crash
658  edit Xtest
659  new
660  only
661  set bufhidden=delete
662  au QuitPre <buffer> close
663  tabnew tab1
664  tabnew tab2
665  1tabn
666  q!
667  call assert_equal(1, tabpagenr())
668  call assert_equal(2, tabpagenr('$'))
669  " clean up
670  while tabpagenr('$') > 1
671    bwipe!
672  endwhile
673  buf Xtest
674endfunc
675
676func Test_tabs()
677  enew!
678  tabnew tab1
679  norm ixxx
680  let a=split(execute(':tabs'), "\n")
681  call assert_equal(['Tab page 1',
682      \              '#   [No Name]',
683      \              'Tab page 2',
684      \              '> + tab1'], a)
685
686  1tabonly!
687  bw!
688endfunc
689
690func Test_tabpage_cmdheight()
691  CheckScreendump
692  CheckRunVimInTerminal
693  call writefile([
694        \ 'set laststatus=2',
695        \ 'set cmdheight=2',
696        \ 'tabnew',
697        \ 'set cmdheight=3',
698        \ 'tabnext',
699        \ 'redraw!',
700        \ 'echo "hello\nthere"',
701        \ 'tabnext',
702        \ 'redraw',
703	\ ], 'XTest_tabpage_cmdheight')
704  " Check that cursor line is concealed
705  let buf = RunVimInTerminal('-S XTest_tabpage_cmdheight', {'statusoff': 3})
706  call VerifyScreenDump(buf, 'Test_tabpage_cmdheight', {})
707
708  call StopVimInTerminal(buf)
709  call delete('XTest_tabpage_cmdheight')
710endfunc
711
712" Test for closing the tab page from a command window
713func Test_tabpage_close_cmdwin()
714  throw 'Skipped: Nvim supports cmdwin freedom #40312'
715  tabnew
716  call feedkeys("q/:tabclose\<CR>\<Esc>", 'xt')
717  call assert_equal(2, tabpagenr('$'))
718  call feedkeys("q/:tabonly\<CR>\<Esc>", 'xt')
719  call assert_equal(2, tabpagenr('$'))
720  tabonly
721endfunc
722
723" Pressing <C-PageUp> in insert mode should go to the previous tab page
724" and <C-PageDown> should go to the next tab page
725func Test_tabpage_Ctrl_Pageup()
726  tabnew
727  call feedkeys("i\<C-PageUp>", 'xt')
728  call assert_equal(1, tabpagenr())
729  call feedkeys("i\<C-PageDown>", 'xt')
730  call assert_equal(2, tabpagenr())
731  %bw!
732endfunc
733
734" Return the terminal key code for selecting a tab page from the tabline. This
735" sequence contains the following codes: a CSI (0x9b), KS_TABLINE (0xf0),
736" KS_FILLER (0x58) and then the tab page number.
737func TabLineSelectPageCode(tabnr)
738  return "\x9b\xf0\x58" ..  nr2char(a:tabnr)
739endfunc
740
741" Return the terminal key code for opening a new tabpage from the tabpage
742" menu. This sequence consists of the following codes: a CSI (0x9b),
743" KS_TABMENU (0xef), KS_FILLER (0x58), the tab page number and
744" TABLINE_MENU_NEW (2).
745func TabMenuNewItemCode(tabnr)
746  return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(2)
747endfunc
748
749" Return the terminal key code for closing a tabpage from the tabpage menu.
750" This sequence consists of the following codes: a CSI (0x9b), KS_TABMENU
751" (0xef), KS_FILLER (0x58), the tab page number and TABLINE_MENU_CLOSE (1).
752func TabMenuCloseItemCode(tabnr)
753  return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(1)
754endfunc
755
756" Test for using the tabpage menu from the insert and normal modes
757func Test_tabline_tabmenu()
758  " only works in GUI
759  CheckGui
760
761  %bw!
762  tabnew
763  tabnew
764  call assert_equal(3, tabpagenr())
765
766  " go to tab page 2 in normal mode
767  call feedkeys(TabLineSelectPageCode(2), "Lx!")
768  call assert_equal(2, tabpagenr())
769
770  " close tab page 3 in normal mode
771  call feedkeys(TabMenuCloseItemCode(3), "Lx!")
772  call assert_equal(2, tabpagenr('$'))
773  call assert_equal(2, tabpagenr())
774
775  " open new tab page before tab page 1 in normal mode
776  call feedkeys(TabMenuNewItemCode(1), "Lx!")
777  call assert_equal(1, tabpagenr())
778  call assert_equal(3, tabpagenr('$'))
779
780  " go to tab page 2 in operator-pending mode (should beep)
781  call assert_beeps('call feedkeys("c" .. TabLineSelectPageCode(2), "Lx!")')
782  call assert_equal(2, tabpagenr())
783  call assert_equal(3, tabpagenr('$'))
784
785  " open new tab page before tab page 1 in operator-pending mode (should beep)
786  call assert_beeps('call feedkeys("c" .. TabMenuNewItemCode(1), "Lx!")')
787  call assert_equal(1, tabpagenr())
788  call assert_equal(4, tabpagenr('$'))
789
790  " open new tab page after tab page 3 in normal mode
791  call feedkeys(TabMenuNewItemCode(4), "Lx!")
792  call assert_equal(4, tabpagenr())
793  call assert_equal(5, tabpagenr('$'))
794
795  " go to tab page 2 in insert mode
796  call feedkeys("i" .. TabLineSelectPageCode(2) .. "\<C-C>", "Lx!")
797  call assert_equal(2, tabpagenr())
798
799  " close tab page 2 in insert mode
800  call feedkeys("i" .. TabMenuCloseItemCode(2) .. "\<C-C>", "Lx!")
801  call assert_equal(4, tabpagenr('$'))
802
803  " open new tab page before tab page 3 in insert mode
804  call feedkeys("i" .. TabMenuNewItemCode(3) .. "\<C-C>", "Lx!")
805  call assert_equal(3, tabpagenr())
806  call assert_equal(5, tabpagenr('$'))
807
808  " open new tab page after tab page 4 in insert mode
809  call feedkeys("i" .. TabMenuNewItemCode(5) .. "\<C-C>", "Lx!")
810  call assert_equal(5, tabpagenr())
811  call assert_equal(6, tabpagenr('$'))
812
813  %bw!
814endfunc
815
816" Test for changing the current tab page from an autocmd when closing a tab
817" page.
818func Test_tabpage_switchtab_on_close()
819  only
820  tabnew
821  tabnew
822  " Test for BufLeave
823  augroup T1
824    au!
825    au BufLeave * tabfirst
826  augroup END
827  tabclose
828  call assert_equal(1, tabpagenr())
829  augroup T1
830    au!
831  augroup END
832
833  " Test for WinLeave
834  $tabnew
835  augroup T1
836    au!
837    au WinLeave * tabfirst
838  augroup END
839  tabclose
840  call assert_equal(1, tabpagenr())
841  augroup T1
842    au!
843  augroup END
844
845  " Test for TabLeave
846  $tabnew
847  augroup T1
848    au!
849    au TabLeave * tabfirst
850  augroup END
851  tabclose
852  call assert_equal(1, tabpagenr())
853  augroup T1
854    au!
855  augroup END
856  augroup! T1
857  tabonly
858endfunc
859
860" Test for closing the destination tabpage when jumping from one to another.
861func Test_tabpage_close_on_switch()
862  tabnew
863  tabnew
864  edit Xfile
865  augroup T2
866    au!
867    au BufLeave Xfile 1tabclose
868  augroup END
869  tabfirst
870  call assert_equal(2, tabpagenr())
871  call assert_equal('Xfile', @%)
872  augroup T2
873    au!
874  augroup END
875  augroup! T2
876  %bw!
877endfunc
878
879" Test for jumping to last accessed tabpage
880func Test_lastused_tabpage()
881  tabonly!
882  call assert_equal(0, tabpagenr('#'))
883  call assert_beeps('call feedkeys("g\<Tab>", "xt")')
884  call assert_beeps('call feedkeys("\<C-Tab>", "xt")')
885  call assert_beeps('call feedkeys("\<C-W>g\<Tab>", "xt")')
886  call assert_fails('tabnext #', 'E475:')
887
888  " open four tab pages
889  tabnew
890  tabnew
891  tabnew
892
893  2tabnext
894
895  " Test for g<Tab>
896  call assert_equal(4, tabpagenr('#'))
897  call feedkeys("g\<Tab>", "xt")
898  call assert_equal(4, tabpagenr())
899  call assert_equal(2, tabpagenr('#'))
900
901  " Test for <C-Tab>
902  call feedkeys("\<C-Tab>", "xt")
903  call assert_equal(2, tabpagenr())
904  call assert_equal(4, tabpagenr('#'))
905
906  " Test for <C-W>g<Tab>
907  call feedkeys("\<C-W>g\<Tab>", "xt")
908  call assert_equal(4, tabpagenr())
909  call assert_equal(2, tabpagenr('#'))
910
911  " Test for :tabnext #
912  tabnext #
913  call assert_equal(2, tabpagenr())
914  call assert_equal(4, tabpagenr('#'))
915
916  " Try to jump to a closed tab page
917  tabclose #
918  call assert_equal(0, tabpagenr('#'))
919  call feedkeys("g\<Tab>", "xt")
920  call assert_equal(2, tabpagenr())
921  call feedkeys("\<C-Tab>", "xt")
922  call assert_equal(2, tabpagenr())
923  call feedkeys("\<C-W>g\<Tab>", "xt")
924  call assert_equal(2, tabpagenr())
925  call assert_fails('tabnext #', 'E475:')
926  call assert_equal(2, tabpagenr())
927
928  " Test for :tabonly #
929  let wnum = win_getid()
930  $tabnew
931  tabonly #
932  call assert_equal(wnum, win_getid())
933  call assert_equal(1, tabpagenr('$'))
934
935  " Test for :tabmove #
936  tabnew
937  let wnum = win_getid()
938  tabnew
939  tabnew
940  tabnext 2
941  tabmove #
942  call assert_equal(4, tabpagenr())
943  call assert_equal(wnum, win_getid())
944
945  tabonly!
946endfunc
947
948" Closing a tab page must keep the last used tab page when it is another one.
949func Test_lastused_tabpage_close()
950  tabonly!
951  tabnew
952  tabnew
953  tabfirst
954  tablast
955  call assert_equal(1, tabpagenr('#'))
956
957  tabclose
958  call assert_equal(1, tabpagenr('#'))
959  call feedkeys("g\<Tab>", "xt")
960  call assert_equal(1, tabpagenr())
961
962  tabonly!
963endfunc
964
965" Test for tabpage allocation failure
966func Test_tabpage_alloc_failure()
967  CheckFunction test_alloc_fail
968  call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
969  call assert_fails('tabnew', 'E342:')
970
971  call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
972  edit Xfile1
973  call assert_fails('tabedit Xfile2', 'E342:')
974  call assert_equal(1, winnr('$'))
975  call assert_equal(1, tabpagenr('$'))
976  call assert_equal('Xfile1', @%)
977
978  new
979  call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
980  call assert_fails('wincmd T', 'E342:')
981  bw!
982
983  call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
984  call assert_fails('tab split', 'E342:')
985  call assert_equal(2, winnr('$'))
986  call assert_equal(1, tabpagenr('$'))
987endfunc
988
989func Test_tabpage_tabclose()
990  " Default behaviour, move to the right.
991  call s:reconstruct_tabpage_for_test(6)
992  norm! 4gt
993  setl tcl=
994  tabclose
995  call assert_equal("n3", bufname())
996
997  " Move to the left.
998  call s:reconstruct_tabpage_for_test(6)
999  norm! 4gt
1000  setl tcl=left
1001  tabclose
1002  call assert_equal("n1", bufname())
1003
1004  " Move to the last used tab page.
1005  call s:reconstruct_tabpage_for_test(6)
1006  norm! 5gt
1007  norm! 2gt
1008  setl tcl=uselast
1009  tabclose
1010  call assert_equal("n3", bufname())
1011
1012  " Same, but the last used tab page is invalid. Move to the right.
1013  call s:reconstruct_tabpage_for_test(6)
1014  norm! 5gt
1015  norm! 3gt
1016  setl tcl=uselast
1017  tabclose 5
1018  tabclose!
1019  call assert_equal("n2", bufname())
1020
1021  " Same, but the last used tab page is invalid. Move to the left.
1022  call s:reconstruct_tabpage_for_test(6)
1023  norm! 5gt
1024  norm! 3gt
1025  setl tcl=uselast,left
1026  tabclose 5
1027  tabclose!
1028  call assert_equal("n0", bufname())
1029
1030  " Move left when moving right is not possible.
1031  call s:reconstruct_tabpage_for_test(6)
1032  setl tcl=
1033  norm! 6gt
1034  tabclose
1035  call assert_equal("n3", bufname())
1036
1037  " Move right when moving left is not possible.
1038  call s:reconstruct_tabpage_for_test(6)
1039  setl tcl=left
1040  norm! 1gt
1041  tabclose
1042  call assert_equal("n0", bufname())
1043
1044  setl tcl&
1045endfunc
1046
1047" this was giving ml_get errors
1048func Test_tabpage_last_line()
1049  enew
1050  call setline(1, repeat(['a'], &lines + 5))
1051  $
1052  tabnew
1053  call setline(1, repeat(['b'], &lines + 20))
1054  $
1055  tabNext
1056  call assert_equal('a', getline('.'))
1057
1058  bwipe!
1059  bwipe!
1060endfunc
1061
1062" this was causing an endless loop
1063func Test_tabpage_drop_tabmove()
1064  augroup TestTabpageTabmove
1065    au!
1066    autocmd! TabEnter * :if tabpagenr() > 1 | tabmove - | endif
1067  augroup end
1068  $tab drop XTab_99.log
1069  $tab drop XTab_98.log
1070  $tab drop XTab_97.log
1071
1072  autocmd! TestTabpageTabmove
1073  augroup! TestTabpageTabmove
1074
1075  " clean up
1076  bwipe!
1077  bwipe!
1078  bwipe!
1079endfunc
1080
1081" Test that settabvar() shouldn't change the last accessed tabpage.
1082func Test_lastused_tabpage_settabvar()
1083  tabonly!
1084  tabnew
1085  tabnew
1086  tabnew
1087  call assert_equal(3, tabpagenr('#'))
1088
1089  call settabvar(2, 'myvar', 'tabval')
1090  call assert_equal('tabval', gettabvar(2, 'myvar'))
1091  call assert_equal(3, tabpagenr('#'))
1092
1093  bwipe!
1094  bwipe!
1095  bwipe!
1096endfunc
1097
1098" vim: shiftwidth=2 sts=2 expandtab