xref: /vim-8.2.3635/src/testdir/test_tabpage.vim (revision cb03397a)
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  tabdo call append(line('$'), tabpagenr())
15  tabclose! 2
16  tabrewind
17  let line1 = getline('$')
18  undo
19  q
20  tablast
21  let line2 = getline('$')
22  q!
23  call append(line('$'), line1)
24  call append(line('$'), line2)
25  unlet line1 line2
26  call assert_equal(['', '3', '1', '4'], getline(1, '$'))
27  "
28  " Test for settabvar() and gettabvar() functions. Open a new tab page and
29  " set 3 variables to a number, string and a list. Verify that the variables
30  " are correctly set.
31  tabnew
32  tabfirst
33  call settabvar(2, 'val_num', 100)
34  call settabvar(2, 'val_str', 'SetTabVar test')
35  call settabvar(2, 'val_list', ['red', 'blue', 'green'])
36  "
37  call assert_true(gettabvar(2, 'val_num') == 100 && gettabvar(2, 'val_str') == 'SetTabVar test' && gettabvar(2, 'val_list') == ['red', 'blue', 'green'])
38
39  tabnext 2
40  call assert_true(t:val_num == 100 && t:val_str == 'SetTabVar test'  && t:val_list == ['red', 'blue', 'green'])
41  tabclose
42
43  if has('gui') || has('clientserver')
44    " 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  endif
68  "
69  "
70  for i in range(9) | tabnew | endfor
71  normal! 1gt
72  call assert_equal(1, tabpagenr())
73  tabmove 5
74  call assert_equal(5, tabpagenr())
75  .tabmove
76  call assert_equal(5, tabpagenr())
77  tabmove -
78  call assert_equal(4, tabpagenr())
79  tabmove +
80  call assert_equal(5, tabpagenr())
81  tabmove -2
82  call assert_equal(3, tabpagenr())
83  tabmove +4
84  call assert_equal(7, tabpagenr())
85  tabmove
86  call assert_equal(10, tabpagenr())
87  tabmove -20
88  call assert_equal(1, tabpagenr())
89  tabmove +20
90  call assert_equal(10, tabpagenr())
91  0tabmove
92  call assert_equal(1, tabpagenr())
93  $tabmove
94  call assert_equal(10, tabpagenr())
95  tabmove 0
96  call assert_equal(1, tabpagenr())
97  tabmove $
98  call assert_equal(10, tabpagenr())
99  3tabmove
100  call assert_equal(4, tabpagenr())
101  7tabmove 5
102  call assert_equal(5, tabpagenr())
103  call assert_fails("tabmove foo", 'E474:')
104endfunc
105
106" Test autocommands
107function Test_tabpage_with_autocmd()
108  if !has('autocmd')
109    return
110  endif
111  tabonly!
112  command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
113  augroup TestTabpageGroup
114    au!
115    autocmd TabEnter * call add(s:li, 'TabEnter')
116    autocmd WinEnter * call add(s:li, 'WinEnter')
117    autocmd BufEnter * call add(s:li, 'BufEnter')
118    autocmd TabLeave * call add(s:li, 'TabLeave')
119    autocmd WinLeave * call add(s:li, 'WinLeave')
120    autocmd BufLeave * call add(s:li, 'BufLeave')
121  augroup END
122
123  let s:li = []
124  let t:a='a'
125  C tab split
126  call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
127  let s:li = []
128  let t:a='b'
129  C tabnew
130  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
131  let t:a='c'
132  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
133  call assert_equal(['a', 'b', 'c'], s:li)
134
135  let s:li = []
136  C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
137  call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
138  let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
139  call assert_equal(['2', '4', '6'], s:li)
140
141  let s:li = []
142  let w:a='a'
143  C vsplit
144  call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
145  let s:li = []
146  let w:a='a'
147  let tabn=tabpagenr()
148  let winr=range(1, winnr('$'))
149  C tabnext 1
150  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
151  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
152  call assert_equal(['a', 'a'], s:li)
153  let s:li = []
154  C call map(copy(winr), 'settabwinvar('.tabn.', v:val, ''a'', v:val*2)')
155  let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
156  call assert_equal(['2', '4'], s:li)
157
158  augroup TabDestructive
159    autocmd TabEnter * :C tabnext 2 | C tabclose 3
160  augroup END
161  let s:li = []
162  C tabnext 3
163  call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
164  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
165
166  autocmd! TabDestructive TabEnter
167  let s:li = []
168  C tabnew
169  call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
170  let s:li = []
171  C tabnext 1
172  call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
173
174  autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
175  let s:li = []
176  C tabnext 3
177  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)
178  call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
179
180  delcommand C
181  autocmd! TabDestructive
182  augroup! TabDestructive
183  autocmd! TestTabpageGroup
184  augroup! TestTabpageGroup
185  tabonly!
186  bw!
187endfunction
188
189function Test_tabpage_with_tab_modifier()
190  for n in range(4)
191    tabedit
192  endfor
193
194  function s:check_tab(pre_nr, cmd, post_nr)
195    exec 'tabnext ' . a:pre_nr
196    exec a:cmd
197    call assert_equal(a:post_nr, tabpagenr())
198    call assert_equal('help', &filetype)
199    helpclose
200  endfunc
201
202  call s:check_tab(1, 'tab help', 2)
203  call s:check_tab(1, '3tab help', 4)
204  call s:check_tab(1, '.tab help', 2)
205  call s:check_tab(1, '.+1tab help', 3)
206  call s:check_tab(1, '0tab help', 1)
207  call s:check_tab(2, '+tab help', 4)
208  call s:check_tab(2, '+2tab help', 5)
209  call s:check_tab(4, '-tab help', 4)
210  call s:check_tab(4, '-2tab help', 3)
211  call s:check_tab(3, '$tab help', 6)
212  call assert_fails('99tab help', 'E16:')
213  call assert_fails('+99tab help', 'E16:')
214  call assert_fails('-99tab help', 'E16:')
215
216  delfunction s:check_tab
217  tabonly!
218  bw!
219endfunction
220
221" vim: shiftwidth=2 sts=2 expandtab
222