blob: 94f695323fee50751c6d541f3c819ca9da9754e4 [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())
glepnir2975a542024-02-09 19:30:26 +0100159 tab drop f4
160 call assert_equal(1, tabpagenr('#'))
Bram Moolenaar8281a162023-04-20 18:07:57 +0100161
162 tab drop f3
glepnir2975a542024-02-09 19:30:26 +0100163 call assert_equal(4, tabpagenr())
164 call assert_equal(2, tabpagenr('#'))
165 bwipe!
Bram Moolenaar8281a162023-04-20 18:07:57 +0100166 bwipe!
167 bwipe!
168 bwipe!
169 call assert_equal(1, tabpagenr('$'))
Rocco Maof96dc8d2024-01-23 21:27:19 +0100170
171 call assert_equal(1, winnr('$'))
172 call assert_equal('', bufname(''))
173 call writefile(['L1', 'L2'], 'Xdropfile', 'D')
174
175 " Test for ':tab drop single-file': reuse current buffer
176 let expected_nr = bufnr()
177 tab drop Xdropfile
178 call assert_equal(1, tabpagenr('$'))
179 call assert_equal(expected_nr, bufnr())
180 call assert_equal('L2', getline(2))
181 bwipe!
182
183 " Test for ':tab drop single-file': not reuse modified buffer
184 set modified
185 let expected_nr = bufnr() + 1
186 tab drop Xdropfile
187 call assert_equal(2, tabpagenr())
188 call assert_equal(2, tabpagenr('$'))
189 call assert_equal(expected_nr, bufnr())
190 call assert_equal('L2', getline(2))
191 bwipe!
192
193 " Test for ':tab drop single-file': multiple tabs already exist
194 tab split f2
195 tab split f3
196 let expected_nr = bufnr() + 1
197 tab drop Xdropfile
198 call assert_equal(4, tabpagenr())
199 call assert_equal(4, tabpagenr('$'))
200 call assert_equal(expected_nr, bufnr())
201 call assert_equal('L2', getline(2))
202 %bwipe!
203
204 " Test for ':tab drop multi-files': reuse current buffer
205 let expected_nr = bufnr()
206 tab drop Xdropfile f1 f2 f3
207 call assert_equal(1, tabpagenr())
208 call assert_equal(4, tabpagenr('$'))
209 call assert_equal(expected_nr, bufnr())
210 call assert_equal('L2', getline(2))
211 %bwipe!
212
213 " Test for ':tab drop multi-files': not reuse modified buffer
214 set modified
215 let expected_nr = bufnr() + 1
216 tab drop Xdropfile f1 f2 f3
217 call assert_equal(2, tabpagenr())
218 call assert_equal(5, tabpagenr('$'))
219 call assert_equal(expected_nr, bufnr())
220 call assert_equal('L2', getline(2))
221 %bwipe!
222
223 " Test for ':tab drop multi-files': multiple tabs already exist
224 tab split f2
225 tab split f3
226 let expected_nr = bufnr() + 1
227 tab drop a b c
228 call assert_equal(4, tabpagenr())
229 call assert_equal(6, tabpagenr('$'))
230 call assert_equal(expected_nr, bufnr())
231 let expected_nr = bufnr() + 3
232 tab drop Xdropfile f1 f2 f3
233 call assert_equal(5, tabpagenr())
234 call assert_equal(8, tabpagenr('$'))
235 call assert_equal(expected_nr, bufnr())
236 call assert_equal('L2', getline(2))
237 %bwipe!
Bram Moolenaar8281a162023-04-20 18:07:57 +0100238endfunc
239
Bram Moolenaar1381d792016-08-18 22:11:42 +0200240" Test autocommands
241function Test_tabpage_with_autocmd()
Bram Moolenaar1381d792016-08-18 22:11:42 +0200242 command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
243 augroup TestTabpageGroup
244 au!
245 autocmd TabEnter * call add(s:li, 'TabEnter')
246 autocmd WinEnter * call add(s:li, 'WinEnter')
247 autocmd BufEnter * call add(s:li, 'BufEnter')
248 autocmd TabLeave * call add(s:li, 'TabLeave')
249 autocmd WinLeave * call add(s:li, 'WinLeave')
250 autocmd BufLeave * call add(s:li, 'BufLeave')
251 augroup END
252
253 let s:li = []
254 let t:a='a'
255 C tab split
256 call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
257 let s:li = []
258 let t:a='b'
259 C tabnew
260 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
261 let t:a='c'
262 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
263 call assert_equal(['a', 'b', 'c'], s:li)
264
265 let s:li = []
266 C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
267 call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
268 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
269 call assert_equal(['2', '4', '6'], s:li)
270
271 let s:li = []
272 let w:a='a'
273 C vsplit
274 call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
275 let s:li = []
276 let w:a='a'
277 let tabn=tabpagenr()
278 let winr=range(1, winnr('$'))
279 C tabnext 1
280 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
281 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
282 call assert_equal(['a', 'a'], s:li)
283 let s:li = []
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200284 C call map(copy(winr), '(v:val*2)->settabwinvar(' .. tabn .. ', v:val, ''a'')')
Bram Moolenaar1381d792016-08-18 22:11:42 +0200285 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
286 call assert_equal(['2', '4'], s:li)
287
288 augroup TabDestructive
289 autocmd TabEnter * :C tabnext 2 | C tabclose 3
290 augroup END
291 let s:li = []
292 C tabnext 3
293 call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
294 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
295
296 autocmd! TabDestructive TabEnter
297 let s:li = []
298 C tabnew
299 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
300 let s:li = []
301 C tabnext 1
302 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
303
304 autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
305 let s:li = []
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100306 call assert_equal(3, tabpagenr('$'))
307 C tabnext 2
308 call assert_equal(2, tabpagenr('$'))
309 call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
Bram Moolenaar1381d792016-08-18 22:11:42 +0200310 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
311
312 delcommand C
313 autocmd! TabDestructive
314 augroup! TabDestructive
315 autocmd! TestTabpageGroup
316 augroup! TestTabpageGroup
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100317 1tabonly!
Bram Moolenaar1381d792016-08-18 22:11:42 +0200318endfunction
319
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100320" Test autocommands on tab drop
321function Test_tabpage_with_autocmd_tab_drop()
322 augroup TestTabpageGroup
323 au!
324 autocmd TabEnter * call add(s:li, 'TabEnter')
325 autocmd WinEnter * call add(s:li, 'WinEnter')
326 autocmd BufEnter * call add(s:li, 'BufEnter')
327 autocmd TabLeave * call add(s:li, 'TabLeave')
328 autocmd WinLeave * call add(s:li, 'WinLeave')
329 autocmd BufLeave * call add(s:li, 'BufLeave')
330 augroup END
331
332 let s:li = []
333 tab drop test1
Rocco Maof96dc8d2024-01-23 21:27:19 +0100334 call assert_equal(['BufEnter'], s:li)
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100335
336 let s:li = []
337 tab drop test2 test3
338 call assert_equal([
339 \ 'TabLeave', 'TabEnter', 'TabLeave', 'TabEnter',
340 \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter',
Rocco Maof96dc8d2024-01-23 21:27:19 +0100341 \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter', 'BufEnter'], s:li)
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100342
343 autocmd! TestTabpageGroup
344 augroup! TestTabpageGroup
345 1tabonly!
346endfunction
347
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200348function Test_tabpage_with_tab_modifier()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100349 CheckFeature quickfix
350
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200351 for n in range(4)
352 tabedit
353 endfor
354
355 function s:check_tab(pre_nr, cmd, post_nr)
356 exec 'tabnext ' . a:pre_nr
357 exec a:cmd
358 call assert_equal(a:post_nr, tabpagenr())
Bram Moolenaar100f5c92016-09-06 21:33:52 +0200359 call assert_equal('help', &buftype)
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200360 helpclose
361 endfunc
362
363 call s:check_tab(1, 'tab help', 2)
364 call s:check_tab(1, '3tab help', 4)
365 call s:check_tab(1, '.tab help', 2)
366 call s:check_tab(1, '.+1tab help', 3)
367 call s:check_tab(1, '0tab help', 1)
368 call s:check_tab(2, '+tab help', 4)
369 call s:check_tab(2, '+2tab help', 5)
370 call s:check_tab(4, '-tab help', 4)
371 call s:check_tab(4, '-2tab help', 3)
372 call s:check_tab(3, '$tab help', 6)
373 call assert_fails('99tab help', 'E16:')
374 call assert_fails('+99tab help', 'E16:')
375 call assert_fails('-99tab help', 'E16:')
376
377 delfunction s:check_tab
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100378 1tabonly!
379endfunction
380
381function Check_tab_count(pre_nr, cmd, post_nr)
382 exec 'tabnext' a:pre_nr
383 normal! G
384 exec a:cmd
385 call assert_equal(a:post_nr, tabpagenr(), a:cmd)
386endfunc
387
388" Test for [count] of tabnext
389function Test_tabpage_with_tabnext()
390 for n in range(4)
391 tabedit
392 call setline(1, ['', '', '3'])
393 endfor
394
395 call Check_tab_count(1, 'tabnext', 2)
396 call Check_tab_count(1, '3tabnext', 3)
397 call Check_tab_count(1, '.tabnext', 1)
398 call Check_tab_count(1, '.+1tabnext', 2)
399 call Check_tab_count(2, '+tabnext', 3)
400 call Check_tab_count(2, '+2tabnext', 4)
401 call Check_tab_count(4, '-tabnext', 3)
402 call Check_tab_count(4, '-2tabnext', 2)
403 call Check_tab_count(3, '$tabnext', 5)
404 call assert_fails('0tabnext', 'E16:')
405 call assert_fails('99tabnext', 'E16:')
406 call assert_fails('+99tabnext', 'E16:')
407 call assert_fails('-99tabnext', 'E16:')
408 call Check_tab_count(1, 'tabnext 3', 3)
409 call Check_tab_count(2, 'tabnext +', 3)
410 call Check_tab_count(2, 'tabnext +2', 4)
411 call Check_tab_count(4, 'tabnext -', 3)
412 call Check_tab_count(4, 'tabnext -2', 2)
413 call Check_tab_count(3, 'tabnext $', 5)
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200414 call assert_fails('tabnext 0', 'E475:')
415 call assert_fails('tabnext .', 'E475:')
416 call assert_fails('tabnext -+', 'E475:')
417 call assert_fails('tabnext +2-', 'E475:')
418 call assert_fails('tabnext $3', 'E475:')
419 call assert_fails('tabnext 99', 'E475:')
420 call assert_fails('tabnext +99', 'E475:')
421 call assert_fails('tabnext -99', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100422
423 1tabonly!
424endfunction
425
426" Test for [count] of tabprevious
427function Test_tabpage_with_tabprevious()
428 for n in range(5)
429 tabedit
430 call setline(1, ['', '', '3'])
431 endfor
432
433 for cmd in ['tabNext', 'tabprevious']
434 call Check_tab_count(6, cmd, 5)
435 call Check_tab_count(6, '3' . cmd, 3)
436 call Check_tab_count(6, '8' . cmd, 4)
437 call Check_tab_count(6, cmd . ' 3', 3)
438 call Check_tab_count(6, cmd . ' 8', 4)
439 for n in range(2)
Bram Moolenaar9d489562020-07-30 20:08:50 +0200440 for c in ['0', '.+3', '+', '+2', '-', '-2', '$', '+99', '-99']
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100441 if n == 0 " pre count
442 let entire_cmd = c . cmd
443 let err_code = 'E16:'
444 else
445 let entire_cmd = cmd . ' ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200446 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100447 endif
448 call assert_fails(entire_cmd, err_code)
449 endfor
450 endfor
451 endfor
452
453 1tabonly!
454endfunction
455
456function s:reconstruct_tabpage_for_test(nr)
457 let n = (a:nr > 2) ? a:nr - 2 : 1
458 1tabonly!
459 0tabedit n0
460 for n in range(1, n)
461 exec '$tabedit n' . n
462 if n == 1
463 call setline(1, ['', '', '3'])
464 endif
465 endfor
466endfunc
467
Bram Moolenaardbe88692018-05-20 14:57:22 +0200468func Test_tabpage_ctrl_pgup_pgdown()
469 enew!
470 tabnew tab1
471 tabnew tab2
472
473 call assert_equal(3, tabpagenr())
474 exe "norm! \<C-PageUp>"
475 call assert_equal(2, tabpagenr())
476 exe "norm! \<C-PageDown>"
477 call assert_equal(3, tabpagenr())
478
479 " Check wrapping at last or first page.
480 exe "norm! \<C-PageDown>"
481 call assert_equal(1, tabpagenr())
482 exe "norm! \<C-PageUp>"
483 call assert_equal(3, tabpagenr())
484
485 " With a count, <C-PageUp> and <C-PageDown> are not symmetrical somehow:
486 " - {count}<C-PageUp> goes {count} pages downward (relative count)
487 " - {count}<C-PageDown> goes to page number {count} (absolute count)
488 exe "norm! 2\<C-PageUp>"
489 call assert_equal(1, tabpagenr())
490 exe "norm! 2\<C-PageDown>"
491 call assert_equal(2, tabpagenr())
492
493 1tabonly!
494endfunc
495
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100496" Test for [count] of tabclose
497function Test_tabpage_with_tabclose()
498
499 " pre count
500 call s:reconstruct_tabpage_for_test(6)
501 call Check_tab_count(3, 'tabclose!', 3)
502 call Check_tab_count(1, '3tabclose', 1)
503 call Check_tab_count(4, '4tabclose', 3)
504 call Check_tab_count(3, '1tabclose', 2)
505 call Check_tab_count(2, 'tabclose', 1)
506 call assert_equal(1, tabpagenr('$'))
507 call assert_equal('', bufname(''))
508
509 call s:reconstruct_tabpage_for_test(6)
510 call Check_tab_count(2, '$tabclose', 2)
511 call Check_tab_count(4, '.tabclose', 4)
512 call Check_tab_count(3, '.+tabclose', 3)
513 call Check_tab_count(3, '.-2tabclose', 2)
514 call Check_tab_count(1, '.+1tabclose!', 1)
515 call assert_equal(1, tabpagenr('$'))
516 call assert_equal('', bufname(''))
517
518 " post count
519 call s:reconstruct_tabpage_for_test(6)
520 call Check_tab_count(3, 'tabclose!', 3)
521 call Check_tab_count(1, 'tabclose 3', 1)
522 call Check_tab_count(4, 'tabclose 4', 3)
523 call Check_tab_count(3, 'tabclose 1', 2)
524 call Check_tab_count(2, 'tabclose', 1)
525 call assert_equal(1, tabpagenr('$'))
526 call assert_equal('', bufname(''))
527
528 call s:reconstruct_tabpage_for_test(6)
529 call Check_tab_count(2, 'tabclose $', 2)
530 call Check_tab_count(4, 'tabclose', 4)
531 call Check_tab_count(3, 'tabclose +', 3)
532 call Check_tab_count(3, 'tabclose -2', 2)
533 call Check_tab_count(1, 'tabclose! +1', 1)
534 call assert_equal(1, tabpagenr('$'))
535 call assert_equal('', bufname(''))
536
537 call s:reconstruct_tabpage_for_test(6)
538 for n in range(2)
539 for c in ['0', '$3', '99', '+99', '-99']
540 if n == 0 " pre count
541 let entire_cmd = c . 'tabclose'
542 let err_code = 'E16:'
543 else
544 let entire_cmd = 'tabclose ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200545 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100546 endif
547 call assert_fails(entire_cmd, err_code)
548 call assert_equal(6, tabpagenr('$'))
549 endfor
550 endfor
551
552 call assert_fails('3tabclose', 'E37:')
553 call assert_fails('tabclose 3', 'E37:')
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200554 call assert_fails('tabclose -+', 'E475:')
555 call assert_fails('tabclose +2-', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100556 call assert_equal(6, tabpagenr('$'))
557
558 1tabonly!
559endfunction
560
561" Test for [count] of tabonly
562function Test_tabpage_with_tabonly()
563
564 " Test for the normal behavior (pre count only)
565 let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
566 for c in tc
567 call s:reconstruct_tabpage_for_test(6)
568 let entire_cmd = c[1] . 'tabonly' . c[2]
569 call Check_tab_count(c[0], entire_cmd, 1)
570 call assert_equal(1, tabpagenr('$'))
571 endfor
572
573 " Test for the normal behavior
574 let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
575 \ [2, '', '!'],
576 \ [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
577 \ ]
578 for n in range(2)
579 for c in tc2
580 call s:reconstruct_tabpage_for_test(6)
581 if n == 0 " pre count
582 let entire_cmd = c[1] . 'tabonly' . c[2]
583 else
584 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
585 endif
586 call Check_tab_count(c[0], entire_cmd, 1)
587 call assert_equal(1, tabpagenr('$'))
588 endfor
589 endfor
590
591 " Test for the error behavior
592 for n in range(2)
593 for c in ['0', '$3', '99', '+99', '-99']
594 call s:reconstruct_tabpage_for_test(6)
595 if n == 0 " pre count
596 let entire_cmd = c . 'tabonly'
597 let err_code = 'E16:'
598 else
599 let entire_cmd = 'tabonly ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200600 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100601 endif
602 call assert_fails(entire_cmd, err_code)
603 call assert_equal(6, tabpagenr('$'))
604 endfor
605 endfor
606
607 " Test for the error behavior (post count only)
608 for c in tc
609 call s:reconstruct_tabpage_for_test(6)
610 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200611 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100612 call assert_fails(entire_cmd, err_code)
613 call assert_equal(6, tabpagenr('$'))
614 endfor
615
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200616 call assert_fails('tabonly -+', 'E475:')
617 call assert_fails('tabonly +2-', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100618 call assert_equal(6, tabpagenr('$'))
619
620 1tabonly!
621 new
622 only!
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200623endfunction
624
Bram Moolenaar5a497892016-09-03 16:29:04 +0200625func Test_tabnext_on_buf_unload1()
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200626 " This once caused a crash
627 new
628 tabedit
629 tabfirst
630 au BufUnload <buffer> tabnext
631 q
632
633 while tabpagenr('$') > 1
Bram Moolenaar5a497892016-09-03 16:29:04 +0200634 bwipe!
635 endwhile
636endfunc
637
638func Test_tabnext_on_buf_unload2()
639 " This once caused a crash
640 tabedit
641 autocmd BufUnload <buffer> tabnext
642 file x
643 edit y
644
645 while tabpagenr('$') > 1
646 bwipe!
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200647 endwhile
648endfunc
649
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200650func Test_close_on_quitpre()
651 " This once caused a crash
Bram Moolenaarce11de82017-10-26 22:00:00 +0200652 edit Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200653 new
654 only
655 set bufhidden=delete
656 au QuitPre <buffer> close
657 tabnew tab1
658 tabnew tab2
659 1tabn
660 q!
661 call assert_equal(1, tabpagenr())
662 call assert_equal(2, tabpagenr('$'))
663 " clean up
664 while tabpagenr('$') > 1
665 bwipe!
666 endwhile
Bram Moolenaarce11de82017-10-26 22:00:00 +0200667 buf Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200668endfunc
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200669
Bram Moolenaardbe88692018-05-20 14:57:22 +0200670func Test_tabs()
671 enew!
672 tabnew tab1
673 norm ixxx
674 let a=split(execute(':tabs'), "\n")
675 call assert_equal(['Tab page 1',
676 \ ' [No Name]',
677 \ 'Tab page 2',
678 \ '> + tab1'], a)
679
680 1tabonly!
681 bw!
682endfunc
683
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200684func Test_tabpage_cmdheight()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200685 CheckRunVimInTerminal
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200686 call writefile([
687 \ 'set laststatus=2',
688 \ 'set cmdheight=2',
689 \ 'tabnew',
690 \ 'set cmdheight=3',
691 \ 'tabnext',
692 \ 'redraw!',
693 \ 'echo "hello\nthere"',
694 \ 'tabnext',
695 \ 'redraw',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100696 \ ], 'XTest_tabpage_cmdheight', 'D')
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200697 " Check that cursor line is concealed
698 let buf = RunVimInTerminal('-S XTest_tabpage_cmdheight', {'statusoff': 3})
699 call VerifyScreenDump(buf, 'Test_tabpage_cmdheight', {})
700
701 call StopVimInTerminal(buf)
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200702endfunc
703
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100704" Test for closing the tab page from a command window
705func Test_tabpage_close_cmdwin()
706 tabnew
707 call feedkeys("q/:tabclose\<CR>\<Esc>", 'xt')
708 call assert_equal(2, tabpagenr('$'))
709 call feedkeys("q/:tabonly\<CR>\<Esc>", 'xt')
710 call assert_equal(2, tabpagenr('$'))
711 tabonly
712endfunc
713
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200714" Pressing <C-PageUp> in insert mode should go to the previous tab page
715" and <C-PageDown> should go to the next tab page
716func Test_tabpage_Ctrl_Pageup()
717 tabnew
718 call feedkeys("i\<C-PageUp>", 'xt')
719 call assert_equal(1, tabpagenr())
720 call feedkeys("i\<C-PageDown>", 'xt')
721 call assert_equal(2, tabpagenr())
722 %bw!
723endfunc
724
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200725" Return the terminal key code for selecting a tab page from the tabline. This
726" sequence contains the following codes: a CSI (0x9b), KS_TABLINE (0xf0),
727" KS_FILLER (0x58) and then the tab page number.
728func TabLineSelectPageCode(tabnr)
729 return "\x9b\xf0\x58" .. nr2char(a:tabnr)
730endfunc
731
732" Return the terminal key code for opening a new tabpage from the tabpage
733" menu. This sequence consists of the following codes: a CSI (0x9b),
734" KS_TABMENU (0xef), KS_FILLER (0x58), the tab page number and
735" TABLINE_MENU_NEW (2).
736func TabMenuNewItemCode(tabnr)
737 return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(2)
738endfunc
739
740" Return the terminal key code for closing a tabpage from the tabpage menu.
741" This sequence consists of the following codes: a CSI (0x9b), KS_TABMENU
742" (0xef), KS_FILLER (0x58), the tab page number and TABLINE_MENU_CLOSE (1).
743func TabMenuCloseItemCode(tabnr)
744 return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(1)
745endfunc
746
747" Test for using the tabpage menu from the insert and normal modes
748func Test_tabline_tabmenu()
749 " only works in GUI
750 CheckGui
751
752 %bw!
753 tabnew
754 tabnew
755 call assert_equal(3, tabpagenr())
756
757 " go to tab page 2 in normal mode
758 call feedkeys(TabLineSelectPageCode(2), "Lx!")
759 call assert_equal(2, tabpagenr())
760
761 " close tab page 3 in normal mode
762 call feedkeys(TabMenuCloseItemCode(3), "Lx!")
763 call assert_equal(2, tabpagenr('$'))
764 call assert_equal(2, tabpagenr())
765
766 " open new tab page before tab page 1 in normal mode
767 call feedkeys(TabMenuNewItemCode(1), "Lx!")
768 call assert_equal(1, tabpagenr())
769 call assert_equal(3, tabpagenr('$'))
770
771 " go to tab page 2 in operator-pending mode (should beep)
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200772 call assert_beeps('call feedkeys("c" .. TabLineSelectPageCode(2), "Lx!")')
773 call assert_equal(2, tabpagenr())
774 call assert_equal(3, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200775
776 " open new tab page before tab page 1 in operator-pending mode (should beep)
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200777 call assert_beeps('call feedkeys("c" .. TabMenuNewItemCode(1), "Lx!")')
778 call assert_equal(1, tabpagenr())
779 call assert_equal(4, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200780
781 " open new tab page after tab page 3 in normal mode
782 call feedkeys(TabMenuNewItemCode(4), "Lx!")
783 call assert_equal(4, tabpagenr())
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200784 call assert_equal(5, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200785
786 " go to tab page 2 in insert mode
787 call feedkeys("i" .. TabLineSelectPageCode(2) .. "\<C-C>", "Lx!")
788 call assert_equal(2, tabpagenr())
789
790 " close tab page 2 in insert mode
791 call feedkeys("i" .. TabMenuCloseItemCode(2) .. "\<C-C>", "Lx!")
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200792 call assert_equal(4, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200793
794 " open new tab page before tab page 3 in insert mode
795 call feedkeys("i" .. TabMenuNewItemCode(3) .. "\<C-C>", "Lx!")
796 call assert_equal(3, tabpagenr())
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200797 call assert_equal(5, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200798
799 " open new tab page after tab page 4 in insert mode
800 call feedkeys("i" .. TabMenuNewItemCode(5) .. "\<C-C>", "Lx!")
801 call assert_equal(5, tabpagenr())
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200802 call assert_equal(6, tabpagenr('$'))
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200803
804 %bw!
805endfunc
806
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200807" Test for changing the current tab page from an autocmd when closing a tab
808" page.
809func Test_tabpage_switchtab_on_close()
810 only
811 tabnew
812 tabnew
813 " Test for BufLeave
814 augroup T1
815 au!
816 au BufLeave * tabfirst
817 augroup END
818 tabclose
819 call assert_equal(1, tabpagenr())
820 augroup T1
821 au!
822 augroup END
823
824 " Test for WinLeave
825 $tabnew
826 augroup T1
827 au!
828 au WinLeave * tabfirst
829 augroup END
830 tabclose
831 call assert_equal(1, tabpagenr())
832 augroup T1
833 au!
834 augroup END
835
836 " Test for TabLeave
837 $tabnew
838 augroup T1
839 au!
840 au TabLeave * tabfirst
841 augroup END
842 tabclose
843 call assert_equal(1, tabpagenr())
844 augroup T1
845 au!
846 augroup END
847 augroup! T1
848 tabonly
849endfunc
850
851" Test for closing the destination tabpage when jumping from one to another.
852func Test_tabpage_close_on_switch()
853 tabnew
854 tabnew
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100855 edit Xtabfile
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200856 augroup T2
857 au!
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100858 au BufLeave Xtabfile 1tabclose
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200859 augroup END
860 tabfirst
861 call assert_equal(2, tabpagenr())
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100862 call assert_equal('Xtabfile', @%)
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200863 augroup T2
864 au!
865 augroup END
866 augroup! T2
867 %bw!
868endfunc
869
Bram Moolenaar62a23252020-08-09 14:04:42 +0200870" Test for jumping to last accessed tabpage
871func Test_lastused_tabpage()
872 tabonly!
873 call assert_equal(0, tabpagenr('#'))
874 call assert_beeps('call feedkeys("g\<Tab>", "xt")')
875 call assert_beeps('call feedkeys("\<C-Tab>", "xt")')
876 call assert_beeps('call feedkeys("\<C-W>g\<Tab>", "xt")')
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200877 call assert_fails('tabnext #', 'E475:')
Bram Moolenaar62a23252020-08-09 14:04:42 +0200878
879 " open four tab pages
880 tabnew
881 tabnew
882 tabnew
883
884 2tabnext
885
886 " Test for g<Tab>
887 call assert_equal(4, tabpagenr('#'))
888 call feedkeys("g\<Tab>", "xt")
889 call assert_equal(4, tabpagenr())
890 call assert_equal(2, tabpagenr('#'))
891
892 " Test for <C-Tab>
893 call feedkeys("\<C-Tab>", "xt")
894 call assert_equal(2, tabpagenr())
895 call assert_equal(4, tabpagenr('#'))
896
897 " Test for <C-W>g<Tab>
898 call feedkeys("\<C-W>g\<Tab>", "xt")
899 call assert_equal(4, tabpagenr())
900 call assert_equal(2, tabpagenr('#'))
901
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200902 " Test for :tabnext #
903 tabnext #
904 call assert_equal(2, tabpagenr())
905 call assert_equal(4, tabpagenr('#'))
906
Bram Moolenaar62a23252020-08-09 14:04:42 +0200907 " Try to jump to a closed tab page
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200908 tabclose #
Bram Moolenaar62a23252020-08-09 14:04:42 +0200909 call assert_equal(0, tabpagenr('#'))
910 call feedkeys("g\<Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200911 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200912 call feedkeys("\<C-Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200913 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200914 call feedkeys("\<C-W>g\<Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200915 call assert_equal(2, tabpagenr())
916 call assert_fails('tabnext #', 'E475:')
917 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200918
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200919 " Test for :tabonly #
920 let wnum = win_getid()
921 $tabnew
922 tabonly #
923 call assert_equal(wnum, win_getid())
924 call assert_equal(1, tabpagenr('$'))
925
926 " Test for :tabmove #
927 tabnew
928 let wnum = win_getid()
929 tabnew
930 tabnew
931 tabnext 2
932 tabmove #
933 call assert_equal(4, tabpagenr())
934 call assert_equal(wnum, win_getid())
935
936 tabonly!
Bram Moolenaar62a23252020-08-09 14:04:42 +0200937endfunc
938
Yegappan Lakshmanan72bb47e2022-04-03 11:22:38 +0100939" Test for tabpage allocation failure
940func Test_tabpage_alloc_failure()
941 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
942 call assert_fails('tabnew', 'E342:')
943
944 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
945 edit Xfile1
946 call assert_fails('tabedit Xfile2', 'E342:')
947 call assert_equal(1, winnr('$'))
948 call assert_equal(1, tabpagenr('$'))
949 call assert_equal('Xfile1', @%)
950
951 new
952 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
953 call assert_fails('wincmd T', 'E342:')
954 bw!
955
956 call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
957 call assert_fails('tab split', 'E342:')
958 call assert_equal(2, winnr('$'))
959 call assert_equal(1, tabpagenr('$'))
960endfunc
961
Bram Moolenaar99ad3a82023-02-27 17:18:01 +0000962" this was giving ml_get errors
963func Test_tabpage_last_line()
964 enew
965 call setline(1, repeat(['a'], &lines + 5))
966 $
967 tabnew
968 call setline(1, repeat(['b'], &lines + 20))
969 $
970 tabNext
971 call assert_equal('a', getline('.'))
972
973 bwipe!
974 bwipe!
975endfunc
976
Christian Brabandtdf12e392023-12-16 13:55:32 +0100977" this was causing an endless loop
978func Test_tabpage_drop_tabmove()
979 augroup TestTabpageTabmove
980 au!
981 autocmd! TabEnter * :if tabpagenr() > 1 | tabmove - | endif
982 augroup end
983 $tab drop XTab_99.log
984 $tab drop XTab_98.log
985 $tab drop XTab_97.log
986
987 autocmd! TestTabpageTabmove
988 augroup! TestTabpageTabmove
989
990 " clean up
991 bwipe!
992 bwipe!
993 bwipe!
994endfunc
995
Bram Moolenaar1381d792016-08-18 22:11:42 +0200996" vim: shiftwidth=2 sts=2 expandtab