robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Island of misfit toys. 2--- @diagnostic disable: deprecated 3 4local t =require ( 'test.testutil' ) 5local n =require ( 'test.functional.testnvim' )() 6local Screen =require ( 'test.functional.ui.screen' ) 7local describe , it , before_each =t . describe , t . it , t . before_each 8local clear , eval , eq , ok =n . clear , n . eval , t . eq , t . ok 9local api , command , fn =n . api , n . command , n . fn 10local pcall_err , assert_alive =t . pcall_err , n . assert_alive 11local insert , exec , feed =n . insert , n . exec , n . feed 12 13describe ( 'deprecated' , function () 14before_each ( n . clear ) 15 16describe ( 'nvim_notify' , function () 17it ( 'can notify a info message' , function () 18n . api . nvim_notify ( 'hello world' , 2 , {} ) 19end ) 20 21it ( 'can be overridden' , function () 22n . command ( 'lua vim.notify = function(...) return 42 end' ) 23t . eq ( 42 , n . api . nvim_exec_lua ( "return vim.notify('Hello world')" , {} )) 24n . api . nvim_notify ( 'hello world' , 4 , {} ) 25end ) 26end ) 27 28describe ( 'nvim_*get_option functions' , function () 29it ( 'does not leak memory' , function () 30-- String opts caused memory leaks in these functions in Github#32361 31n . exec_lua ( [[ 32vim.api.nvim_get_option('rtp') 33vim.api.nvim_win_get_option(vim.api.nvim_get_current_win(), 'foldmethod') 34vim.api.nvim_buf_get_option(0, 'fileformat') 35]] ) 36end ) 37end ) 38 39describe ( 'API: highlight' , function () 40clear () 41Screen . new () -- initialize Screen.colors 42 43local expected_rgb ={ 44background =Screen . colors . Yellow , 45foreground =Screen . colors . Red , 46special =Screen . colors . Blue , 47bold =true , 48} 49local expected_cterm ={ 50background =10 , 51underline =true , 52} 53local expected_rgb2 ={ 54background =Screen . colors . Yellow , 55foreground =Screen . colors . Red , 56special =Screen . colors . Blue , 57bold =true , 58italic =true , 59reverse =true , 60underline =true , 61strikethrough =true , 62altfont =true , 63nocombine =true , 64} 65local expected_undercurl ={ 66background =Screen . colors . Yellow , 67foreground =Screen . colors . Red , 68special =Screen . colors . Blue , 69undercurl =true , 70} 71 72before_each ( function () 73clear () 74command ( 75'hi NewHighlight cterm=underline ctermbg=green guifg=red guibg=yellow guisp=blue gui=bold' 76) 77end ) 78 79it ( 'nvim_get_hl_by_id' , function () 80local hl_id =eval ( "hlID('NewHighlight')" ) 81eq ( expected_cterm , api . nvim_get_hl_by_id ( hl_id , false )) 82 83hl_id =eval ( "hlID('NewHighlight')" ) 84-- Test valid id. 85eq ( expected_rgb , api . nvim_get_hl_by_id ( hl_id , true )) 86 87-- Test invalid id. 88eq ( 'Invalid highlight id: 30000' , pcall_err ( api . nvim_get_hl_by_id , 30000 , false )) 89 90-- Test all highlight properties. 91command ( 'hi NewHighlight gui=underline,bold,italic,reverse,strikethrough,altfont,nocombine' ) 92eq ( expected_rgb2 , api . nvim_get_hl_by_id ( hl_id , true )) 93 94-- Test undercurl 95command ( 'hi NewHighlight gui=undercurl' ) 96eq ( expected_undercurl , api . nvim_get_hl_by_id ( hl_id , true )) 97 98-- Test nil argument. 99eq ( 100'Wrong type for argument 1 when calling nvim_get_hl_by_id, expecting Integer' , 101pcall_err ( api . nvim_get_hl_by_id , { nil } , false ) 102) 103 104-- Test 0 argument. 105eq ( 'Invalid highlight id: 0' , pcall_err ( api . nvim_get_hl_by_id , 0 , false )) 106 107-- Test -1 argument. 108eq ( 'Invalid highlight id: -1' , pcall_err ( api . nvim_get_hl_by_id , - 1 , false )) 109 110-- Test highlight group without ctermbg value. 111command ( 'hi Normal ctermfg=red ctermbg=yellow' ) 112command ( 'hi NewConstant ctermfg=green guifg=white guibg=blue' ) 113hl_id =eval ( "hlID('NewConstant')" ) 114eq ( { foreground =10 } , api . nvim_get_hl_by_id ( hl_id , false )) 115 116-- Test highlight group without ctermfg value. 117command ( 'hi clear NewConstant' ) 118command ( 'hi NewConstant ctermbg=Magenta guifg=white guibg=blue' ) 119eq ( { background =13 } , api . nvim_get_hl_by_id ( hl_id , false )) 120 121-- Test highlight group with ctermfg and ctermbg values. 122command ( 'hi clear NewConstant' ) 123command ( 'hi NewConstant ctermfg=green ctermbg=Magenta guifg=white guibg=blue' ) 124eq ( { foreground =10 , background =13 } , api . nvim_get_hl_by_id ( hl_id , false )) 125end ) 126 127it ( 'nvim_get_hl_by_name' , function () 128local expected_normal ={ background =Screen . colors . Yellow , foreground =Screen . colors . Red } 129 130-- Test `Normal` default values. 131eq ( {} , api . nvim_get_hl_by_name ( 'Normal' , true )) 132 133eq ( expected_cterm , api . nvim_get_hl_by_name ( 'NewHighlight' , false )) 134eq ( expected_rgb , api . nvim_get_hl_by_name ( 'NewHighlight' , true )) 135 136-- Test `Normal` modified values. 137command ( 'hi Normal guifg=red guibg=yellow' ) 138eq ( expected_normal , api . nvim_get_hl_by_name ( 'Normal' , true )) 139 140-- Test invalid name. 141eq ( 142"Invalid highlight name: 'unknown_highlight'" , 143pcall_err ( api . nvim_get_hl_by_name , 'unknown_highlight' , false ) 144) 145 146-- Test nil argument. 147eq ( 148'Wrong type for argument 1 when calling nvim_get_hl_by_name, expecting String' , 149pcall_err ( api . nvim_get_hl_by_name , { nil } , false ) 150) 151 152-- Test empty string argument. 153eq ( 'Invalid highlight name' , pcall_err ( api . nvim_get_hl_by_name , '' , false )) 154 155-- Test "standout" attribute. #8054 156eq ( { underline =true } , api . nvim_get_hl_by_name ( 'cursorline' , false )) 157command ( 158'hi CursorLine cterm=standout,underline term=standout,underline gui=standout,underline' 159) 160command ( 'set cursorline' ) 161eq ( { underline =true , standout =true } , api . nvim_get_hl_by_name ( 'cursorline' , false )) 162 163-- Test cterm & Normal values. #18024 (tail) & #18980 164-- Ensure Normal, and groups that match Normal return their fg & bg cterm values 165api . nvim_set_hl ( 0 , 'Normal' , { ctermfg =17 , ctermbg =213 } ) 166api . nvim_set_hl ( 0 , 'NotNormal' , { ctermfg =17 , ctermbg =213 , nocombine =true } ) 167-- Note colors are "cterm" values, not rgb-as-ints 168eq ( { foreground =17 , background =213 } , api . nvim_get_hl_by_name ( 'Normal' , false )) 169eq ( 170{ foreground =17 , background =213 , nocombine =true } , 171api . nvim_get_hl_by_name ( 'NotNormal' , false ) 172) 173end ) 174 175it ( 'nvim_get_hl_id_by_name' , function () 176-- precondition: use a hl group that does not yet exist 177eq ( 178"Invalid highlight name: 'Shrubbery'" , 179pcall_err ( api . nvim_get_hl_by_name , 'Shrubbery' , true ) 180) 181eq ( 0 , fn . hlID ( 'Shrubbery' )) 182 183local hl_id =api . nvim_get_hl_id_by_name ( 'Shrubbery' ) 184ok ( hl_id > 0 ) 185eq ( hl_id , fn . hlID ( 'Shrubbery' )) 186 187command ( 'hi Shrubbery guifg=#888888 guibg=#888888' ) 188eq ( 189{ foreground =tonumber ( '0x888888' ), background =tonumber ( '0x888888' ) } , 190api . nvim_get_hl_by_id ( hl_id , true ) 191) 192eq ( 193{ foreground =tonumber ( '0x888888' ), background =tonumber ( '0x888888' ) } , 194api . nvim_get_hl_by_name ( 'Shrubbery' , true ) 195) 196end ) 197 198it ( "nvim_buf_add_highlight to other buffer doesn't crash if undo is disabled #12873" , function () 199command ( 'vsplit file' ) 200local err , _ =pcall ( api . nvim_set_option_value , 'undofile' , false , { buf =1 } ) 201eq ( true , err ) 202err , _ =pcall ( api . nvim_set_option_value , 'undolevels' , - 1 , { buf =1 } ) 203eq ( true , err ) 204err , _ =pcall ( api . nvim_buf_add_highlight , 1 , - 1 , 'Question' , 0 , 0 , - 1 ) 205eq ( true , err ) 206assert_alive () 207end ) 208end ) 209 210describe ( '{get,set}_height' , function () 211it ( 'works' , function () 212command ( 'vsplit' ) 213eq ( 214api . nvim_win_get_height ( api . nvim_list_wins ()[ 2 ]), 215api . nvim_win_get_height ( api . nvim_list_wins ()[ 1 ]) 216) 217api . nvim_set_current_win ( api . nvim_list_wins ()[ 2 ]) 218command ( 'split' ) 219eq ( 220api . nvim_win_get_height ( api . nvim_list_wins ()[ 2 ]), 221math . floor ( api . nvim_win_get_height ( api . nvim_list_wins ()[ 1 ]) / 2 ) 222) 223api . nvim_win_set_height ( api . nvim_list_wins ()[ 2 ], 2 ) 224eq ( 2 , api . nvim_win_get_height ( api . nvim_list_wins ()[ 2 ])) 225end ) 226 227it ( 'failure modes' , function () 228command ( 'split' ) 229eq ( 'Invalid window id: 999999' , pcall_err ( api . nvim_win_set_height , 999999 , 10 )) 230eq ( 231'Wrong type for argument 2 when calling nvim_win_set_height, expecting Integer' , 232pcall_err ( api . nvim_win_set_height , 0 , 0.9 ) 233) 234end ) 235 236it ( 'correctly handles height=1' , function () 237command ( 'split' ) 238api . nvim_set_current_win ( api . nvim_list_wins ()[ 1 ]) 239api . nvim_win_set_height ( api . nvim_list_wins ()[ 2 ], 1 ) 240eq ( 1 , api . nvim_win_get_height ( api . nvim_list_wins ()[ 2 ])) 241end ) 242 243it ( 'correctly handles height=1 with a winbar' , function () 244command ( 'set winbar=foobar' ) 245command ( 'set winminheight=0' ) 246command ( 'split' ) 247api . nvim_set_current_win ( api . nvim_list_wins ()[ 1 ]) 248api . nvim_win_set_height ( api . nvim_list_wins ()[ 2 ], 1 ) 249eq ( 1 , api . nvim_win_get_height ( api . nvim_list_wins ()[ 2 ])) 250end ) 251 252it ( 'do not cause ml_get errors with foldmethod=expr #19989' , function () 253insert ( [[ 254aaaaa 255bbbbb 256ccccc]] ) 257command ( 'set foldmethod=expr' ) 258exec ( [[ 259new 260let w = nvim_get_current_win() 261wincmd w 262call nvim_win_set_height(w, 5) 263]] ) 264feed ( 'l' ) 265eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 266end ) 267end ) 268 269describe ( '{get,set}_width' , function () 270it ( 'works' , function () 271command ( 'split' ) 272eq ( 273api . nvim_win_get_width ( api . nvim_list_wins ()[ 2 ]), 274api . nvim_win_get_width ( api . nvim_list_wins ()[ 1 ]) 275) 276api . nvim_set_current_win ( api . nvim_list_wins ()[ 2 ]) 277command ( 'vsplit' ) 278eq ( 279api . nvim_win_get_width ( api . nvim_list_wins ()[ 2 ]), 280math . floor ( api . nvim_win_get_width ( api . nvim_list_wins ()[ 1 ]) / 2 ) 281) 282api . nvim_win_set_width ( api . nvim_list_wins ()[ 2 ], 2 ) 283eq ( 2 , api . nvim_win_get_width ( api . nvim_list_wins ()[ 2 ])) 284end ) 285 286it ( 'failure modes' , function () 287command ( 'vsplit' ) 288eq ( 'Invalid window id: 999999' , pcall_err ( api . nvim_win_set_width , 999999 , 10 )) 289eq ( 290'Wrong type for argument 2 when calling nvim_win_set_width, expecting Integer' , 291pcall_err ( api . nvim_win_set_width , 0 , 0.9 ) 292) 293end ) 294 295it ( 'do not cause ml_get errors with foldmethod=expr #19989' , function () 296insert ( [[ 297aaaaa 298bbbbb 299ccccc]] ) 300command ( 'set foldmethod=expr' ) 301exec ( [[ 302vnew 303let w = nvim_get_current_win() 304wincmd w 305call nvim_win_set_width(w, 5) 306]] ) 307feed ( 'l' ) 308eq ( '' , api . nvim_get_vvar ( 'errmsg' )) 309end ) 310end ) 311end )