neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

008_autocommands_spec.lua (2642B)


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