xref: /vim-8.2.3635/src/testdir/test_tabpage.vim (revision bc2eada5)
1" 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
14  %del
15  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
44  if has('gui') || has('clientserver')
45    " 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
77  endif
78  "
79  "
80  for i in range(9) | tabnew | endfor
81  normal! 1gt
82  call assert_equal(1, tabpagenr())
83  tabmove 5
84  call assert_equal(5, tabpagenr())
85  .tabmove
86  call assert_equal(5, tabpagenr())
87  tabmove -
88  call assert_equal(4, tabpagenr())
89  tabmove +
90  call assert_equal(5, tabpagenr())
91  tabmove -2
92  call assert_equal(3, tabpagenr())
93  tabmove +4
94  call assert_equal(7, tabpagenr())
95  tabmove
96  call assert_equal(10, tabpagenr())
97  tabmove -20
98  call assert_equal(1, tabpagenr())
99  tabmove +20
100  call assert_equal(10, tabpagenr())
101  0tabmove
102  call assert_equal(1, tabpagenr())
103  $tabmove
104  call assert_equal(10, tabpagenr())
105  tabmove 0
106  call assert_equal(1, tabpagenr())
107  tabmove $
108  call assert_equal(10, tabpagenr())
109  3tabmove
110  call assert_equal(4, tabpagenr())
111  7tabmove 5
112  call assert_equal(5, tabpagenr())
113  call assert_fails("tabmove foo", 'E474:')
114endfunc
115
116" Test autocommands
117function Test_tabpage_with_autocmd()
118  if !has('autocmd')
119    return
120  endif
121  tabonly!
122  command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
123  augroup TestTabpageGroup
124    au!
125    autocmd TabEnter * call add(s:li, 'TabEnter')
126    autocmd WinEnter * call add(s:li, 'WinEnter')
127    autocmd BufEnter * call add(s:li, 'BufEnter')
128    autocmd TabLeave * call add(s:li, 'TabLeave')
129    autocmd WinLeave * call add(s:li, 'WinLeave')
130    autocmd BufLeave * call add(s:li, 'BufLeave')
131  augroup END
132
133  let s:li = []
134  let t:a='a'
135  C tab split
136  call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
137  let s:li = []
138  let t:a='b'
139  C tabnew
140  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
141  let t:a='c'
142  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
143  call assert_equal(['a', 'b', 'c'], s:li)
144
145  let s:li = []
146  C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
147  call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
148  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
149  call assert_equal(['2', '4', '6'], s:li)
150
151  let s:li = []
152  let w:a='a'
153  C vsplit
154  call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
155  let s:li = []
156  let w:a='a'
157  let tabn=tabpagenr()
158  let winr=range(1, winnr('$'))
159  C tabnext 1
160  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
161  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
162  call assert_equal(['a', 'a'], s:li)
163  let s:li = []
164  C call map(copy(winr), 'settabwinvar('.tabn.', v:val, ''a'', v:val*2)')
165  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
166  call assert_equal(['2', '4'], s:li)
167
168  augroup TabDestructive
169    autocmd TabEnter * :C tabnext 2 | C tabclose 3
170  augroup END
171  let s:li = []
172  C tabnext 3
173  call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
174  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
175
176  autocmd! TabDestructive TabEnter
177  let s:li = []
178  C tabnew
179  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
180  let s:li = []
181  C tabnext 1
182  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
183
184  autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
185  let s:li = []
186  C tabnext 3
187  call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ===', 'BufEnter', '=== tabclose 3 ==='], s:li)
188  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
189
190  delcommand C
191  autocmd! TabDestructive
192  augroup! TabDestructive
193  autocmd! TestTabpageGroup
194  augroup! TestTabpageGroup
195  tabonly!
196  bw!
197endfunction
198
199function Test_tabpage_with_tab_modifier()
200  for n in range(4)
201    tabedit
202  endfor
203
204  function s:check_tab(pre_nr, cmd, post_nr)
205    exec 'tabnext ' . a:pre_nr
206    exec a:cmd
207    call assert_equal(a:post_nr, tabpagenr())
208    call assert_equal('help', &buftype)
209    helpclose
210  endfunc
211
212  call s:check_tab(1, 'tab help', 2)
213  call s:check_tab(1, '3tab help', 4)
214  call s:check_tab(1, '.tab help', 2)
215  call s:check_tab(1, '.+1tab help', 3)
216  call s:check_tab(1, '0tab help', 1)
217  call s:check_tab(2, '+tab help', 4)
218  call s:check_tab(2, '+2tab help', 5)
219  call s:check_tab(4, '-tab help', 4)
220  call s:check_tab(4, '-2tab help', 3)
221  call s:check_tab(3, '$tab help', 6)
222  call assert_fails('99tab help', 'E16:')
223  call assert_fails('+99tab help', 'E16:')
224  call assert_fails('-99tab help', 'E16:')
225
226  delfunction s:check_tab
227  tabonly!
228  bw!
229endfunction
230
231func Test_tabnext_on_buf_unload1()
232  " This once caused a crash
233  new
234  tabedit
235  tabfirst
236  au BufUnload <buffer> tabnext
237  q
238
239  while tabpagenr('$') > 1
240    bwipe!
241  endwhile
242endfunc
243
244func Test_tabnext_on_buf_unload2()
245  " This once caused a crash
246  tabedit
247  autocmd BufUnload <buffer> tabnext
248  file x
249  edit y
250
251  while tabpagenr('$') > 1
252    bwipe!
253  endwhile
254endfunc
255
256
257" vim: shiftwidth=2 sts=2 expandtab
258