robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Test for BufWritePre autocommand that deletes or unloads the buffer. 2-- Test for BufUnload autocommand that unloads all other buffers. 3 4local t =require ( 'test.testutil' ) 5local n =require ( 'test.functional.testnvim' )() 6 7local describe , it , before_each , setup , teardown = 8t . describe , t . it , t . before_each , t . setup , t . teardown 9local source =n . source 10local clear , command , expect , eq , eval =n . clear , n . command , n . expect , t . eq , n . eval 11local write_file , dedent =t . write_file , t . dedent 12local read_file =t . read_file 13local expect_exit =n . expect_exit 14 15describe ( 'autocommands that delete and unload buffers:' , function () 16local test_file ='Xtest-008_autocommands.out' 17local text1 =dedent ( [[ 18start of Xxx1 19test 20end of Xxx]] ) 21local text2 =text1 : gsub ( '1' , '2' ) 22setup ( function () 23write_file ( 'Xxx1' , text1 .. '\n' ) 24write_file ( 'Xxx2' , text2 .. '\n' ) 25end ) 26teardown ( function () 27os . remove ( test_file ) 28os . remove ( 'Xxx1' ) 29os . remove ( 'Xxx2' ) 30end ) 31before_each ( clear ) 32 33it ( 'BufWritePre, BufUnload' , function () 34command ( 'au BufWritePre Xxx1 bunload' ) 35command ( 'au BufWritePre Xxx2 bwipe' ) 36command ( 'e Xxx2' ) 37eq ( 'Xxx2' , eval ( 'bufname("%")' )) 38command ( 'e Xxx1' ) 39eq ( 'Xxx1' , eval ( 'bufname("%")' )) 40-- The legacy test file did not check the error message. 41command ( 'let v:errmsg = "no error"' ) 42command ( 'silent! write' ) 43eq ( 'E203: Autocommands deleted or unloaded buffer to be written' , eval ( 'v:errmsg' )) 44eq ( 'Xxx2' , eval ( 'bufname("%")' )) 45expect ( text2 ) 46-- Start editing Xxx2. 47command ( 'e! Xxx2' ) 48-- The legacy test file did not check the error message. 49command ( 'let v:errmsg = "no error"' ) 50-- Write Xxx2, will delete the buffer and give an error msg. 51command ( 'silent! write' ) 52eq ( 'E203: Autocommands deleted or unloaded buffer to be written' , eval ( 'v:errmsg' )) 53eq ( 'Xxx1' , eval ( 'bufname("%")' )) 54expect ( text1 ) 55end ) 56it ( 'BufUnload, VimLeave' , function () 57source ( [[ 58func CloseAll() 59let i = 0 60while i <= bufnr('$') 61if i != bufnr('%') && bufloaded(i) 62exe i . "bunload" 63endif 64let i += 1 65endwhile 66endfunc 67func WriteToOut() 68edit! ]] .. test_file .. [[ 69 70$put ='VimLeave done' 71write 72endfunc 73set shada='100 74au BufUnload * call CloseAll() 75au VimLeave * call WriteToOut() 76]] ) 77-- Must disable 'hidden' so that the BufUnload autocmd is triggered between 78-- each :edit 79command ( 'set nohidden' ) 80command ( 'silent! edit Xxx2' ) 81command ( 'silent! edit Xxx1' ) 82command ( 'silent! edit README.md' ) -- an existing file 83command ( 'silent! split new2' ) 84expect_exit ( command , 'silent! quit' ) 85eq ( 'VimLeave done' , string . match ( read_file ( test_file ), '^%s*(.-)%s*$' )) 86end ) 87end )