diff options
author | Jan Edmund Lazo <jan.lazo@mail.utoronto.ca> | 2019-07-28 16:07:37 -0400 |
---|---|---|
committer | Jan Edmund Lazo <jan.lazo@mail.utoronto.ca> | 2019-07-28 16:11:54 -0400 |
commit | 98d389ce55f2436148ff3b2faed7fa0d4ab712af (patch) | |
tree | f9280c29ef4b7b0d6edd233cfb4816c2e95ae1b2 | |
parent | fe2ada737510b91655bda3aac4f6385de6410c0d (diff) | |
download | rneovim-98d389ce55f2436148ff3b2faed7fa0d4ab712af.tar.gz rneovim-98d389ce55f2436148ff3b2faed7fa0d4ab712af.tar.bz2 rneovim-98d389ce55f2436148ff3b2faed7fa0d4ab712af.zip |
vim-patch:8.1.1765: get(func, dict, def) does not work properly
Problem: get(func, dict, def) does not work properly.
Solution: Handle NULL dict better. (Takuya Fujiwara, closes vim/vim#4734)
https://github.com/vim/vim/commit/f91aac5e3e3b8b1633d84eac2687ebbd76d8133b
-rw-r--r-- | src/nvim/eval.c | 13 | ||||
-rw-r--r-- | src/nvim/testdir/test_getvar.vim | 44 | ||||
-rw-r--r-- | src/nvim/testdir/test_partial.vim | 5 |
3 files changed, 59 insertions, 3 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 6931c5e4fb..b9f5863499 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -9600,6 +9600,7 @@ static void f_get(typval_T *argvars, typval_T *rettv, FunPtr fptr) dictitem_T *di; dict_T *d; typval_T *tv = NULL; + bool what_is_dict = false; if (argvars[0].v_type == VAR_LIST) { if ((l = argvars[0].vval.v_list) != NULL) { @@ -9641,7 +9642,10 @@ static void f_get(typval_T *argvars, typval_T *rettv, FunPtr fptr) func_ref(rettv->vval.v_string); } } else if (strcmp(what, "dict") == 0) { - tv_dict_set_ret(rettv, pt->pt_dict); + what_is_dict = true; + if (pt->pt_dict != NULL) { + tv_dict_set_ret(rettv, pt->pt_dict); + } } else if (strcmp(what, "args") == 0) { rettv->v_type = VAR_LIST; if (tv_list_alloc_ret(rettv, pt->pt_argc) != NULL) { @@ -9652,7 +9656,12 @@ static void f_get(typval_T *argvars, typval_T *rettv, FunPtr fptr) } else { EMSG2(_(e_invarg2), what); } - return; + + // When {what} == "dict" and pt->pt_dict == NULL, evaluate the + // third argument + if (!what_is_dict) { + return; + } } } else { EMSG2(_(e_listdictarg), "get()"); diff --git a/src/nvim/testdir/test_getvar.vim b/src/nvim/testdir/test_getvar.vim index d6b6b69aa8..3b61d68ebc 100644 --- a/src/nvim/testdir/test_getvar.vim +++ b/src/nvim/testdir/test_getvar.vim @@ -1,4 +1,5 @@ -" Tests for getwinvar(), gettabvar() and gettabwinvar(). +" Tests for getwinvar(), gettabvar(), gettabwinvar() and get(). + func Test_var() " Use strings to test for memory leaks. First, check that in an empty " window, gettabvar() returns the correct value @@ -102,3 +103,44 @@ func Test_gettabvar_in_tabline() close redrawstatus! endfunc + +" Test get() function using default value. + +" get({dict}, {key} [, {default}]) +func Test_get_dict() + let d = {'foo': 42} + call assert_equal(42, get(d, 'foo', 99)) + call assert_equal(999, get(d, 'bar', 999)) +endfunc + +" get({list}, {idx} [, {default}]) +func Test_get_list() + let l = [1,2,3] + call assert_equal(1, get(l, 0, 999)) + call assert_equal(3, get(l, -1, 999)) + call assert_equal(999, get(l, 3, 999)) +endfunc + +" get({blob}, {idx} [, {default}]) - in test_blob.vim + +" get({lambda}, {what} [, {default}]) +func Test_get_lambda() + let l:L = {-> 42} + call assert_match('^<lambda>', get(l:L, 'name')) + call assert_equal(l:L, get(l:L, 'func')) + call assert_equal({'lambda has': 'no dict'}, get(l:L, 'dict', {'lambda has': 'no dict'})) + call assert_equal(0, get(l:L, 'dict')) + call assert_equal([], get(l:L, 'args')) +endfunc + +" get({func}, {what} [, {default}]) +func Test_get_func() + let l:F = function('tr') + call assert_equal('tr', get(l:F, 'name')) + call assert_equal(l:F, get(l:F, 'func')) + call assert_equal({'func has': 'no dict'}, get(l:F, 'dict', {'func has': 'no dict'})) + call assert_equal(0, get(l:F, 'dict')) + call assert_equal([], get(l:F, 'args')) +endfunc + +" get({partial}, {what} [, {default}]) - in test_partial.vim diff --git a/src/nvim/testdir/test_partial.vim b/src/nvim/testdir/test_partial.vim index de5c26c2dd..590e18e024 100644 --- a/src/nvim/testdir/test_partial.vim +++ b/src/nvim/testdir/test_partial.vim @@ -285,6 +285,11 @@ func Test_get_partial_items() call assert_equal('MyDictFunc', get(Func, 'name')) call assert_equal([], get(Func, 'args')) call assert_true(empty( get(Func, 'dict'))) + + let P = function('substitute', ['hello there', 'there']) + let dict = {'partial has': 'no dict'} + call assert_equal(dict, get(P, 'dict', dict)) + call assert_equal(0, get(l:P, 'dict')) endfunc func Test_compare_partials() |