aboutsummaryrefslogtreecommitdiff
path: root/test/functional/legacy/autocmd_option_spec.lua
blob: 0c7e43bf31ab5a9246d49b5aa3093afb460591c4 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
local helpers = require('test.functional.helpers')(after_each)
local nvim = helpers.meths
local clear, eq, neq = helpers.clear, helpers.eq, helpers.neq
local curbuf, buf = helpers.curbuf, helpers.bufmeths
local curwin = helpers.curwin
local redir_exec = helpers.redir_exec
local source, command = helpers.source, helpers.command

local function declare_hook_function()
  source([[
    fu! AutoCommand(match, bufnr, winnr)
      let l:acc = {
      \   'option' : a:match,
      \   'oldval' : v:option_old,
      \   'newval' : v:option_new,
      \   'scope'  : v:option_type,
      \   'attr'   : {
      \     'bufnr' : a:bufnr,
      \     'winnr' : a:winnr,
      \   }
      \ }
      call add(g:ret, l:acc)
    endfu
  ]])
end

local function set_hook(pattern)
  command(
    'au OptionSet '
    .. pattern ..
    ' :call AutoCommand(expand("<amatch>"), bufnr("%"), winnr())'
  )
end

local function init_var()
  command('let g:ret = []')
end

local function get_result()
  local ret = nvim.get_var('ret')
  init_var()
  return ret
end

local function expected_table(option, oldval, newval, scope, attr)
  return {
    option = option,
    oldval = tostring(oldval),
    newval = tostring(newval),
    scope  = scope,
    attr   = attr,
  }
end

