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