aboutsummaryrefslogtreecommitdiff
path: root/test/functional/legacy/008_autocommands_spec.lua
blob: 4088cd1644c7a002a267eeb893c09fa176354052 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
-- 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)
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
local read_file = helpers.read_file
local expect_exit = helpers.expect_exit

describe('autocommands that delete and unload buffers:', function()
  local test_file = 'Xtest-008_autocommands.out'
  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()
    os.remove(test_file)
    os.remove('Xxx1')
    os.remove('Xxx2')
  end)
  before_each(clear)

  it('BufWritePre, BufUnload', function()
    command('au BufWritePre Xxx1 bunload')
    command('au BufWritePre Xxx2 bwipe')
    command('e Xxx2')
    eq('Xxx2', eval('bufname("%")'))
    command('e Xxx1')
    eq('Xxx1', eval('bufname("%")'))
    -- The legacy test file did not check the error message.
    command('let v:errmsg = "no error"')
    command('silent! write')
    eq('E203: Autocommands deleted or unloaded buffer to be written',
      eval('v:errmsg'))
    eq('Xxx2', eval('bufname("%")'))
    expect(text2)
    -- Start editing Xxx2.
    command('e! Xxx2')
    -- The legacy test file did not check the error message.
    command('let v:errmsg = "no error"')
    -- Write Xxx2, will delete the buffer and give an error msg.
    command('silent! write')
    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()
	edit! ]]..test_file..[[

	$put ='VimLeave done'
	write
      endfunc
      set shada='100
      au BufUnload * call CloseAll()
      au VimLeave * call WriteToOut()
    ]])
    -- Must disable 'hidden' so that the BufUnload autocmd is triggered between
    -- each :edit
    command('set nohidden')
    command('silent! edit Xxx2')
    command('silent! edit Xxx1')
    command('silent! edit Makefile') -- an existing file
    command('silent! split new2')
    expect_exit(command, 'silent! quit')
    eq('VimLeave done',
       string.match(read_file(test_file), "^%s*(.-)%s*$"))
  end)
end)