diff options
Diffstat (limited to 'test/functional/ex_cmds/dict_notifications_spec.lua')
-rw-r--r-- | test/functional/ex_cmds/dict_notifications_spec.lua | 194 |
1 files changed, 100 insertions, 94 deletions
diff --git a/test/functional/ex_cmds/dict_notifications_spec.lua b/test/functional/ex_cmds/dict_notifications_spec.lua index 6a0b40bd88..fc7714d16b 100644 --- a/test/functional/ex_cmds/dict_notifications_spec.lua +++ b/test/functional/ex_cmds/dict_notifications_spec.lua @@ -1,6 +1,7 @@ local helpers = require('test.functional.helpers')(after_each) local assert_alive = helpers.assert_alive -local clear, nvim, source = helpers.clear, helpers.nvim, helpers.source +local clear, source = helpers.clear, helpers.source +local api = helpers.api local insert = helpers.insert local eq, next_msg = helpers.eq, helpers.next_msg local exc_exec = helpers.exc_exec @@ -8,14 +9,13 @@ local exec_lua = helpers.exec_lua local command = helpers.command local eval = helpers.eval - describe('Vimscript dictionary notifications', function() local channel before_each(function() clear() - channel = nvim('get_api_info')[1] - nvim('set_var', 'channel', channel) + channel = api.nvim_get_chan_info(0).id + api.nvim_set_var('channel', channel) end) -- the same set of tests are applied to top-level dictionaries(g:, b:, w: and @@ -29,9 +29,9 @@ describe('Vimscript dictionary notifications', function() key = 'watched' end if opval == '' then - command(('unlet %s[\'%s\']'):format(dict_expr, key)) + command(("unlet %s['%s']"):format(dict_expr, key)) else - command(('let %s[\'%s\'] %s'):format(dict_expr, key, opval)) + command(("let %s['%s'] %s"):format(dict_expr, key, opval)) end end @@ -40,9 +40,9 @@ describe('Vimscript dictionary notifications', function() key = 'watched' end if opval == '' then - exec_lua(('vim.api.nvim_del_var(\'%s\')'):format(key)) + exec_lua(("vim.api.nvim_del_var('%s')"):format(key)) else - exec_lua(('vim.api.nvim_set_var(\'%s\', %s)'):format(key, opval)) + exec_lua(("vim.api.nvim_set_var('%s', %s)"):format(key, opval)) end end @@ -60,15 +60,15 @@ describe('Vimscript dictionary notifications', function() local function verify_echo() -- helper to verify that no notifications are sent after certain change -- to a dict - nvim('command', "call rpcnotify(g:channel, 'echo')") - eq({'notification', 'echo', {}}, next_msg()) + command("call rpcnotify(g:channel, 'echo')") + eq({ 'notification', 'echo', {} }, next_msg()) end local function verify_value(vals, key) if not key then key = 'watched' end - eq({'notification', 'values', {key, vals}}, next_msg()) + eq({ 'notification', 'values', { key, vals } }, next_msg()) end describe(dict_expr .. ' watcher', function() @@ -81,20 +81,20 @@ describe('Vimscript dictionary notifications', function() before_each(function() source([[ function! g:Changed(dict, key, value) - if a:dict isnot ]]..dict_expr..[[ | + if a:dict isnot ]] .. dict_expr .. [[ | throw 'invalid dict' endif call rpcnotify(g:channel, 'values', a:key, a:value) endfunction - call dictwatcheradd(]]..dict_expr..[[, "watched", "g:Changed") - call dictwatcheradd(]]..dict_expr..[[, "watched2", "g:Changed") + call dictwatcheradd(]] .. dict_expr .. [[, "watched", "g:Changed") + call dictwatcheradd(]] .. dict_expr .. [[, "watched2", "g:Changed") ]]) end) after_each(function() source([[ - call dictwatcherdel(]]..dict_expr..[[, "watched", "g:Changed") - call dictwatcherdel(]]..dict_expr..[[, "watched2", "g:Changed") + call dictwatcherdel(]] .. dict_expr .. [[, "watched", "g:Changed") + call dictwatcherdel(]] .. dict_expr .. [[, "watched2", "g:Changed") ]]) update('= "test"') update('= "test2"', 'watched2') @@ -134,99 +134,99 @@ describe('Vimscript dictionary notifications', function() it('is triggered by remove()', function() update('= "test"') - verify_value({new = 'test'}) - nvim('command', 'call remove('..dict_expr..', "watched")') - verify_value({old = 'test'}) + verify_value({ new = 'test' }) + command('call remove(' .. dict_expr .. ', "watched")') + verify_value({ old = 'test' }) end) if is_g then it('is triggered by remove() when updated with nvim_*_var', function() update_with_api('"test"') - verify_value({new = 'test'}) - nvim('command', 'call remove('..dict_expr..', "watched")') - verify_value({old = 'test'}) + verify_value({ new = 'test' }) + command('call remove(' .. dict_expr .. ', "watched")') + verify_value({ old = 'test' }) end) it('is triggered by remove() when updated with vim.g', function() update_with_vim_g('= "test"') - verify_value({new = 'test'}) - nvim('command', 'call remove('..dict_expr..', "watched")') - verify_value({old = 'test'}) + verify_value({ new = 'test' }) + command('call remove(' .. dict_expr .. ', "watched")') + verify_value({ old = 'test' }) end) end it('is triggered by extend()', function() update('= "xtend"') - verify_value({new = 'xtend'}) - nvim('command', [[ - call extend(]]..dict_expr..[[, {'watched': 'xtend2', 'watched2': 5, 'watched3': 'a'}) + verify_value({ new = 'xtend' }) + command([[ + call extend(]] .. dict_expr .. [[, {'watched': 'xtend2', 'watched2': 5, 'watched3': 'a'}) ]]) - verify_value({old = 'xtend', new = 'xtend2'}) - verify_value({new = 5}, 'watched2') + verify_value({ old = 'xtend', new = 'xtend2' }) + verify_value({ new = 5 }, 'watched2') update('') - verify_value({old = 'xtend2'}) + verify_value({ old = 'xtend2' }) update('', 'watched2') - verify_value({old = 5}, 'watched2') + verify_value({ old = 5 }, 'watched2') update('', 'watched3') verify_echo() end) it('is triggered with key patterns', function() source([[ - call dictwatcheradd(]]..dict_expr..[[, "wat*", "g:Changed") + call dictwatcheradd(]] .. dict_expr .. [[, "wat*", "g:Changed") ]]) update('= 1') - verify_value({new = 1}) - verify_value({new = 1}) + verify_value({ new = 1 }) + verify_value({ new = 1 }) update('= 3', 'watched2') - verify_value({new = 3}, 'watched2') - verify_value({new = 3}, 'watched2') + verify_value({ new = 3 }, 'watched2') + verify_value({ new = 3 }, 'watched2') verify_echo() source([[ - call dictwatcherdel(]]..dict_expr..[[, "wat*", "g:Changed") + call dictwatcherdel(]] .. dict_expr .. [[, "wat*", "g:Changed") ]]) -- watch every key pattern source([[ - call dictwatcheradd(]]..dict_expr..[[, "*", "g:Changed") + call dictwatcheradd(]] .. dict_expr .. [[, "*", "g:Changed") ]]) update('= 3', 'another_key') update('= 4', 'another_key') update('', 'another_key') update('= 2') - 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}) + 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 }) verify_echo() source([[ - call dictwatcherdel(]]..dict_expr..[[, "*", "g:Changed") + call dictwatcherdel(]] .. dict_expr .. [[, "*", "g:Changed") ]]) end) it('is triggered for empty keys', function() command([[ - call dictwatcheradd(]]..dict_expr..[[, "", "g:Changed") + call dictwatcheradd(]] .. dict_expr .. [[, "", "g:Changed") ]]) update('= 1', '') - verify_value({new = 1}, '') + verify_value({ new = 1 }, '') update('= 2', '') - verify_value({old = 1, new = 2}, '') + verify_value({ old = 1, new = 2 }, '') command([[ - call dictwatcherdel(]]..dict_expr..[[, "", "g:Changed") + call dictwatcherdel(]] .. dict_expr .. [[, "", "g:Changed") ]]) end) it('is triggered for empty keys when using catch-all *', function() command([[ - call dictwatcheradd(]]..dict_expr..[[, "*", "g:Changed") + call dictwatcheradd(]] .. dict_expr .. [[, "*", "g:Changed") ]]) update('= 1', '') - verify_value({new = 1}, '') + verify_value({ new = 1 }, '') update('= 2', '') - verify_value({old = 1, new = 2}, '') + verify_value({ old = 1, new = 2 }, '') command([[ - call dictwatcherdel(]]..dict_expr..[[, "*", "g:Changed") + call dictwatcherdel(]] .. dict_expr .. [[, "*", "g:Changed") ]]) end) @@ -244,31 +244,31 @@ describe('Vimscript dictionary notifications', function() end test_updates({ - {'= 3', {new = 3}}, - {'= 6', {old = 3, new = 6}}, - {'+= 3', {old = 6, new = 9}}, - {'', {old = 9}} + { '= 3', { new = 3 } }, + { '= 6', { old = 3, new = 6 } }, + { '+= 3', { old = 6, new = 9 } }, + { '', { old = 9 } }, }) test_updates({ - {'= "str"', {new = 'str'}}, - {'= "str2"', {old = 'str', new = 'str2'}}, - {'.= "2str"', {old = 'str2', new = 'str22str'}}, - {'', {old = 'str22str'}} + { '= "str"', { new = 'str' } }, + { '= "str2"', { old = 'str', new = 'str2' } }, + { '.= "2str"', { old = 'str2', new = 'str22str' } }, + { '', { old = 'str22str' } }, }) test_updates({ - {'= [1, 2]', {new = {1, 2}}}, - {'= [1, 2, 3]', {old = {1, 2}, new = {1, 2, 3}}}, + { '= [1, 2]', { new = { 1, 2 } } }, + { '= [1, 2, 3]', { old = { 1, 2 }, new = { 1, 2, 3 } } }, -- the += will update the list in place, so old and new are the same - {'+= [4, 5]', {old = {1, 2, 3, 4, 5}, new = {1, 2, 3, 4, 5}}}, - {'', {old = {1, 2, 3, 4 ,5}}} + { '+= [4, 5]', { old = { 1, 2, 3, 4, 5 }, new = { 1, 2, 3, 4, 5 } } }, + { '', { old = { 1, 2, 3, 4, 5 } } }, }) test_updates({ - {'= {"k": "v"}', {new = {k = 'v'}}}, - {'= {"k1": 2}', {old = {k = 'v'}, new = {k1 = 2}}}, - {'', {old = {k1 = 2}}}, + { '= {"k": "v"}', { new = { k = 'v' } } }, + { '= {"k1": 2}', { old = { k = 'v' }, new = { k1 = 2 } } }, + { '', { old = { k1 = 2 } } }, }) end) end @@ -294,18 +294,18 @@ describe('Vimscript dictionary notifications', function() end) it('invokes all callbacks when the key is changed', function() - nvim('command', 'let g:key = "value"') - eq({'notification', '1', {'key', {new = 'value'}}}, next_msg()) - eq({'notification', '2', {'key', {new = 'value'}}}, next_msg()) + command('let g:key = "value"') + eq({ 'notification', '1', { 'key', { new = 'value' } } }, next_msg()) + eq({ 'notification', '2', { 'key', { new = 'value' } } }, next_msg()) end) it('only removes watchers that fully match dict, key and callback', function() - nvim('command', 'let g:key = "value"') - eq({'notification', '1', {'key', {new = 'value'}}}, next_msg()) - eq({'notification', '2', {'key', {new = 'value'}}}, next_msg()) - nvim('command', 'call dictwatcherdel(g:, "key", "g:Watcher1")') - nvim('command', 'let g:key = "v2"') - eq({'notification', '2', {'key', {old = 'value', new = 'v2'}}}, next_msg()) + command('let g:key = "value"') + eq({ 'notification', '1', { 'key', { new = 'value' } } }, next_msg()) + eq({ 'notification', '2', { 'key', { new = 'value' } } }, next_msg()) + command('call dictwatcherdel(g:, "key", "g:Watcher1")') + command('let g:key = "v2"') + eq({ 'notification', '2', { 'key', { old = 'value', new = 'v2' } } }, next_msg()) end) end) @@ -315,8 +315,10 @@ describe('Vimscript dictionary notifications', function() call rpcnotify(g:channel, '1', a:key, a:value) endfunction ]]) - eq('Vim(call):E46: Cannot change read-only variable "dictwatcheradd() argument"', - exc_exec('call dictwatcheradd(v:_null_dict, "x", "g:Watcher1")')) + eq( + 'Vim(call):E46: Cannot change read-only variable "dictwatcheradd() argument"', + exc_exec('call dictwatcheradd(v:_null_dict, "x", "g:Watcher1")') + ) end) describe('errors', function() @@ -333,13 +335,17 @@ describe('Vimscript dictionary notifications', function() -- WARNING: This suite depends on the above tests it('fails to remove if no watcher with matching callback is found', function() - eq("Vim(call):Couldn't find a watcher matching key and callback", - exc_exec('call dictwatcherdel(g:, "key", "g:Watcher1")')) + eq( + "Vim(call):Couldn't find a watcher matching key and callback", + exc_exec('call dictwatcherdel(g:, "key", "g:Watcher1")') + ) end) it('fails to remove if no watcher with matching key is found', function() - eq("Vim(call):Couldn't find a watcher matching key and callback", - exc_exec('call dictwatcherdel(g:, "invalid_key", "g:Watcher2")')) + eq( + "Vim(call):Couldn't find a watcher matching key and callback", + exc_exec('call dictwatcherdel(g:, "invalid_key", "g:Watcher2")') + ) end) it("does not fail to add/remove if the callback doesn't exist", function() @@ -348,8 +354,10 @@ describe('Vimscript dictionary notifications', function() end) it('fails to remove watcher from v:_null_dict', function() - eq("Vim(call):Couldn't find a watcher matching key and callback", - exc_exec('call dictwatcherdel(v:_null_dict, "x", "g:Watcher2")')) + eq( + "Vim(call):Couldn't find a watcher matching key and callback", + exc_exec('call dictwatcherdel(v:_null_dict, "x", "g:Watcher2")') + ) end) --[[ @@ -373,7 +381,7 @@ describe('Vimscript dictionary notifications', function() ]]) command('call g:ReplaceWatcher2()') command('let g:key = "value"') - eq({'notification', '2b', {'key', {old = 'v2', new = 'value'}}}, next_msg()) + eq({ 'notification', '2b', { 'key', { old = 'v2', new = 'value' } } }, next_msg()) end) it('does not crash when freeing a watched dictionary', function() @@ -400,7 +408,7 @@ describe('Vimscript dictionary notifications', function() call dictwatcheradd(d, 'foo', {dict, key, value -> rpcnotify(g:channel, '2', key, value)}) let d.foo = 'bar' ]]) - eq({'notification', '2', {'foo', {old = 'baz', new = 'bar'}}}, next_msg()) + eq({ 'notification', '2', { 'foo', { old = 'baz', new = 'bar' } } }, next_msg()) end) end) @@ -412,12 +420,11 @@ describe('Vimscript dictionary notifications', function() call dictwatcheradd(b:, 'changedtick', 'OnTickChanged') ]]) - insert('t'); - eq({'notification', 'SendChangeTick', {'changedtick', {old = 2, new = 3}}}, - next_msg()) + insert('t') + eq({ 'notification', 'SendChangeTick', { 'changedtick', { old = 2, new = 3 } } }, next_msg()) command([[call dictwatcherdel(b:, 'changedtick', 'OnTickChanged')]]) - insert('t'); + insert('t') assert_alive() end) @@ -479,7 +486,7 @@ describe('Vimscript dictionary notifications', function() let g:d.foo = 23 ]]) eq(23, eval('g:d.foo')) - eq({"W1"}, eval('g:calls')) + eq({ 'W1' }, eval('g:calls')) end) it('calls watcher deleted in callback', function() @@ -507,7 +514,6 @@ describe('Vimscript dictionary notifications', function() let g:d.foo = 123 ]]) eq(123, eval('g:d.foo')) - eq({"W1", "W2", "W2", "W1"}, eval('g:calls')) + eq({ 'W1', 'W2', 'W2', 'W1' }, eval('g:calls')) end) - end) |