changeset 22165:c512e6f57ff2 v8.2.1632

patch 8.2.1632: not checking the context of test_fails() Commit: https://github.com/vim/vim/commit/44d6652d561d628d12e3ff7f6636ea7d1f805ced Author: Bram Moolenaar <Bram@vim.org> Date: Sun Sep 6 22:26:57 2020 +0200 patch 8.2.1632: not checking the context of test_fails() Problem: Not checking the context of test_fails(). Solution: Add the line number and context arguments. Give error if assert_fails() argument types are wrong.
author Bram Moolenaar <Bram@vim.org>
date Sun, 06 Sep 2020 22:30:03 +0200
parents 7b7500b8b68e
children d8552f77a4be
files src/errors.h src/testdir/test_assert.vim src/testdir/test_vim9_func.vim src/testdir/test_vim9_script.vim src/testing.c src/version.c
diffstat 6 files changed, 63 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/errors.h
+++ b/src/errors.h
@@ -23,6 +23,8 @@ EXTERN char e_invalid_command_str[]
 	INIT(= N_("E476: Invalid command: %s"));
 EXTERN char e_cannot_slice_dictionary[]
 	INIT(= N_("E719: cannot slice a Dictionary"));
+EXTERN char e_assert_fails_second_arg[]
+	INIT(= N_("E856: assert_fails() second argument must be a string or a list with one or two strings"));
 EXTERN char e_cannot_index_special_variable[]
 	INIT(= N_("E909: Cannot index a special variable"));
 EXTERN char e_missing_let_str[]
