blob: fae95ab98f879309e2cac01e4594bc2a9141e8d8 [file] [log] [blame]
Bram Moolenaar1381d792016-08-18 22:11:42 +02001" Tests for tabpage
2
3function Test_tabpage()
4 bw!
5 " Simple test for opening and closing a tab page
6 tabnew
7 call assert_equal(2, tabpagenr())
8 quit
9
10 " Open three tab pages and use ":tabdo"
11 0tabnew
12 1tabnew
13 $tabnew
Bram Moolenaar3e8474d2016-10-12 17:52:42 +020014 %del
Bram Moolenaar1381d792016-08-18 22:11:42 +020015 tabdo call append(line('$'), tabpagenr())
16 tabclose! 2
17 tabrewind
18 let line1 = getline('$')
19 undo
20 q
21 tablast
22 let line2 = getline('$')
23 q!
24 call append(line('$'), line1)
25 call append(line('$'), line2)
26 unlet line1 line2
27 call assert_equal(['', '3', '1', '4'], getline(1, '$'))
28 "
29 " Test for settabvar() and gettabvar() functions. Open a new tab page and
30 " set 3 variables to a number, string and a list. Verify that the variables
31 " are correctly set.
32 tabnew
33 tabfirst
34 call settabvar(2, 'val_num', 100)
35 call settabvar(2, 'val_str', 'SetTabVar test')
36 call settabvar(2, 'val_list', ['red', 'blue', 'green'])
37 "
38 call assert_true(gettabvar(2, 'val_num') == 100 && gettabvar(2, 'val_str') == 'SetTabVar test' && gettabvar(2, 'val_list') == ['red', 'blue', 'green'])
39
40 tabnext 2
41 call assert_true(t:val_num == 100 && t:val_str == 'SetTabVar test' && t:val_list == ['red', 'blue', 'green'])
42 tabclose
43
Bram Moolenaar5a656862018-02-12 22:08:06 +010044 " Test for ":tab drop exist-file" to keep current window.
45 sp test1
46 tab drop test1
47 call assert_true(tabpagenr('$') == 1 && winnr('$') == 2 && winnr() == 1)
48 close
49 "
50 "
51 " Test for ":tab drop new-file" to keep current window of tabpage 1.
52 split
53 tab drop newfile
54 call assert_true(tabpagenr('$') == 2 && tabpagewinnr(1, '$') == 2 && tabpagewinnr(1) == 1)
55 tabclose
56 q
57 "
58 "
59 " Test for ":tab drop multi-opend-file" to keep current tabpage and window.
60 new test1
61 tabnew
62 new test1
63 tab drop test1
64 call assert_true(tabpagenr() == 2 && tabpagewinnr(2, '$') == 2 && tabpagewinnr(2) == 1)
65 tabclose
66 q
67 "
68 "
69 " Test for ":tab drop vertical-split-window" to jump test1 buffer
70 tabedit test1
71 vnew
72 tabfirst
73 tab drop test1
74 call assert_equal([2, 2, 2, 2], [tabpagenr('$'), tabpagenr(), tabpagewinnr(2, '$'), tabpagewinnr(2)])
75 1tabonly
Bram Moolenaar1381d792016-08-18 22:11:42 +020076 "
77 "
78 for i in range(9) | tabnew | endfor
79 normal! 1gt
80 call assert_equal(1, tabpagenr())
81 tabmove 5
82 call assert_equal(5, tabpagenr())
83 .tabmove
84 call assert_equal(5, tabpagenr())
85 tabmove -
86 call assert_equal(4, tabpagenr())
87 tabmove +
88 call assert_equal(5, tabpagenr())
89 tabmove -2
90 call assert_equal(3, tabpagenr())
91 tabmove +4
92 call assert_equal(7, tabpagenr())
93 tabmove
94 call assert_equal(10, tabpagenr())
Bram Moolenaar1381d792016-08-18 22:11:42 +020095 0tabmove
96 call assert_equal(1, tabpagenr())
97 $tabmove
98 call assert_equal(10, tabpagenr())
99 tabmove 0
100 call assert_equal(1, tabpagenr())
101 tabmove $
102 call assert_equal(10, tabpagenr())
103 3tabmove
104 call assert_equal(4, tabpagenr())
105 7tabmove 5
106 call assert_equal(5, tabpagenr())
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100107 call assert_fails("99tabmove", 'E16:')
108 call assert_fails("+99tabmove", 'E16:')
109 call assert_fails("-99tabmove", 'E16:')
Bram Moolenaar1381d792016-08-18 22:11:42 +0200110 call assert_fails("tabmove foo", 'E474:')
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100111 call assert_fails("tabmove 99", 'E474:')
112 call assert_fails("tabmove +99", 'E474:')
113 call assert_fails("tabmove -99", 'E474:')
114 call assert_fails("tabmove -3+", 'E474:')
115 call assert_fails("tabmove $3", 'E474:')
116 1tabonly!
Bram Moolenaar1381d792016-08-18 22:11:42 +0200117endfunc
118
119" Test autocommands
120function Test_tabpage_with_autocmd()
121 if !has('autocmd')
122 return
123 endif
Bram Moolenaar1381d792016-08-18 22:11:42 +0200124 command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
125 augroup TestTabpageGroup
126 au!
127 autocmd TabEnter * call add(s:li, 'TabEnter')
128 autocmd WinEnter * call add(s:li, 'WinEnter')
129 autocmd BufEnter * call add(s:li, 'BufEnter')
130 autocmd TabLeave * call add(s:li, 'TabLeave')
131 autocmd WinLeave * call add(s:li, 'WinLeave')
132 autocmd BufLeave * call add(s:li, 'BufLeave')
133 augroup END
134
135 let s:li = []
136 let t:a='a'
137 C tab split
138 call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
139 let s:li = []
140 let t:a='b'
141 C tabnew
142 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
143 let t:a='c'
144 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
145 call assert_equal(['a', 'b', 'c'], s:li)
146
147 let s:li = []
148 C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
149 call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
150 let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
151 call assert_equal(['2', '4', '6'], s:li)
152
153 let s:li = []
154 let w:a='a'
155 C vsplit
156 call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
157 let s:li = []
158 let w:a='a'
159 let tabn=tabpagenr()
160 let winr=range(1, winnr('$'))
161 C tabnext 1
162 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
163 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
164 call assert_equal(['a', 'a'], s:li)
165 let s:li = []
166 C call map(copy(winr), 'settabwinvar('.tabn.', v:val, ''a'', v:val*2)')
167 let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
168 call assert_equal(['2', '4'], s:li)
169
170 augroup TabDestructive
171 autocmd TabEnter * :C tabnext 2 | C tabclose 3
172 augroup END
173 let s:li = []
174 C tabnext 3
175 call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
176 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
177
178 autocmd! TabDestructive TabEnter
179 let s:li = []
180 C tabnew
181 call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
182 let s:li = []
183 C tabnext 1
184 call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
185
186 autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
187 let s:li = []
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100188 call assert_equal(3, tabpagenr('$'))
189 C tabnext 2
190 call assert_equal(2, tabpagenr('$'))
191 call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
Bram Moolenaar1381d792016-08-18 22:11:42 +0200192 call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
193
194 delcommand C
195 autocmd! TabDestructive
196 augroup! TabDestructive
197 autocmd! TestTabpageGroup
198 augroup! TestTabpageGroup
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100199 1tabonly!
Bram Moolenaar1381d792016-08-18 22:11:42 +0200200endfunction
201
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200202function Test_tabpage_with_tab_modifier()
203 for n in range(4)
204 tabedit
205 endfor
206
207 function s:check_tab(pre_nr, cmd, post_nr)
208 exec 'tabnext ' . a:pre_nr
209 exec a:cmd
210 call assert_equal(a:post_nr, tabpagenr())
Bram Moolenaar100f5c92016-09-06 21:33:52 +0200211 call assert_equal('help', &buftype)
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200212 helpclose
213 endfunc
214
215 call s:check_tab(1, 'tab help', 2)
216 call s:check_tab(1, '3tab help', 4)
217 call s:check_tab(1, '.tab help', 2)
218 call s:check_tab(1, '.+1tab help', 3)
219 call s:check_tab(1, '0tab help', 1)
220 call s:check_tab(2, '+tab help', 4)
221 call s:check_tab(2, '+2tab help', 5)
222 call s:check_tab(4, '-tab help', 4)
223 call s:check_tab(4, '-2tab help', 3)
224 call s:check_tab(3, '$tab help', 6)
225 call assert_fails('99tab help', 'E16:')
226 call assert_fails('+99tab help', 'E16:')
227 call assert_fails('-99tab help', 'E16:')
228
229 delfunction s:check_tab
Bram Moolenaar2f72c702017-01-29 14:48:10 +0100230 1tabonly!
231endfunction
232
233function Check_tab_count(pre_nr, cmd, post_nr)
234 exec 'tabnext' a:pre_nr
235 normal! G
236 exec a:cmd
237 call assert_equal(a:post_nr, tabpagenr(), a:cmd)
238endfunc
239
240" Test for [count] of tabnext
241function Test_tabpage_with_tabnext()
242 for n in range(4)
243 tabedit
244 call setline(1, ['', '', '3'])
245 endfor
246
247 call Check_tab_count(1, 'tabnext', 2)
248 call Check_tab_count(1, '3tabnext', 3)
249 call Check_tab_count(1, '.tabnext', 1)
250 call Check_tab_count(1, '.+1tabnext', 2)
251 call Check_tab_count(2, '+tabnext', 3)
252 call Check_tab_count(2, '+2tabnext', 4)
253 call Check_tab_count(4, '-tabnext', 3)
254 call Check_tab_count(4, '-2tabnext', 2)
255 call Check_tab_count(3, '$tabnext', 5)
256 call assert_fails('0tabnext', 'E16:')
257 call assert_fails('99tabnext', 'E16:')
258 call assert_fails('+99tabnext', 'E16:')
259 call assert_fails('-99tabnext', 'E16:')
260 call Check_tab_count(1, 'tabnext 3', 3)
261 call Check_tab_count(2, 'tabnext +', 3)
262 call Check_tab_count(2, 'tabnext +2', 4)
263 call Check_tab_count(4, 'tabnext -', 3)
264 call Check_tab_count(4, 'tabnext -2', 2)
265 call Check_tab_count(3, 'tabnext $', 5)
266 call assert_fails('tabnext 0', 'E474:')
267 call assert_fails('tabnext .', 'E474:')
268 call assert_fails('tabnext -+', 'E474:')
269 call assert_fails('tabnext +2-', 'E474:')
270 call assert_fails('tabnext $3', 'E474:')
271 call assert_fails('tabnext 99', 'E474:')
272 call assert_fails('tabnext +99', 'E474:')
273 call assert_fails('tabnext -99', 'E474:')
274
275 1tabonly!
276endfunction
277
278" Test for [count] of tabprevious
279function Test_tabpage_with_tabprevious()
280 for n in range(5)
281 tabedit
282 call setline(1, ['', '', '3'])
283 endfor
284
285 for cmd in ['tabNext', 'tabprevious']
286 call Check_tab_count(6, cmd, 5)
287 call Check_tab_count(6, '3' . cmd, 3)
288 call Check_tab_count(6, '8' . cmd, 4)
289 call Check_tab_count(6, cmd . ' 3', 3)
290 call Check_tab_count(6, cmd . ' 8', 4)
291 for n in range(2)
292 for c in ['0', '.+3', '+', '+2' , '-', '-2' , '$', '+99', '-99']
293 if n == 0 " pre count
294 let entire_cmd = c . cmd
295 let err_code = 'E16:'
296 else
297 let entire_cmd = cmd . ' ' . c
298 let err_code = 'E474:'
299 endif
300 call assert_fails(entire_cmd, err_code)
301 endfor
302 endfor
303 endfor
304
305 1tabonly!
306endfunction
307
308function s:reconstruct_tabpage_for_test(nr)
309 let n = (a:nr > 2) ? a:nr - 2 : 1
310 1tabonly!
311 0tabedit n0
312 for n in range(1, n)
313 exec '$tabedit n' . n
314 if n == 1
315 call setline(1, ['', '', '3'])
316 endif
317 endfor
318endfunc
319
320" Test for [count] of tabclose
321function Test_tabpage_with_tabclose()
322
323 " pre count
324 call s:reconstruct_tabpage_for_test(6)
325 call Check_tab_count(3, 'tabclose!', 3)
326 call Check_tab_count(1, '3tabclose', 1)
327 call Check_tab_count(4, '4tabclose', 3)
328 call Check_tab_count(3, '1tabclose', 2)
329 call Check_tab_count(2, 'tabclose', 1)
330 call assert_equal(1, tabpagenr('$'))
331 call assert_equal('', bufname(''))
332
333 call s:reconstruct_tabpage_for_test(6)
334 call Check_tab_count(2, '$tabclose', 2)
335 call Check_tab_count(4, '.tabclose', 4)
336 call Check_tab_count(3, '.+tabclose', 3)
337 call Check_tab_count(3, '.-2tabclose', 2)
338 call Check_tab_count(1, '.+1tabclose!', 1)
339 call assert_equal(1, tabpagenr('$'))
340 call assert_equal('', bufname(''))
341
342 " post count
343 call s:reconstruct_tabpage_for_test(6)
344 call Check_tab_count(3, 'tabclose!', 3)
345 call Check_tab_count(1, 'tabclose 3', 1)
346 call Check_tab_count(4, 'tabclose 4', 3)
347 call Check_tab_count(3, 'tabclose 1', 2)
348 call Check_tab_count(2, 'tabclose', 1)
349 call assert_equal(1, tabpagenr('$'))
350 call assert_equal('', bufname(''))
351
352 call s:reconstruct_tabpage_for_test(6)
353 call Check_tab_count(2, 'tabclose $', 2)
354 call Check_tab_count(4, 'tabclose', 4)
355 call Check_tab_count(3, 'tabclose +', 3)
356 call Check_tab_count(3, 'tabclose -2', 2)
357 call Check_tab_count(1, 'tabclose! +1', 1)
358 call assert_equal(1, tabpagenr('$'))
359 call assert_equal('', bufname(''))
360
361 call s:reconstruct_tabpage_for_test(6)
362 for n in range(2)
363 for c in ['0', '$3', '99', '+99', '-99']
364 if n == 0 " pre count
365 let entire_cmd = c . 'tabclose'
366 let err_code = 'E16:'
367 else
368 let entire_cmd = 'tabclose ' . c
369 let err_code = 'E474:'
370 endif
371 call assert_fails(entire_cmd, err_code)
372 call assert_equal(6, tabpagenr('$'))
373 endfor
374 endfor
375
376 call assert_fails('3tabclose', 'E37:')
377 call assert_fails('tabclose 3', 'E37:')
378 call assert_fails('tabclose -+', 'E474:')
379 call assert_fails('tabclose +2-', 'E474:')
380 call assert_equal(6, tabpagenr('$'))
381
382 1tabonly!
383endfunction
384
385" Test for [count] of tabonly
386function Test_tabpage_with_tabonly()
387
388 " Test for the normal behavior (pre count only)
389 let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
390 for c in tc
391 call s:reconstruct_tabpage_for_test(6)
392 let entire_cmd = c[1] . 'tabonly' . c[2]
393 call Check_tab_count(c[0], entire_cmd, 1)
394 call assert_equal(1, tabpagenr('$'))
395 endfor
396
397 " Test for the normal behavior
398 let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
399 \ [2, '', '!'],
400 \ [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
401 \ ]
402 for n in range(2)
403 for c in tc2
404 call s:reconstruct_tabpage_for_test(6)
405 if n == 0 " pre count
406 let entire_cmd = c[1] . 'tabonly' . c[2]
407 else
408 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
409 endif
410 call Check_tab_count(c[0], entire_cmd, 1)
411 call assert_equal(1, tabpagenr('$'))
412 endfor
413 endfor
414
415 " Test for the error behavior
416 for n in range(2)
417 for c in ['0', '$3', '99', '+99', '-99']
418 call s:reconstruct_tabpage_for_test(6)
419 if n == 0 " pre count
420 let entire_cmd = c . 'tabonly'
421 let err_code = 'E16:'
422 else
423 let entire_cmd = 'tabonly ' . c
424 let err_code = 'E474:'
425 endif
426 call assert_fails(entire_cmd, err_code)
427 call assert_equal(6, tabpagenr('$'))
428 endfor
429 endfor
430
431 " Test for the error behavior (post count only)
432 for c in tc
433 call s:reconstruct_tabpage_for_test(6)
434 let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
435 let err_code = 'E474:'
436 call assert_fails(entire_cmd, err_code)
437 call assert_equal(6, tabpagenr('$'))
438 endfor
439
440 call assert_fails('tabonly -+', 'E474:')
441 call assert_fails('tabonly +2-', 'E474:')
442 call assert_equal(6, tabpagenr('$'))
443
444 1tabonly!
445 new
446 only!
Bram Moolenaar9b7f8ce2016-08-21 19:07:17 +0200447endfunction
448
Bram Moolenaar5a497892016-09-03 16:29:04 +0200449func Test_tabnext_on_buf_unload1()
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200450 " This once caused a crash
451 new
452 tabedit
453 tabfirst
454 au BufUnload <buffer> tabnext
455 q
456
457 while tabpagenr('$') > 1
Bram Moolenaar5a497892016-09-03 16:29:04 +0200458 bwipe!
459 endwhile
460endfunc
461
462func Test_tabnext_on_buf_unload2()
463 " This once caused a crash
464 tabedit
465 autocmd BufUnload <buffer> tabnext
466 file x
467 edit y
468
469 while tabpagenr('$') > 1
470 bwipe!
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200471 endwhile
472endfunc
473
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200474func Test_close_on_quitpre()
475 " This once caused a crash
Bram Moolenaarce11de82017-10-26 22:00:00 +0200476 edit Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200477 new
478 only
479 set bufhidden=delete
480 au QuitPre <buffer> close
481 tabnew tab1
482 tabnew tab2
483 1tabn
484 q!
485 call assert_equal(1, tabpagenr())
486 call assert_equal(2, tabpagenr('$'))
487 " clean up
488 while tabpagenr('$') > 1
489 bwipe!
490 endwhile
Bram Moolenaarce11de82017-10-26 22:00:00 +0200491 buf Xtest
Bram Moolenaar0ea50702017-07-08 14:44:50 +0200492endfunc
Bram Moolenaar11fbc282016-09-02 21:48:32 +0200493
Bram Moolenaar1381d792016-08-18 22:11:42 +0200494" vim: shiftwidth=2 sts=2 expandtab