blob: eed26f72218143fc4106964674d4455389511532 [file] [log] [blame]
Bram Moolenaar1381d792016-08-18 22:11:42 +02001" Tests for tabpage
2
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +02003source screendump.vim
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01004source check.vim
Bram Moolenaardbe88692018-05-20 14:57:22 +02005
Bram Moolenaar1381d792016-08-18 22:11:42 +02006function Test_tabpage()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01007 CheckFeature quickfix
8
Bram Moolenaar1381d792016-08-18 22:11:42 +02009 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
Bram Moolenaar3e8474d2016-10-12 17:52:42 +020019 %del
Bram Moolenaar1381d792016-08-18 22:11:42 +020020 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)
Bram Moolenaaraad222c2019-09-06 22:46:09 +020040 eval 'SetTabVar test'->settabvar(2, 'val_str')
Bram Moolenaar1381d792016-08-18 22:11:42 +020041 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
Bram Moolenaar5a656862018-02-12 22:08:06 +010049 " 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 "
Bram Moolenaar1bc353b2019-09-01 14:45:28 +020064 " Test for ":tab drop multi-opened-file" to keep current tabpage and window.
Bram Moolenaar5a656862018-02-12 22:08:06 +010065 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
Bram Moolenaar1381d792016-08-18 22:11:42 +020081 "
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())
Bram Moolenaar1381d792016-08-18 22:11:42 +0200100 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())
Bram Moolenaar82a12462019-01-22 22:41:42 +0100112 -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())
Bram Moolenaar7cc59652018-08-07 13:14:46 +0200120
121 " The following are a no-op
122 norm! 2gt
123 call assert_equal(2, tabpagenr())
124 tabmove 2
125 call assert_equal(2, tabpagenr())
126 2tabmove
127 call assert_equal(2, tabpagenr())
128 tabmove 1
129 call assert_equal(2, tabpagenr())
130 1tabmove
131 call assert_equal(2, tabpagenr())
132
Bram Moolenaar62a23252020-08-09 14:04:42 +0200133 call assert_fails('let t = tabpagenr("@")', 'E15:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +0200134 call assert_equal(0, tabpagewinnr(-1))
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100135 call assert_fails("99tabmove", 'E16:')
136 call assert_fails("+99tabmove", 'E16:')
137 call assert_fails("-99tabmove", 'E16:')
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200138 call assert_fails("tabmove foo", 'E475:')
139 call assert_fails("tabmove 99", 'E475:')
140 call assert_fails("tabmove +99", 'E475:')
141 call assert_fails("tabmove -99", 'E475:')
142 call assert_fails("tabmove -3+", 'E475:')
143 call assert_fails("tabmove $3", 'E475:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100144 call assert_fails("%tabonly", 'E16:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100145 1tabonly!
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200146 tabmove 1
147 call assert_equal(1, tabpagenr())
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100148 tabnew
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200149 call assert_fails("-2tabmove", 'E16:')
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100150 tabonly!
Bram Moolenaar1381d792016-08-18 22:11:42 +0200151endfunc
152
Bram Moolenaar8281a162023-04-20 18:07:57 +0100153func Test_tabpage_drop()
154 edit f1
155 tab split f2
156 tab split f3
157 normal! gt
158 call assert_equal(1, tabpagenr())
159
160 tab drop f3
161 call assert_equal(3, tabpagenr())
162 call assert_equal(1, tabpagenr('#'))
163 bwipe!
164 bwipe!
165 bwipe!
166 call assert_equal(1, tabpagenr('$'))
167endfunc
168
Bram Moolenaar1381d792016-08-18 22:11:42 +0200169" Test autocommands
170function Test_tabpage_with_autocmd()
Bram Moolenaar1381d792016-08-18 22:11:42 +0200171 command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
172 augroup TestTabpageGroup
173 au!
174 autocmd TabEnter * call add(s:li, 'TabEnter')
175 autocmd WinEnter * call add(s:li, 'WinEnter')
176 autocmd BufEnter * call add(s:li, 'BufEnter')
177 autocmd TabLeave * call add(s:li, 'TabLeave')
178 autocmd WinLeave * call add(s:li, 'WinLeave')
179 autocmd BufLeave * call add(s:li, 'BufLeave')
180 augroup END
181
182 let s:li = []
183 let t:a='a'
184 C tab split
185 call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
186 let s:li = []
187 let t:a='b'
188 C tabnew
189 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
190 let t:a='c'
191 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
192 call assert_equal(['a', 'b', 'c'], s:li)
193
194 let s:li = []
195 C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
196 call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
197 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
198 call assert_equal(['2', '4', '6'], s:li)
199
200 let s:li = []
201 let w:a='a'
202 C vsplit
203 call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
204 let s:li = []
205 let w:a='a'
206 let tabn=tabpagenr()
207 let winr=range(1, winnr('$'))
208 C tabnext 1
209 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
210 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
211 call assert_equal(['a', 'a'], s:li)
212 let s:li = []
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200213 C call map(copy(winr), '(v:val*2)->settabwinvar(' .. tabn .. ', v:val, ''a'')')
Bram Moolenaar1381d792016-08-18 22:11:42 +0200214 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
215 call assert_equal(['2', '4'], s:li)
216
217 augroup TabDestructive
218 autocmd TabEnter * :C tabnext 2 | C tabclose 3
219 augroup END
220 let s:li = []
221 C tabnext 3
222 call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
223 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
224
225 autocmd! TabDestructive TabEnter
226 let s:li = []
227 C tabnew
228 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
229 let s:li = []
230 C tabnext 1
231 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
232
233 autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
234 let s:li = []
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100235 call assert_equal(3, tabpagenr('$'))
236 C tabnext 2
237 call assert_equal(2, tabpagenr('$'))
238 call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
Bram Moolenaar1381d792016-08-18 22:11:42 +0200239 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
240
241 delcommand C
242 autocmd! TabDestructive
243 augroup! TabDestructive
244 autocmd! TestTabpageGroup
245 augroup! TestTabpageGroup
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100246 1tabonly!
Bram Moolenaar1381d792016-08-18 22:11:42 +0200247endfunction
248
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100249" Test autocommands on tab drop
250function Test_tabpage_with_autocmd_tab_drop()
251 augroup TestTabpageGroup
252 au!
253 autocmd TabEnter * call add(s:li, 'TabEnter')
254 autocmd WinEnter * call add(s:li, 'WinEnter')
255 autocmd BufEnter * call add(s:li, 'BufEnter')
256 autocmd TabLeave * call add(s:li, 'TabLeave')
257 autocmd WinLeave * call add(s:li, 'WinLeave')
258 autocmd BufLeave * call add(s:li, 'BufLeave')
259 augroup END
260
261 let s:li = []
262 tab drop test1
263 call assert_equal(['BufLeave', 'BufEnter'], s:li)
264
265 let s:li = []
266 tab drop test2 test3
267 call assert_equal([
268 \ 'TabLeave', 'TabEnter', 'TabLeave', 'TabEnter',
269 \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter',
270 \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
271
272 autocmd! TestTabpageGroup
273 augroup! TestTabpageGroup
274 1tabonly!
275endfunction
276
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200277function Test_tabpage_with_tab_modifier()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100278 CheckFeature quickfix
279
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200280 for n in range(4)
281 tabedit
282 endfor
283
284 function s:check_tab(pre_nr, cmd, post_nr)
285 exec 'tabnext ' . a:pre_nr
286 exec a:cmd
287 call assert_equal(a:post_nr, tabpagenr())
Bram Moolenaar100f5c92016-09-06 21:33:52 +0200288 call assert_equal('help', &buftype)
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200289 helpclose
290 endfunc
291
292 call s:check_tab(1, 'tab help', 2)
293 call s:check_tab(1, '3tab help', 4)
294 call s:check_tab(1, '.tab help', 2)
295 call s:check_tab(1, '.+1tab help', 3)
296 call s:check_tab(1, '0tab help', 1)
297 call s:check_tab(2, '+tab help', 4)
298 call s:check_tab(2, '+2tab help', 5)
299 call s:check_tab(4, '-tab help', 4)
300 call s:check_tab(4, '-2tab help', 3)
301 call s:check_tab(3, '$tab help', 6)
302 call assert_fails('99tab help', 'E16:')
303 call assert_fails('+99tab help', 'E16:')
304 call assert_fails('-99tab help', 'E16:')
305
306 delfunction s:check_tab
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100307 1tabonly!
308endfunction
309
310function Check_tab_count(pre_nr, cmd, post_nr)
311 exec 'tabnext' a:pre_nr
312 normal! G
313 exec a:cmd
314 call assert_equal(a:post_nr, tabpagenr(), a:cmd)
315endfunc
316
317" Test for [count] of tabnext
318function Test_tabpage_with_tabnext()
319 for n in range(4)
320 tabedit
321 call setline(1, ['', '', '3'])
322 endfor
323
324 call Check_tab_count(1, 'tabnext', 2)
325 call Check_tab_count(1, '3tabnext', 3)
326 call Check_tab_count(1, '.tabnext', 1)
327 call Check_tab_count(1, '.+1tabnext', 2)
328 call Check_tab_count(2, '+tabnext', 3)
329 call Check_tab_count(2, '+2tabnext', 4)
330 call Check_tab_count(4, '-tabnext', 3)
331 call Check_tab_count(4, '-2tabnext', 2)
332 call Check_tab_count(3, '$tabnext', 5)
333 call assert_fails('0tabnext', 'E16:')
334 call assert_fails('99tabnext', 'E16:')
335 call assert_fails('+99tabnext', 'E16:')
336 call assert_fails('-99tabnext', 'E16:')
337 call Check_tab_count(1, 'tabnext 3', 3)
338 call Check_tab_count(2, 'tabnext +', 3)
339 call Check_tab_count(2, 'tabnext +2', 4)
340 call Check_tab_count(4, 'tabnext -', 3)
341 call Check_tab_count(4, 'tabnext -2', 2)
342 call Check_tab_count(3, 'tabnext $', 5)
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200343 call assert_fails('tabnext 0', 'E475:')
344 call assert_fails('tabnext .', 'E475:')
345 call assert_fails('tabnext -+', 'E475:')
346 call assert_fails('tabnext +2-', 'E475:')
347 call assert_fails('tabnext $3', 'E475:')
348 call assert_fails('tabnext 99', 'E475:')
349 call assert_fails('tabnext +99', 'E475:')
350 call assert_fails('tabnext -99', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100351
352 1tabonly!
353endfunction
354
355" Test for [count] of tabprevious
356function Test_tabpage_with_tabprevious()
357 for n in range(5)
358 tabedit
359 call setline(1, ['', '', '3'])
360 endfor
361
362 for cmd in ['tabNext', 'tabprevious']
363 call Check_tab_count(6, cmd, 5)
364 call Check_tab_count(6, '3' . cmd, 3)
365 call Check_tab_count(6, '8' . cmd, 4)
366 call Check_tab_count(6, cmd . ' 3', 3)
367 call Check_tab_count(6, cmd . ' 8', 4)
368 for n in range(2)
Bram Moolenaar9d489562020-07-30 20:08:50 +0200369 for c in ['0', '.+3', '+', '+2', '-', '-2', '$', '+99', '-99']
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100370 if n == 0 " pre count
371 let entire_cmd = c . cmd
372 let err_code = 'E16:'
373 else
374 let entire_cmd = cmd . ' ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200375 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100376 endif
377 call assert_fails(entire_cmd, err_code)
378 endfor
379 endfor
380 endfor
381
382 1tabonly!
383endfunction
384
385function s:reconstruct_tabpage_for_test(nr)
386 let n = (a:nr > 2) ? a:nr - 2 : 1
387 1tabonly!
388 0tabedit n0
389 for n in range(1, n)
390 exec '$tabedit n' . n
391 if n == 1
392 call setline(1, ['', '', '3'])
393 endif
394 endfor
395endfunc
396
Bram Moolenaardbe88692018-05-20 14:57:22 +0200397func Test_tabpage_ctrl_pgup_pgdown()
398 enew!
399 tabnew tab1
400 tabnew tab2
401
402 call assert_equal(3, tabpagenr())
403 exe "norm! \<C-PageUp>"
404 call assert_equal(2, tabpagenr())
405 exe "norm! \<C-PageDown>"
406 call assert_equal(3, tabpagenr())
407
408 " Check wrapping at last or first page.
409 exe "norm! \<C-PageDown>"
410 call assert_equal(1, tabpagenr())
411 exe "norm! \<C-PageUp>"
412 call assert_equal(3, tabpagenr())
413
414 " With a count, <C-PageUp> and <C-PageDown> are not symmetrical somehow:
415 " - {count}<C-PageUp> goes {count} pages downward (relative count)
416 " - {count}<C-PageDown> goes to page number {count} (absolute count)
417 exe "norm! 2\<C-PageUp>"
418 call assert_equal(1, tabpagenr())
419 exe "norm! 2\<C-PageDown>"
420 call assert_equal(2, tabpagenr())
421
422 1tabonly!
423endfunc
424
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100425" Test for [count] of tabclose
426function Test_tabpage_with_tabclose()
427
428 " pre count
429 call s:reconstruct_tabpage_for_test(6)
430 call Check_tab_count(3, 'tabclose!', 3)
431 call Check_tab_count(1, '3tabclose', 1)
432 call Check_tab_count(4, '4tabclose', 3)
433 call Check_tab_count(3, '1tabclose', 2)
434 call Check_tab_count(2, 'tabclose', 1)
435 call assert_equal(1, tabpagenr('$'))
436 call assert_equal('', bufname(''))
437
438 call s:reconstruct_tabpage_for_test(6)
439 call Check_tab_count(2, '$tabclose', 2)
440 call Check_tab_count(4, '.tabclose', 4)
441 call Check_tab_count(3, '.+tabclose', 3)
442 call Check_tab_count(3, '.-2tabclose', 2)
443 call Check_tab_count(1, '.+1tabclose!', 1)
444 call assert_equal(1, tabpagenr('$'))
445 call assert_equal('', bufname(''))
446
447 " post count
448 call s:reconstruct_tabpage_for_test(6)
449 call Check_tab_count(3, 'tabclose!', 3)
450 call Check_tab_count(1, 'tabclose 3', 1)
451 call Check_tab_count(4, 'tabclose 4', 3)
452 call Check_tab_count(3, 'tabclose 1', 2)
453 call Check_tab_count(2, 'tabclose', 1)
454 call assert_equal(1, tabpagenr('$'))
455 call assert_equal('', bufname(''))
456
457 call s:reconstruct_tabpage_for_test(6)
458 call Check_tab_count(2, 'tabclose $', 2)
459 call Check_tab_count(4, 'tabclose', 4)
460 call Check_tab_count(3, 'tabclose +', 3)
461 call Check_tab_count(3, 'tabclose -2', 2)
462 call Check_tab_count(1, 'tabclose! +1', 1)
463 call assert_equal(1, tabpagenr('$'))
464 call assert_equal('', bufname(''))
465
466 call s:reconstruct_tabpage_for_test(6)
467 for n in range(2)
468 for c in ['0', '$3', '99', '+99', '-99']
469 if n == 0 " pre count
470 let entire_cmd = c . 'tabclose'
471 let err_code = 'E16:'
472 else
473 let entire_cmd = 'tabclose ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200474 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100475 endif
476 call assert_fails(entire_cmd, err_code)
477 call assert_equal(6, tabpagenr('$'))
478 endfor
479 endfor
480
481 call assert_fails('3tabclose', 'E37:')
482 call assert_fails('tabclose 3', 'E37:')
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200483 call assert_fails('tabclose -+', 'E475:')
484 call assert_fails('tabclose +2-', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100485 call assert_equal(6, tabpagenr('$'))
486
487 1tabonly!
488endfunction
489
490" Test for [count] of tabonly
491function Test_tabpage_with_tabonly()
492
493 " Test for the normal behavior (pre count only)
494 let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
495 for c in tc
496 call s:reconstruct_tabpage_for_test(6)
497 let entire_cmd = c[1] . 'tabonly' . c[2]
498 call Check_tab_count(c[0], entire_cmd, 1)
499 call assert_equal(1, tabpagenr('$'))
500 endfor
501
502 " Test for the normal behavior
503 let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
504 \ [2, '', '!'],
505 \ [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
506 \ ]
507 for n in range(2)
508 for c in tc2
509 call s:reconstruct_tabpage_for_test(6)
510 if n == 0 " pre count
511 let entire_cmd = c[1] . 'tabonly' . c[2]
512 else
513 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
514 endif
515 call Check_tab_count(c[0], entire_cmd, 1)
516 call assert_equal(1, tabpagenr('$'))
517 endfor
518 endfor
519
520 " Test for the error behavior
521 for n in range(2)
522 for c in ['0', '$3', '99', '+99', '-99']
523 call s:reconstruct_tabpage_for_test(6)
524 if n == 0 " pre count
525 let entire_cmd = c . 'tabonly'
526 let err_code = 'E16:'
527 else
528 let entire_cmd = 'tabonly ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200529 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100530 endif
531 call assert_fails(entire_cmd, err_code)
532 call assert_equal(6, tabpagenr('$'))
533 endfor
534 endfor
535
536 " Test for the error behavior (post count only)
537 for c in tc
538 call s:reconstruct_tabpage_for_test(6)
539 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200540 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100541 call assert_fails(entire_cmd, err_code)
542 call assert_equal(6, tabpagenr('$'))
543 endfor
544
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200545 call assert_fails('tabonly -+', 'E475:')
546 call assert_fails('tabonly +2-', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100547 call assert_equal(6, tabpagenr('$'))
548
549 1tabonly!
550 new
551 only!
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200552endfunction
553
Bram Moolenaar5a497892016-09-03 16:29:04 +0200554func Test_tabnext_on_buf_unload1()
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200555 " This once caused a crash
556 new
557 tabedit
558 tabfirst
559 au BufUnload <buffer> tabnext
560 q
561
562 while tabpagenr('$') > 1
Bram Moolenaar5a497892016-09-03 16:29:04 +0200563 bwipe!
564 endwhile
565endfunc
566
567func Test_tabnext_on_buf_unload2()
568 " This once caused a crash
569 tabedit
570 autocmd BufUnload <buffer> tabnext
571 file x
572 edit y
573
574 while tabpagenr('$') > 1
575 bwipe!
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200576 endwhile
577endfunc
578
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200579func Test_close_on_quitpre()
580 " This once caused a crash
Bram Moolenaarce11de82017-10-26 22:00:00 +0200581 edit Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200582 new
583 only
584 set bufhidden=delete
585 au QuitPre <buffer> close
586 tabnew tab1
587 tabnew tab2
588 1tabn
589 q!
590 call assert_equal(1, tabpagenr())
591 call assert_equal(2, tabpagenr('$'))
592 " clean up
593 while tabpagenr('$') > 1
594 bwipe!
595 endwhile
Bram Moolenaarce11de82017-10-26 22:00:00 +0200596 buf Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200597endfunc
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200598
Bram Moolenaardbe88692018-05-20 14:57:22 +0200599func Test_tabs()
600 enew!
601 tabnew tab1
602 norm ixxx
603 let a=split(execute(':tabs'), "\n")
604 call assert_equal(['Tab page 1',
605 \ ' [No Name]',
606 \ 'Tab page 2',
607 \ '> + tab1'], a)
608
609 1tabonly!
610 bw!
611endfunc
612
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200613func Test_tabpage_cmdheight()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200614 CheckRunVimInTerminal
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200615 call writefile([
616 \ 'set laststatus=2',
617 \ 'set cmdheight=2',
618 \ 'tabnew',
619 \ 'set cmdheight=3',
620 \ 'tabnext',
621 \ 'redraw!',
622 \ 'echo "hello\nthere"',
623 \ 'tabnext',
624 \ 'redraw',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100625 \ ], 'XTest_tabpage_cmdheight', 'D')
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200626 " Check that cursor line is concealed
627 let buf = RunVimInTerminal('-S XTest_tabpage_cmdheight', {'statusoff': 3})
628 call VerifyScreenDump(buf, 'Test_tabpage_cmdheight', {})
629
630 call StopVimInTerminal(buf)
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200631endfunc
632
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100633" Test for closing the tab page from a command window
634func Test_tabpage_close_cmdwin()
635 tabnew
636 call feedkeys("q/:tabclose\<CR>\<Esc>", 'xt')
637 call assert_equal(2, tabpagenr('$'))
638 call feedkeys("q/:tabonly\<CR>\<Esc>", 'xt')
639 call assert_equal(2, tabpagenr('$'))
640 tabonly
641endfunc
642
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200643" Pressing <C-PageUp> in insert mode should go to the previous tab page
644" and <C-PageDown> should go to the next tab page
645func Test_tabpage_Ctrl_Pageup()
646 tabnew
647 call feedkeys("i\<C-PageUp>", 'xt')
648 call assert_equal(1, tabpagenr())
649 call feedkeys("i\<C-PageDown>", 'xt')
650 call assert_equal(2, tabpagenr())
651 %bw!
652endfunc
653
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200654" Return the terminal key code for selecting a tab page from the tabline. This
655" sequence contains the following codes: a CSI (0x9b), KS_TABLINE (0xf0),
656" KS_FILLER (0x58) and then the tab page number.
657func TabLineSelectPageCode(tabnr)
658 return "\x9b\xf0\x58" .. nr2char(a:tabnr)
659endfunc
660
661" Return the terminal key code for opening a new tabpage from the tabpage
662" menu. This sequence consists of the following codes: a CSI (0x9b),
663" KS_TABMENU (0xef), KS_FILLER (0x58), the tab page number and
664" TABLINE_MENU_NEW (2).
665func TabMenuNewItemCode(tabnr)
666 return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(2)
667endfunc
668
669" Return the terminal key code for closing a tabpage from the tabpage menu.
670" This sequence consists of the following codes: a CSI (0x9b), KS_TABMENU
671" (0xef), KS_FILLER (0x58), the tab page number and TABLINE_MENU_CLOSE (1).
672func TabMenuCloseItemCode(tabnr)
673 return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(1)
674endfunc
675
676" Test for using the tabpage menu from the insert and normal modes
677func Test_tabline_tabmenu()
678 " only works in GUI
679 CheckGui
680
681 %bw!
682 tabnew
683 tabnew
684 call assert_equal(3, tabpagenr())
685
686 " go to tab page 2 in normal mode
687 call feedkeys(TabLineSelectPageCode(2), "Lx!")
688 call assert_equal(2, tabpagenr())
689
690 " close tab page 3 in normal mode
691 call feedkeys(TabMenuCloseItemCode(3), "Lx!")
692 call assert_equal(2, tabpagenr('$'))
693 call assert_equal(2, tabpagenr())
694
695 " open new tab page before tab page 1 in normal mode
696 call feedkeys(TabMenuNewItemCode(1), "Lx!")
697 call assert_equal(1, tabpagenr())
698 call assert_equal(3, tabpagenr('$'))
699
700 " go to tab page 2 in operator-pending mode (should beep)
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200701 call assert_beeps('call feedkeys("c" .. TabLineSelectPageCode(2), "Lx!")')
702 call assert_equal(2, tabpagenr())
703 call assert_equal(3, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200704
705 " open new tab page before tab page 1 in operator-pending mode (should beep)
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200706 call assert_beeps('call feedkeys("c" .. TabMenuNewItemCode(1), "Lx!")')
707 call assert_equal(1, tabpagenr())
708 call assert_equal(4, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200709
710 " open new tab page after tab page 3 in normal mode
711 call feedkeys(TabMenuNewItemCode(4), "Lx!")
712 call assert_equal(4, tabpagenr())
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200713 call assert_equal(5, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200714
715 " go to tab page 2 in insert mode
716 call feedkeys("i" .. TabLineSelectPageCode(2) .. "\<C-C>", "Lx!")
717 call assert_equal(2, tabpagenr())
718
719 " close tab page 2 in insert mode
720 call feedkeys("i" .. TabMenuCloseItemCode(2) .. "\<C-C>", "Lx!")
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200721 call assert_equal(4, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200722
723 " open new tab page before tab page 3 in insert mode
724 call feedkeys("i" .. TabMenuNewItemCode(3) .. "\<C-C>", "Lx!")
725 call assert_equal(3, tabpagenr())
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200726 call assert_equal(5, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200727
728 " open new tab page after tab page 4 in insert mode
729 call feedkeys("i" .. TabMenuNewItemCode(5) .. "\<C-C>", "Lx!")
730 call assert_equal(5, tabpagenr())
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200731 call assert_equal(6, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200732
733 %bw!
734endfunc
735
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200736" Test for changing the current tab page from an autocmd when closing a tab
737" page.
738func Test_tabpage_switchtab_on_close()
739 only
740 tabnew
741 tabnew
742 " Test for BufLeave
743 augroup T1
744 au!
745 au BufLeave * tabfirst
746 augroup END
747 tabclose
748 call assert_equal(1, tabpagenr())
749 augroup T1
750 au!
751 augroup END
752
753 " Test for WinLeave
754 $tabnew
755 augroup T1
756 au!
757 au WinLeave * tabfirst
758 augroup END
759 tabclose
760 call assert_equal(1, tabpagenr())
761 augroup T1
762 au!
763 augroup END
764
765 " Test for TabLeave
766 $tabnew
767 augroup T1
768 au!
769 au TabLeave * tabfirst
770 augroup END
771 tabclose
772 call assert_equal(1, tabpagenr())
773 augroup T1
774 au!
775 augroup END
776 augroup! T1
777 tabonly
778endfunc
779
780" Test for closing the destination tabpage when jumping from one to another.
781func Test_tabpage_close_on_switch()
782 tabnew
783 tabnew
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100784 edit Xtabfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200785 augroup T2
786 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100787 au BufLeave Xtabfile 1tabclose
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200788 augroup END
789 tabfirst
790 call assert_equal(2, tabpagenr())
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100791 call assert_equal('Xtabfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200792 augroup T2
793 au!
794 augroup END
795 augroup! T2
796 %bw!
797endfunc
798
Bram Moolenaar62a23252020-08-09 14:04:42 +0200799" Test for jumping to last accessed tabpage
800func Test_lastused_tabpage()
801 tabonly!
802 call assert_equal(0, tabpagenr('#'))
803 call assert_beeps('call feedkeys("g\<Tab>", "xt")')
804 call assert_beeps('call feedkeys("\<C-Tab>", "xt")')
805 call assert_beeps('call feedkeys("\<C-W>g\<Tab>", "xt")')
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200806 call assert_fails('tabnext #', 'E475:')
Bram Moolenaar62a23252020-08-09 14:04:42 +0200807
808 " open four tab pages
809 tabnew
810 tabnew
811 tabnew
812
813 2tabnext
814
815 " Test for g<Tab>
816 call assert_equal(4, tabpagenr('#'))
817 call feedkeys("g\<Tab>", "xt")
818 call assert_equal(4, tabpagenr())
819 call assert_equal(2, tabpagenr('#'))
820
821 " Test for <C-Tab>
822 call feedkeys("\<C-Tab>", "xt")
823 call assert_equal(2, tabpagenr())
824 call assert_equal(4, tabpagenr('#'))
825
826 " Test for <C-W>g<Tab>
827 call feedkeys("\<C-W>g\<Tab>", "xt")
828 call assert_equal(4, tabpagenr())
829 call assert_equal(2, tabpagenr('#'))
830
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200831 " Test for :tabnext #
832 tabnext #
833 call assert_equal(2, tabpagenr())
834 call assert_equal(4, tabpagenr('#'))
835
Bram Moolenaar62a23252020-08-09 14:04:42 +0200836 " Try to jump to a closed tab page
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200837 tabclose #
Bram Moolenaar62a23252020-08-09 14:04:42 +0200838 call assert_equal(0, tabpagenr('#'))
839 call feedkeys("g\<Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200840 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200841 call feedkeys("\<C-Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200842 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200843 call feedkeys("\<C-W>g\<Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200844 call assert_equal(2, tabpagenr())
845 call assert_fails('tabnext #', 'E475:')
846 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200847
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200848 " Test for :tabonly #
849 let wnum = win_getid()
850 $tabnew
851 tabonly #
852 call assert_equal(wnum, win_getid())
853 call assert_equal(1, tabpagenr('$'))
854
855 " Test for :tabmove #
856 tabnew
857 let wnum = win_getid()
858 tabnew
859 tabnew
860 tabnext 2
861 tabmove #
862 call assert_equal(4, tabpagenr())
863 call assert_equal(wnum, win_getid())
864
865 tabonly!
Bram Moolenaar62a23252020-08-09 14:04:42 +0200866endfunc
867
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100868" Test for tabpage allocation failure
869func Test_tabpage_alloc_failure()
870 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
871 call assert_fails('tabnew', 'E342:')
872
873 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
874 edit Xfile1
875 call assert_fails('tabedit Xfile2', 'E342:')
876 call assert_equal(1, winnr('$'))
877 call assert_equal(1, tabpagenr('$'))
878 call assert_equal('Xfile1', @%)
879
880 new
881 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
882 call assert_fails('wincmd T', 'E342:')
883 bw!
884
885 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
886 call assert_fails('tab split', 'E342:')
887 call assert_equal(2, winnr('$'))
888 call assert_equal(1, tabpagenr('$'))
889endfunc
890
Bram Moolenaar99ad3a82023-02-27 17:18:01 +0000891" this was giving ml_get errors
892func Test_tabpage_last_line()
893 enew
894 call setline(1, repeat(['a'], &lines + 5))
895 $
896 tabnew
897 call setline(1, repeat(['b'], &lines + 20))
898 $
899 tabNext
900 call assert_equal('a', getline('.'))
901
902 bwipe!
903 bwipe!
904endfunc
905
Bram Moolenaar1381d792016-08-18 22:11:42 +0200906" vim: shiftwidth=2 sts=2 expandtab