changeset 24484:bc1a533148d7 v8.2.2782

patch 8.2.2782: Vim9: blob operations not fully tested Commit: https://github.com/vim/vim/commit/39211cba723a2cb58a97c7e08826713164b86efc Author: Bram Moolenaar <Bram@vim.org> Date: Sun Apr 18 15:48:04 2021 +0200 patch 8.2.2782: Vim9: blob operations not fully tested Problem: Vim9: blob operations not fully tested. Solution: Make more blob tests run in Vim9 script. Fix filter(). Make insert() give an error for a null blob, like add().
author Bram Moolenaar <Bram@vim.org>
date Sun, 18 Apr 2021 16:00:06 +0200
parents 798fcf242d48
children 243d48e704cb
files src/list.c src/testdir/test_blob.vim src/testdir/test_vim9_builtin.vim src/version.c
diffstat 4 files changed, 254 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/src/list.c
+++ b/src/list.c
@@ -2223,7 +2223,7 @@ filter_map(typval_T *argvars, typval_T *
 		if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
 								   || did_emsg)
 		    break;
-		if (newtv.v_type != VAR_NUMBER)
+		if (newtv.v_type != VAR_NUMBER && newtv.v_type != VAR_BOOL)
 		{
 		    clear_tv(&newtv);
 		    emsg(_(e_invalblob));
@@ -2736,7 +2736,6 @@ f_insert(typval_T *argvars, typval_T *re
 {
     long	before = 0;
     listitem_T	*item;
-    list_T	*l;
     int		error = FALSE;
 
     if (argvars[0].v_type == VAR_BLOB)
@@ -2745,7 +2744,11 @@ f_insert(typval_T *argvars, typval_T *re
 	char_u	    *p;
 
 	if (argvars[0].vval.v_blob == NULL)
+	{
+	    if (in_vim9script())
+		emsg(_(e_cannot_add_to_null_blob));
 	    return;
+	}
 
 	len = blob_len(argvars[0].vval.v_blob);
 	if (argvars[2].v_type != VAR_UNKNOWN)
@@ -2779,30 +2782,39 @@ f_insert(typval_T *argvars, typval_T *re
     }
     else if (argvars[0].v_type != VAR_LIST)
 	semsg(_(e_listblobarg), "insert()");
-    else if ((l = argvars[0].vval.v_list) != NULL
-	    && !value_check_lock(l->lv_lock,
-				     (char_u *)N_("insert() argument"), TRUE))
+    else
     {
-	if (argvars[2].v_type != VAR_UNKNOWN)
-	    before = (long)tv_get_number_chk(&argvars[2], &error);
-	if (error)
-	    return;		// type error; errmsg already given
+	list_T	*l = argvars[0].vval.v_list;
+
+	if (l == NULL)
+	{
+	    if (in_vim9script())
+		emsg(_(e_cannot_add_to_null_list));
+	}
+	else if (!value_check_lock(l->lv_lock,
+				     (char_u *)N_("insert() argument"), TRUE))
+	{
+	    if (argvars[2].v_type != VAR_UNKNOWN)
+		before = (long)tv_get_number_chk(&argvars[2], &error);
+	    if (error)
+		return;		// type error; errmsg already given
 
-	if (before == l->lv_len)
-	    item = NULL;
-	else
-	{
-	    item = list_find(l, before);
-	    if (item == NULL)
+	    if (before == l->lv_len)
+		item = NULL;
+	    else
 	    {
-		semsg(_(e_listidx), before);
-		l = NULL;
+		item = list_find(l, before);
+		if (item == NULL)
+		{
+		    semsg(_(e_listidx), before);
+		    l = NULL;
+		}
 	    }
-	}
-	if (l != NULL)
-	{
-	    (void)list_insert_tv(l, &argvars[1], item);
-	    copy_tv(&argvars[0], rettv);
+	    if (l != NULL)
+	    {
+		(void)list_insert_tv(l, &argvars[1], item);
+		copy_tv(&argvars[0], rettv);
+	    }
 	}
     }
 }
--- a/src/testdir/test_blob.vim
+++ b/src/testdir/test_blob.vim
@@ -379,45 +379,84 @@ endfunc
 
 " Test removing items in blob
 func Test_blob_func_remove()
-  " Test removing 1 element
-  let b = 0zDEADBEEF
-  call assert_equal(0xDE, remove(b, 0))
-  call assert_equal(0zADBEEF, b)
+  let lines =<< trim END
+      #" Test removing 1 element
+      VAR b = 0zDEADBEEF
+      call assert_equal(0xDE, remove(b, 0))
+      call assert_equal(0zADBEEF, b)
 
-  let b = 0zDEADBEEF
-  call assert_equal(0xEF, remove(b, -1))
-  call assert_equal(0zDEADBE, b)
+      LET b = 0zDEADBEEF
+      call assert_equal(0xEF, remove(b, -1))
+      call assert_equal(0zDEADBE, b)
 
-  let b = 0zDEADBEEF
-  call assert_equal(0xAD, remove(b, 1))
-  call assert_equal(0zDEBEEF, b)
+      LET b = 0zDEADBEEF
+      call assert_equal(0xAD, remove(b, 1))
+      call assert_equal(0zDEBEEF, b)
 
-  " Test removing range of element(s)
-  let b = 0zDEADBEEF
-  call assert_equal(0zBE, remove(b, 2, 2))
-  call assert_equal(0zDEADEF, b)
+      #" Test removing range of element(s)
+      LET b = 0zDEADBEEF
+      call assert_equal(0zBE, remove(b, 2, 2))
+      call assert_equal(0zDEADEF, b)
 
-  let b = 0zDEADBEEF
-  call assert_equal(0zADBE, remove(b, 1, 2))
-  call assert_equal(0zDEEF, b)
+      LET b = 0zDEADBEEF
+      call assert_equal(0zADBE, remove(b, 1, 2))
+      call assert_equal(0zDEEF, b)
+  END
+  call CheckLegacyAndVim9Success(lines)
 
   " Test invalid cases
-  let b = 0zDEADBEEF
-  call assert_fails("call remove(b, 5)", 'E979:')
-  call assert_fails("call remove(b, 1, 5)", 'E979:')
-  call assert_fails("call remove(b, 3, 2)", 'E979:')
-  call assert_fails("call remove(1, 0)", 'E896:')
-  call assert_fails("call remove(b, b)", 'E974:')
-  call assert_fails("call remove(b, 1, [])", 'E745:')
-  call assert_fails("call remove(test_null_blob(), 1, 2)", 'E979:')
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call remove(b, 5)
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E979:')
+
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call remove(b, 1, 5)
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E979:')
+
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call remove(b, 3, 2)
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E979:')
+
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call remove(1, 0)
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E896:')
+
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call remove(b, b)
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E974:')
+
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call remove(b, 1, [])
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E745:')
+
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call remove(test_null_blob(), 1, 2)
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E979:')
 endfunc
 
 func Test_blob_read_write()
-  let b = 0zDEADBEEF
-  call writefile(b, 'Xblob')
-  let br = readfile('Xblob', 'B')
-  call assert_equal(b, br)
-  call delete('Xblob')
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call writefile(b, 'Xblob')
+      VAR br = readfile('Xblob', 'B')
+      call assert_equal(b, br)
+      call delete('Xblob')
+  END
+  call CheckLegacyAndVim9Success(lines)
 
   " This was crashing when calling readfile() with a directory.
   call assert_fails("call readfile('.', 'B')", 'E17: "." is a directory')
@@ -425,84 +464,173 @@ endfunc
 
 " filter() item in blob
 func Test_blob_filter()
-  call assert_equal(test_null_blob(), filter(test_null_blob(), '0'))
-  call assert_equal(0z, filter(0zDEADBEEF, '0'))
-  call assert_equal(0zADBEEF, filter(0zDEADBEEF, 'v:val != 0xDE'))
-  call assert_equal(0zDEADEF, filter(0zDEADBEEF, 'v:val != 0xBE'))
-  call assert_equal(0zDEADBE, filter(0zDEADBEEF, 'v:val != 0xEF'))
-  call assert_equal(0zDEADBEEF, filter(0zDEADBEEF, '1'))
-  call assert_equal(0z01030103, filter(0z010203010203, 'v:val != 0x02'))
-  call assert_equal(0zADEF, filter(0zDEADBEEF, 'v:key % 2'))
+  let lines =<< trim END
+      call assert_equal(test_null_blob(), filter(test_null_blob(), '0'))
+      call assert_equal(0z, filter(0zDEADBEEF, '0'))
+      call assert_equal(0zADBEEF, filter(0zDEADBEEF, 'v:val != 0xDE'))
+      call assert_equal(0zDEADEF, filter(0zDEADBEEF, 'v:val != 0xBE'))
+      call assert_equal(0zDEADBE, filter(0zDEADBEEF, 'v:val != 0xEF'))
+      call assert_equal(0zDEADBEEF, filter(0zDEADBEEF, '1'))
+      call assert_equal(0z01030103, filter(0z010203010203, 'v:val != 0x02'))
+      call assert_equal(0zADEF, filter(0zDEADBEEF, 'v:key % 2'))
+  END
+  call CheckLegacyAndVim9Success(lines)
 endfunc
 
 " map() item in blob
 func Test_blob_map()
-  call assert_equal(0zDFAEBFF0, map(0zDEADBEEF, 'v:val + 1'))
-  call assert_equal(0z00010203, map(0zDEADBEEF, 'v:key'))
-  call assert_equal(0zDEAEC0F2, map(0zDEADBEEF, 'v:key + v:val'))
+  let lines =<< trim END
+      call assert_equal(0zDFAEBFF0, map(0zDEADBEEF, 'v:val + 1'))
+      call assert_equal(0z00010203, map(0zDEADBEEF, 'v:key'))
+      call assert_equal(0zDEAEC0F2, map(0zDEADBEEF, 'v:key + v:val'))
+  END
+  call CheckLegacyAndVim9Success(lines)
 
-  call assert_fails("call map(0z00, '[9]')", 'E978:')
+  let lines =<< trim END
+      call map(0z00, '[9]')
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E978:')
 endfunc
 
 func Test_blob_index()
-  call assert_equal(2, index(0zDEADBEEF, 0xBE))
-  call assert_equal(-1, index(0zDEADBEEF, 0))
-  call assert_equal(2, index(0z11111111, 0x11, 2))
-  call assert_equal(3, 0z11110111->index(0x11, 2))
-  call assert_equal(2, index(0z11111111, 0x11, -2))
-  call assert_equal(3, index(0z11110111, 0x11, -2))
-  call assert_equal(0, index(0z11110111, 0x11, -10))
-  call assert_fails("echo index(0z11110111, 0x11, [])", 'E745:')
-  call assert_equal(-1, index(test_null_blob(), 1))
+  let lines =<< trim END
+      call assert_equal(2, index(0zDEADBEEF, 0xBE))
+      call assert_equal(-1, index(0zDEADBEEF, 0))
+      call assert_equal(2, index(0z11111111, 0x11, 2))
+      call assert_equal(3, 0z11110111->index(0x11, 2))
+      call assert_equal(2, index(0z11111111, 0x11, -2))
+      call assert_equal(3, index(0z11110111, 0x11, -2))
+      call assert_equal(0, index(0z11110111, 0x11, -10))
+      call assert_equal(-1, index(test_null_blob(), 1))
+  END
+  call CheckLegacyAndVim9Success(lines)
 
-  call assert_fails('call index("asdf", 0)', 'E897:')
+  let lines =<< trim END
+      echo index(0z11110111, 0x11, [])
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E745:')
+
+  let lines =<< trim END
+      call index("asdf", 0)
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E897:')
 endfunc
 
 func Test_blob_insert()
-  let b = 0zDEADBEEF
-  call insert(b, 0x33)
-  call assert_equal(0z33DEADBEEF, b)
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call insert(b, 0x33)
+      call assert_equal(0z33DEADBEEF, b)
+
+      LET b = 0zDEADBEEF
+      call insert(b, 0x33, 2)
+      call assert_equal(0zDEAD33BEEF, b)
+  END
+  call CheckLegacyAndVim9Success(lines)
 
-  let b = 0zDEADBEEF
-  call insert(b, 0x33, 2)
-  call assert_equal(0zDEAD33BEEF, b)
+  " only works in legacy script
+  call assert_equal(0, insert(test_null_blob(), 0x33))
+
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call insert(b, -1)
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E475:')
+
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call insert(b, 257)
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E475:')
 
-  call assert_fails('call insert(b, -1)', 'E475:')
-  call assert_fails('call insert(b, 257)', 'E475:')
-  call assert_fails('call insert(b, 0, [9])', 'E745:')
-  call assert_fails('call insert(b, 0, -20)', 'E475:')
-  call assert_fails('call insert(b, 0, 20)', 'E475:')
-  call assert_fails('call insert(b, [])', 'E745:')
-  call assert_equal(0, insert(test_null_blob(), 0x33))
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call insert(b, 0, [9])
+  END
+  call CheckLegacyAndVim9Failure(lines, ['E745:', 'E1013:', 'E745:'])
+
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call insert(b, 0, -20)
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E475:')
+
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call insert(b, 0, 20)
+  END
+  call CheckLegacyAndVim9Failure(lines, 'E475:')
+
+  let lines =<< trim END
+      VAR b = 0zDEADBEEF
+      call insert(b, [])
+  END
+  call CheckLegacyAndVim9Failure(lines, ['E745:', 'E1013:', 'E745:'])
+
+  let lines =<< trim END
+      insert(test_null_blob(), 0x33)
+  END
+  call CheckDefExecAndScriptFailure(lines, 'E1131:')
 endfunc
 
 func Test_blob_reverse()
-  call assert_equal(0zEFBEADDE, reverse(0zDEADBEEF))
-  call assert_equal(0zBEADDE, reverse(0zDEADBE))
-  call assert_equal(0zADDE, reverse(0zDEAD))
-  call assert_equal(0zDE, reverse(0zDE))
-  call assert_equal(0z, reverse(test_null_blob()))
+  let lines =<< trim END
+      call assert_equal(0zEFBEADDE, reverse(0zDEADBEEF))
+      call assert_equal(0zBEADDE, reverse(0zDEADBE))
+      call assert_equal(0zADDE, reverse(0zDEAD))
+      call assert_equal(0zDE, reverse(0zDE))
+      call assert_equal(0z, reverse(test_null_blob()))
+  END
+  call CheckLegacyAndVim9Success(lines)
 endfunc
 
 func Test_blob_json_encode()
-  call assert_equal('[222,173,190,239]', json_encode(0zDEADBEEF))
-  call assert_equal('[]', json_encode(0z))
+  let lines =<< trim END
+      call assert_equal('[222,173,190,239]', json_encode(0zDEADBEEF))
+      call assert_equal('[]', json_encode(0z))
+  END
+  call CheckLegacyAndVim9Success(lines)
 endfunc
 
 func Test_blob_lock()
-  let b = 0z112233
-  lockvar b
-  call assert_fails('let b = 0z44', 'E741:')
-  unlockvar b
-  let b = 0z44
+  let lines =<< trim END
+      let b = 0z112233
+      lockvar b
+      unlockvar b
+      let b = 0z44
+  END
+  call CheckScriptSuccess(lines)
+
+  let lines =<< trim END
+      vim9script
+      var b = 0z112233
+      lockvar b
+      unlockvar b
+      b = 0z44
+  END
+  call CheckScriptSuccess(lines)
+
+  let lines =<< trim END
+      let b = 0z112233
+      lockvar b
+      let b = 0z44
+  END
+  call CheckScriptFailure(lines, 'E741:')
+
+  let lines =<< trim END
+      vim9script
+      var b = 0z112233
+      lockvar b
+      b = 0z44
+  END
+  call CheckScriptFailure(lines, 'E741:')
 endfunc
 
 func Test_blob_sort()
   if has('float')
-    call assert_fails('call sort([1.0, 0z11], "f")', 'E975:')
-  else
-    call assert_fails('call sort(["abc", 0z11], "f")', 'E702:')
+    call CheckLegacyAndVim9Failure(['call sort([1.0, 0z11], "f")'], 'E975:')
   endif
+  call CheckLegacyAndVim9Failure(['call sort(["abc", 0z11], "f")'], 'E892:')
 endfunc
 
 " vim: shiftwidth=2 sts=2 expandtab
--- a/src/testdir/test_vim9_builtin.vim
+++ b/src/testdir/test_vim9_builtin.vim
@@ -719,6 +719,16 @@ def Test_insert()
   endfor
   res->assert_equal(6)
 
+  var lines =<< trim END
+      insert(test_null_list(), 123)
+  END
+  CheckDefExecAndScriptFailure(lines, 'E1130:', 1)
+
+  lines =<< trim END
+      insert(test_null_blob(), 123)
+  END
+  CheckDefExecAndScriptFailure(lines, 'E1131:', 1)
+
   assert_equal([1, 2, 3], insert([2, 3], 1))
   assert_equal([1, 2, 3], insert([2, 3], s:number_one))
   assert_equal([1, 2, 3], insert([1, 2], 3, 2))
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    2782,
+/**/
     2781,
 /**/
     2780,