aboutsummaryrefslogtreecommitdiff
path: root/test/functional/legacy/autocmd_option_spec.lua
blob: 257a4ac5d4dc0416ec0d698d0810f877f6d6e854 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
local helpers = require('test.functional.helpers')
local clear, nvim, eq = helpers.clear, helpers.nvim, helpers.eq
local source, execute = helpers.source, helpers.execute

describe('au OptionSet', function()
  describe('with * as <amatch>', function()
    describe('matches when being set any option', function()
      local function expected_str(option, oldval, newval, scope)
        return ''
          .. string.format('Autocmd Option: <%s>,', option)
          .. string.format(' OldVal: <%s>,', oldval)
          .. string.format(' NewVal: <%s>,', newval)
          .. string.format(' Scope: <%s>', scope)
      end

      local function get_result()
        return nvim('get_var', 'ret')
      end

      local function expected_combination(option, oldval, newval, scope)
        eq(expected_str(option, oldval, newval, scope), get_result())
        execute([[let g:ret = '']])
      end

      local function expected_empty()
        eq('', get_result())
      end

      before_each(function()
        clear()

        source([[
          fu! AutoCommand(match)
            let g:ret.=printf('Autocmd Option: <%s>,', a:match)
            let g:ret.=printf(' OldVal: <%s>,', v:option_old)
            let g:ret.=printf(' NewVal: <%s>,', v:option_new)
            let g:ret.=printf(' Scope: <%s>', v:option_type)
          endfu
          
          au OptionSet * :call AutoCommand(expand("<amatch>"))
        ]])

        execute([[let g:ret = '']])
      end)

      it('should set number option', function()
        execute('set nu')
        expected_combination('number', 0, 1, 'global')

        execute('setlocal nonu')
        expected_combination('number', 1, 0, 'local')

        execute('setglobal nonu')
        expected_combination('number', 1, 0, 'global')
      end)

      it('should set autoindent option',function()
        execute('setlocal ai')
        expected_combination('autoindent', 0, 1, 'local')

        execute('setglobal ai')
        expected_combination('autoindent', 0, 1, 'global')

        execute('set noai')
        expected_combination('autoindent', 1, 0, 'global')
      end)

      it('should invert global autoindent option',function()
        execute('set ai!')
        expected_combination('autoindent', 0, 1, 'global')
      end)

      it('should set several global list and number option',function()
        execute('set list nu')
        eq(expected_str('list', 0, 1, 'global') .. expected_str('number', 0, 1, 'global'),
          get_result())
      end)

      it('should not print anything, use :noa.', function()
        execute('noa set nolist nonu')
        expected_empty()
      end)

      it('should set global acd', function()
        execute('setlocal acd')
        expected_combination('autochdir', 0, 1, 'local')
      end)

      it('should set autoread', function()
        execute('set noar')
        expected_combination('autoread', 1, 0, 'global')

        execute('setlocal ar')
        expected_combination('autoread', 0, 1, 'local')
      end)

      it('should invert global autoread', function()
        execute('setglobal invar')
        expected_combination('autoread', 1, 0, 'global')
      end)

      it('should set option backspace through :let', function()
        execute('let &bs=""')
        expected_combination('backspace', 'indent,eol,start', '', 'global')
      end)

      describe('setting option through setbufvar()', function()
        it('shouldn\'t trigger because option name is invalid', function()
          execute('call setbufvar(1, "&l:bk", 1)')
          expected_empty()
        end)

        it('should trigger, use correct option name.', function()
          execute('call setbufvar(1, "&backup", 1)')
          expected_combination('backup', 0, 1, 'local')
        end)
      end)
    end)
  end)
end)