robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local clear , eq =n . clear , t . eq 6local command =n . command 7local eval =n . eval 8local exec =n . exec 9local exec_capture =n . exec_capture 10 11describe ( 'TabClosed' , function () 12before_each ( clear ) 13 14describe ( 'au TabClosed' , function () 15describe ( 'with * as <afile>' , function () 16it ( 'matches when closing any tab' , function () 17command ( 18'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()' 19) 20repeat 21command ( 'tabnew' ) 22until eval ( 'tabpagenr()' ) == 6 -- current tab is now 6 23eq ( 'tabclosed:6:6:5' , exec_capture ( 'tabclose' )) -- close last 6, current tab is now 5 24eq ( 'tabclosed:5:5:4' , exec_capture ( 'close' )) -- close last window on tab, closes tab 25eq ( 'tabclosed:2:2:3' , exec_capture ( '2tabclose' )) -- close tab 2, current tab is now 3 26eq ( 'tabclosed:1:1:2\ntabclosed:1:1:1' , exec_capture ( 'tabonly' )) -- close tabs 1 and 2 27end ) 28 29it ( 'is triggered when closing a window via bdelete from another tab' , function () 30command ( 31'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()' 32) 33command ( '1tabedit Xtestfile' ) 34command ( '1tabedit Xtestfile' ) 35command ( 'normal! 1gt' ) 36eq ( { 1 , 3 } , eval ( '[tabpagenr(), tabpagenr("$")]' )) 37eq ( 'tabclosed:2:2:1\ntabclosed:2:2:1' , exec_capture ( 'bdelete Xtestfile' )) 38eq ( { 1 , 1 } , eval ( '[tabpagenr(), tabpagenr("$")]' )) 39end ) 40 41it ( 'is triggered when closing a window via bdelete from current tab' , function () 42command ( 43'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()' 44) 45command ( 'file Xtestfile1' ) 46command ( '1tabedit Xtestfile2' ) 47command ( '1tabedit Xtestfile2' ) 48 49-- Only one tab is closed, and the alternate file is used for the other. 50eq ( { 2 , 3 } , eval ( '[tabpagenr(), tabpagenr("$")]' )) 51eq ( 'tabclosed:2:2:2' , exec_capture ( 'bdelete Xtestfile2' )) 52eq ( 'Xtestfile1' , eval ( 'bufname("")' )) 53end ) 54 55it ( 'triggers after tab page is properly freed' , function () 56exec ( [[ 57let s:tp = nvim_get_current_tabpage() 58let g:buf = bufnr() 59 60setlocal bufhidden=wipe 61tabnew 62au TabClosed * ++once let g:tp_valid = nvim_tabpage_is_valid(s:tp) 63\| let g:curbuf = bufnr() 64\| let g:abuf = expand('<abuf>') 65 66call nvim_buf_delete(g:buf, #{force: 1}) 67]] ) 68eq ( false , eval ( 'g:tp_valid' )) 69eq ( false , eval ( 'nvim_buf_is_valid(g:buf)' )) 70eq ( eval ( 'g:curbuf' ), tonumber ( eval ( 'g:abuf' ))) -- Falls back to curbuf. 71 72exec ( [[ 73tabnew 74let g:buf = bufnr() 75let s:win = win_getid() 76 77tabfirst 78au TabClosed * ++once let g:abuf = expand('<abuf>') 79 80call nvim_win_close(s:win, 1) 81]] ) 82eq ( true , eval ( 'nvim_buf_is_valid(g:buf)' )) 83eq ( eval ( 'g:buf' ), tonumber ( eval ( 'g:abuf' ))) 84 85exec ( [[ 86tabnew 87let s:win = win_getid() 88 89tabfirst 90let g:buf = nvim_create_buf(1, 1) 91au BufHidden * ++once call nvim_win_set_buf(s:win, g:buf) 92au TabClosed * ++once let g:abuf = expand('<abuf>') 93 94call nvim_win_close(s:win, 1) 95]] ) 96-- BufHidden switched buffers at the last moment; TabClosed's <abuf> should show that. 97eq ( eval ( 'g:buf' ), tonumber ( eval ( 'g:abuf' ))) 98end ) 99end ) 100 101describe ( 'with NR as <afile>' , function () 102it ( 'matches when closing a tab whose index is NR' , function () 103command ( 104'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()' 105) 106command ( 'au! TabClosed 2 echom "tabclosed:match"' ) 107repeat 108command ( 'tabnew' ) 109until eval ( 'tabpagenr()' ) == 7 -- current tab is now 7 110-- sanity check, we shouldn't match on tabs with numbers other than 2 111eq ( 'tabclosed:7:7:6' , exec_capture ( 'tabclose' )) 112-- close tab page 2, current tab is now 5 113eq ( 'tabclosed:2:2:5\ntabclosed:match' , exec_capture ( '2tabclose' )) 114end ) 115end ) 116 117describe ( 'with close' , function () 118it ( 'is triggered' , function () 119command ( 120'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()' 121) 122command ( 'tabedit Xtestfile' ) 123eq ( { 2 , 2 } , eval ( '[tabpagenr(), tabpagenr("$")]' )) 124eq ( 'tabclosed:2:2:1' , exec_capture ( 'close' )) 125eq ( { 1 , 1 } , eval ( '[tabpagenr(), tabpagenr("$")]' )) 126end ) 127end ) 128end ) 129end )