bufenter_spec.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local command = n.command
  5. local eq = t.eq
  6. local eval = n.eval
  7. local request = n.request
  8. local source = n.source
  9. describe('autocmd BufEnter', function()
  10. before_each(clear)
  11. it("triggered by nvim_command('edit <dir>')", function()
  12. command("autocmd BufEnter * if isdirectory(expand('<afile>')) | let g:dir_bufenter = 1 | endif")
  13. request('nvim_command', 'split .')
  14. eq(1, eval("exists('g:dir_bufenter')")) -- Did BufEnter for the directory.
  15. eq(2, eval("bufnr('%')")) -- Switched to the dir buffer.
  16. end)
  17. it('triggered by "try|:split <dir>|endtry" in a function', function()
  18. command("autocmd BufEnter * if isdirectory(expand('<afile>')) | let g:dir_bufenter = 1 | endif")
  19. source([[
  20. function! Test()
  21. try
  22. exe 'split .'
  23. catch
  24. endtry
  25. endfunction
  26. ]])
  27. command('call Test()')
  28. eq(1, eval("exists('g:dir_bufenter')")) -- Did BufEnter for the directory.
  29. eq(2, eval("bufnr('%')")) -- Switched to the dir buffer.
  30. end)
  31. it('triggered by ":split normal|:help|:bw"', function()
  32. n.add_builddir_to_rtp()
  33. command('split normal')
  34. command('wincmd j')
  35. command('help')
  36. command('wincmd L')
  37. command('autocmd BufEnter normal let g:bufentered = 1')
  38. command('bw')
  39. eq(1, eval('bufnr("%")')) -- The cursor is back to the bottom window
  40. eq(0, eval("exists('g:bufentered')")) -- The autocmd hasn't been triggered
  41. end)
  42. end)