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