local function expected_combination(...)
  local args = {...}
  local ret = get_result()

  if not (#args == #ret) then
    local expecteds = {}
    for _, v in pairs(args) do
      table.insert(expecteds, expected_table(unpack(v)))
    end
    eq(expecteds, ret)
    return
  end

  for i, v in ipairs(args) do
    local attr = v[5]
    if not attr then
      -- remove attr entries
      ret[i].attr = nil
    else
      -- remove attr entries which are not required
      for k in pairs(ret[i].attr) do
        if not attr[k] then
          ret[i].attr[k] = nil
        end
      end
    end
    eq(expected_table(unpack(v)), ret[i])
  end
end

local function expected_empty()
  eq({}, get_result())
end

local function make_buffer()
  local old_buf = curbuf()
  command('botright new')
  local new_buf = curbuf()
  command('wincmd p') -- move previous window

  neq(old_buf, new_buf)
  eq(old_buf, curbuf())

  return new_buf
end

local function get_new_window_number()
  local old_win = curwin()
  command('botright new')
  local new_win = curwin()
  local new_winnr = redir_exec('echo winnr()')
  command('wincmd p') -- move previous window

  neq(old_win, new_win)
  eq(old_win, curwin())

  return new_winnr:gsub('\n', '')
end

describe('au OptionSet', function()
  describe('with any opton (*)', function()

    before_each(function()
      clear()
      declare_hook_function()
      init_var()
      set_hook('*')
    end)

    it('should be called in setting number option', function()
      command('set nu')
      expected_combination({'number', 0, 1, 'global'})

      command('setlocal nonu')
      expected_combination({'number', 1, 0, 'local'})

      command('setglobal nonu')
      expected_combination({'number', 1, 0, 'global'})
    end)

    it('should be called in setting autoindent option',function()
      command('setlocal ai')
      expected_combination({'autoindent', 0, 1, 'local'})

      command('setglobal ai')
      expected_combination({'autoindent', 0, 1, 'global'})

      command('set noai')
      expected_combination({'autoindent', 1, 0, 'global'})
    end)

    it('should be called in inverting global autoindent option',function()
      command('set ai!')
      expected_combination({'autoindent', 0, 1, 'global'})
    end)

    it('should be called in being unset local autoindent option',function()
      command('setlocal ai')
      expected_combination({'autoindent', 0, 1, 'local'})

      command('setlocal ai<')
      expected_combination({'autoindent', 1, 0, 'local'})
    end)

    it('should be called in setting global list and number option at the same time',function()
      command('set list nu')
      expected_combination(
        {'list', 0, 1, 'global'},
        {'number', 0, 1, 'global'}
      )
    end)

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

    it('should be called in setting local acd', function()
      command('setlocal acd')
      expected_combination({'autochdir', 0, 1, 'local'})
    end)

    it('should be called in setting autoread', function()
      command('set noar')
      expected_combination({'autoread', 1, 0, 'global'})

      command('setlocal ar')
      expected_combination({'autoread', 0, 1, 'local'})
    end)

    it('should be called in inverting global autoread', function()
      command('setglobal invar')
      expected_combination({'autoread', 1, 0, 'global'})
    end)

    it('should be called in setting backspace option through :let', function()
      command('let &bs=""')
      expected_combination({'backspace', 'indent,eol,start', '', 'global'})
    end)

    describe('being set by setbufvar()', function()
      it('should not trigger because option name is invalid', function()
        command('silent! call setbufvar(1, "&l:bk", 1)')
        expected_empty()
      end)

      it('should trigger using correct option name', function()
        command('call setbufvar(1, "&backup", 1)')
        expected_combination({'backup', 0, 1, 'local'})
      end)

      it('should trigger if the current buffer is different from the targetted buffer', function()
        local new_buffer = make_buffer()
        local new_bufnr = buf.get_number(new_buffer)

        command('call setbufvar(' .. new_bufnr .. ', "&buftype", "nofile")')
        expected_combination({'buftype', '', 'nofile', 'local', {bufnr = new_bufnr}})
      end)
    end)
  end)

  describe('with specific option', function()

    before_each(function()
      clear()
      declare_hook_function()
      init_var()
    end)

    it('should be called iff setting readonly', function()
      set_hook('readonly')

      command('set nu')
      expected_empty()

      command('setlocal ro')
      expected_combination({'readonly', 0, 1, 'local'})

      command('setglobal ro')
      expected_combination({'readonly', 0, 1, 'global'})

      command('set noro')
      expected_combination({'readonly', 1, 0, 'global'})
    end)

    describe('being set by setbufvar()', function()
      it('should not trigger because option name does not match with backup', function()
        set_hook('backup')

        command('silent! call setbufvar(1, "&l:bk", 1)')
        expected_empty()
      end)

      it('should trigger, use correct option name backup', function()
        set_hook('backup')

        command('call setbufvar(1, "&backup", 1)')
        expected_combination({'backup', 0, 1, 'local'})
      end)

      it('should trigger if the current buffer is different from the targetted buffer', function()
        set_hook('buftype')

        local new_buffer = make_buffer()
        local new_bufnr = buf.get_number(new_buffer)

        command('call setbufvar(' .. new_bufnr .. ', "&buftype", "nofile")')
        expected_combination({'buftype', '', 'nofile', 'local', {bufnr = new_bufnr}})
      end)
    end)

    describe('being set by setwinvar()', function()
      it('should not trigger because option name does not match with backup', function()
        set_hook('backup')

        command('silent! call setwinvar(1, "&l:bk", 1)')
        expected_empty()
      end)

      it('should trigger, use correct option name backup', function()
        set_hook('backup')

        command('call setwinvar(1, "&backup", 1)')
        expected_combination({'backup', 0, 1, 'local'})
      end)

      it('should not trigger if the current window is different from the targetted window', function()
        set_hook('cursorcolumn')

        local new_winnr = get_new_window_number()

        command('call setwinvar(' .. new_winnr .. ', "&cursorcolumn", 1)')
        -- expected_combination({'cursorcolumn', 0, 1, 'local', {winnr = new_winnr}})
        expected_empty()
      end)
    end)

    describe('being set by neovim api', function()
      it('should trigger if a boolean option be set globally', function()
        set_hook('autochdir')

        nvim.set_option('autochdir', true)
        eq(true, nvim.get_option('autochdir'))
        expected_combination({'autochdir', '0', '1', 'global'})
      end)

      it('should trigger if a number option be set globally', function()
        set_hook('cmdheight')

        nvim.set_option('cmdheight', 5)
        eq(5, nvim.get_option('cmdheight'))
        expected_combination({'cmdheight', 1, 5, 'global'})
      end)

      it('should trigger if a string option be set globally', function()
        set_hook('ambiwidth')

        nvim.set_option('ambiwidth', 'double')
        eq('double', nvim.get_option('ambiwidth'))
        expected_combination({'ambiwidth', 'single', 'double', 'global'})
      end)
    end)
  end)
end)