2015-07-13 02:58:00 -07:00
|
|
|
-- Test for BufWritePre autocommand that deletes or unloads the buffer.
|
|
|
|
-- Test for BufUnload autocommand that unloads all other buffers.
|
|
|
|
|
|
|
|
local helpers = require('test.functional.helpers')(after_each)
|
2018-10-17 00:24:45 -07:00
|
|
|
local source = helpers.source
|
|
|
|
local clear, command, expect, eq, eval = helpers.clear, helpers.command, helpers.expect, helpers.eq, helpers.eval
|
|
|
|
local write_file, dedent = helpers.write_file, helpers.dedent
|
2018-01-31 19:12:37 -07:00
|
|
|
local read_file = helpers.read_file
|
2022-06-08 14:22:50 -07:00
|
|
|
local expect_exit = helpers.expect_exit
|
2015-07-13 02:58:00 -07:00
|
|
|
|
|
|
|
describe('autocommands that delete and unload buffers:', function()
|
2018-01-31 19:12:37 -07:00
|
|
|
local test_file = 'Xtest-008_autocommands.out'
|
2015-07-13 02:58:00 -07:00
|
|
|
local text1 = dedent([[
|
|
|
|
start of Xxx1
|
|
|
|
test
|
|
|
|
end of Xxx]])
|
|
|
|
local text2 = text1:gsub('1', '2')
|
|
|
|
setup(function()
|
|
|
|
write_file('Xxx1', text1..'\n')
|
|
|
|
write_file('Xxx2', text2..'\n')
|
|
|
|
end)
|
|
|
|
teardown(function()
|
2018-01-31 19:12:37 -07:00
|
|
|
os.remove(test_file)
|
2015-07-13 02:58:00 -07:00
|
|
|
os.remove('Xxx1')
|
|
|
|
os.remove('Xxx2')
|
|
|
|
end)
|
|
|
|
before_each(clear)
|
|
|
|
|
|
|
|
it('BufWritePre, BufUnload', function()
|
2018-10-17 00:24:45 -07:00
|
|
|
command('au BufWritePre Xxx1 bunload')
|
|
|
|
command('au BufWritePre Xxx2 bwipe')
|
|
|
|
command('e Xxx2')
|
2015-07-13 02:58:00 -07:00
|
|
|
eq('Xxx2', eval('bufname("%")'))
|
2018-10-17 00:24:45 -07:00
|
|
|
command('e Xxx1')
|
2015-07-13 02:58:00 -07:00
|
|
|
eq('Xxx1', eval('bufname("%")'))
|
|
|
|
-- The legacy test file did not check the error message.
|
2018-10-17 00:24:45 -07:00
|
|
|
command('let v:errmsg = "no error"')
|
|
|
|
command('silent! write')
|
2015-07-13 02:58:00 -07:00
|
|
|
eq('E203: Autocommands deleted or unloaded buffer to be written',
|
|
|
|
eval('v:errmsg'))
|
|
|
|
eq('Xxx2', eval('bufname("%")'))
|
|
|
|
expect(text2)
|
|
|
|
-- Start editing Xxx2.
|
2018-10-17 00:24:45 -07:00
|
|
|
command('e! Xxx2')
|
2015-07-13 02:58:00 -07:00
|
|
|
-- The legacy test file did not check the error message.
|
2018-10-17 00:24:45 -07:00
|
|
|
command('let v:errmsg = "no error"')
|
2015-07-13 02:58:00 -07:00
|
|
|
-- Write Xxx2, will delete the buffer and give an error msg.
|
2018-10-17 00:24:45 -07:00
|
|
|
command('silent! write')
|
2015-07-13 02:58:00 -07:00
|
|
|
eq('E203: Autocommands deleted or unloaded buffer to be written',
|
|
|
|
eval('v:errmsg'))
|
|
|
|
eq('Xxx1', eval('bufname("%")'))
|
|
|
|
expect(text1)
|
|
|
|
end)
|
|
|
|
it('BufUnload, VimLeave', function()
|
|
|
|
source([[
|
|
|
|
func CloseAll()
|
|
|
|
let i = 0
|
|
|
|
while i <= bufnr('$')
|
|
|
|
if i != bufnr('%') && bufloaded(i)
|
|
|
|
exe i . "bunload"
|
|
|
|
endif
|
|
|
|
let i += 1
|
|
|
|
endwhile
|
|
|
|
endfunc
|
|
|
|
func WriteToOut()
|
2018-01-31 19:12:37 -07:00
|
|
|
edit! ]]..test_file..[[
|
|
|
|
|
2015-07-13 02:58:00 -07:00
|
|
|
$put ='VimLeave done'
|
|
|
|
write
|
|
|
|
endfunc
|
|
|
|
set shada='100
|
|
|
|
au BufUnload * call CloseAll()
|
|
|
|
au VimLeave * call WriteToOut()
|
|
|
|
]])
|
2021-08-17 20:30:58 -07:00
|
|
|
-- Must disable 'hidden' so that the BufUnload autocmd is triggered between
|
|
|
|
-- each :edit
|
|
|
|
command('set nohidden')
|
2018-10-17 00:24:45 -07:00
|
|
|
command('silent! edit Xxx2')
|
|
|
|
command('silent! edit Xxx1')
|
|
|
|
command('silent! edit Makefile') -- an existing file
|
|
|
|
command('silent! split new2')
|
2022-06-08 14:22:50 -07:00
|
|
|
expect_exit(command, 'silent! quit')
|
2017-02-03 20:03:08 -07:00
|
|
|
eq('VimLeave done',
|
2018-01-31 19:12:37 -07:00
|
|
|
string.match(read_file(test_file), "^%s*(.-)%s*$"))
|
2015-07-13 02:58:00 -07:00
|
|
|
end)
|
|
|
|
end)
|