robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqMerge pull request #40977 from zeertzjq/vim-9.2.0806e5aac37eb3

master
6.3 KiB263 linesraw
1" Test for tabline
2
3source shared.vim
4source view_util.vim
5source check.vim
6source screendump.vim
7
8func TablineWithCaughtError()
9  let s:func_in_tabline_called = 1
10  try
11    call eval('unknown expression')
12  catch
13  endtry
14  return ''
15endfunc
16
17func TablineWithError()
18  let s:func_in_tabline_called = 1
19  call eval('unknown expression')
20  return ''
21endfunc
22
23func Test_caught_error_in_tabline()
24  if has('gui')
25    set guioptions-=e
26  endif
27  let showtabline_save = &showtabline
28  set showtabline=2
29  let s:func_in_tabline_called = 0
30  let tabline = '%{TablineWithCaughtError()}'
31  let &tabline = tabline
32  redraw!
33  call assert_true(s:func_in_tabline_called)
34  call assert_equal(tabline, &tabline)
35  set tabline=
36  let &showtabline = showtabline_save
37endfunc
38
39func Test_tabline_will_be_disabled_with_error()
40  if has('gui')
41    set guioptions-=e
42  endif
43  let showtabline_save = &showtabline
44  set showtabline=2
45  let s:func_in_tabline_called = 0
46  let tabline = '%{TablineWithError()}'
47  try
48    let &tabline = tabline
49    redraw!
50  catch
51  endtry
52  call assert_true(s:func_in_tabline_called)
53  call assert_equal('', &tabline)
54  set tabline=
55  let &showtabline = showtabline_save
56endfunc
57
58func Test_redrawtabline()
59  if has('gui')
60    set guioptions-=e
61  endif
62  let showtabline_save = &showtabline
63  set showtabline=2
64  set tabline=%{bufnr('$')}
65  edit Xtabline1
66  edit Xtabline2
67  redraw
68  call assert_match(bufnr('$') . '', Screenline(1))
69  au BufAdd * redrawtabline
70  badd Xtabline3
71  call assert_match(bufnr('$') . '', Screenline(1))
72
73  set tabline=
74  let &showtabline = showtabline_save
75  au! Bufadd
76endfunc
77
78" Test for the "%T" and "%X" flags in the 'tabline' option
79func MyTabLine()
80  let s = ''
81  for i in range(tabpagenr('$'))
82    " set the tab page number (for mouse clicks)
83    let s .= '%' . (i + 1) . 'T'
84
85    " the label is made by MyTabLabel()
86    let s .= ' %{MyTabLabel(' . (i + 1) . ')} '
87  endfor
88
89  " after the last tab fill with TabLineFill and reset tab page nr
90  let s .= '%T'
91
92  " right-align the label to close the current tab page
93  if tabpagenr('$') > 1
94    let s .= '%=%Xclose'
95  endif
96
97  return s
98endfunc
99
100func MyTabLabel(n)
101  let buflist = tabpagebuflist(a:n)
102  let winnr = tabpagewinnr(a:n)
103  return bufname(buflist[winnr - 1])
104endfunc
105
106func Test_tabline_flags()
107  if has('gui')
108    set guioptions-=e
109  endif
110  set tabline=%!MyTabLine()
111  edit Xtabline1
112  tabnew Xtabline2
113  redrawtabline
114  call assert_match('^ Xtabline1  Xtabline2\s\+close$', Screenline(1))
115  set tabline=
116  %bw!
117endfunc
118
119function EmptyTabname()
120  return ""
121endfunction
122
123function MakeTabLine() abort
124  let titles = map(range(1, tabpagenr('$')), '"%( %" . v:val . "T%{EmptyTabname()}%T %)"')
125  let sep = 'あ'
126  let tabpages = join(titles, sep)
127  return tabpages .. sep .. '%=%999X X'
128endfunction
129
130func Test_tabline_empty_group()
131  " this was reading invalid memory
132  set tabline=%!MakeTabLine()
133  tabnew
134  redraw!
135
136  bw!
137  set tabline=
138endfunc
139
140" When there are exactly 20 tabline format items (the exact size of the
141" initial tabline items array), test that we don't write beyond the size
142" of the array.
143func Test_tabline_20_format_items_no_overrun()
144  set showtabline=2
145
146  let tabline = repeat('%#StatColorHi2#', 20)
147  let &tabline = tabline
148  redrawtabline
149
150  set showtabline& tabline&
151endfunc
152
153func Test_mouse_click_in_tab()
154  " This used to crash because TabPageIdxs[] was not initialized
155  let lines =<< trim END
156      tabnew
157      set mouse=a
158      exe "norm \<LeftMouse>"
159  END
160  call writefile(lines, 'Xclickscript')
161  call RunVim([], [], "-e -s -S Xclickscript -c qa")
162
163  call delete('Xclickscript')
164endfunc
165
166func Test_tabline_showcmd()
167  CheckScreendump
168
169  let lines =<< trim END
170    func MyTabLine()
171      return '%S'
172    endfunc
173
174    set showtabline=2
175    set tabline=%!MyTabLine()
176    set showcmdloc=tabline
177    call setline(1, ['a', 'b', 'c'])
178    set foldopen+=jump
179    1,2fold
180    3
181  END
182  call writefile(lines, 'XTest_tabline', 'D')
183
184  let buf = RunVimInTerminal('-S XTest_tabline', {'rows': 6})
185
186  call term_sendkeys(buf, "g")
187  call VerifyScreenDump(buf, 'Test_tabline_showcmd_1', {})
188
189  " typing "gg" should open the fold
190  call term_sendkeys(buf, "g")
191  call VerifyScreenDump(buf, 'Test_tabline_showcmd_2', {})
192
193  call term_sendkeys(buf, "\<C-V>Gl")
194  call VerifyScreenDump(buf, 'Test_tabline_showcmd_3', {})
195
196  call term_sendkeys(buf, "\<Esc>1234")
197  call VerifyScreenDump(buf, 'Test_tabline_showcmd_4', {})
198
199  call term_sendkeys(buf, "\<Esc>:set tabline=\<CR>")
200  call term_sendkeys(buf, ":\<CR>")
201  call term_sendkeys(buf, "1234")
202  call VerifyScreenDump(buf, 'Test_tabline_showcmd_5', {})
203
204  call StopVimInTerminal(buf)
205endfunc
206
207func Test_tabline_showcmd_redraw_tabline()
208  CheckRunVimInTerminal
209
210  let lines =<< trim END
211    set showcmd showcmdloc=tabline showtabline=2 tabline=%S timeoutlen=0
212    nnoremap g :redraw<CR>
213    nnoremap gc <Nop>
214  END
215  call writefile(lines, 'XTest_tabline_showcmd_redraw', 'D')
216
217  let buf = RunVimInTerminal('-S XTest_tabline_showcmd_redraw', #{rows: 6, cols: 40})
218  call term_sendkeys(buf, 'g')
219  call WaitForAssert({-> assert_match(':redraw', term_getline(buf, 6))})
220  call WaitForAssert({-> assert_notmatch('^:', term_getline(buf, 1))})
221
222  call StopVimInTerminal(buf)
223endfunc
224
225func TruncTabLine()
226  return '%1T口口%2Ta' .. repeat('b', &columns - 4) .. '%999X%#TabLine#c'
227endfunc
228
229" Test 'tabline' with truncated double-width label at the start.
230func Test_tabline_truncated_double_width()
231  let save_TabLine = nvim_get_hl(0, #{name: 'TabLine'})
232  " Nvim: avoid combining TabLine with TabLineFill in custom tabline
233  hi TabLine cterm=underline,nocombine gui=underline,nocombine
234  tabnew
235  redraw
236  call assert_match('X$', Screenline(1))
237  let attr_TabLineFill = screenattr(1, &columns - 1)
238  let attr_TabLine = screenattr(1, &columns)
239  call assert_notequal(attr_TabLine, attr_TabLineFill)
240
241  set tabline=%!TruncTabLine()
242  redraw
243  call assert_equal('<a' .. repeat('b', &columns - 4) .. 'c', Screenline(1))
244  call assert_equal(attr_TabLineFill, screenattr(1, &columns - 2))
245  call assert_equal(attr_TabLine, screenattr(1, &columns - 1))
246  call assert_equal(attr_TabLine, screenattr(1, &columns))
247
248  bw!
249  set tabline=
250  call nvim_set_hl(0, 'TabLine', save_TabLine)
251endfunc
252
253func Test_tabline_empty()
254  set showtabline=2 tabline=%!'%{}%'
255  try
256    redraw!
257  catch
258  endtry
259  set showtabline&
260  set tabline&
261endfunc
262
263" vim: shiftwidth=2 sts=2 expandtab