blob: e561064ebc1aeb11b774d582e06d1f0fec90f847 [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
153" Test autocommands
154function Test_tabpage_with_autocmd()
Bram Moolenaar1381d792016-08-18 22:11:42 +0200155 command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
156 augroup TestTabpageGroup
157 au!
158 autocmd TabEnter * call add(s:li, 'TabEnter')
159 autocmd WinEnter * call add(s:li, 'WinEnter')
160 autocmd BufEnter * call add(s:li, 'BufEnter')
161 autocmd TabLeave * call add(s:li, 'TabLeave')
162 autocmd WinLeave * call add(s:li, 'WinLeave')
163 autocmd BufLeave * call add(s:li, 'BufLeave')
164 augroup END
165
166 let s:li = []
167 let t:a='a'
168 C tab split
169 call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
170 let s:li = []
171 let t:a='b'
172 C tabnew
173 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
174 let t:a='c'
175 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
176 call assert_equal(['a', 'b', 'c'], s:li)
177
178 let s:li = []
179 C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
180 call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
181 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
182 call assert_equal(['2', '4', '6'], s:li)
183
184 let s:li = []
185 let w:a='a'
186 C vsplit
187 call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
188 let s:li = []
189 let w:a='a'
190 let tabn=tabpagenr()
191 let winr=range(1, winnr('$'))
192 C tabnext 1
193 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
194 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
195 call assert_equal(['a', 'a'], s:li)
196 let s:li = []
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200197 C call map(copy(winr), '(v:val*2)->settabwinvar(' .. tabn .. ', v:val, ''a'')')
Bram Moolenaar1381d792016-08-18 22:11:42 +0200198 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
199 call assert_equal(['2', '4'], s:li)
200
201 augroup TabDestructive
202 autocmd TabEnter * :C tabnext 2 | C tabclose 3
203 augroup END
204 let s:li = []
205 C tabnext 3
206 call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
207 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
208
209 autocmd! TabDestructive TabEnter
210 let s:li = []
211 C tabnew
212 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
213 let s:li = []
214 C tabnext 1
215 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
216
217 autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
218 let s:li = []
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100219 call assert_equal(3, tabpagenr('$'))
220 C tabnext 2
221 call assert_equal(2, tabpagenr('$'))
222 call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
Bram Moolenaar1381d792016-08-18 22:11:42 +0200223 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
224
225 delcommand C
226 autocmd! TabDestructive
227 augroup! TabDestructive
228 autocmd! TestTabpageGroup
229 augroup! TestTabpageGroup
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100230 1tabonly!
Bram Moolenaar1381d792016-08-18 22:11:42 +0200231endfunction
232
Bram Moolenaarc10b5212020-01-13 20:54:51 +0100233" Test autocommands on tab drop
234function Test_tabpage_with_autocmd_tab_drop()
235 augroup TestTabpageGroup
236 au!
237 autocmd TabEnter * call add(s:li, 'TabEnter')
238 autocmd WinEnter * call add(s:li, 'WinEnter')
239 autocmd BufEnter * call add(s:li, 'BufEnter')
240 autocmd TabLeave * call add(s:li, 'TabLeave')
241 autocmd WinLeave * call add(s:li, 'WinLeave')
242 autocmd BufLeave * call add(s:li, 'BufLeave')
243 augroup END
244
245 let s:li = []
246 tab drop test1
247 call assert_equal(['BufLeave', 'BufEnter'], s:li)
248
249 let s:li = []
250 tab drop test2 test3
251 call assert_equal([
252 \ 'TabLeave', 'TabEnter', 'TabLeave', 'TabEnter',
253 \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter',
254 \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
255
256 autocmd! TestTabpageGroup
257 augroup! TestTabpageGroup
258 1tabonly!
259endfunction
260
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200261function Test_tabpage_with_tab_modifier()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100262 CheckFeature quickfix
263
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200264 for n in range(4)
265 tabedit
266 endfor
267
268 function s:check_tab(pre_nr, cmd, post_nr)
269 exec 'tabnext ' . a:pre_nr
270 exec a:cmd
271 call assert_equal(a:post_nr, tabpagenr())
Bram Moolenaar100f5c92016-09-06 21:33:52 +0200272 call assert_equal('help', &buftype)
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200273 helpclose
274 endfunc
275
276 call s:check_tab(1, 'tab help', 2)
277 call s:check_tab(1, '3tab help', 4)
278 call s:check_tab(1, '.tab help', 2)
279 call s:check_tab(1, '.+1tab help', 3)
280 call s:check_tab(1, '0tab help', 1)
281 call s:check_tab(2, '+tab help', 4)
282 call s:check_tab(2, '+2tab help', 5)
283 call s:check_tab(4, '-tab help', 4)
284 call s:check_tab(4, '-2tab help', 3)
285 call s:check_tab(3, '$tab help', 6)
286 call assert_fails('99tab help', 'E16:')
287 call assert_fails('+99tab help', 'E16:')
288 call assert_fails('-99tab help', 'E16:')
289
290 delfunction s:check_tab
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100291 1tabonly!
292endfunction
293
294function Check_tab_count(pre_nr, cmd, post_nr)
295 exec 'tabnext' a:pre_nr
296 normal! G
297 exec a:cmd
298 call assert_equal(a:post_nr, tabpagenr(), a:cmd)
299endfunc
300
301" Test for [count] of tabnext
302function Test_tabpage_with_tabnext()
303 for n in range(4)
304 tabedit
305 call setline(1, ['', '', '3'])
306 endfor
307
308 call Check_tab_count(1, 'tabnext', 2)
309 call Check_tab_count(1, '3tabnext', 3)
310 call Check_tab_count(1, '.tabnext', 1)
311 call Check_tab_count(1, '.+1tabnext', 2)
312 call Check_tab_count(2, '+tabnext', 3)
313 call Check_tab_count(2, '+2tabnext', 4)
314 call Check_tab_count(4, '-tabnext', 3)
315 call Check_tab_count(4, '-2tabnext', 2)
316 call Check_tab_count(3, '$tabnext', 5)
317 call assert_fails('0tabnext', 'E16:')
318 call assert_fails('99tabnext', 'E16:')
319 call assert_fails('+99tabnext', 'E16:')
320 call assert_fails('-99tabnext', 'E16:')
321 call Check_tab_count(1, 'tabnext 3', 3)
322 call Check_tab_count(2, 'tabnext +', 3)
323 call Check_tab_count(2, 'tabnext +2', 4)
324 call Check_tab_count(4, 'tabnext -', 3)
325 call Check_tab_count(4, 'tabnext -2', 2)
326 call Check_tab_count(3, 'tabnext $', 5)
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200327 call assert_fails('tabnext 0', 'E475:')
328 call assert_fails('tabnext .', 'E475:')
329 call assert_fails('tabnext -+', 'E475:')
330 call assert_fails('tabnext +2-', 'E475:')
331 call assert_fails('tabnext $3', 'E475:')
332 call assert_fails('tabnext 99', 'E475:')
333 call assert_fails('tabnext +99', 'E475:')
334 call assert_fails('tabnext -99', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100335
336 1tabonly!
337endfunction
338
339" Test for [count] of tabprevious
340function Test_tabpage_with_tabprevious()
341 for n in range(5)
342 tabedit
343 call setline(1, ['', '', '3'])
344 endfor
345
346 for cmd in ['tabNext', 'tabprevious']
347 call Check_tab_count(6, cmd, 5)
348 call Check_tab_count(6, '3' . cmd, 3)
349 call Check_tab_count(6, '8' . cmd, 4)
350 call Check_tab_count(6, cmd . ' 3', 3)
351 call Check_tab_count(6, cmd . ' 8', 4)
352 for n in range(2)
Bram Moolenaar9d489562020-07-30 20:08:50 +0200353 for c in ['0', '.+3', '+', '+2', '-', '-2', '$', '+99', '-99']
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100354 if n == 0 " pre count
355 let entire_cmd = c . cmd
356 let err_code = 'E16:'
357 else
358 let entire_cmd = cmd . ' ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200359 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100360 endif
361 call assert_fails(entire_cmd, err_code)
362 endfor
363 endfor
364 endfor
365
366 1tabonly!
367endfunction
368
369function s:reconstruct_tabpage_for_test(nr)
370 let n = (a:nr > 2) ? a:nr - 2 : 1
371 1tabonly!
372 0tabedit n0
373 for n in range(1, n)
374 exec '$tabedit n' . n
375 if n == 1
376 call setline(1, ['', '', '3'])
377 endif
378 endfor
379endfunc
380
Bram Moolenaardbe88692018-05-20 14:57:22 +0200381func Test_tabpage_ctrl_pgup_pgdown()
382 enew!
383 tabnew tab1
384 tabnew tab2
385
386 call assert_equal(3, tabpagenr())
387 exe "norm! \<C-PageUp>"
388 call assert_equal(2, tabpagenr())
389 exe "norm! \<C-PageDown>"
390 call assert_equal(3, tabpagenr())
391
392 " Check wrapping at last or first page.
393 exe "norm! \<C-PageDown>"
394 call assert_equal(1, tabpagenr())
395 exe "norm! \<C-PageUp>"
396 call assert_equal(3, tabpagenr())
397
398 " With a count, <C-PageUp> and <C-PageDown> are not symmetrical somehow:
399 " - {count}<C-PageUp> goes {count} pages downward (relative count)
400 " - {count}<C-PageDown> goes to page number {count} (absolute count)
401 exe "norm! 2\<C-PageUp>"
402 call assert_equal(1, tabpagenr())
403 exe "norm! 2\<C-PageDown>"
404 call assert_equal(2, tabpagenr())
405
406 1tabonly!
407endfunc
408
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100409" Test for [count] of tabclose
410function Test_tabpage_with_tabclose()
411
412 " pre count
413 call s:reconstruct_tabpage_for_test(6)
414 call Check_tab_count(3, 'tabclose!', 3)
415 call Check_tab_count(1, '3tabclose', 1)
416 call Check_tab_count(4, '4tabclose', 3)
417 call Check_tab_count(3, '1tabclose', 2)
418 call Check_tab_count(2, 'tabclose', 1)
419 call assert_equal(1, tabpagenr('$'))
420 call assert_equal('', bufname(''))
421
422 call s:reconstruct_tabpage_for_test(6)
423 call Check_tab_count(2, '$tabclose', 2)
424 call Check_tab_count(4, '.tabclose', 4)
425 call Check_tab_count(3, '.+tabclose', 3)
426 call Check_tab_count(3, '.-2tabclose', 2)
427 call Check_tab_count(1, '.+1tabclose!', 1)
428 call assert_equal(1, tabpagenr('$'))
429 call assert_equal('', bufname(''))
430
431 " post count
432 call s:reconstruct_tabpage_for_test(6)
433 call Check_tab_count(3, 'tabclose!', 3)
434 call Check_tab_count(1, 'tabclose 3', 1)
435 call Check_tab_count(4, 'tabclose 4', 3)
436 call Check_tab_count(3, 'tabclose 1', 2)
437 call Check_tab_count(2, 'tabclose', 1)
438 call assert_equal(1, tabpagenr('$'))
439 call assert_equal('', bufname(''))
440
441 call s:reconstruct_tabpage_for_test(6)
442 call Check_tab_count(2, 'tabclose $', 2)
443 call Check_tab_count(4, 'tabclose', 4)
444 call Check_tab_count(3, 'tabclose +', 3)
445 call Check_tab_count(3, 'tabclose -2', 2)
446 call Check_tab_count(1, 'tabclose! +1', 1)
447 call assert_equal(1, tabpagenr('$'))
448 call assert_equal('', bufname(''))
449
450 call s:reconstruct_tabpage_for_test(6)
451 for n in range(2)
452 for c in ['0', '$3', '99', '+99', '-99']
453 if n == 0 " pre count
454 let entire_cmd = c . 'tabclose'
455 let err_code = 'E16:'
456 else
457 let entire_cmd = 'tabclose ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200458 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100459 endif
460 call assert_fails(entire_cmd, err_code)
461 call assert_equal(6, tabpagenr('$'))
462 endfor
463 endfor
464
465 call assert_fails('3tabclose', 'E37:')
466 call assert_fails('tabclose 3', 'E37:')
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200467 call assert_fails('tabclose -+', 'E475:')
468 call assert_fails('tabclose +2-', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100469 call assert_equal(6, tabpagenr('$'))
470
471 1tabonly!
472endfunction
473
474" Test for [count] of tabonly
475function Test_tabpage_with_tabonly()
476
477 " Test for the normal behavior (pre count only)
478 let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
479 for c in tc
480 call s:reconstruct_tabpage_for_test(6)
481 let entire_cmd = c[1] . 'tabonly' . c[2]
482 call Check_tab_count(c[0], entire_cmd, 1)
483 call assert_equal(1, tabpagenr('$'))
484 endfor
485
486 " Test for the normal behavior
487 let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
488 \ [2, '', '!'],
489 \ [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
490 \ ]
491 for n in range(2)
492 for c in tc2
493 call s:reconstruct_tabpage_for_test(6)
494 if n == 0 " pre count
495 let entire_cmd = c[1] . 'tabonly' . c[2]
496 else
497 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
498 endif
499 call Check_tab_count(c[0], entire_cmd, 1)
500 call assert_equal(1, tabpagenr('$'))
501 endfor
502 endfor
503
504 " Test for the error behavior
505 for n in range(2)
506 for c in ['0', '$3', '99', '+99', '-99']
507 call s:reconstruct_tabpage_for_test(6)
508 if n == 0 " pre count
509 let entire_cmd = c . 'tabonly'
510 let err_code = 'E16:'
511 else
512 let entire_cmd = 'tabonly ' . c
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200513 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100514 endif
515 call assert_fails(entire_cmd, err_code)
516 call assert_equal(6, tabpagenr('$'))
517 endfor
518 endfor
519
520 " Test for the error behavior (post count only)
521 for c in tc
522 call s:reconstruct_tabpage_for_test(6)
523 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200524 let err_code = 'E475:'
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100525 call assert_fails(entire_cmd, err_code)
526 call assert_equal(6, tabpagenr('$'))
527 endfor
528
Bram Moolenaar8930caa2020-07-23 16:37:03 +0200529 call assert_fails('tabonly -+', 'E475:')
530 call assert_fails('tabonly +2-', 'E475:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100531 call assert_equal(6, tabpagenr('$'))
532
533 1tabonly!
534 new
535 only!
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200536endfunction
537
Bram Moolenaar5a497892016-09-03 16:29:04 +0200538func Test_tabnext_on_buf_unload1()
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200539 " This once caused a crash
540 new
541 tabedit
542 tabfirst
543 au BufUnload <buffer> tabnext
544 q
545
546 while tabpagenr('$') > 1
Bram Moolenaar5a497892016-09-03 16:29:04 +0200547 bwipe!
548 endwhile
549endfunc
550
551func Test_tabnext_on_buf_unload2()
552 " This once caused a crash
553 tabedit
554 autocmd BufUnload <buffer> tabnext
555 file x
556 edit y
557
558 while tabpagenr('$') > 1
559 bwipe!
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200560 endwhile
561endfunc
562
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200563func Test_close_on_quitpre()
564 " This once caused a crash
Bram Moolenaarce11de82017-10-26 22:00:00 +0200565 edit Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200566 new
567 only
568 set bufhidden=delete
569 au QuitPre <buffer> close
570 tabnew tab1
571 tabnew tab2
572 1tabn
573 q!
574 call assert_equal(1, tabpagenr())
575 call assert_equal(2, tabpagenr('$'))
576 " clean up
577 while tabpagenr('$') > 1
578 bwipe!
579 endwhile
Bram Moolenaarce11de82017-10-26 22:00:00 +0200580 buf Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200581endfunc
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200582
Bram Moolenaardbe88692018-05-20 14:57:22 +0200583func Test_tabs()
584 enew!
585 tabnew tab1
586 norm ixxx
587 let a=split(execute(':tabs'), "\n")
588 call assert_equal(['Tab page 1',
589 \ ' [No Name]',
590 \ 'Tab page 2',
591 \ '> + tab1'], a)
592
593 1tabonly!
594 bw!
595endfunc
596
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200597func Test_tabpage_cmdheight()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200598 CheckRunVimInTerminal
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200599 call writefile([
600 \ 'set laststatus=2',
601 \ 'set cmdheight=2',
602 \ 'tabnew',
603 \ 'set cmdheight=3',
604 \ 'tabnext',
605 \ 'redraw!',
606 \ 'echo "hello\nthere"',
607 \ 'tabnext',
608 \ 'redraw',
609 \ ], 'XTest_tabpage_cmdheight')
610 " Check that cursor line is concealed
611 let buf = RunVimInTerminal('-S XTest_tabpage_cmdheight', {'statusoff': 3})
612 call VerifyScreenDump(buf, 'Test_tabpage_cmdheight', {})
613
614 call StopVimInTerminal(buf)
Bram Moolenaar4fa06872019-05-06 22:03:39 +0200615 call delete('XTest_tabpage_cmdheight')
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +0200616endfunc
617
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100618" Test for closing the tab page from a command window
619func Test_tabpage_close_cmdwin()
620 tabnew
621 call feedkeys("q/:tabclose\<CR>\<Esc>", 'xt')
622 call assert_equal(2, tabpagenr('$'))
623 call feedkeys("q/:tabonly\<CR>\<Esc>", 'xt')
624 call assert_equal(2, tabpagenr('$'))
625 tabonly
626endfunc
627
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200628" Pressing <C-PageUp> in insert mode should go to the previous tab page
629" and <C-PageDown> should go to the next tab page
630func Test_tabpage_Ctrl_Pageup()
631 tabnew
632 call feedkeys("i\<C-PageUp>", 'xt')
633 call assert_equal(1, tabpagenr())
634 call feedkeys("i\<C-PageDown>", 'xt')
635 call assert_equal(2, tabpagenr())
636 %bw!
637endfunc
638
Bram Moolenaar8c524f72020-06-21 13:23:45 +0200639" Return the terminal key code for selecting a tab page from the tabline. This
640" sequence contains the following codes: a CSI (0x9b), KS_TABLINE (0xf0),
641" KS_FILLER (0x58) and then the tab page number.
642func TabLineSelectPageCode(tabnr)
643 return "\x9b\xf0\x58" .. nr2char(a:tabnr)
644endfunc
645
646" Return the terminal key code for opening a new tabpage from the tabpage
647" menu. This sequence consists of the following codes: a CSI (0x9b),
648" KS_TABMENU (0xef), KS_FILLER (0x58), the tab page number and
649" TABLINE_MENU_NEW (2).
650func TabMenuNewItemCode(tabnr)
651 return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(2)
652endfunc
653
654" Return the terminal key code for closing a tabpage from the tabpage menu.
655" This sequence consists of the following codes: a CSI (0x9b), KS_TABMENU
656" (0xef), KS_FILLER (0x58), the tab page number and TABLINE_MENU_CLOSE (1).
657func TabMenuCloseItemCode(tabnr)
658 return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(1)
659endfunc
660
661" Test for using the tabpage menu from the insert and normal modes
662func Test_tabline_tabmenu()
663 " only works in GUI
664 CheckGui
665
666 %bw!
667 tabnew
668 tabnew
669 call assert_equal(3, tabpagenr())
670
671 " go to tab page 2 in normal mode
672 call feedkeys(TabLineSelectPageCode(2), "Lx!")
673 call assert_equal(2, tabpagenr())
674
675 " close tab page 3 in normal mode
676 call feedkeys(TabMenuCloseItemCode(3), "Lx!")
677 call assert_equal(2, tabpagenr('$'))
678 call assert_equal(2, tabpagenr())
679
680 " open new tab page before tab page 1 in normal mode
681 call feedkeys(TabMenuNewItemCode(1), "Lx!")
682 call assert_equal(1, tabpagenr())
683 call assert_equal(3, tabpagenr('$'))
684
685 " go to tab page 2 in operator-pending mode (should beep)
686 call assert_beeps('call feedkeys("f" .. TabLineSelectPageCode(2), "Lx!")')
687
688 " open new tab page before tab page 1 in operator-pending mode (should beep)
689 call assert_beeps('call feedkeys("f" .. TabMenuNewItemCode(1), "Lx!")')
690
691 " open new tab page after tab page 3 in normal mode
692 call feedkeys(TabMenuNewItemCode(4), "Lx!")
693 call assert_equal(4, tabpagenr())
694 call assert_equal(4, tabpagenr('$'))
695
696 " go to tab page 2 in insert mode
697 call feedkeys("i" .. TabLineSelectPageCode(2) .. "\<C-C>", "Lx!")
698 call assert_equal(2, tabpagenr())
699
700 " close tab page 2 in insert mode
701 call feedkeys("i" .. TabMenuCloseItemCode(2) .. "\<C-C>", "Lx!")
702 call assert_equal(3, tabpagenr('$'))
703
704 " open new tab page before tab page 3 in insert mode
705 call feedkeys("i" .. TabMenuNewItemCode(3) .. "\<C-C>", "Lx!")
706 call assert_equal(3, tabpagenr())
707 call assert_equal(4, tabpagenr('$'))
708
709 " open new tab page after tab page 4 in insert mode
710 call feedkeys("i" .. TabMenuNewItemCode(5) .. "\<C-C>", "Lx!")
711 call assert_equal(5, tabpagenr())
712 call assert_equal(5, tabpagenr('$'))
713
714 %bw!
715endfunc
716
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200717" Test for changing the current tab page from an autocmd when closing a tab
718" page.
719func Test_tabpage_switchtab_on_close()
720 only
721 tabnew
722 tabnew
723 " Test for BufLeave
724 augroup T1
725 au!
726 au BufLeave * tabfirst
727 augroup END
728 tabclose
729 call assert_equal(1, tabpagenr())
730 augroup T1
731 au!
732 augroup END
733
734 " Test for WinLeave
735 $tabnew
736 augroup T1
737 au!
738 au WinLeave * tabfirst
739 augroup END
740 tabclose
741 call assert_equal(1, tabpagenr())
742 augroup T1
743 au!
744 augroup END
745
746 " Test for TabLeave
747 $tabnew
748 augroup T1
749 au!
750 au TabLeave * tabfirst
751 augroup END
752 tabclose
753 call assert_equal(1, tabpagenr())
754 augroup T1
755 au!
756 augroup END
757 augroup! T1
758 tabonly
759endfunc
760
761" Test for closing the destination tabpage when jumping from one to another.
762func Test_tabpage_close_on_switch()
763 tabnew
764 tabnew
765 edit Xfile
766 augroup T2
767 au!
768 au BufLeave Xfile 1tabclose
769 augroup END
770 tabfirst
771 call assert_equal(2, tabpagenr())
772 call assert_equal('Xfile', @%)
773 augroup T2
774 au!
775 augroup END
776 augroup! T2
777 %bw!
778endfunc
779
Bram Moolenaar62a23252020-08-09 14:04:42 +0200780" Test for jumping to last accessed tabpage
781func Test_lastused_tabpage()
782 tabonly!
783 call assert_equal(0, tabpagenr('#'))
784 call assert_beeps('call feedkeys("g\<Tab>", "xt")')
785 call assert_beeps('call feedkeys("\<C-Tab>", "xt")')
786 call assert_beeps('call feedkeys("\<C-W>g\<Tab>", "xt")')
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200787 call assert_fails('tabnext #', 'E475:')
Bram Moolenaar62a23252020-08-09 14:04:42 +0200788
789 " open four tab pages
790 tabnew
791 tabnew
792 tabnew
793
794 2tabnext
795
796 " Test for g<Tab>
797 call assert_equal(4, tabpagenr('#'))
798 call feedkeys("g\<Tab>", "xt")
799 call assert_equal(4, tabpagenr())
800 call assert_equal(2, tabpagenr('#'))
801
802 " Test for <C-Tab>
803 call feedkeys("\<C-Tab>", "xt")
804 call assert_equal(2, tabpagenr())
805 call assert_equal(4, tabpagenr('#'))
806
807 " Test for <C-W>g<Tab>
808 call feedkeys("\<C-W>g\<Tab>", "xt")
809 call assert_equal(4, tabpagenr())
810 call assert_equal(2, tabpagenr('#'))
811
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200812 " Test for :tabnext #
813 tabnext #
814 call assert_equal(2, tabpagenr())
815 call assert_equal(4, tabpagenr('#'))
816
Bram Moolenaar62a23252020-08-09 14:04:42 +0200817 " Try to jump to a closed tab page
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200818 tabclose #
Bram Moolenaar62a23252020-08-09 14:04:42 +0200819 call assert_equal(0, tabpagenr('#'))
820 call feedkeys("g\<Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200821 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200822 call feedkeys("\<C-Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200823 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200824 call feedkeys("\<C-W>g\<Tab>", "xt")
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200825 call assert_equal(2, tabpagenr())
826 call assert_fails('tabnext #', 'E475:')
827 call assert_equal(2, tabpagenr())
Bram Moolenaar62a23252020-08-09 14:04:42 +0200828
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +0200829 " Test for :tabonly #
830 let wnum = win_getid()
831 $tabnew
832 tabonly #
833 call assert_equal(wnum, win_getid())
834 call assert_equal(1, tabpagenr('$'))
835
836 " Test for :tabmove #
837 tabnew
838 let wnum = win_getid()
839 tabnew
840 tabnew
841 tabnext 2
842 tabmove #
843 call assert_equal(4, tabpagenr())
844 call assert_equal(wnum, win_getid())
845
846 tabonly!
Bram Moolenaar62a23252020-08-09 14:04:42 +0200847endfunc
848
Bram Moolenaar1381d792016-08-18 22:11:42 +0200849" vim: shiftwidth=2 sts=2 expandtab