@@ -250,4 +252,8 @@ EXTERN char e_overlapping_ranges_for_nr[
 	INIT(= N_("E1113: Overlapping ranges for 0x%lx"));
 EXTERN char e_only_values_of_0x100_and_higher_supported[]
 	INIT(= N_("E1114: Only values of 0x100 and higher supported"));
+EXTERN char e_assert_fails_fourth_argument[]
+	INIT(= N_("E1115: assert_fails() fourth argument must be a number"));
+EXTERN char e_assert_fails_fifth_argument[]
+	INIT(= N_("E1116: assert_fails() fifth argument must be a string"));
 #endif
--- a/src/testdir/test_assert.vim
+++ b/src/testdir/test_assert.vim
@@ -269,6 +269,20 @@ func Test_assert_fail_fails()
     let exp = v:exception
   endtry
   call assert_match("E856: assert_fails() second argument", exp)
+
+  try
+    call assert_equal(1, assert_fails('xxx', 'E492', '', 'burp'))
+  catch
+    let exp = v:exception
+  endtry
+  call assert_match("E1115: assert_fails() fourth argument must be a number", exp)
+
+  try
+    call assert_equal(1, assert_fails('xxx', 'E492', '', 54, 123))
+  catch
+    let exp = v:exception
+  endtry
+  call assert_match("E1116: assert_fails() fifth argument must be a string", exp)
 endfunc
 
 func Test_assert_fails_in_try_block()
--- a/src/testdir/test_vim9_func.vim
+++ b/src/testdir/test_vim9_func.vim
@@ -524,7 +524,7 @@ enddef
 
 def Test_error_in_nested_function()
   # Error in called function requires unwinding the call stack.
-  assert_fails('FuncWithForwardCall()', 'E1096:', 1, 'FuncWithForwardCall')
+  assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
 enddef
 
 def Test_return_type_wrong()
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -1343,7 +1343,7 @@ def Test_vim9_import_export()
     defcompile
   END
   writefile(import_star_as_lines_no_dot, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1060:')
+  assert_fails('source Ximport.vim', 'E1060:', '', 2, 'Func')
 
   let import_star_as_lines_dot_space =<< trim END
     vim9script
@@ -1354,7 +1354,7 @@ def Test_vim9_import_export()
     defcompile
   END
   writefile(import_star_as_lines_dot_space, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1074:')
+  assert_fails('source Ximport.vim', 'E1074:', '', 1, 'Func')
 
   let import_star_as_lines_missing_name =<< trim END
     vim9script
@@ -1365,7 +1365,7 @@ def Test_vim9_import_export()
     defcompile
   END
   writefile(import_star_as_lines_missing_name, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1048:')
+  assert_fails('source Ximport.vim', 'E1048:', '', 1, 'Func')
 
   let import_star_as_lbr_lines =<< trim END
     vim9script
@@ -1387,7 +1387,7 @@ def Test_vim9_import_export()
     import * from './Xexport.vim'
   END
   writefile(import_star_lines, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1045:')
+  assert_fails('source Ximport.vim', 'E1045:', '', 2, 'Ximport.vim')
 
   # try to import something that exists but is not exported
   let import_not_exported_lines =<< trim END
@@ -1395,7 +1395,7 @@ def Test_vim9_import_export()
     import name from './Xexport.vim'
   END
   writefile(import_not_exported_lines, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1049:')
+  assert_fails('source Ximport.vim', 'E1049:', '', 2, 'Ximport.vim')
 
   # try to import something that is already defined
   let import_already_defined =<< trim END
@@ -1404,7 +1404,7 @@ def Test_vim9_import_export()
     import exported from './Xexport.vim'
   END
   writefile(import_already_defined, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1073:')
+  assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
 
   # try to import something that is already defined
   import_already_defined =<< trim END
@@ -1413,7 +1413,7 @@ def Test_vim9_import_export()
     import * as exported from './Xexport.vim'
   END
   writefile(import_already_defined, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1073:')
+  assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
 
   # try to import something that is already defined
   import_already_defined =<< trim END
@@ -1422,7 +1422,7 @@ def Test_vim9_import_export()
     import {exported} from './Xexport.vim'
   END
   writefile(import_already_defined, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1073:')
+  assert_fails('source Ximport.vim', 'E1073:', '', 3, 'Ximport.vim')
 
   # import a very long name, requires making a copy
   let import_long_name_lines =<< trim END
@@ -1430,35 +1430,35 @@ def Test_vim9_import_export()
     import name012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 from './Xexport.vim'
   END
   writefile(import_long_name_lines, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1048:')
+  assert_fails('source Ximport.vim', 'E1048:', '', 2, 'Ximport.vim')
 
   let import_no_from_lines =<< trim END
     vim9script
     import name './Xexport.vim'
   END
   writefile(import_no_from_lines, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1070:')
+  assert_fails('source Ximport.vim', 'E1070:', '', 2, 'Ximport.vim')
 
   let import_invalid_string_lines =<< trim END
     vim9script
     import name from Xexport.vim
   END
   writefile(import_invalid_string_lines, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1071:')
+  assert_fails('source Ximport.vim', 'E1071:', '', 2, 'Ximport.vim')
 
   let import_wrong_name_lines =<< trim END
     vim9script
     import name from './XnoExport.vim'
   END
   writefile(import_wrong_name_lines, 'Ximport.vim')
-  assert_fails('source Ximport.vim', 'E1053:')
+  assert_fails('source Ximport.vim', 'E1053:', '', 2, 'Ximport.vim')
 
   let import_missing_comma_lines =<< trim END
     vim9script
     import {exported name} from './Xexport.vim'
   END
   writefile(import_missing_comma_lines, 'Ximport3.vim')
-  assert_fails('source Ximport3.vim', 'E1046:')
+  assert_fails('source Ximport3.vim', 'E1046:', '', 2, 'Ximport3.vim')
 
   delete('Ximport.vim')
   delete('Ximport3.vim')
@@ -1646,7 +1646,7 @@ def Test_vim9script_reload_import()
     let valone = 5678
   END
   writefile(lines, 'Xreload.vim')
-  assert_fails('source Xreload.vim', 'E1041:')
+  assert_fails('source Xreload.vim', 'E1041:', '', 3, 'Xreload.vim')
 
   delete('Xreload.vim')
   delete('Ximport.vim')
@@ -1745,7 +1745,7 @@ def Test_vim9script_reload_delfunc()
   let nono_lines =<< trim END
     def g:DoCheck(no_exists: bool)
       assert_equal('yes', FuncYes())
-      assert_fails('FuncNo()', 'E117:')
+      assert_fails('FuncNo()', 'E117:', '', 2, 'DoCheck')
     enddef
   END
 
--- a/src/testing.c
+++ b/src/testing.c
@@ -550,7 +550,7 @@ f_assert_fails(typval_T *argvars, typval
     garray_T	ga;
     int		save_trylevel = trylevel;
     int		called_emsg_before = called_emsg;
-    int		wrong_arg = FALSE;
+    char	*wrong_arg_msg = NULL;
 
     // trylevel must be zero for a ":throw" command to be considered failed
     trylevel = 0;
@@ -590,7 +590,7 @@ f_assert_fails(typval_T *argvars, typval
 
 	    if (list == NULL || list->lv_len < 1 || list->lv_len > 2)
 	    {
-		wrong_arg = TRUE;
+		wrong_arg_msg = e_assert_fails_second_arg;
 		goto theend;
 	    }
 	    CHECK_LIST_MATERIALIZE(list);
@@ -611,26 +611,38 @@ f_assert_fails(typval_T *argvars, typval
 	}
 	else
 	{
-	    wrong_arg = TRUE;
+	    wrong_arg_msg = e_assert_fails_second_arg;
 	    goto theend;
 	}
 
 	if (!error_found && argvars[2].v_type != VAR_UNKNOWN
-		&& argvars[3].v_type == VAR_NUMBER)
+		&& argvars[3].v_type != VAR_UNKNOWN)
 	{
-	    if (argvars[3].vval.v_number >= 0
-		&& argvars[3].vval.v_number != emsg_assert_fails_lnum)
+	    if (argvars[3].v_type != VAR_NUMBER)
+	    {
+		wrong_arg_msg = e_assert_fails_fourth_argument;
+		goto theend;
+	    }
+	    else if (argvars[3].vval.v_number >= 0
+			 && argvars[3].vval.v_number != emsg_assert_fails_lnum)
 	    {
 		error_found = TRUE;
 		error_found_index = 3;
 	    }
-	    if (!error_found && argvars[4].v_type == VAR_STRING
-		    && argvars[4].vval.v_string != NULL
+	    if (!error_found && argvars[4].v_type != VAR_UNKNOWN)
+	    {
+		if (argvars[4].v_type != VAR_STRING)
+		{
+		    wrong_arg_msg = e_assert_fails_fifth_argument;
+		    goto theend;
+		}
+		else if (argvars[4].vval.v_string != NULL
 		    && !pattern_match(argvars[4].vval.v_string,
 					     emsg_assert_fails_context, FALSE))
-	    {
-		error_found = TRUE;
-		error_found_index = 4;
+		{
+		    error_found = TRUE;
+		    error_found_index = 4;
+		}
 	    }
 	}
 
@@ -672,8 +684,8 @@ theend:
     emsg_assert_fails_used = FALSE;
     VIM_CLEAR(emsg_assert_fails_msg);
     set_vim_var_string(VV_ERRMSG, NULL, 0);
-    if (wrong_arg)
-	emsg(_("E856: assert_fails() second argument must be a string or a list with one or two strings"));
+    if (wrong_arg_msg != NULL)
+	emsg(_(wrong_arg_msg));
 }
 
 /*
--- a/src/version.c
+++ b/src/version.c
@@ -755,6 +755,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1632,
+/**/
     1631,
 /**/
     1630,