diff options
-rw-r--r-- | src/nvim/api/private/helpers.c | 13 | ||||
-rw-r--r-- | test/functional/api/buffer_spec.lua | 11 | ||||
-rw-r--r-- | test/functional/api/tabpage_spec.lua | 7 | ||||
-rw-r--r-- | test/functional/api/vim_spec.lua | 7 | ||||
-rw-r--r-- | test/functional/api/window_spec.lua | 7 | ||||
-rw-r--r-- | test/functional/eval/changedtick_spec.lua | 15 | ||||
-rw-r--r-- | test/functional/helpers.lua | 9 |
7 files changed, 61 insertions, 8 deletions
diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index 23f5b10e18..7efa086af2 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -131,6 +131,19 @@ Object dict_set_var(dict_T *dict, String key, Object value, bool del, dictitem_T *di = dict_find(dict, (char_u *)key.data, (int)key.size); + if (di != NULL) { + if (di->di_flags & DI_FLAGS_RO) { + api_set_error(err, Exception, _("Key is read-only: %s"), key.data); + return rv; + } else if (di->di_flags & DI_FLAGS_FIX) { + api_set_error(err, Exception, _("Key is fixed: %s"), key.data); + return rv; + } else if (di->di_flags & DI_FLAGS_LOCK) { + api_set_error(err, Exception, _("Key is locked: %s"), key.data); + return rv; + } + } + if (del) { // Delete the key if (di == NULL) { diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua index 075504fc8c..e58ae440cf 100644 --- a/test/functional/api/buffer_spec.lua +++ b/test/functional/api/buffer_spec.lua @@ -6,6 +6,8 @@ local funcs = helpers.funcs local request = helpers.request local neq = helpers.neq local NIL = helpers.NIL +local meth_pcall = helpers.meth_pcall +local command = helpers.command describe('api/buf', function() before_each(clear) @@ -251,6 +253,15 @@ describe('api/buf', function() eq(1, funcs.exists('b:lua')) curbufmeths.del_var('lua') eq(0, funcs.exists('b:lua')) + eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(curbufmeths.del_var, 'lua')) + curbufmeths.set_var('lua', 1) + command('lockvar b:lua') + eq({false, 'Key is locked: lua'}, meth_pcall(curbufmeths.del_var, 'lua')) + eq({false, 'Key is locked: lua'}, meth_pcall(curbufmeths.set_var, 'lua', 1)) + eq({false, 'Key is read-only: changedtick'}, + meth_pcall(curbufmeths.del_var, 'changedtick')) + eq({false, 'Key is read-only: changedtick'}, + meth_pcall(curbufmeths.set_var, 'changedtick', 1)) end) end) diff --git a/test/functional/api/tabpage_spec.lua b/test/functional/api/tabpage_spec.lua index e10f30085f..d7ef53a88f 100644 --- a/test/functional/api/tabpage_spec.lua +++ b/test/functional/api/tabpage_spec.lua @@ -6,6 +6,8 @@ local curtabmeths = helpers.curtabmeths local funcs = helpers.funcs local request = helpers.request local NIL = helpers.NIL +local meth_pcall = helpers.meth_pcall +local command = helpers.command describe('api/tabpage', function() before_each(clear) @@ -32,6 +34,11 @@ describe('api/tabpage', function() eq(1, funcs.exists('t:lua')) curtabmeths.del_var('lua') eq(0, funcs.exists('t:lua')) + eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(curtabmeths.del_var, 'lua')) + curtabmeths.set_var('lua', 1) + command('lockvar t:lua') + eq({false, 'Key is locked: lua'}, meth_pcall(curtabmeths.del_var, 'lua')) + eq({false, 'Key is locked: lua'}, meth_pcall(curtabmeths.set_var, 'lua', 1)) end) it('tabpage_set_var returns the old value', function() diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 551f408c57..3348368a36 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -7,6 +7,8 @@ local os_name = helpers.os_name local meths = helpers.meths local funcs = helpers.funcs local request = helpers.request +local meth_pcall = helpers.meth_pcall +local command = helpers.command describe('api', function() before_each(clear) @@ -117,6 +119,11 @@ describe('api', function() eq(1, funcs.exists('g:lua')) meths.del_var('lua') eq(0, funcs.exists('g:lua')) + eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(meths.del_var, 'lua')) + meths.set_var('lua', 1) + command('lockvar lua') + eq({false, 'Key is locked: lua'}, meth_pcall(meths.del_var, 'lua')) + eq({false, 'Key is locked: lua'}, meth_pcall(meths.set_var, 'lua', 1)) end) it('vim_set_var returns the old value', function() diff --git a/test/functional/api/window_spec.lua b/test/functional/api/window_spec.lua index 465bda6bc9..deffc68994 100644 --- a/test/functional/api/window_spec.lua +++ b/test/functional/api/window_spec.lua @@ -8,6 +8,8 @@ local curwinmeths = helpers.curwinmeths local funcs = helpers.funcs local request = helpers.request local NIL = helpers.NIL +local meth_pcall = helpers.meth_pcall +local command = helpers.command -- check if str is visible at the beginning of some line local function is_visible(str) @@ -137,6 +139,11 @@ describe('api/win', function() eq(1, funcs.exists('w:lua')) curwinmeths.del_var('lua') eq(0, funcs.exists('w:lua')) + eq({false, 'Key "lua" doesn\'t exist'}, meth_pcall(curwinmeths.del_var, 'lua')) + curwinmeths.set_var('lua', 1) + command('lockvar w:lua') + eq({false, 'Key is locked: lua'}, meth_pcall(curwinmeths.del_var, 'lua')) + eq({false, 'Key is locked: lua'}, meth_pcall(curwinmeths.set_var, 'lua', 1)) end) it('window_set_var returns the old value', function() diff --git a/test/functional/eval/changedtick_spec.lua b/test/functional/eval/changedtick_spec.lua index 74e581bd3c..f443f31bc0 100644 --- a/test/functional/eval/changedtick_spec.lua +++ b/test/functional/eval/changedtick_spec.lua @@ -1,16 +1,17 @@ local helpers = require('test.functional.helpers')(after_each) -local curbufmeths = helpers.curbufmeths -local clear = helpers.clear local eq = helpers.eq local neq = helpers.neq local eval = helpers.eval local feed = helpers.feed +local clear = helpers.clear local funcs = helpers.funcs local meths = helpers.meths local command = helpers.command local exc_exec = helpers.exc_exec local redir_exec = helpers.redir_exec +local meth_pcall = helpers.meth_pcall +local curbufmeths = helpers.curbufmeths before_each(clear) @@ -66,9 +67,8 @@ describe('b:changedtick', function() redir_exec('let b:.changedtick = ' .. ctn)) eq('\nE46: Cannot change read-only variable "d.changedtick"', redir_exec('let d.changedtick = ' .. ctn)) - -- FIXME - -- eq({fales, ''}, - -- {pcall(curbufmeths.set_var, 'changedtick', ctn)}) + eq({false, 'Key is read-only: changedtick'}, + meth_pcall(curbufmeths.set_var, 'changedtick', ctn)) eq('\nE795: Cannot delete variable b:changedtick', redir_exec('unlet b:changedtick')) @@ -78,9 +78,8 @@ describe('b:changedtick', function() redir_exec('unlet b:["changedtick"]')) eq('\nE46: Cannot change read-only variable "d.changedtick"', redir_exec('unlet d.changedtick')) - -- FIXME - -- eq({}, - -- {pcall(curbufmeths.del_var, 'changedtick')}) + eq({false, 'Key is read-only: changedtick'}, + meth_pcall(curbufmeths.del_var, 'changedtick')) eq(ct, changedtick()) eq('\nE46: Cannot change read-only variable "b:["changedtick"]"', diff --git a/test/functional/helpers.lua b/test/functional/helpers.lua index 53cbf8d4a1..14cab293ac 100644 --- a/test/functional/helpers.lua +++ b/test/functional/helpers.lua @@ -534,6 +534,14 @@ local function skip_fragile(pending_fn, cond) return false end +local function meth_pcall(...) + local ret = {pcall(...)} + if type(ret[2]) == 'string' then + ret[2] = ret[2]:gsub('^[^:]+:%d+: ', '') + end + return ret +end + local funcs = create_callindex(nvim_call) local meths = create_callindex(nvim) local uimeths = create_callindex(ui) @@ -604,6 +612,7 @@ local M = { skip_fragile = skip_fragile, set_shell_powershell = set_shell_powershell, tmpname = tmpname, + meth_pcall = meth_pcall, NIL = mpack.NIL, } |