blob: 2eae3deb595c5931eff1e88c294dc22dd161483a [file] [log] [blame]
Bram Moolenaar1381d792016-08-18 22:11:42 +02001" Tests for tabpage
2
Bram Moolenaardbe88692018-05-20 14:57:22 +02003
Bram Moolenaar1381d792016-08-18 22:11:42 +02004function Test_tabpage()
5 bw!
6 " Simple test for opening and closing a tab page
7 tabnew
8 call assert_equal(2, tabpagenr())
9 quit
10
11 " Open three tab pages and use ":tabdo"
12 0tabnew
13 1tabnew
14 $tabnew
Bram Moolenaar3e8474d2016-10-12 17:52:42 +020015 %del
Bram Moolenaar1381d792016-08-18 22:11:42 +020016 tabdo call append(line('$'), tabpagenr())
17 tabclose! 2
18 tabrewind
19 let line1 = getline('$')
20 undo
21 q
22 tablast
23 let line2 = getline('$')
24 q!
25 call append(line('$'), line1)
26 call append(line('$'), line2)
27 unlet line1 line2
28 call assert_equal(['', '3', '1', '4'], getline(1, '$'))
29 "
30 " Test for settabvar() and gettabvar() functions. Open a new tab page and
31 " set 3 variables to a number, string and a list. Verify that the variables
32 " are correctly set.
33 tabnew
34 tabfirst
35 call settabvar(2, 'val_num', 100)
36 call settabvar(2, 'val_str', 'SetTabVar test')
37 call settabvar(2, 'val_list', ['red', 'blue', 'green'])
38 "
39 call assert_true(gettabvar(2, 'val_num') == 100 && gettabvar(2, 'val_str') == 'SetTabVar test' && gettabvar(2, 'val_list') == ['red', 'blue', 'green'])
40
41 tabnext 2
42 call assert_true(t:val_num == 100 && t:val_str == 'SetTabVar test' && t:val_list == ['red', 'blue', 'green'])
43 tabclose
44
Bram Moolenaar5a656862018-02-12 22:08:06 +010045 " Test for ":tab drop exist-file" to keep current window.
46 sp test1
47 tab drop test1
48 call assert_true(tabpagenr('$') == 1 && winnr('$') == 2 && winnr() == 1)
49 close
50 "
51 "
52 " Test for ":tab drop new-file" to keep current window of tabpage 1.
53 split
54 tab drop newfile
55 call assert_true(tabpagenr('$') == 2 && tabpagewinnr(1, '$') == 2 && tabpagewinnr(1) == 1)
56 tabclose
57 q
58 "
59 "
60 " Test for ":tab drop multi-opend-file" to keep current tabpage and window.
61 new test1
62 tabnew
63 new test1
64 tab drop test1
65 call assert_true(tabpagenr() == 2 && tabpagewinnr(2, '$') == 2 && tabpagewinnr(2) == 1)
66 tabclose
67 q
68 "
69 "
70 " Test for ":tab drop vertical-split-window" to jump test1 buffer
71 tabedit test1
72 vnew
73 tabfirst
74 tab drop test1
75 call assert_equal([2, 2, 2, 2], [tabpagenr('$'), tabpagenr(), tabpagewinnr(2, '$'), tabpagewinnr(2)])
76 1tabonly
Bram Moolenaar1381d792016-08-18 22:11:42 +020077 "
78 "
79 for i in range(9) | tabnew | endfor
80 normal! 1gt
81 call assert_equal(1, tabpagenr())
82 tabmove 5
83 call assert_equal(5, tabpagenr())
84 .tabmove
85 call assert_equal(5, tabpagenr())
86 tabmove -
87 call assert_equal(4, tabpagenr())
88 tabmove +
89 call assert_equal(5, tabpagenr())
90 tabmove -2
91 call assert_equal(3, tabpagenr())
92 tabmove +4
93 call assert_equal(7, tabpagenr())
94 tabmove
95 call assert_equal(10, tabpagenr())
Bram Moolenaar1381d792016-08-18 22:11:42 +020096 0tabmove
97 call assert_equal(1, tabpagenr())
98 $tabmove
99 call assert_equal(10, tabpagenr())
100 tabmove 0
101 call assert_equal(1, tabpagenr())
102 tabmove $
103 call assert_equal(10, tabpagenr())
104 3tabmove
105 call assert_equal(4, tabpagenr())
106 7tabmove 5
107 call assert_equal(5, tabpagenr())
Bram Moolenaar82a12462019-01-22 22:41:42 +0100108 -tabmove
109 call assert_equal(4, tabpagenr())
110 +tabmove
111 call assert_equal(5, tabpagenr())
112 -2tabmove
113 call assert_equal(3, tabpagenr())
114 +3tabmove
115 call assert_equal(6, tabpagenr())
Bram Moolenaar7cc59652018-08-07 13:14:46 +0200116
117 " The following are a no-op
118 norm! 2gt
119 call assert_equal(2, tabpagenr())
120 tabmove 2
121 call assert_equal(2, tabpagenr())
122 2tabmove
123 call assert_equal(2, tabpagenr())
124 tabmove 1
125 call assert_equal(2, tabpagenr())
126 1tabmove
127 call assert_equal(2, tabpagenr())
128
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100129 call assert_fails("99tabmove", 'E16:')
130 call assert_fails("+99tabmove", 'E16:')
131 call assert_fails("-99tabmove", 'E16:')
Bram Moolenaar1381d792016-08-18 22:11:42 +0200132 call assert_fails("tabmove foo", 'E474:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100133 call assert_fails("tabmove 99", 'E474:')
134 call assert_fails("tabmove +99", 'E474:')
135 call assert_fails("tabmove -99", 'E474:')
136 call assert_fails("tabmove -3+", 'E474:')
137 call assert_fails("tabmove $3", 'E474:')
138 1tabonly!
Bram Moolenaar1381d792016-08-18 22:11:42 +0200139endfunc
140
141" Test autocommands
142function Test_tabpage_with_autocmd()
Bram Moolenaar1381d792016-08-18 22:11:42 +0200143 command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
144 augroup TestTabpageGroup
145 au!
146 autocmd TabEnter * call add(s:li, 'TabEnter')
147 autocmd WinEnter * call add(s:li, 'WinEnter')
148 autocmd BufEnter * call add(s:li, 'BufEnter')
149 autocmd TabLeave * call add(s:li, 'TabLeave')
150 autocmd WinLeave * call add(s:li, 'WinLeave')
151 autocmd BufLeave * call add(s:li, 'BufLeave')
152 augroup END
153
154 let s:li = []
155 let t:a='a'
156 C tab split
157 call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
158 let s:li = []
159 let t:a='b'
160 C tabnew
161 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
162 let t:a='c'
163 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
164 call assert_equal(['a', 'b', 'c'], s:li)
165
166 let s:li = []
167 C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
168 call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
169 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
170 call assert_equal(['2', '4', '6'], s:li)
171
172 let s:li = []
173 let w:a='a'
174 C vsplit
175 call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
176 let s:li = []
177 let w:a='a'
178 let tabn=tabpagenr()
179 let winr=range(1, winnr('$'))
180 C tabnext 1
181 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
182 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
183 call assert_equal(['a', 'a'], s:li)
184 let s:li = []
185 C call map(copy(winr), 'settabwinvar('.tabn.', v:val, ''a'', v:val*2)')
186 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
187 call assert_equal(['2', '4'], s:li)
188
189 augroup TabDestructive
190 autocmd TabEnter * :C tabnext 2 | C tabclose 3
191 augroup END
192 let s:li = []
193 C tabnext 3
194 call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
195 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
196
197 autocmd! TabDestructive TabEnter
198 let s:li = []
199 C tabnew
200 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
201 let s:li = []
202 C tabnext 1
203 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
204
205 autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
206 let s:li = []
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100207 call assert_equal(3, tabpagenr('$'))
208 C tabnext 2
209 call assert_equal(2, tabpagenr('$'))
210 call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
Bram Moolenaar1381d792016-08-18 22:11:42 +0200211 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
212
213 delcommand C
214 autocmd! TabDestructive
215 augroup! TabDestructive
216 autocmd! TestTabpageGroup
217 augroup! TestTabpageGroup
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100218 1tabonly!
Bram Moolenaar1381d792016-08-18 22:11:42 +0200219endfunction
220
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200221function Test_tabpage_with_tab_modifier()
222 for n in range(4)
223 tabedit
224 endfor
225
226 function s:check_tab(pre_nr, cmd, post_nr)
227 exec 'tabnext ' . a:pre_nr
228 exec a:cmd
229 call assert_equal(a:post_nr, tabpagenr())
Bram Moolenaar100f5c92016-09-06 21:33:52 +0200230 call assert_equal('help', &buftype)
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200231 helpclose
232 endfunc
233
234 call s:check_tab(1, 'tab help', 2)
235 call s:check_tab(1, '3tab help', 4)
236 call s:check_tab(1, '.tab help', 2)
237 call s:check_tab(1, '.+1tab help', 3)
238 call s:check_tab(1, '0tab help', 1)
239 call s:check_tab(2, '+tab help', 4)
240 call s:check_tab(2, '+2tab help', 5)
241 call s:check_tab(4, '-tab help', 4)
242 call s:check_tab(4, '-2tab help', 3)
243 call s:check_tab(3, '$tab help', 6)
244 call assert_fails('99tab help', 'E16:')
245 call assert_fails('+99tab help', 'E16:')
246 call assert_fails('-99tab help', 'E16:')
247
248 delfunction s:check_tab
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100249 1tabonly!
250endfunction
251
252function Check_tab_count(pre_nr, cmd, post_nr)
253 exec 'tabnext' a:pre_nr
254 normal! G
255 exec a:cmd
256 call assert_equal(a:post_nr, tabpagenr(), a:cmd)
257endfunc
258
259" Test for [count] of tabnext
260function Test_tabpage_with_tabnext()
261 for n in range(4)
262 tabedit
263 call setline(1, ['', '', '3'])
264 endfor
265
266 call Check_tab_count(1, 'tabnext', 2)
267 call Check_tab_count(1, '3tabnext', 3)
268 call Check_tab_count(1, '.tabnext', 1)
269 call Check_tab_count(1, '.+1tabnext', 2)
270 call Check_tab_count(2, '+tabnext', 3)
271 call Check_tab_count(2, '+2tabnext', 4)
272 call Check_tab_count(4, '-tabnext', 3)
273 call Check_tab_count(4, '-2tabnext', 2)
274 call Check_tab_count(3, '$tabnext', 5)
275 call assert_fails('0tabnext', 'E16:')
276 call assert_fails('99tabnext', 'E16:')
277 call assert_fails('+99tabnext', 'E16:')
278 call assert_fails('-99tabnext', 'E16:')
279 call Check_tab_count(1, 'tabnext 3', 3)
280 call Check_tab_count(2, 'tabnext +', 3)
281 call Check_tab_count(2, 'tabnext +2', 4)
282 call Check_tab_count(4, 'tabnext -', 3)
283 call Check_tab_count(4, 'tabnext -2', 2)
284 call Check_tab_count(3, 'tabnext $', 5)
285 call assert_fails('tabnext 0', 'E474:')
286 call assert_fails('tabnext .', 'E474:')
287 call assert_fails('tabnext -+', 'E474:')
288 call assert_fails('tabnext +2-', 'E474:')
289 call assert_fails('tabnext $3', 'E474:')
290 call assert_fails('tabnext 99', 'E474:')
291 call assert_fails('tabnext +99', 'E474:')
292 call assert_fails('tabnext -99', 'E474:')
293
294 1tabonly!
295endfunction
296
297" Test for [count] of tabprevious
298function Test_tabpage_with_tabprevious()
299 for n in range(5)
300 tabedit
301 call setline(1, ['', '', '3'])
302 endfor
303
304 for cmd in ['tabNext', 'tabprevious']
305 call Check_tab_count(6, cmd, 5)
306 call Check_tab_count(6, '3' . cmd, 3)
307 call Check_tab_count(6, '8' . cmd, 4)
308 call Check_tab_count(6, cmd . ' 3', 3)
309 call Check_tab_count(6, cmd . ' 8', 4)
310 for n in range(2)
311 for c in ['0', '.+3', '+', '+2' , '-', '-2' , '$', '+99', '-99']
312 if n == 0 " pre count
313 let entire_cmd = c . cmd
314 let err_code = 'E16:'
315 else
316 let entire_cmd = cmd . ' ' . c
317 let err_code = 'E474:'
318 endif
319 call assert_fails(entire_cmd, err_code)
320 endfor
321 endfor
322 endfor
323
324 1tabonly!
325endfunction
326
327function s:reconstruct_tabpage_for_test(nr)
328 let n = (a:nr > 2) ? a:nr - 2 : 1
329 1tabonly!
330 0tabedit n0
331 for n in range(1, n)
332 exec '$tabedit n' . n
333 if n == 1
334 call setline(1, ['', '', '3'])
335 endif
336 endfor
337endfunc
338
Bram Moolenaardbe88692018-05-20 14:57:22 +0200339func Test_tabpage_ctrl_pgup_pgdown()
340 enew!
341 tabnew tab1
342 tabnew tab2
343
344 call assert_equal(3, tabpagenr())
345 exe "norm! \<C-PageUp>"
346 call assert_equal(2, tabpagenr())
347 exe "norm! \<C-PageDown>"
348 call assert_equal(3, tabpagenr())
349
350 " Check wrapping at last or first page.
351 exe "norm! \<C-PageDown>"
352 call assert_equal(1, tabpagenr())
353 exe "norm! \<C-PageUp>"
354 call assert_equal(3, tabpagenr())
355
356 " With a count, <C-PageUp> and <C-PageDown> are not symmetrical somehow:
357 " - {count}<C-PageUp> goes {count} pages downward (relative count)
358 " - {count}<C-PageDown> goes to page number {count} (absolute count)
359 exe "norm! 2\<C-PageUp>"
360 call assert_equal(1, tabpagenr())
361 exe "norm! 2\<C-PageDown>"
362 call assert_equal(2, tabpagenr())
363
364 1tabonly!
365endfunc
366
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100367" Test for [count] of tabclose
368function Test_tabpage_with_tabclose()
369
370 " pre count
371 call s:reconstruct_tabpage_for_test(6)
372 call Check_tab_count(3, 'tabclose!', 3)
373 call Check_tab_count(1, '3tabclose', 1)
374 call Check_tab_count(4, '4tabclose', 3)
375 call Check_tab_count(3, '1tabclose', 2)
376 call Check_tab_count(2, 'tabclose', 1)
377 call assert_equal(1, tabpagenr('$'))
378 call assert_equal('', bufname(''))
379
380 call s:reconstruct_tabpage_for_test(6)
381 call Check_tab_count(2, '$tabclose', 2)
382 call Check_tab_count(4, '.tabclose', 4)
383 call Check_tab_count(3, '.+tabclose', 3)
384 call Check_tab_count(3, '.-2tabclose', 2)
385 call Check_tab_count(1, '.+1tabclose!', 1)
386 call assert_equal(1, tabpagenr('$'))
387 call assert_equal('', bufname(''))
388
389 " post count
390 call s:reconstruct_tabpage_for_test(6)
391 call Check_tab_count(3, 'tabclose!', 3)
392 call Check_tab_count(1, 'tabclose 3', 1)
393 call Check_tab_count(4, 'tabclose 4', 3)
394 call Check_tab_count(3, 'tabclose 1', 2)
395 call Check_tab_count(2, 'tabclose', 1)
396 call assert_equal(1, tabpagenr('$'))
397 call assert_equal('', bufname(''))
398
399 call s:reconstruct_tabpage_for_test(6)
400 call Check_tab_count(2, 'tabclose $', 2)
401 call Check_tab_count(4, 'tabclose', 4)
402 call Check_tab_count(3, 'tabclose +', 3)
403 call Check_tab_count(3, 'tabclose -2', 2)
404 call Check_tab_count(1, 'tabclose! +1', 1)
405 call assert_equal(1, tabpagenr('$'))
406 call assert_equal('', bufname(''))
407
408 call s:reconstruct_tabpage_for_test(6)
409 for n in range(2)
410 for c in ['0', '$3', '99', '+99', '-99']
411 if n == 0 " pre count
412 let entire_cmd = c . 'tabclose'
413 let err_code = 'E16:'
414 else
415 let entire_cmd = 'tabclose ' . c
416 let err_code = 'E474:'
417 endif
418 call assert_fails(entire_cmd, err_code)
419 call assert_equal(6, tabpagenr('$'))
420 endfor
421 endfor
422
423 call assert_fails('3tabclose', 'E37:')
424 call assert_fails('tabclose 3', 'E37:')
425 call assert_fails('tabclose -+', 'E474:')
426 call assert_fails('tabclose +2-', 'E474:')
427 call assert_equal(6, tabpagenr('$'))
428
429 1tabonly!
430endfunction
431
432" Test for [count] of tabonly
433function Test_tabpage_with_tabonly()
434
435 " Test for the normal behavior (pre count only)
436 let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
437 for c in tc
438 call s:reconstruct_tabpage_for_test(6)
439 let entire_cmd = c[1] . 'tabonly' . c[2]
440 call Check_tab_count(c[0], entire_cmd, 1)
441 call assert_equal(1, tabpagenr('$'))
442 endfor
443
444 " Test for the normal behavior
445 let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
446 \ [2, '', '!'],
447 \ [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
448 \ ]
449 for n in range(2)
450 for c in tc2
451 call s:reconstruct_tabpage_for_test(6)
452 if n == 0 " pre count
453 let entire_cmd = c[1] . 'tabonly' . c[2]
454 else
455 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
456 endif
457 call Check_tab_count(c[0], entire_cmd, 1)
458 call assert_equal(1, tabpagenr('$'))
459 endfor
460 endfor
461
462 " Test for the error behavior
463 for n in range(2)
464 for c in ['0', '$3', '99', '+99', '-99']
465 call s:reconstruct_tabpage_for_test(6)
466 if n == 0 " pre count
467 let entire_cmd = c . 'tabonly'
468 let err_code = 'E16:'
469 else
470 let entire_cmd = 'tabonly ' . c
471 let err_code = 'E474:'
472 endif
473 call assert_fails(entire_cmd, err_code)
474 call assert_equal(6, tabpagenr('$'))
475 endfor
476 endfor
477
478 " Test for the error behavior (post count only)
479 for c in tc
480 call s:reconstruct_tabpage_for_test(6)
481 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
482 let err_code = 'E474:'
483 call assert_fails(entire_cmd, err_code)
484 call assert_equal(6, tabpagenr('$'))
485 endfor
486
487 call assert_fails('tabonly -+', 'E474:')
488 call assert_fails('tabonly +2-', 'E474:')
489 call assert_equal(6, tabpagenr('$'))
490
491 1tabonly!
492 new
493 only!
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200494endfunction
495
Bram Moolenaar5a497892016-09-03 16:29:04 +0200496func Test_tabnext_on_buf_unload1()
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200497 " This once caused a crash
498 new
499 tabedit
500 tabfirst
501 au BufUnload <buffer> tabnext
502 q
503
504 while tabpagenr('$') > 1
Bram Moolenaar5a497892016-09-03 16:29:04 +0200505 bwipe!
506 endwhile
507endfunc
508
509func Test_tabnext_on_buf_unload2()
510 " This once caused a crash
511 tabedit
512 autocmd BufUnload <buffer> tabnext
513 file x
514 edit y
515
516 while tabpagenr('$') > 1
517 bwipe!
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200518 endwhile
519endfunc
520
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200521func Test_close_on_quitpre()
522 " This once caused a crash
Bram Moolenaarce11de82017-10-26 22:00:00 +0200523 edit Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200524 new
525 only
526 set bufhidden=delete
527 au QuitPre <buffer> close
528 tabnew tab1
529 tabnew tab2
530 1tabn
531 q!
532 call assert_equal(1, tabpagenr())
533 call assert_equal(2, tabpagenr('$'))
534 " clean up
535 while tabpagenr('$') > 1
536 bwipe!
537 endwhile
Bram Moolenaarce11de82017-10-26 22:00:00 +0200538 buf Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200539endfunc
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200540
Bram Moolenaardbe88692018-05-20 14:57:22 +0200541func Test_tabs()
542 enew!
543 tabnew tab1
544 norm ixxx
545 let a=split(execute(':tabs'), "\n")
546 call assert_equal(['Tab page 1',
547 \ ' [No Name]',
548 \ 'Tab page 2',
549 \ '> + tab1'], a)
550
551 1tabonly!
552 bw!
553endfunc
554
Bram Moolenaar1381d792016-08-18 22:11:42 +0200555" vim: shiftwidth=2 sts=2 expandtab