test_tabline.vim (5693B)
1 " Test for tabline 2 3 source shared.vim 4 source view_util.vim 5 source check.vim 6 source screendump.vim 7 8 func TablineWithCaughtError() 9 let s:func_in_tabline_called = 1 10 try 11 call eval('unknown expression') 12 catch 13 endtry 14 return '' 15 endfunc 16 17 func TablineWithError() 18 let s:func_in_tabline_called = 1 19 call eval('unknown expression') 20 return '' 21 endfunc 22 23 func 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 37 endfunc 38 39 func 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 56 endfunc 57 58 func 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 76 endfunc 77 78 " Test for the "%T" and "%X" flags in the 'tabline' option 79 func 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 98 endfunc 99 100 func MyTabLabel(n) 101 let buflist = tabpagebuflist(a:n) 102 let winnr = tabpagewinnr(a:n) 103 return bufname(buflist[winnr - 1]) 104 endfunc 105 106 func 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! 117 endfunc 118 119 function EmptyTabname() 120 return "" 121 endfunction 122 123 function 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' 128 endfunction 129 130 func Test_tabline_empty_group() 131 " this was reading invalid memory 132 set tabline=%!MakeTabLine() 133 tabnew 134 redraw! 135 136 bw! 137 set tabline= 138 endfunc 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. 143 func 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& 151 endfunc 152 153 func 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') 164 endfunc 165 166 func 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) 205 endfunc 206 207 func TruncTabLine() 208 return '%1T口口%2Ta' .. repeat('b', &columns - 4) .. '%999X%#TabLine#c' 209 endfunc 210 211 " Test 'tabline' with truncated double-width label at the start. 212 func Test_tabline_truncated_double_width() 213 let save_TabLine = nvim_get_hl(0, #{name: 'TabLine'}) 214 " Nvim: avoid combining TabLine with TabLineFill in custom tabline 215 hi TabLine cterm=underline,nocombine gui=underline,nocombine 216 tabnew 217 redraw 218 call assert_match('X$', Screenline(1)) 219 let attr_TabLineFill = screenattr(1, &columns - 1) 220 let attr_TabLine = screenattr(1, &columns) 221 call assert_notequal(attr_TabLine, attr_TabLineFill) 222 223 set tabline=%!TruncTabLine() 224 redraw 225 call assert_equal('<a' .. repeat('b', &columns - 4) .. 'c', Screenline(1)) 226 call assert_equal(attr_TabLineFill, screenattr(1, &columns - 2)) 227 call assert_equal(attr_TabLine, screenattr(1, &columns - 1)) 228 call assert_equal(attr_TabLine, screenattr(1, &columns)) 229 230 bw! 231 set tabline= 232 call nvim_set_hl(0, 'TabLine', save_TabLine) 233 endfunc 234 235 " vim: shiftwidth=2 sts=2 expandtab