blob: 46aed221e4053dbecdd885b6c3e963413cb112b3 [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('$'))
Rocco Maof96dc8d2024-01-23 21:27:19 +0100167
168 call assert_equal(1, winnr('$'))
169 call assert_equal('', bufname(''))
170 call writefile(['L1', 'L2'], 'Xdropfile', 'D')
171
172 " Test for ':tab drop single-file': reuse current buffer
173 let expected_nr = bufnr()
174 tab drop Xdropfile
175 call assert_equal(1, tabpagenr('$'))
176 call assert_equal(expected_nr, bufnr())
177 call assert_equal('L2', getline(2))
178 bwipe!
179
180 " Test for ':tab drop single-file': not reuse modified buffer
181 set modified
182 let expected_nr = bufnr() + 1
183 tab drop Xdropfile
184 call assert_equal(2, tabpagenr())
185 call assert_equal(2, tabpagenr('$'))
186 call assert_equal(expected_nr, bufnr())
187 call assert_equal('L2', getline(2))
188 bwipe!
189
190 " Test for ':tab drop single-file': multiple tabs already exist
191 tab split f2
192 tab split f3
193 let expected_nr = bufnr() + 1
194 tab drop Xdropfile
195 call assert_equal(4, tabpagenr())
196 call assert_equal(4, tabpagenr('$'))
197 call assert_equal(expected_nr, bufnr())
198 call assert_equal('L2', getline(2))
199 %bwipe!
200
201 " Test for ':tab drop multi-files': reuse current buffer
202 let expected_nr = bufnr()
203 tab drop Xdropfile f1 f2 f3
204 call assert_equal(1, 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': not reuse modified buffer
211 set modified
212 let expected_nr = bufnr() + 1
213 tab drop Xdropfile f1 f2 f3
214 call assert_equal(2, tabpagenr())
215 call assert_equal(5, tabpagenr('$'))
216 call assert_equal(expected_nr, bufnr())
217 call assert_equal('L2', getline(2))
218 %bwipe!
219
220 " Test for ':tab drop multi-files': multiple tabs already exist
221 tab split f2
222 tab split f3
223 let expected_nr = bufnr() + 1
224 tab drop a b c
225 call assert_equal(4, tabpagenr())
226 call assert_equal(6, tabpagenr('$'))
227 call assert_equal(expected_nr, bufnr())
228 let expected_nr = bufnr() + 3
229 tab drop Xdropfile f1 f2 f3
230 call assert_equal(5, tabpagenr())
231 call assert_equal(8, tabpagenr('$'))
232 call assert_equal(expected_nr, bufnr())
233 call assert_equal('L2', getline(2))
234 %bwipe!
Bram Moolenaar8281a162023-04-20 18:07:57 +0100235endfunc
236
Bram Moolenaar1381d792016-08-18 22:11:42 +0200237" Test autocommands
238function Test_tabpage_with_autocmd()
Bram Moolenaar1381d792016-08-18 22:11:42 +0200239 command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
240 augroup TestTabpageGroup
241 au!
242 autocmd TabEnter * call add(s:li, 'TabEnter')
243 autocmd WinEnter * call add(s:li, 'WinEnter')
244 autocmd BufEnter * call add(s:li, 'BufEnter')
245 autocmd TabLeave * call add(s:li, 'TabLeave')
246 autocmd WinLeave * call add(s:li, 'WinLeave')
247 autocmd BufLeave * call add(s:li, 'BufLeave')
248 augroup END
249
250 let s:li = []
251 let t:a='a'
252 C tab split
253 call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
254 let s:li = []
255 let t:a='b'
256 C tabnew
257 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
258 let t:a='c'
259 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
260 call assert_equal(['a', 'b', 'c'], s:li)
261
262 let s:li = []
263 C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
264 call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
265 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
266 call assert_equal(['2', '4', '6'], s:li)
267
268 let s:li = []
269 let w:a='a'
270 C vsplit
271 call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
272 let s:li = []
273 let w:a='a'
274 let tabn=tabpagenr()
275 let winr=range(1, winnr('$'))
276 C tabnext 1
277 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
278 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
279 call assert_equal(['a', 'a'], s:li)
280 let s:li = []
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200281 C call map(copy(winr), '(v:val*2)->settabwinvar(' .. tabn .. ', v:val, ''a'')')
Bram Moolenaar1381d792016-08-18 22:11:42 +0200282 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
283 call assert_equal(['2', '4'], s:li)
284
285 augroup TabDestructive
286 autocmd TabEnter * :C tabnext 2 | C tabclose 3
287 augroup END
288 let s:li = []
289 C tabnext 3
290 call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
291 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
292
293 autocmd! TabDestructive TabEnter
294 let s:li = []
295 C tabnew
296 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
297 let s:li = []
298 C tabnext 1
299 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
300
301 autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
302 let s:li = []
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100303 call assert_equal(3, tabpagenr('$'))
304 C tabnext 2
305 call assert_equal(2, tabpagenr('$'))
306 call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
Bram Moolenaar1381d792016-08-18 22:11:42 +0200307 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
308
309 delcommand C
310 autocmd! TabDestructive
311 augroup! TabDestructive
312 autocmd! TestTabpageGroup
313 augroup! TestTabpageGroup
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100314 1tabonly!
Bram Moolenaar1381d792016-08-18 22:11:42 +0200315endfunction
316
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100317" Test autocommands on tab drop
318function Test_tabpage_with_autocmd_tab_drop()
319 augroup TestTabpageGroup
320 au!
321 autocmd TabEnter * call add(s:li, 'TabEnter')
322 autocmd WinEnter * call add(s:li, 'WinEnter')
323 autocmd BufEnter * call add(s:li, 'BufEnter')
324 autocmd TabLeave * call add(s:li, 'TabLeave')
325 autocmd WinLeave * call add(s:li, 'WinLeave')
326 autocmd BufLeave * call add(s:li, 'BufLeave')
327 augroup END
328
329 let s:li = []
330 tab drop test1
Rocco Maof96dc8d2024-01-23 21:27:19 +0100331 call assert_equal(['BufEnter'], s:li)
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100332
333 let s:li = []
334 tab drop test2 test3
335 call assert_equal([
336 \ 'TabLeave', 'TabEnter', 'TabLeave', 'TabEnter',
337 \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter',
Rocco Maof96dc8d2024-01-23 21:27:19 +0100338 \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter', 'BufEnter'], s:li)
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100339
340 autocmd! TestTabpageGroup
341 augroup! TestTabpageGroup
342 1tabonly!
343endfunction
344
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200345function Test_tabpage_with_tab_modifier()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100346 CheckFeature quickfix
347
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200348 for n in range(4)
349 tabedit
350 endfor
351
352 function s:check_tab(pre_nr, cmd, post_nr)
353 exec 'tabnext ' . a:pre_nr
354 exec a:cmd
355 call assert_equal(a:post_nr, tabpagenr())
Bram Moolenaar100f5c92016-09-06 21:33:52 +0200356 call assert_equal('help', &buftype)
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200357 helpclose
358 endfunc
359
360 call s:check_tab(1, 'tab help', 2)
361 call s:check_tab(1, '3tab help', 4)
362 call s:check_tab(1, '.tab help', 2)
363 call s:check_tab(1, '.+1tab help', 3)
364 call s:check_tab(1, '0tab help', 1)
365 call s:check_tab(2, '+tab help', 4)
366 call s:check_tab(2, '+2tab help', 5)
367 call s:check_tab(4, '-tab help', 4)
368 call s:check_tab(4, '-2tab help', 3)
369 call s:check_tab(3, '$tab help', 6)
370 call assert_fails('99tab help', 'E16:')
371 call assert_fails('+99tab help', 'E16:')
372 call assert_fails('-99tab help', 'E16:')
373
374 delfunction s:check_tab
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100375 1tabonly!
376endfunction
377
378function Check_tab_count(pre_nr, cmd, post_nr)
379 exec 'tabnext' a:pre_nr
380 normal! G
381 exec a:cmd
382 call assert_equal(a:post_nr, tabpagenr(), a:cmd)
383endfunc
384
385" Test for [count] of tabnext
386function Test_tabpage_with_tabnext()
387 for n in range(4)
388 tabedit
389 call setline(1, ['', '', '3'])
390 endfor
391
392 call Check_tab_count(1, 'tabnext', 2)
393 call Check_tab_count(1, '3tabnext', 3)
394 call Check_tab_count(1, '.tabnext', 1)
395 call Check_tab_count(1, '.+1tabnext', 2)
396 call Check_tab_count(2, '+tabnext', 3)
397 call Check_tab_count(2, '+2tabnext', 4)
398 call Check_tab_count(4, '-tabnext', 3)
399 call Check_tab_count(4, '-2tabnext', 2)
400 call Check_tab_count(3, '$tabnext', 5)
401 call assert_fails('0tabnext', 'E16:')
402 call assert_fails('99tabnext', 'E16:')
403 call assert_fails('+99tabnext', 'E16:')
404 call assert_fails('-99tabnext', 'E16:')
405 call Check_tab_count(1, 'tabnext 3', 3)
406 call Check_tab_count(2, 'tabnext +', 3)
407 call Check_tab_count(2, 'tabnext +2', 4)
408 call Check_tab_count(4, 'tabnext -', 3)
409 call Check_tab_count(4, 'tabnext -2', 2)
410 call Check_tab_count(3, 'tabnext $', 5)
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200411 call assert_fails('tabnext 0', 'E475:')
412 call assert_fails('tabnext .', 'E475:')
413 call assert_fails('tabnext -+', 'E475:')
414 call assert_fails('tabnext +2-', 'E475:')
415 call assert_fails('tabnext $3', 'E475:')
416 call assert_fails('tabnext 99', 'E475:')
417 call assert_fails('tabnext +99', 'E475:')
418 call assert_fails('tabnext -99', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100419
420 1tabonly!
421endfunction
422
423" Test for [count] of tabprevious
424function Test_tabpage_with_tabprevious()
425 for n in range(5)
426 tabedit
427 call setline(1, ['', '', '3'])
428 endfor
429
430 for cmd in ['tabNext', 'tabprevious']
431 call Check_tab_count(6, cmd, 5)
432 call Check_tab_count(6, '3' . cmd, 3)
433 call Check_tab_count(6, '8' . cmd, 4)
434 call Check_tab_count(6, cmd . ' 3', 3)
435 call Check_tab_count(6, cmd . ' 8', 4)
436 for n in range(2)
Bram Moolenaar9d489562020-07-30 20:08:50 +0200437 for c in ['0', '.+3', '+', '+2', '-', '-2', '$', '+99', '-99']
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100438 if n == 0 " pre count
439 let entire_cmd = c . cmd
440 let err_code = 'E16:'
441 else
442 let entire_cmd = cmd . ' ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200443 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100444 endif
445 call assert_fails(entire_cmd, err_code)
446 endfor
447 endfor
448 endfor
449
450 1tabonly!
451endfunction
452
453function s:reconstruct_tabpage_for_test(nr)
454 let n = (a:nr > 2) ? a:nr - 2 : 1
455 1tabonly!
456 0tabedit n0
457 for n in range(1, n)
458 exec '$tabedit n' . n
459 if n == 1
460 call setline(1, ['', '', '3'])
461 endif
462 endfor
463endfunc
464
Bram Moolenaardbe88692018-05-20 14:57:22 +0200465func Test_tabpage_ctrl_pgup_pgdown()
466 enew!
467 tabnew tab1
468 tabnew tab2
469
470 call assert_equal(3, tabpagenr())
471 exe "norm! \<C-PageUp>"
472 call assert_equal(2, tabpagenr())
473 exe "norm! \<C-PageDown>"
474 call assert_equal(3, tabpagenr())
475
476 " Check wrapping at last or first page.
477 exe "norm! \<C-PageDown>"
478 call assert_equal(1, tabpagenr())
479 exe "norm! \<C-PageUp>"
480 call assert_equal(3, tabpagenr())
481
482 " With a count, <C-PageUp> and <C-PageDown> are not symmetrical somehow:
483 " - {count}<C-PageUp> goes {count} pages downward (relative count)
484 " - {count}<C-PageDown> goes to page number {count} (absolute count)
485 exe "norm! 2\<C-PageUp>"
486 call assert_equal(1, tabpagenr())
487 exe "norm! 2\<C-PageDown>"
488 call assert_equal(2, tabpagenr())
489
490 1tabonly!
491endfunc
492
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100493" Test for [count] of tabclose
494function Test_tabpage_with_tabclose()
495
496 " pre count
497 call s:reconstruct_tabpage_for_test(6)
498 call Check_tab_count(3, 'tabclose!', 3)
499 call Check_tab_count(1, '3tabclose', 1)
500 call Check_tab_count(4, '4tabclose', 3)
501 call Check_tab_count(3, '1tabclose', 2)
502 call Check_tab_count(2, 'tabclose', 1)
503 call assert_equal(1, tabpagenr('$'))
504 call assert_equal('', bufname(''))
505
506 call s:reconstruct_tabpage_for_test(6)
507 call Check_tab_count(2, '$tabclose', 2)
508 call Check_tab_count(4, '.tabclose', 4)
509 call Check_tab_count(3, '.+tabclose', 3)
510 call Check_tab_count(3, '.-2tabclose', 2)
511 call Check_tab_count(1, '.+1tabclose!', 1)
512 call assert_equal(1, tabpagenr('$'))
513 call assert_equal('', bufname(''))
514
515 " post count
516 call s:reconstruct_tabpage_for_test(6)
517 call Check_tab_count(3, 'tabclose!', 3)
518 call Check_tab_count(1, 'tabclose 3', 1)
519 call Check_tab_count(4, 'tabclose 4', 3)
520 call Check_tab_count(3, 'tabclose 1', 2)
521 call Check_tab_count(2, 'tabclose', 1)
522 call assert_equal(1, tabpagenr('$'))
523 call assert_equal('', bufname(''))
524
525 call s:reconstruct_tabpage_for_test(6)
526 call Check_tab_count(2, 'tabclose $', 2)
527 call Check_tab_count(4, 'tabclose', 4)
528 call Check_tab_count(3, 'tabclose +', 3)
529 call Check_tab_count(3, 'tabclose -2', 2)
530 call Check_tab_count(1, 'tabclose! +1', 1)
531 call assert_equal(1, tabpagenr('$'))
532 call assert_equal('', bufname(''))
533
534 call s:reconstruct_tabpage_for_test(6)
535 for n in range(2)
536 for c in ['0', '$3', '99', '+99', '-99']
537 if n == 0 " pre count
538 let entire_cmd = c . 'tabclose'
539 let err_code = 'E16:'
540 else
541 let entire_cmd = 'tabclose ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200542 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100543 endif
544 call assert_fails(entire_cmd, err_code)
545 call assert_equal(6, tabpagenr('$'))
546 endfor
547 endfor
548
549 call assert_fails('3tabclose', 'E37:')
550 call assert_fails('tabclose 3', 'E37:')
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200551 call assert_fails('tabclose -+', 'E475:')
552 call assert_fails('tabclose +2-', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100553 call assert_equal(6, tabpagenr('$'))
554
555 1tabonly!
556endfunction
557
558" Test for [count] of tabonly
559function Test_tabpage_with_tabonly()
560
561 " Test for the normal behavior (pre count only)
562 let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
563 for c in tc
564 call s:reconstruct_tabpage_for_test(6)
565 let entire_cmd = c[1] . 'tabonly' . c[2]
566 call Check_tab_count(c[0], entire_cmd, 1)
567 call assert_equal(1, tabpagenr('$'))
568 endfor
569
570 " Test for the normal behavior
571 let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
572 \ [2, '', '!'],
573 \ [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
574 \ ]
575 for n in range(2)
576 for c in tc2
577 call s:reconstruct_tabpage_for_test(6)
578 if n == 0 " pre count
579 let entire_cmd = c[1] . 'tabonly' . c[2]
580 else
581 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
582 endif
583 call Check_tab_count(c[0], entire_cmd, 1)
584 call assert_equal(1, tabpagenr('$'))
585 endfor
586 endfor
587
588 " Test for the error behavior
589 for n in range(2)
590 for c in ['0', '$3', '99', '+99', '-99']
591 call s:reconstruct_tabpage_for_test(6)
592 if n == 0 " pre count
593 let entire_cmd = c . 'tabonly'
594 let err_code = 'E16:'
595 else
596 let entire_cmd = 'tabonly ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200597 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100598 endif
599 call assert_fails(entire_cmd, err_code)
600 call assert_equal(6, tabpagenr('$'))
601 endfor
602 endfor
603
604 " Test for the error behavior (post count only)
605 for c in tc
606 call s:reconstruct_tabpage_for_test(6)
607 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200608 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100609 call assert_fails(entire_cmd, err_code)
610 call assert_equal(6, tabpagenr('$'))
611 endfor
612
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200613 call assert_fails('tabonly -+', 'E475:')
614 call assert_fails('tabonly +2-', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100615 call assert_equal(6, tabpagenr('$'))
616
617 1tabonly!
618 new
619 only!
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200620endfunction
621
Bram Moolenaar5a497892016-09-03 16:29:04 +0200622func Test_tabnext_on_buf_unload1()
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200623 " This once caused a crash
624 new
625 tabedit
626 tabfirst
627 au BufUnload <buffer> tabnext
628 q
629
630 while tabpagenr('$') > 1
Bram Moolenaar5a497892016-09-03 16:29:04 +0200631 bwipe!
632 endwhile
633endfunc
634
635func Test_tabnext_on_buf_unload2()
636 " This once caused a crash
637 tabedit
638 autocmd BufUnload <buffer> tabnext
639 file x
640 edit y
641
642 while tabpagenr('$') > 1
643 bwipe!
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200644 endwhile
645endfunc
646
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200647func Test_close_on_quitpre()
648 " This once caused a crash
Bram Moolenaarce11de82017-10-26 22:00:00 +0200649 edit Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200650 new
651 only
652 set bufhidden=delete
653 au QuitPre <buffer> close
654 tabnew tab1
655 tabnew tab2
656 1tabn
657 q!
658 call assert_equal(1, tabpagenr())
659 call assert_equal(2, tabpagenr('$'))
660 " clean up
661 while tabpagenr('$') > 1
662 bwipe!
663 endwhile
Bram Moolenaarce11de82017-10-26 22:00:00 +0200664 buf Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200665endfunc
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200666
Bram Moolenaardbe88692018-05-20 14:57:22 +0200667func Test_tabs()
668 enew!
669 tabnew tab1
670 norm ixxx
671 let a=split(execute(':tabs'), "\n")
672 call assert_equal(['Tab page 1',
673 \ ' [No Name]',
674 \ 'Tab page 2',
675 \ '> + tab1'], a)
676
677 1tabonly!
678 bw!
679endfunc
680
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200681func Test_tabpage_cmdheight()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200682 CheckRunVimInTerminal
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200683 call writefile([
684 \ 'set laststatus=2',
685 \ 'set cmdheight=2',
686 \ 'tabnew',
687 \ 'set cmdheight=3',
688 \ 'tabnext',
689 \ 'redraw!',
690 \ 'echo "hello\nthere"',
691 \ 'tabnext',
692 \ 'redraw',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100693 \ ], 'XTest_tabpage_cmdheight', 'D')
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200694 " Check that cursor line is concealed
695 let buf = RunVimInTerminal('-S XTest_tabpage_cmdheight', {'statusoff': 3})
696 call VerifyScreenDump(buf, 'Test_tabpage_cmdheight', {})
697
698 call StopVimInTerminal(buf)
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200699endfunc
700
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100701" Test for closing the tab page from a command window
702func Test_tabpage_close_cmdwin()
703 tabnew
704 call feedkeys("q/:tabclose\<CR>\<Esc>", 'xt')
705 call assert_equal(2, tabpagenr('$'))
706 call feedkeys("q/:tabonly\<CR>\<Esc>", 'xt')
707 call assert_equal(2, tabpagenr('$'))
708 tabonly
709endfunc
710
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200711" Pressing <C-PageUp> in insert mode should go to the previous tab page
712" and <C-PageDown> should go to the next tab page
713func Test_tabpage_Ctrl_Pageup()
714 tabnew
715 call feedkeys("i\<C-PageUp>", 'xt')
716 call assert_equal(1, tabpagenr())
717 call feedkeys("i\<C-PageDown>", 'xt')
718 call assert_equal(2, tabpagenr())
719 %bw!
720endfunc
721
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200722" Return the terminal key code for selecting a tab page from the tabline. This
723" sequence contains the following codes: a CSI (0x9b), KS_TABLINE (0xf0),
724" KS_FILLER (0x58) and then the tab page number.
725func TabLineSelectPageCode(tabnr)
726 return "\x9b\xf0\x58" .. nr2char(a:tabnr)
727endfunc
728
729" Return the terminal key code for opening a new tabpage from the tabpage
730" menu. This sequence consists of the following codes: a CSI (0x9b),
731" KS_TABMENU (0xef), KS_FILLER (0x58), the tab page number and
732" TABLINE_MENU_NEW (2).
733func TabMenuNewItemCode(tabnr)
734 return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(2)
735endfunc
736
737" Return the terminal key code for closing a tabpage from the tabpage menu.
738" This sequence consists of the following codes: a CSI (0x9b), KS_TABMENU
739" (0xef), KS_FILLER (0x58), the tab page number and TABLINE_MENU_CLOSE (1).
740func TabMenuCloseItemCode(tabnr)
741 return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(1)
742endfunc
743
744" Test for using the tabpage menu from the insert and normal modes
745func Test_tabline_tabmenu()
746 " only works in GUI
747 CheckGui
748
749 %bw!
750 tabnew
751 tabnew
752 call assert_equal(3, tabpagenr())
753
754 " go to tab page 2 in normal mode
755 call feedkeys(TabLineSelectPageCode(2), "Lx!")
756 call assert_equal(2, tabpagenr())
757
758 " close tab page 3 in normal mode
759 call feedkeys(TabMenuCloseItemCode(3), "Lx!")
760 call assert_equal(2, tabpagenr('$'))
761 call assert_equal(2, tabpagenr())
762
763 " open new tab page before tab page 1 in normal mode
764 call feedkeys(TabMenuNewItemCode(1), "Lx!")
765 call assert_equal(1, tabpagenr())
766 call assert_equal(3, tabpagenr('$'))
767
768 " go to tab page 2 in operator-pending mode (should beep)
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200769 call assert_beeps('call feedkeys("c" .. TabLineSelectPageCode(2), "Lx!")')
770 call assert_equal(2, tabpagenr())
771 call assert_equal(3, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200772
773 " open new tab page before tab page 1 in operator-pending mode (should beep)
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200774 call assert_beeps('call feedkeys("c" .. TabMenuNewItemCode(1), "Lx!")')
775 call assert_equal(1, tabpagenr())
776 call assert_equal(4, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200777
778 " open new tab page after tab page 3 in normal mode
779 call feedkeys(TabMenuNewItemCode(4), "Lx!")
780 call assert_equal(4, tabpagenr())
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200781 call assert_equal(5, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200782
783 " go to tab page 2 in insert mode
784 call feedkeys("i" .. TabLineSelectPageCode(2) .. "\<C-C>", "Lx!")
785 call assert_equal(2, tabpagenr())
786
787 " close tab page 2 in insert mode
788 call feedkeys("i" .. TabMenuCloseItemCode(2) .. "\<C-C>", "Lx!")
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200789 call assert_equal(4, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200790
791 " open new tab page before tab page 3 in insert mode
792 call feedkeys("i" .. TabMenuNewItemCode(3) .. "\<C-C>", "Lx!")
793 call assert_equal(3, tabpagenr())
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200794 call assert_equal(5, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200795
796 " open new tab page after tab page 4 in insert mode
797 call feedkeys("i" .. TabMenuNewItemCode(5) .. "\<C-C>", "Lx!")
798 call assert_equal(5, tabpagenr())
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200799 call assert_equal(6, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200800
801 %bw!
802endfunc
803
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200804" Test for changing the current tab page from an autocmd when closing a tab
805" page.
806func Test_tabpage_switchtab_on_close()
807 only
808 tabnew
809 tabnew
810 " Test for BufLeave
811 augroup T1
812 au!
813 au BufLeave * tabfirst
814 augroup END
815 tabclose
816 call assert_equal(1, tabpagenr())
817 augroup T1
818 au!
819 augroup END
820
821 " Test for WinLeave
822 $tabnew
823 augroup T1
824 au!
825 au WinLeave * tabfirst
826 augroup END
827 tabclose
828 call assert_equal(1, tabpagenr())
829 augroup T1
830 au!
831 augroup END
832
833 " Test for TabLeave
834 $tabnew
835 augroup T1
836 au!
837 au TabLeave * tabfirst
838 augroup END
839 tabclose
840 call assert_equal(1, tabpagenr())
841 augroup T1
842 au!
843 augroup END
844 augroup! T1
845 tabonly
846endfunc
847
848" Test for closing the destination tabpage when jumping from one to another.
849func Test_tabpage_close_on_switch()
850 tabnew
851 tabnew
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100852 edit Xtabfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200853 augroup T2
854 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100855 au BufLeave Xtabfile 1tabclose
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200856 augroup END
857 tabfirst
858 call assert_equal(2, tabpagenr())
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100859 call assert_equal('Xtabfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200860 augroup T2
861 au!
862 augroup END
863 augroup! T2
864 %bw!
865endfunc
866
Bram Moolenaar62a23252020-08-09 14:04:42 +0200867" Test for jumping to last accessed tabpage
868func Test_lastused_tabpage()
869 tabonly!
870 call assert_equal(0, tabpagenr('#'))
871 call assert_beeps('call feedkeys("g\<Tab>", "xt")')
872 call assert_beeps('call feedkeys("\<C-Tab>", "xt")')
873 call assert_beeps('call feedkeys("\<C-W>g\<Tab>", "xt")')
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200874 call assert_fails('tabnext #', 'E475:')
Bram Moolenaar62a23252020-08-09 14:04:42 +0200875
876 " open four tab pages
877 tabnew
878 tabnew
879 tabnew
880
881 2tabnext
882
883 " Test for g<Tab>
884 call assert_equal(4, tabpagenr('#'))
885 call feedkeys("g\<Tab>", "xt")
886 call assert_equal(4, tabpagenr())
887 call assert_equal(2, tabpagenr('#'))
888
889 " Test for <C-Tab>
890 call feedkeys("\<C-Tab>", "xt")
891 call assert_equal(2, tabpagenr())
892 call assert_equal(4, tabpagenr('#'))
893
894 " Test for <C-W>g<Tab>
895 call feedkeys("\<C-W>g\<Tab>", "xt")
896 call assert_equal(4, tabpagenr())
897 call assert_equal(2, tabpagenr('#'))
898
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200899 " Test for :tabnext #
900 tabnext #
901 call assert_equal(2, tabpagenr())
902 call assert_equal(4, tabpagenr('#'))
903
Bram Moolenaar62a23252020-08-09 14:04:42 +0200904 " Try to jump to a closed tab page
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200905 tabclose #
Bram Moolenaar62a23252020-08-09 14:04:42 +0200906 call assert_equal(0, tabpagenr('#'))
907 call feedkeys("g\<Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200908 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200909 call feedkeys("\<C-Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200910 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200911 call feedkeys("\<C-W>g\<Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200912 call assert_equal(2, tabpagenr())
913 call assert_fails('tabnext #', 'E475:')
914 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200915
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200916 " Test for :tabonly #
917 let wnum = win_getid()
918 $tabnew
919 tabonly #
920 call assert_equal(wnum, win_getid())
921 call assert_equal(1, tabpagenr('$'))
922
923 " Test for :tabmove #
924 tabnew
925 let wnum = win_getid()
926 tabnew
927 tabnew
928 tabnext 2
929 tabmove #
930 call assert_equal(4, tabpagenr())
931 call assert_equal(wnum, win_getid())
932
933 tabonly!
Bram Moolenaar62a23252020-08-09 14:04:42 +0200934endfunc
935
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100936" Test for tabpage allocation failure
937func Test_tabpage_alloc_failure()
938 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
939 call assert_fails('tabnew', 'E342:')
940
941 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
942 edit Xfile1
943 call assert_fails('tabedit Xfile2', 'E342:')
944 call assert_equal(1, winnr('$'))
945 call assert_equal(1, tabpagenr('$'))
946 call assert_equal('Xfile1', @%)
947
948 new
949 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
950 call assert_fails('wincmd T', 'E342:')
951 bw!
952
953 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
954 call assert_fails('tab split', 'E342:')
955 call assert_equal(2, winnr('$'))
956 call assert_equal(1, tabpagenr('$'))
957endfunc
958
Bram Moolenaar99ad3a82023-02-27 17:18:01 +0000959" this was giving ml_get errors
960func Test_tabpage_last_line()
961 enew
962 call setline(1, repeat(['a'], &lines + 5))
963 $
964 tabnew
965 call setline(1, repeat(['b'], &lines + 20))
966 $
967 tabNext
968 call assert_equal('a', getline('.'))
969
970 bwipe!
971 bwipe!
972endfunc
973
Christian Brabandtdf12e392023-12-16 13:55:32 +0100974" this was causing an endless loop
975func Test_tabpage_drop_tabmove()
976 augroup TestTabpageTabmove
977 au!
978 autocmd! TabEnter * :if tabpagenr() > 1 | tabmove - | endif
979 augroup end
980 $tab drop XTab_99.log
981 $tab drop XTab_98.log
982 $tab drop XTab_97.log
983
984 autocmd! TestTabpageTabmove
985 augroup! TestTabpageTabmove
986
987 " clean up
988 bwipe!
989 bwipe!
990 bwipe!
991endfunc
992
Bram Moolenaar1381d792016-08-18 22:11:42 +0200993" vim: shiftwidth=2 sts=2 expandtab