robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local clear , eq , ok =n . clear , t . eq , t . ok 7local matches =t . matches 8local exec =n . exec 9local api =n . api 10local fn =n . fn 11local request =n . request 12local NIL =vim . NIL 13local pcall_err =t . pcall_err 14local command =n . command 15 16describe ( 'api/tabpage' , function () 17before_each ( clear ) 18 19describe ( 'list_wins and get_win' , function () 20it ( 'works' , function () 21command ( 'tabnew' ) 22command ( 'vsplit' ) 23local tab1 , tab2 =unpack ( api . nvim_list_tabpages ()) 24local win1 , win2 , win3 =unpack ( api . nvim_list_wins ()) 25eq ( { win1 } , api . nvim_tabpage_list_wins ( tab1 )) 26eq ( win1 , api . nvim_tabpage_get_win ( tab1 )) 27eq ( { win2 , win3 } , api . nvim_tabpage_list_wins ( tab2 )) 28eq ( win2 , api . nvim_tabpage_get_win ( tab2 )) 29api . nvim_set_current_win ( win3 ) 30eq ( win3 , api . nvim_tabpage_get_win ( tab2 )) 31command ( 'tabprev' ) 32eq ( win1 , api . nvim_tabpage_get_win ( tab1 )) 33eq ( win3 , api . nvim_tabpage_get_win ( tab2 )) 34end ) 35 36it ( 'validates args' , function () 37eq ( 'Invalid tabpage id: 23' , pcall_err ( api . nvim_tabpage_list_wins , 23 )) 38end ) 39end ) 40 41describe ( 'set_win' , function () 42it ( 'works' , function () 43command ( 'tabnew' ) 44command ( 'vsplit' ) 45local tab1 , tab2 =unpack ( api . nvim_list_tabpages ()) 46local win1 , win2 , win3 =unpack ( api . nvim_list_wins ()) 47eq ( { win1 } , api . nvim_tabpage_list_wins ( tab1 )) 48eq ( { win2 , win3 } , api . nvim_tabpage_list_wins ( tab2 )) 49eq ( win2 , api . nvim_tabpage_get_win ( tab2 )) 50api . nvim_tabpage_set_win ( tab2 , win3 ) 51eq ( win3 , api . nvim_tabpage_get_win ( tab2 )) 52end ) 53 54it ( 'works in non-current tabpages' , function () 55command ( 'tabnew' ) 56command ( 'vsplit' ) 57local tab1 , tab2 =unpack ( api . nvim_list_tabpages ()) 58local win1 , win2 , win3 =unpack ( api . nvim_list_wins ()) 59eq ( { win1 } , api . nvim_tabpage_list_wins ( tab1 )) 60eq ( { win2 , win3 } , api . nvim_tabpage_list_wins ( tab2 )) 61eq ( win2 , api . nvim_tabpage_get_win ( tab2 )) 62eq ( win2 , api . nvim_get_current_win ()) 63 64command ( 'tabprev' ) 65 66eq ( tab1 , api . nvim_get_current_tabpage ()) 67 68eq ( win2 , api . nvim_tabpage_get_win ( tab2 )) 69api . nvim_tabpage_set_win ( tab2 , win3 ) 70eq ( win3 , api . nvim_tabpage_get_win ( tab2 )) 71 72command ( 'tabnext' ) 73eq ( win3 , api . nvim_get_current_win ()) 74end ) 75 76it ( 'throws an error when the window does not belong to the tabpage' , function () 77command ( 'tabnew' ) 78command ( 'vsplit' ) 79local tab1 , tab2 =unpack ( api . nvim_list_tabpages ()) 80local win1 , win2 , win3 =unpack ( api . nvim_list_wins ()) 81eq ( { win1 } , api . nvim_tabpage_list_wins ( tab1 )) 82eq ( { win2 , win3 } , api . nvim_tabpage_list_wins ( tab2 )) 83eq ( win2 , api . nvim_get_current_win ()) 84 85eq ( 86string . format ( 'Window does not belong to tabpage %d' , tab2 ), 87pcall_err ( api . nvim_tabpage_set_win , tab2 , win1 ) 88) 89 90eq ( 91string . format ( 'Window does not belong to tabpage %d' , tab1 ), 92pcall_err ( api . nvim_tabpage_set_win , tab1 , win3 ) 93) 94end ) 95 96it ( 'checks textlock restrictions' , function () 97command ( 'autocmd TextYankPost * ++once call nvim_open_tabpage(0, 0, {})' ) 98matches ( 'E565:' , pcall_err ( command , 'yank' )) 99eq ( 1 , fn . tabpagenr ( '$' )) 100 101exec ( [[ 102new 103call setline(1, 'foo') 104setlocal debug=throw indentexpr=nvim_tabpage_set_win(0,win_getid()) 105]] ) 106local cur_win =api . nvim_get_current_win () 107matches ( 'E565: Not allowed to change text or change window' , pcall_err ( command , 'normal! ==' )) 108eq ( cur_win , api . nvim_get_current_win ()) 109end ) 110end ) 111 112describe ( '{get,set,del}_var' , function () 113it ( 'works' , function () 114api . nvim_tabpage_set_var ( 0 , 'lua' , { 1 , 2 , { [ '3' ] =1 } } ) 115eq ( { 1 , 2 , { [ '3' ] =1 } } , api . nvim_tabpage_get_var ( 0 , 'lua' )) 116eq ( { 1 , 2 , { [ '3' ] =1 } } , api . nvim_eval ( 't:lua' )) 117eq ( 1 , fn . exists ( 't:lua' )) 118api . nvim_tabpage_del_var ( 0 , 'lua' ) 119eq ( 0 , fn . exists ( 't:lua' )) 120eq ( 'Key not found: lua' , pcall_err ( api . nvim_tabpage_del_var , 0 , 'lua' )) 121api . nvim_tabpage_set_var ( 0 , 'lua' , 1 ) 122command ( 'lockvar t:lua' ) 123eq ( 'Key is locked: lua' , pcall_err ( api . nvim_tabpage_del_var , 0 , 'lua' )) 124eq ( 'Key is locked: lua' , pcall_err ( api . nvim_tabpage_set_var , 0 , 'lua' , 1 )) 125end ) 126 127it ( 'tabpage_set_var returns the old value' , function () 128local val1 ={ 1 , 2 , { [ '3' ] =1 } } 129local val2 ={ 4 , 7 } 130eq ( NIL , request ( 'tabpage_set_var' , 0 , 'lua' , val1 )) 131eq ( val1 , request ( 'tabpage_set_var' , 0 , 'lua' , val2 )) 132end ) 133 134it ( 'tabpage_del_var returns the old value' , function () 135local val1 ={ 1 , 2 , { [ '3' ] =1 } } 136local val2 ={ 4 , 7 } 137eq ( NIL , request ( 'tabpage_set_var' , 0 , 'lua' , val1 )) 138eq ( val1 , request ( 'tabpage_set_var' , 0 , 'lua' , val2 )) 139eq ( val2 , request ( 'tabpage_del_var' , 0 , 'lua' )) 140end ) 141end ) 142 143describe ( 'get_number' , function () 144it ( 'works' , function () 145local tabs =api . nvim_list_tabpages () 146eq ( 1 , api . nvim_tabpage_get_number ( tabs [ 1 ])) 147 148command ( 'tabnew' ) 149local tab1 , tab2 =unpack ( api . nvim_list_tabpages ()) 150eq ( 1 , api . nvim_tabpage_get_number ( tab1 )) 151eq ( 2 , api . nvim_tabpage_get_number ( tab2 )) 152 153command ( '-tabmove' ) 154eq ( 2 , api . nvim_tabpage_get_number ( tab1 )) 155eq ( 1 , api . nvim_tabpage_get_number ( tab2 )) 156end ) 157end ) 158 159describe ( 'is_valid' , function () 160it ( 'works' , function () 161command ( 'tabnew' ) 162local tab =api . nvim_list_tabpages ()[ 2 ] 163api . nvim_set_current_tabpage ( tab ) 164ok ( api . nvim_tabpage_is_valid ( tab )) 165command ( 'tabclose' ) 166ok ( not api . nvim_tabpage_is_valid ( tab )) 167end ) 168end ) 169 170describe ( 'open_tabpage' , function () 171it ( 'works' , function () 172local tabs =api . nvim_list_tabpages () 173eq ( 1 , # tabs ) 174local curtab =api . nvim_get_current_tabpage () 175local tab =api . nvim_open_tabpage ( 0 , false , {} ) 176local newtabs =api . nvim_list_tabpages () 177eq ( 2 , # newtabs ) 178eq ( tab , newtabs [ 2 ]) 179eq ( curtab , api . nvim_get_current_tabpage ()) 180 181local tab2 =api . nvim_open_tabpage ( 0 , true , {} ) 182local newtabs2 =api . nvim_list_tabpages () 183eq ( 3 , # newtabs2 ) 184eq ( { 185tabs [ 1 ], 186tab2 , -- new tabs open after the current tab 187tab , 188} , newtabs2 ) 189eq ( tab2 , newtabs2 [ 2 ]) 190eq ( tab , newtabs2 [ 3 ]) 191eq ( tab2 , api . nvim_get_current_tabpage ()) 192end ) 193 194it ( 'respects the `after` option' , function () 195local tab1 =api . nvim_get_current_tabpage () 196command ( 'tabnew' ) 197local tab2 =api . nvim_get_current_tabpage () 198command ( 'tabnew' ) 199local tab3 =api . nvim_get_current_tabpage () 200 201local newtabs =api . nvim_list_tabpages () 202eq ( 3 , # newtabs ) 203eq ( { 204tab1 , 205tab2 , 206-- new_tab, 207tab3 , 208} , newtabs ) 209 210local new_tab =api . nvim_open_tabpage ( 0 , false , { after =api . nvim_tabpage_get_number ( tab2 ) } ) 211local newtabs2 =api . nvim_list_tabpages () 212eq ( 4 , # newtabs2 ) 213eq ( { 214tab1 , 215tab2 , 216new_tab , 217tab3 , 218} , newtabs2 ) 219eq ( tab3 , api . nvim_get_current_tabpage ()) 220end ) 221 222it ( 'respects the `enter` argument' , function () 223local screen =Screen . new ( 50 , 8 ) 224eq ( 1 , # api . nvim_list_tabpages ()) 225local tab1 =api . nvim_get_current_tabpage () 226 227local new_tab =api . nvim_open_tabpage ( 0 , false , {} ) 228local newtabs =api . nvim_list_tabpages () 229eq ( 2 , # newtabs ) 230eq ( { tab1 , new_tab } , newtabs ) 231eq ( tab1 , api . nvim_get_current_tabpage ()) 232-- Tabline redrawn when not entering. 233screen : expect ( [[ 234{5: [No Name] }{24: [No Name] }{2: }{24:X}| 235^ | 236{1:~ }|*5 237| 238]] ) 239 240local new_tab2 =api . nvim_open_tabpage ( 0 , true , {} ) 241local newtabs2 =api . nvim_list_tabpages () 242eq ( 3 , # newtabs2 ) 243eq ( { tab1 , new_tab2 , new_tab } , newtabs2 ) 244eq ( new_tab2 , api . nvim_get_current_tabpage ()) 245-- Tabline redrawn. (when entering) 246screen : expect ( [[ 247{24: [No Name] }{5: [No Name] }{24: [No Name] }{2: }{24:X}| 248^ | 249{1:~ }|*5 250| 251]] ) 252 253api . nvim_open_tabpage ( 0 , false , {} ) 254-- Tabline redrawn when not entering, and when there's already one. 255screen : expect ( [[ 256{24: [No Name] }{5: [No Name] }{24: [No Name] [No Name] }{2: }{24:X}| 257^ | 258{1:~ }|*5 259| 260]] ) 261end ) 262 263it ( 'applies autocmds in the context of the new tabpage' , function () 264exec ( [=[ 265let g:events = [] 266autocmd WinNew * let g:events += [['WinNew', nvim_get_current_tabpage(), win_getid()]] 267autocmd WinEnter * let g:events += [['WinEnter', nvim_get_current_tabpage(), win_getid()]] 268autocmd TabNew * let g:events += [['TabNew', nvim_get_current_tabpage(), win_getid()]] 269autocmd TabEnter * let g:events += [['TabEnter', nvim_get_current_tabpage(), win_getid()]] 270autocmd BufEnter * let g:events += [['BufEnter', nvim_get_current_tabpage(), win_getid()]] 271autocmd BufLeave * let g:events += [['BufLeave', nvim_get_current_tabpage(), win_getid()]] 272autocmd BufWinEnter * let g:events += [['BufWinEnter', nvim_get_current_tabpage(), win_getid()]] 273]=] ) 274 275local new_tab =api . nvim_open_tabpage ( 0 , true , {} ) 276local new_win =api . nvim_tabpage_get_win ( new_tab ) 277eq ( { 278{ 'WinNew' , new_tab , new_win } , 279{ 'WinEnter' , new_tab , new_win } , 280{ 'TabNew' , new_tab , new_win } , 281{ 'TabEnter' , new_tab , new_win } , 282} , api . nvim_get_var ( 'events' )) 283eq ( new_win , api . nvim_get_current_win ()) 284 285api . nvim_set_var ( 'events' , {} ) 286new_tab =api . nvim_open_tabpage ( api . nvim_create_buf ( true , true ), true , {} ) 287new_win =api . nvim_tabpage_get_win ( new_tab ) 288eq ( { 289{ 'WinNew' , new_tab , new_win } , 290{ 'WinEnter' , new_tab , new_win } , 291{ 'TabNew' , new_tab , new_win } , 292{ 'TabEnter' , new_tab , new_win } , 293{ 'BufLeave' , new_tab , new_win } , 294{ 'BufEnter' , new_tab , new_win } , 295{ 'BufWinEnter' , new_tab , new_win } , 296} , api . nvim_get_var ( 'events' )) 297 298local curwin =new_win 299api . nvim_set_var ( 'events' , {} ) 300new_tab =api . nvim_open_tabpage ( 0 , false , {} ) 301new_win =api . nvim_tabpage_get_win ( new_tab ) 302eq ( 303{ { 'WinNew' , new_tab , new_win } , { 'TabNew' , new_tab , new_win } } , 304api . nvim_get_var ( 'events' ) 305) 306eq ( curwin , api . nvim_get_current_win ()) 307 308api . nvim_set_var ( 'events' , {} ) 309new_tab =api . nvim_open_tabpage ( api . nvim_create_buf ( true , true ), false , {} ) 310new_win =api . nvim_tabpage_get_win ( new_tab ) 311eq ( { 312{ 'WinNew' , new_tab , new_win } , 313{ 'TabNew' , new_tab , new_win } , 314{ 'BufWinEnter' , new_tab , new_win } , 315} , api . nvim_get_var ( 'events' )) 316eq ( curwin , api . nvim_get_current_win ()) 317end ) 318 319it ( 'handles nasty autocmds' , function () 320command ( 'autocmd WinNewPre * ++once call nvim_open_tabpage(0, 0, {})' ) 321matches ( 'E1312:' , pcall_err ( command , 'split' )) 322 323command ( 'autocmd TabNew * ++once quit' ) 324eq ( 'Tabpage was closed immediately' , pcall_err ( api . nvim_open_tabpage , 0 , false , {} )) 325command ( 'autocmd BufEnter * ++once quit' ) 326local buf =api . nvim_create_buf ( true , true ) 327eq ( 'Tabpage was closed immediately' , pcall_err ( api . nvim_open_tabpage , buf , true , {} )) 328 329-- No error if autocmds delete target buffer, if new tabpage is still valid to return. 330command ( 'autocmd BufEnter * ++once buffer # | bwipeout! #' ) 331local new_tp =api . nvim_open_tabpage ( buf , true , {} ) 332eq ( false , api . nvim_buf_is_valid ( buf )) 333eq ( new_tp , api . nvim_get_current_tabpage ()) 334end ) 335 336it ( 'handles edge cases for positioning' , function () 337-- Start with 3 tabs 338local tab1 =api . nvim_get_current_tabpage () 339command ( 'tabnew' ) 340local tab2 =api . nvim_get_current_tabpage () 341command ( 'tabnew' ) 342local tab3 =api . nvim_get_current_tabpage () 343 344local initial_tabs =api . nvim_list_tabpages () 345eq ( 3 , # initial_tabs ) 346eq ( { tab1 , tab2 , tab3 } , initial_tabs ) 347 348-- Test after=0: should become first tab 349local first_tab =api . nvim_open_tabpage ( 0 , false , { after =0 } ) 350local tabs_after_first =api . nvim_list_tabpages () 351eq ( 4 , # tabs_after_first ) 352eq ( { first_tab , tab1 , tab2 , tab3 } , tabs_after_first ) 353 354-- Test after=-1: should insert after current tab (tab3) 355local explicit_after_current =api . nvim_open_tabpage ( 0 , false , { after =- 1 } ) 356local tabs_after_current =api . nvim_list_tabpages () 357eq ( 5 , # tabs_after_current ) 358eq ( { first_tab , tab1 , tab2 , tab3 , explicit_after_current } , tabs_after_current ) 359 360-- Test inserting before a middle tab (before tab2, which is now number 3) 361local before_middle =api . nvim_open_tabpage ( 0 , false , { after =2 } ) 362local tabs_after_middle =api . nvim_list_tabpages () 363eq ( 6 , # tabs_after_middle ) 364eq ( { first_tab , tab1 , before_middle , tab2 , tab3 , explicit_after_current } , tabs_after_middle ) 365 366eq ( tab3 , api . nvim_get_current_tabpage ()) 367 368-- Test default behavior (after current) 369local default_after_current =api . nvim_open_tabpage ( 0 , false , {} ) 370local final_tabs =api . nvim_list_tabpages () 371eq ( 7 , # final_tabs ) 372eq ( { 373first_tab , 374tab1 , 375before_middle , 376tab2 , 377tab3 , 378default_after_current , 379explicit_after_current , 380} , final_tabs ) 381end ) 382 383it ( 'handles position beyond last tab' , function () 384-- Create a few tabs first 385local tab1 =api . nvim_get_current_tabpage () 386command ( 'tabnew' ) 387local tab2 =api . nvim_get_current_tabpage () 388command ( 'tabnew' ) 389local tab3 =api . nvim_get_current_tabpage () 390 391eq ( 3 , # api . nvim_list_tabpages ()) 392eq ( { tab1 , tab2 , tab3 } , api . nvim_list_tabpages ()) 393 394-- Test that requesting position beyond last tab still works 395-- (should place it at the end) 396local new_tab =api . nvim_open_tabpage ( 0 , false , { after =10 } ) -- Way beyond the last tab 397local final_tabs =api . nvim_list_tabpages () 398eq ( 4 , # final_tabs ) 399-- Should append at the end 400eq ( { tab1 , tab2 , tab3 , new_tab } , final_tabs ) 401end ) 402 403it ( 'works with specific buffer' , function () 404local curbuf =api . nvim_get_current_buf () 405local new_tab =api . nvim_open_tabpage ( 0 , false , {} ) 406api . nvim_set_current_tabpage ( new_tab ) 407eq ( curbuf , api . nvim_get_current_buf ()) 408 409local buf =api . nvim_create_buf ( false , false ) 410api . nvim_buf_set_lines ( buf , 0 , - 1 , false , { 'test content' } ) 411 412local original_tab =api . nvim_get_current_tabpage () 413local original_buf =api . nvim_get_current_buf () 414new_tab =api . nvim_open_tabpage ( buf , true , {} ) -- Enter the tab to make testing easier 415 416-- Check that new tab has the specified buffer 417eq ( new_tab , api . nvim_get_current_tabpage ()) 418eq ( buf , api . nvim_get_current_buf ()) 419eq ( { 'test content' } , api . nvim_buf_get_lines ( buf , 0 , - 1 , false )) 420 421-- Switch back and check original tab still has original buffer 422api . nvim_set_current_tabpage ( original_tab ) 423eq ( original_buf , api . nvim_get_current_buf ()) 424 425-- Test invalid buffer 426eq ( 'Invalid buffer id: 999' , pcall_err ( api . nvim_open_tabpage , 999 , true , {} )) 427end ) 428 429it ( 'handles complex positioning scenarios' , function () 430-- Create 5 tabs total 431local tabs ={ api . nvim_get_current_tabpage () } 432for i =2 , 5 do 433command ( 'tabnew' ) 434tabs [ i ] =api . nvim_get_current_tabpage () 435end 436eq ( 5 , # api . nvim_list_tabpages ()) 437 438-- Go to middle tab (tab 3) 439api . nvim_set_current_tabpage ( tabs [ 3 ]) 440 441-- Insert after=0 (after current, which is tab 3) 442local new_after_current =api . nvim_open_tabpage ( 0 , false , {} ) 443local result_tabs =api . nvim_list_tabpages () 444eq ( 6 , # result_tabs ) 445eq ( { 446tabs [ 1 ], 447tabs [ 2 ], 448tabs [ 3 ], 449new_after_current , 450tabs [ 4 ], 451tabs [ 5 ], 452} , result_tabs ) 453 454-- Insert as number 2 (before tab2, which will become number 3) 455local new_at_pos2 =api . nvim_open_tabpage ( 0 , false , { after =1 } ) 456local final_result =api . nvim_list_tabpages () 457eq ( 7 , # final_result ) 458eq ( { 459tabs [ 1 ], 460new_at_pos2 , 461tabs [ 2 ], 462tabs [ 3 ], 463new_after_current , 464tabs [ 4 ], 465tabs [ 5 ], 466} , final_result ) 467end ) 468 469it ( 'preserves tab order when entering new tabs' , function () 470local tab1 =api . nvim_get_current_tabpage () 471command ( 'tabnew' ) 472local tab2 =api . nvim_get_current_tabpage () 473 474-- Create new tab with enter=true, should insert after current (tab2) 475local tab3 =api . nvim_open_tabpage ( 0 , true , { after =- 1 } ) 476local tabs =api . nvim_list_tabpages () 477eq ( 3 , # tabs ) 478eq ( { tab1 , tab2 , tab3 } , tabs ) 479eq ( tab3 , api . nvim_get_current_tabpage ()) 480 481-- Create another with enter=true and specific position 482api . nvim_set_current_tabpage ( tab1 ) 483local tab4 =api . nvim_open_tabpage ( 0 , true , { after =0 } ) -- Should become first tab 484local final_tabs =api . nvim_list_tabpages () 485eq ( 4 , # final_tabs ) 486eq ( { tab4 , tab1 , tab2 , tab3 } , final_tabs ) 487eq ( tab4 , api . nvim_get_current_tabpage ()) 488end ) 489end ) 490end )