2015-11-27 08:41:50 -07:00
|
|
|
local helpers = require('test.functional.helpers')
|
2015-12-01 07:41:03 -07:00
|
|
|
local nvim = helpers.meths
|
|
|
|
local clear, eq, neq = helpers.clear, helpers.eq, helpers.neq
|
|
|
|
local curbuf, buf = helpers.curbuf, helpers.bufmeths
|
2015-11-28 01:06:23 -07:00
|
|
|
local source, execute = helpers.source, helpers.execute
|
2015-11-27 08:41:50 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
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)
|
|
|
|
execute(
|
|
|
|
'au OptionSet '
|
|
|
|
.. pattern ..
|
|
|
|
' :call AutoCommand(expand("<amatch>"), bufnr("%"), winnr())'
|
|
|
|
)
|
|
|
|
end
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
local function init_var()
|
|
|
|
execute('let g:ret = []')
|
|
|
|
end
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
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
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
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
|
2015-11-28 01:06:23 -07:00
|
|
|
end
|
2015-12-01 07:41:03 -07:00
|
|
|
end
|
|
|
|
eq(expected_table(unpack(v)), ret[i])
|
|
|
|
end
|
|
|
|
end
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
local function expected_empty()
|
|
|
|
eq({}, get_result())
|
|
|
|
end
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
local function make_buffer()
|
|
|
|
local old_buf = curbuf()
|
|
|
|
execute('new')
|
|
|
|
local new_buf = curbuf()
|
|
|
|
execute('wincmd p') -- move previous window
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
neq(old_buf, new_buf)
|
|
|
|
eq(old_buf, curbuf())
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
return new_buf
|
|
|
|
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()
|
|
|
|
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 be called in setting 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 be called in inverting global autoindent option',function()
|
|
|
|
execute('set ai!')
|
|
|
|
expected_combination({'autoindent', 0, 1, 'global'})
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should be called in being unset local autoindent option',function()
|
|
|
|
execute('setlocal ai')
|
|
|
|
expected_combination({'autoindent', 0, 1, 'local'})
|
|
|
|
|
|
|
|
execute('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()
|
|
|
|
execute('set list nu')
|
|
|
|
expected_combination(
|
|
|
|
{'list', 0, 1, 'global'},
|
|
|
|
{'number', 0, 1, 'global'}
|
|
|
|
)
|
|
|
|
end)
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
it('should not print anything, use :noa', function()
|
|
|
|
execute('noa set nolist nonu')
|
|
|
|
expected_empty()
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should be called in setting local acd', function()
|
|
|
|
execute('setlocal acd')
|
|
|
|
expected_combination({'autochdir', 0, 1, 'local'})
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should be called in setting autoread', function()
|
|
|
|
execute('set noar')
|
|
|
|
expected_combination({'autoread', 1, 0, 'global'})
|
|
|
|
|
|
|
|
execute('setlocal ar')
|
|
|
|
expected_combination({'autoread', 0, 1, 'local'})
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should be called in inverting global autoread', function()
|
|
|
|
execute('setglobal invar')
|
|
|
|
expected_combination({'autoread', 1, 0, 'global'})
|
|
|
|
end)
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
it('should be called in setting backspace option through :let', function()
|
|
|
|
execute('let &bs=""')
|
|
|
|
expected_combination({'backspace', 'indent,eol,start', '', 'global'})
|
|
|
|
end)
|
2015-11-28 04:09:56 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
describe('being set by setbufvar()', function()
|
|
|
|
it('should not trigger because option name is invalid', function()
|
|
|
|
execute('call setbufvar(1, "&l:bk", 1)')
|
|
|
|
expected_empty()
|
2015-11-28 01:06:23 -07:00
|
|
|
end)
|
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
it('should trigger using correct option name', function()
|
|
|
|
execute('call setbufvar(1, "&backup", 1)')
|
|
|
|
expected_combination({'backup', 0, 1, 'local'})
|
2015-11-28 01:06:23 -07:00
|
|
|
end)
|
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
execute('call setbufvar(' .. new_bufnr .. ', "&buftype", "nofile")')
|
|
|
|
expected_combination({'buftype', '', 'nofile', 'local', {bufnr = new_bufnr}})
|
2015-11-28 01:06:23 -07:00
|
|
|
end)
|
2015-12-01 07:41:03 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
describe('with specific option', function()
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
before_each(function()
|
|
|
|
clear()
|
|
|
|
declare_hook_function()
|
|
|
|
init_var()
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('should be called iff setting readonly', function()
|
|
|
|
set_hook('readonly')
|
|
|
|
|
|
|
|
execute('set nu')
|
|
|
|
expected_empty()
|
|
|
|
|
|
|
|
execute('setlocal ro')
|
|
|
|
expected_combination({'readonly', 0, 1, 'local'})
|
|
|
|
|
|
|
|
execute('setglobal ro')
|
|
|
|
expected_combination({'readonly', 0, 1, 'global'})
|
|
|
|
|
|
|
|
execute('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')
|
|
|
|
|
|
|
|
execute('call setbufvar(1, "&l:bk", 1)')
|
2015-11-28 01:06:23 -07:00
|
|
|
expected_empty()
|
|
|
|
end)
|
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
it('should trigger, use correct option name backup', function()
|
|
|
|
set_hook('backup')
|
|
|
|
|
|
|
|
execute('call setbufvar(1, "&backup", 1)')
|
|
|
|
expected_combination({'backup', 0, 1, 'local'})
|
2015-11-28 01:06:23 -07:00
|
|
|
end)
|
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
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)
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
execute('call setbufvar(' .. new_bufnr .. ', "&buftype", "nofile")')
|
|
|
|
expected_combination({'buftype', '', 'nofile', 'local', {bufnr = new_bufnr}})
|
2015-11-28 01:06:23 -07:00
|
|
|
end)
|
2015-12-01 07:41:03 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
describe('being set by neovim api', function()
|
|
|
|
it('should trigger if a boolean option be set globally', function()
|
|
|
|
set_hook('autochdir')
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
nvim.set_option('autochdir', true)
|
|
|
|
eq(true, nvim.get_option('autochdir'))
|
|
|
|
expected_combination({'autochdir', '0', '1', 'global'})
|
2015-11-28 01:06:23 -07:00
|
|
|
end)
|
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
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'})
|
2015-11-28 01:06:23 -07:00
|
|
|
end)
|
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
it('should trigger if a string option be set globally', function()
|
|
|
|
set_hook('ambiwidth')
|
2015-11-28 01:06:23 -07:00
|
|
|
|
2015-12-01 07:41:03 -07:00
|
|
|
nvim.set_option('ambiwidth', 'double')
|
|
|
|
eq('double', nvim.get_option('ambiwidth'))
|
|
|
|
expected_combination({'ambiwidth', 'single', 'double', 'global'})
|
2015-11-28 01:06:23 -07:00
|
|
|
end)
|
|
|
|
end)
|
2015-11-27 08:41:50 -07:00
|
|
|
end)
|
|
|
|
end)
|