]> granicus.if.org Git - vim/commitdiff
patch 8.1.1765: get(func, dict, def) does not work properly v8.1.1765
authorBram Moolenaar <Bram@vim.org>
Sun, 28 Jul 2019 11:21:01 +0000 (13:21 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 28 Jul 2019 11:21:01 +0000 (13:21 +0200)
Problem:    get(func, dict, def) does not work properly.
Solution:   Handle NULL dict better. (Takuya Fujiwara, closes #4734)

src/evalfunc.c
src/testdir/test_getvar.vim
src/testdir/test_partial.vim
src/version.c

index 804dbb47d0381e6af5f6b70eaf80e8dfac332eb0..449bc67b412b9b15b8a90f99a026ebb94d3147e1 100644 (file)
@@ -4197,6 +4197,7 @@ f_get(typval_T *argvars, typval_T *rettv)
     dictitem_T *di;
     dict_T     *d;
     typval_T   *tv = NULL;
+    int                what_is_dict = FALSE;
 
     if (argvars[0].v_type == VAR_BLOB)
     {
@@ -4270,7 +4271,11 @@ f_get(typval_T *argvars, typval_T *rettv)
                }
            }
            else if (STRCMP(what, "dict") == 0)
-               rettv_dict_set(rettv, pt->pt_dict);
+           {
+               what_is_dict = TRUE;
+               if (pt->pt_dict != NULL)
+                   rettv_dict_set(rettv, pt->pt_dict);
+           }
            else if (STRCMP(what, "args") == 0)
            {
                rettv->v_type = VAR_LIST;
@@ -4284,7 +4289,11 @@ f_get(typval_T *argvars, typval_T *rettv)
            }
            else
                semsg(_(e_invarg2), what);
-           return;
+
+           // When {what} == "dict" and pt->pt_dict == NULL, evaluate the
+           // third argument
+           if (!what_is_dict)
+               return;
        }
     }
     else
index d6b6b69aa8143d06d0f9ba3e4b19179246ddf567..3b61d68ebce09d9192660f73f7acf987601c8995 100644 (file)
@@ -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
index b180510943b7fb19e8399309113e4b7ce3a97cf8..e7a7d058f03d939259369207751e246c3be6edf1 100644 (file)
@@ -320,6 +320,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()
index 6619c7b263a8789cc7d1dde262d2ad994b541722..2ec8f4db73eb0a8d9bcb2638f9c3d9278967c9ba 100644 (file)
@@ -777,6 +777,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1765,
 /**/
     1764,
 /**/