2024-04-20 08:44:13 -07:00
|
|
|
local t = require('test.testutil')
|
|
|
|
local n = require('test.functional.testnvim')()
|
|
|
|
|
|
|
|
local assert_alive = n.assert_alive
|
|
|
|
local clear, source = n.clear, n.source
|
|
|
|
local api = n.api
|
|
|
|
local insert = n.insert
|
|
|
|
local eq, next_msg = t.eq, n.next_msg
|
|
|
|
local exc_exec = n.exc_exec
|
|
|
|
local exec_lua = n.exec_lua
|
|
|
|
local command = n.command
|
|
|
|
local eval = n.eval
|
2015-11-05 10:02:48 -07:00
|
|
|
|
2023-06-22 04:09:14 -07:00
|
|
|
describe('Vimscript dictionary notifications', function()
|
2015-11-05 10:02:48 -07:00
|
|
|
local channel
|
|
|
|
|
2016-09-03 16:25:24 -07:00
|
|
|
before_each(function()
|
2015-11-05 10:02:48 -07:00
|
|
|
clear()
|
2024-02-12 06:50:39 -07:00
|
|
|
channel = api.nvim_get_chan_info(0).id
|
2024-01-12 10:59:57 -07:00
|
|
|
api.nvim_set_var('channel', channel)
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- the same set of tests are applied to top-level dictionaries(g:, b:, w: and
|
|
|
|
-- t:) and a dictionary variable, so we generate them in the following
|
|
|
|
-- function.
|
2016-09-03 16:25:24 -07:00
|
|
|
local function gentests(dict_expr, dict_init)
|
2021-08-14 10:19:05 -07:00
|
|
|
local is_g = dict_expr == 'g:'
|
|
|
|
|
2015-11-05 10:02:48 -07:00
|
|
|
local function update(opval, key)
|
|
|
|
if not key then
|
|
|
|
key = 'watched'
|
|
|
|
end
|
|
|
|
if opval == '' then
|
2024-01-02 18:09:18 -07:00
|
|
|
command(("unlet %s['%s']"):format(dict_expr, key))
|
2015-11-05 10:02:48 -07:00
|
|
|
else
|
2024-01-02 18:09:18 -07:00
|
|
|
command(("let %s['%s'] %s"):format(dict_expr, key, opval))
|
2015-11-05 10:02:48 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-14 10:19:05 -07:00
|
|
|
local function update_with_api(opval, key)
|
|
|
|
if not key then
|
|
|
|
key = 'watched'
|
|
|
|
end
|
|
|
|
if opval == '' then
|
2024-01-02 18:09:18 -07:00
|
|
|
exec_lua(("vim.api.nvim_del_var('%s')"):format(key))
|
2021-08-14 10:19:05 -07:00
|
|
|
else
|
2024-01-02 18:09:18 -07:00
|
|
|
exec_lua(("vim.api.nvim_set_var('%s', %s)"):format(key, opval))
|
2021-08-14 10:19:05 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function update_with_vim_g(opval, key)
|
|
|
|
if not key then
|
|
|
|
key = 'watched'
|
|
|
|
end
|
|
|
|
if opval == '' then
|
|
|
|
exec_lua(('vim.g.%s = nil'):format(key))
|
|
|
|
else
|
|
|
|
exec_lua(('vim.g.%s %s'):format(key, opval))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-05 10:02:48 -07:00
|
|
|
local function verify_echo()
|
|
|
|
-- helper to verify that no notifications are sent after certain change
|
|
|
|
-- to a dict
|
2024-01-12 06:11:28 -07:00
|
|
|
command("call rpcnotify(g:channel, 'echo')")
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'echo', {} }, next_msg())
|
2015-11-05 10:02:48 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local function verify_value(vals, key)
|
|
|
|
if not key then
|
|
|
|
key = 'watched'
|
|
|
|
end
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', 'values', { key, vals } }, next_msg())
|
2015-11-05 10:02:48 -07:00
|
|
|
end
|
|
|
|
|
2016-08-20 12:24:34 -07:00
|
|
|
describe(dict_expr .. ' watcher', function()
|
2015-11-05 10:02:48 -07:00
|
|
|
if dict_init then
|
2016-09-03 16:25:24 -07:00
|
|
|
before_each(function()
|
2015-11-05 10:02:48 -07:00
|
|
|
source(dict_init)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
before_each(function()
|
|
|
|
source([[
|
|
|
|
function! g:Changed(dict, key, value)
|
2024-01-02 18:09:18 -07:00
|
|
|
if a:dict isnot ]] .. dict_expr .. [[ |
|
2015-11-05 10:02:48 -07:00
|
|
|
throw 'invalid dict'
|
|
|
|
endif
|
|
|
|
call rpcnotify(g:channel, 'values', a:key, a:value)
|
|
|
|
endfunction
|
2024-01-02 18:09:18 -07:00
|
|
|
call dictwatcheradd(]] .. dict_expr .. [[, "watched", "g:Changed")
|
|
|
|
call dictwatcheradd(]] .. dict_expr .. [[, "watched2", "g:Changed")
|
2015-11-05 10:02:48 -07:00
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
|
|
|
after_each(function()
|
|
|
|
source([[
|
2024-01-02 18:09:18 -07:00
|
|
|
call dictwatcherdel(]] .. dict_expr .. [[, "watched", "g:Changed")
|
|
|
|
call dictwatcherdel(]] .. dict_expr .. [[, "watched2", "g:Changed")
|
2015-11-05 10:02:48 -07:00
|
|
|
]])
|
|
|
|
update('= "test"')
|
|
|
|
update('= "test2"', 'watched2')
|
|
|
|
update('', 'watched2')
|
|
|
|
update('')
|
|
|
|
verify_echo()
|
2021-08-14 10:19:05 -07:00
|
|
|
if is_g then
|
|
|
|
update_with_api('"test"')
|
|
|
|
update_with_api('"test2"', 'watched2')
|
|
|
|
update_with_api('', 'watched2')
|
|
|
|
update_with_api('')
|
|
|
|
verify_echo()
|
|
|
|
update_with_vim_g('= "test"')
|
|
|
|
update_with_vim_g('= "test2"', 'watched2')
|
|
|
|
update_with_vim_g('', 'watched2')
|
|
|
|
update_with_vim_g('')
|
|
|
|
verify_echo()
|
|
|
|
end
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('is not triggered when unwatched keys are updated', function()
|
|
|
|
update('= "noop"', 'unwatched')
|
|
|
|
update('.= "noop2"', 'unwatched')
|
|
|
|
update('', 'unwatched')
|
|
|
|
verify_echo()
|
2021-08-14 10:19:05 -07:00
|
|
|
if is_g then
|
|
|
|
update_with_api('"noop"', 'unwatched')
|
|
|
|
update_with_api('vim.g.unwatched .. "noop2"', 'unwatched')
|
|
|
|
update_with_api('', 'unwatched')
|
|
|
|
verify_echo()
|
|
|
|
update_with_vim_g('= "noop"', 'unwatched')
|
|
|
|
update_with_vim_g('= vim.g.unwatched .. "noop2"', 'unwatched')
|
|
|
|
update_with_vim_g('', 'unwatched')
|
|
|
|
verify_echo()
|
|
|
|
end
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('is triggered by remove()', function()
|
|
|
|
update('= "test"')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ new = 'test' })
|
2024-01-12 06:11:28 -07:00
|
|
|
command('call remove(' .. dict_expr .. ', "watched")')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ old = 'test' })
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|
|
|
|
|
2021-08-14 10:19:05 -07:00
|
|
|
if is_g then
|
|
|
|
it('is triggered by remove() when updated with nvim_*_var', function()
|
|
|
|
update_with_api('"test"')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ new = 'test' })
|
2024-01-12 06:11:28 -07:00
|
|
|
command('call remove(' .. dict_expr .. ', "watched")')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ old = 'test' })
|
2021-08-14 10:19:05 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('is triggered by remove() when updated with vim.g', function()
|
|
|
|
update_with_vim_g('= "test"')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ new = 'test' })
|
2024-01-12 06:11:28 -07:00
|
|
|
command('call remove(' .. dict_expr .. ', "watched")')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ old = 'test' })
|
2021-08-14 10:19:05 -07:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2015-11-05 10:02:48 -07:00
|
|
|
it('is triggered by extend()', function()
|
|
|
|
update('= "xtend"')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ new = 'xtend' })
|
2024-01-12 06:11:28 -07:00
|
|
|
command([[
|
2024-01-02 18:09:18 -07:00
|
|
|
call extend(]] .. dict_expr .. [[, {'watched': 'xtend2', 'watched2': 5, 'watched3': 'a'})
|
2015-11-05 10:02:48 -07:00
|
|
|
]])
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ old = 'xtend', new = 'xtend2' })
|
|
|
|
verify_value({ new = 5 }, 'watched2')
|
2015-11-05 10:02:48 -07:00
|
|
|
update('')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ old = 'xtend2' })
|
2015-11-05 10:02:48 -07:00
|
|
|
update('', 'watched2')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ old = 5 }, 'watched2')
|
2015-11-05 10:02:48 -07:00
|
|
|
update('', 'watched3')
|
|
|
|
verify_echo()
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is triggered with key patterns', function()
|
|
|
|
source([[
|
2024-01-02 18:09:18 -07:00
|
|
|
call dictwatcheradd(]] .. dict_expr .. [[, "wat*", "g:Changed")
|
2015-11-05 10:02:48 -07:00
|
|
|
]])
|
|
|
|
update('= 1')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ new = 1 })
|
|
|
|
verify_value({ new = 1 })
|
2015-11-05 10:02:48 -07:00
|
|
|
update('= 3', 'watched2')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ new = 3 }, 'watched2')
|
|
|
|
verify_value({ new = 3 }, 'watched2')
|
2015-11-05 10:02:48 -07:00
|
|
|
verify_echo()
|
|
|
|
source([[
|
2024-01-02 18:09:18 -07:00
|
|
|
call dictwatcherdel(]] .. dict_expr .. [[, "wat*", "g:Changed")
|
2015-11-05 10:02:48 -07:00
|
|
|
]])
|
|
|
|
-- watch every key pattern
|
|
|
|
source([[
|
2024-01-02 18:09:18 -07:00
|
|
|
call dictwatcheradd(]] .. dict_expr .. [[, "*", "g:Changed")
|
2015-11-05 10:02:48 -07:00
|
|
|
]])
|
|
|
|
update('= 3', 'another_key')
|
|
|
|
update('= 4', 'another_key')
|
|
|
|
update('', 'another_key')
|
|
|
|
update('= 2')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ new = 3 }, 'another_key')
|
|
|
|
verify_value({ old = 3, new = 4 }, 'another_key')
|
|
|
|
verify_value({ old = 4 }, 'another_key')
|
|
|
|
verify_value({ old = 1, new = 2 })
|
|
|
|
verify_value({ old = 1, new = 2 })
|
2015-11-05 10:02:48 -07:00
|
|
|
verify_echo()
|
|
|
|
source([[
|
2024-01-02 18:09:18 -07:00
|
|
|
call dictwatcherdel(]] .. dict_expr .. [[, "*", "g:Changed")
|
2015-11-05 10:02:48 -07:00
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2016-09-03 16:25:24 -07:00
|
|
|
it('is triggered for empty keys', function()
|
|
|
|
command([[
|
2024-01-02 18:09:18 -07:00
|
|
|
call dictwatcheradd(]] .. dict_expr .. [[, "", "g:Changed")
|
2016-09-03 16:25:24 -07:00
|
|
|
]])
|
|
|
|
update('= 1', '')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ new = 1 }, '')
|
2016-09-03 16:25:24 -07:00
|
|
|
update('= 2', '')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ old = 1, new = 2 }, '')
|
2016-09-03 16:25:24 -07:00
|
|
|
command([[
|
2024-01-02 18:09:18 -07:00
|
|
|
call dictwatcherdel(]] .. dict_expr .. [[, "", "g:Changed")
|
2016-09-03 16:25:24 -07:00
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('is triggered for empty keys when using catch-all *', function()
|
|
|
|
command([[
|
2024-01-02 18:09:18 -07:00
|
|
|
call dictwatcheradd(]] .. dict_expr .. [[, "*", "g:Changed")
|
2016-09-03 16:25:24 -07:00
|
|
|
]])
|
|
|
|
update('= 1', '')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ new = 1 }, '')
|
2016-09-03 16:25:24 -07:00
|
|
|
update('= 2', '')
|
2024-01-02 18:09:18 -07:00
|
|
|
verify_value({ old = 1, new = 2 }, '')
|
2016-09-03 16:25:24 -07:00
|
|
|
command([[
|
2024-01-02 18:09:18 -07:00
|
|
|
call dictwatcherdel(]] .. dict_expr .. [[, "*", "g:Changed")
|
2016-09-03 16:25:24 -07:00
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2015-11-05 10:02:48 -07:00
|
|
|
-- test a sequence of updates of different types to ensure proper memory
|
|
|
|
-- management(with ASAN)
|
|
|
|
local function test_updates(tests)
|
|
|
|
it('test change sequence', function()
|
|
|
|
local input, output
|
|
|
|
for i = 1, #tests do
|
|
|
|
input, output = unpack(tests[i])
|
|
|
|
update(input)
|
|
|
|
verify_value(output)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
test_updates({
|
2024-01-02 18:09:18 -07:00
|
|
|
{ '= 3', { new = 3 } },
|
|
|
|
{ '= 6', { old = 3, new = 6 } },
|
|
|
|
{ '+= 3', { old = 6, new = 9 } },
|
|
|
|
{ '', { old = 9 } },
|
2015-11-05 10:02:48 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
test_updates({
|
2024-01-02 18:09:18 -07:00
|
|
|
{ '= "str"', { new = 'str' } },
|
|
|
|
{ '= "str2"', { old = 'str', new = 'str2' } },
|
|
|
|
{ '.= "2str"', { old = 'str2', new = 'str22str' } },
|
|
|
|
{ '', { old = 'str22str' } },
|
2015-11-05 10:02:48 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
test_updates({
|
2024-01-02 18:09:18 -07:00
|
|
|
{ '= [1, 2]', { new = { 1, 2 } } },
|
|
|
|
{ '= [1, 2, 3]', { old = { 1, 2 }, new = { 1, 2, 3 } } },
|
2015-11-05 10:02:48 -07:00
|
|
|
-- the += will update the list in place, so old and new are the same
|
2024-01-02 18:09:18 -07:00
|
|
|
{ '+= [4, 5]', { old = { 1, 2, 3, 4, 5 }, new = { 1, 2, 3, 4, 5 } } },
|
|
|
|
{ '', { old = { 1, 2, 3, 4, 5 } } },
|
2015-11-05 10:02:48 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
test_updates({
|
2024-01-02 18:09:18 -07:00
|
|
|
{ '= {"k": "v"}', { new = { k = 'v' } } },
|
|
|
|
{ '= {"k1": 2}', { old = { k = 'v' }, new = { k1 = 2 } } },
|
|
|
|
{ '', { old = { k1 = 2 } } },
|
2015-11-05 10:02:48 -07:00
|
|
|
})
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
gentests('g:')
|
|
|
|
gentests('b:')
|
|
|
|
gentests('w:')
|
|
|
|
gentests('t:')
|
2016-09-03 16:25:24 -07:00
|
|
|
gentests('g:dict_var', 'let g:dict_var = {}')
|
2015-11-05 10:02:48 -07:00
|
|
|
|
|
|
|
describe('multiple watchers on the same dict/key', function()
|
2016-09-03 16:25:24 -07:00
|
|
|
before_each(function()
|
2015-11-05 10:02:48 -07:00
|
|
|
source([[
|
|
|
|
function! g:Watcher1(dict, key, value)
|
|
|
|
call rpcnotify(g:channel, '1', a:key, a:value)
|
|
|
|
endfunction
|
|
|
|
function! g:Watcher2(dict, key, value)
|
|
|
|
call rpcnotify(g:channel, '2', a:key, a:value)
|
|
|
|
endfunction
|
|
|
|
call dictwatcheradd(g:, "key", "g:Watcher1")
|
|
|
|
call dictwatcheradd(g:, "key", "g:Watcher2")
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('invokes all callbacks when the key is changed', function()
|
2024-01-12 06:11:28 -07:00
|
|
|
command('let g:key = "value"')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', '1', { 'key', { new = 'value' } } }, next_msg())
|
|
|
|
eq({ 'notification', '2', { 'key', { new = 'value' } } }, next_msg())
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('only removes watchers that fully match dict, key and callback', function()
|
2024-01-12 06:11:28 -07:00
|
|
|
command('let g:key = "value"')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', '1', { 'key', { new = 'value' } } }, next_msg())
|
|
|
|
eq({ 'notification', '2', { 'key', { new = 'value' } } }, next_msg())
|
2024-01-12 06:11:28 -07:00
|
|
|
command('call dictwatcherdel(g:, "key", "g:Watcher1")')
|
|
|
|
command('let g:key = "v2"')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', '2', { 'key', { old = 'value', new = 'v2' } } }, next_msg())
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2016-12-04 12:59:25 -07:00
|
|
|
it('errors out when adding to v:_null_dict', function()
|
|
|
|
command([[
|
|
|
|
function! g:Watcher1(dict, key, value)
|
|
|
|
call rpcnotify(g:channel, '1', a:key, a:value)
|
|
|
|
endfunction
|
|
|
|
]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
'Vim(call):E46: Cannot change read-only variable "dictwatcheradd() argument"',
|
|
|
|
exc_exec('call dictwatcheradd(v:_null_dict, "x", "g:Watcher1")')
|
|
|
|
)
|
2016-12-04 12:59:25 -07:00
|
|
|
end)
|
|
|
|
|
2015-11-05 10:02:48 -07:00
|
|
|
describe('errors', function()
|
2016-09-03 16:25:24 -07:00
|
|
|
before_each(function()
|
|
|
|
source([[
|
|
|
|
function! g:Watcher1(dict, key, value)
|
|
|
|
call rpcnotify(g:channel, '1', a:key, a:value)
|
|
|
|
endfunction
|
|
|
|
function! g:Watcher2(dict, key, value)
|
|
|
|
call rpcnotify(g:channel, '2', a:key, a:value)
|
|
|
|
endfunction
|
|
|
|
]])
|
|
|
|
end)
|
|
|
|
|
2015-11-05 10:02:48 -07:00
|
|
|
-- WARNING: This suite depends on the above tests
|
|
|
|
it('fails to remove if no watcher with matching callback is found', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
"Vim(call):Couldn't find a watcher matching key and callback",
|
|
|
|
exc_exec('call dictwatcherdel(g:, "key", "g:Watcher1")')
|
|
|
|
)
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('fails to remove if no watcher with matching key is found', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
"Vim(call):Couldn't find a watcher matching key and callback",
|
|
|
|
exc_exec('call dictwatcherdel(g:, "invalid_key", "g:Watcher2")')
|
|
|
|
)
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|
|
|
|
|
2016-11-01 02:54:32 -07:00
|
|
|
it("does not fail to add/remove if the callback doesn't exist", function()
|
|
|
|
command('call dictwatcheradd(g:, "key", "g:InvalidCb")')
|
|
|
|
command('call dictwatcherdel(g:, "key", "g:InvalidCb")')
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|
|
|
|
|
2016-12-04 12:59:25 -07:00
|
|
|
it('fails to remove watcher from v:_null_dict', function()
|
2024-01-02 18:09:18 -07:00
|
|
|
eq(
|
|
|
|
"Vim(call):Couldn't find a watcher matching key and callback",
|
|
|
|
exc_exec('call dictwatcherdel(v:_null_dict, "x", "g:Watcher2")')
|
|
|
|
)
|
2016-12-04 12:59:25 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
--[[
|
|
|
|
[ it("fails to add/remove if the callback doesn't exist", function()
|
|
|
|
[ eq("Vim(call):Function g:InvalidCb doesn't exist",
|
|
|
|
[ exc_exec('call dictwatcheradd(g:, "key", "g:InvalidCb")'))
|
|
|
|
[ eq("Vim(call):Function g:InvalidCb doesn't exist",
|
|
|
|
[ exc_exec('call dictwatcherdel(g:, "key", "g:InvalidCb")'))
|
|
|
|
[ end)
|
|
|
|
]]
|
|
|
|
|
2016-11-01 02:54:32 -07:00
|
|
|
it('does not fail to replace a watcher function', function()
|
2015-11-05 10:02:48 -07:00
|
|
|
source([[
|
2016-09-03 16:25:24 -07:00
|
|
|
let g:key = 'v2'
|
|
|
|
call dictwatcheradd(g:, "key", "g:Watcher2")
|
2015-11-05 10:02:48 -07:00
|
|
|
function! g:ReplaceWatcher2()
|
2016-11-01 02:54:32 -07:00
|
|
|
function! g:Watcher2(dict, key, value)
|
|
|
|
call rpcnotify(g:channel, '2b', a:key, a:value)
|
2015-11-05 10:02:48 -07:00
|
|
|
endfunction
|
|
|
|
endfunction
|
|
|
|
]])
|
2016-11-01 02:54:32 -07:00
|
|
|
command('call g:ReplaceWatcher2()')
|
|
|
|
command('let g:key = "value"')
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', '2b', { 'key', { old = 'v2', new = 'value' } } }, next_msg())
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|
2017-01-12 11:57:37 -07:00
|
|
|
|
|
|
|
it('does not crash when freeing a watched dictionary', function()
|
|
|
|
source([[
|
2016-12-19 20:09:07 -07:00
|
|
|
function! Watcher(dict, key, value)
|
|
|
|
echo a:key string(a:value)
|
|
|
|
endfunction
|
2017-01-12 11:57:37 -07:00
|
|
|
|
2016-12-19 20:09:07 -07:00
|
|
|
function! MakeWatch()
|
|
|
|
let d = {'foo': 'bar'}
|
|
|
|
call dictwatcheradd(d, 'foo', function('Watcher'))
|
|
|
|
endfunction
|
2017-01-12 11:57:37 -07:00
|
|
|
]])
|
|
|
|
|
|
|
|
command('call MakeWatch()')
|
2021-09-01 09:42:53 -07:00
|
|
|
assert_alive()
|
2017-01-12 11:57:37 -07:00
|
|
|
end)
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|
2016-12-19 20:09:07 -07:00
|
|
|
|
|
|
|
describe('with lambdas', function()
|
|
|
|
it('works correctly', function()
|
|
|
|
source([[
|
|
|
|
let d = {'foo': 'baz'}
|
|
|
|
call dictwatcheradd(d, 'foo', {dict, key, value -> rpcnotify(g:channel, '2', key, value)})
|
|
|
|
let d.foo = 'bar'
|
|
|
|
]])
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'notification', '2', { 'foo', { old = 'baz', new = 'bar' } } }, next_msg())
|
2016-12-19 20:09:07 -07:00
|
|
|
end)
|
|
|
|
end)
|
2019-03-07 05:37:46 -07:00
|
|
|
|
|
|
|
it('for b:changedtick', function()
|
|
|
|
source([[
|
|
|
|
function! OnTickChanged(dict, key, value)
|
|
|
|
call rpcnotify(g:channel, 'SendChangeTick', a:key, a:value)
|
|
|
|
endfunction
|
|
|
|
call dictwatcheradd(b:, 'changedtick', 'OnTickChanged')
|
|
|
|
]])
|
|
|
|
|
2024-01-02 18:09:18 -07:00
|
|
|
insert('t')
|
|
|
|
eq({ 'notification', 'SendChangeTick', { 'changedtick', { old = 2, new = 3 } } }, next_msg())
|
2019-03-07 05:37:46 -07:00
|
|
|
|
|
|
|
command([[call dictwatcherdel(b:, 'changedtick', 'OnTickChanged')]])
|
2024-01-02 18:09:18 -07:00
|
|
|
insert('t')
|
2021-09-01 09:42:53 -07:00
|
|
|
assert_alive()
|
2019-03-07 05:37:46 -07:00
|
|
|
end)
|
|
|
|
|
2019-12-02 03:09:30 -07:00
|
|
|
it('does not cause use-after-free when unletting from callback', function()
|
|
|
|
source([[
|
|
|
|
let g:called = 0
|
|
|
|
function W(...) abort
|
|
|
|
unlet g:d
|
|
|
|
let g:called = 1
|
|
|
|
endfunction
|
|
|
|
let g:d = {}
|
|
|
|
call dictwatcheradd(g:d, '*', function('W'))
|
|
|
|
let g:d.foo = 123
|
|
|
|
]])
|
|
|
|
eq(1, eval('g:called'))
|
|
|
|
end)
|
|
|
|
|
2019-10-09 10:40:50 -07:00
|
|
|
it('does not crash when using dictwatcherdel in callback', function()
|
|
|
|
source([[
|
|
|
|
let g:d = {}
|
|
|
|
|
2019-11-29 01:49:32 -07:00
|
|
|
function! W1(...)
|
|
|
|
" Delete current and following watcher.
|
|
|
|
call dictwatcherdel(g:d, '*', function('W1'))
|
|
|
|
call dictwatcherdel(g:d, '*', function('W2'))
|
2019-10-09 10:40:50 -07:00
|
|
|
try
|
|
|
|
call dictwatcherdel({}, 'meh', function('tr'))
|
|
|
|
catch
|
|
|
|
let g:exc = v:exception
|
|
|
|
endtry
|
|
|
|
endfunction
|
2019-11-29 01:49:32 -07:00
|
|
|
call dictwatcheradd(g:d, '*', function('W1'))
|
|
|
|
|
|
|
|
function! W2(...)
|
|
|
|
endfunction
|
|
|
|
call dictwatcheradd(g:d, '*', function('W2'))
|
2019-10-09 10:40:50 -07:00
|
|
|
|
|
|
|
let g:d.foo = 23
|
|
|
|
]])
|
|
|
|
eq(23, eval('g:d.foo'))
|
|
|
|
eq("Vim(call):Couldn't find a watcher matching key and callback", eval('g:exc'))
|
|
|
|
end)
|
|
|
|
|
2019-12-02 05:38:58 -07:00
|
|
|
it('does not call watcher added in callback', function()
|
|
|
|
source([[
|
|
|
|
let g:d = {}
|
|
|
|
let g:calls = []
|
|
|
|
|
|
|
|
function! W1(...) abort
|
|
|
|
call add(g:calls, 'W1')
|
|
|
|
call dictwatcheradd(g:d, '*', function('W2'))
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! W2(...) abort
|
|
|
|
call add(g:calls, 'W2')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call dictwatcheradd(g:d, '*', function('W1'))
|
|
|
|
let g:d.foo = 23
|
|
|
|
]])
|
|
|
|
eq(23, eval('g:d.foo'))
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'W1' }, eval('g:calls'))
|
2019-12-02 05:38:58 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('calls watcher deleted in callback', function()
|
|
|
|
source([[
|
|
|
|
let g:d = {}
|
|
|
|
let g:calls = []
|
|
|
|
|
|
|
|
function! W1(...) abort
|
|
|
|
call add(g:calls, "W1")
|
|
|
|
call dictwatcherdel(g:d, '*', function('W2'))
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! W2(...) abort
|
|
|
|
call add(g:calls, "W2")
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call dictwatcheradd(g:d, '*', function('W1'))
|
|
|
|
call dictwatcheradd(g:d, '*', function('W2'))
|
|
|
|
let g:d.foo = 123
|
|
|
|
|
|
|
|
unlet g:d
|
|
|
|
let g:d = {}
|
|
|
|
call dictwatcheradd(g:d, '*', function('W2'))
|
|
|
|
call dictwatcheradd(g:d, '*', function('W1'))
|
|
|
|
let g:d.foo = 123
|
|
|
|
]])
|
|
|
|
eq(123, eval('g:d.foo'))
|
2024-01-02 18:09:18 -07:00
|
|
|
eq({ 'W1', 'W2', 'W2', 'W1' }, eval('g:calls'))
|
2019-12-02 05:38:58 -07:00
|
|
|
end)
|
2015-11-05 10:02:48 -07:00
|
|
|
end)
|