008_autocommands_spec.lua 2.5 KB

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