vim-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.

44d6652d56

Co-authored-by: Bram Moolenaar <Bram@vim.org>
This commit is contained in:
zeertzjq
2022-11-05 12:33:10 +08:00
parent 02f80d9a8a
commit 6956971ec7
2 changed files with 41 additions and 15 deletions

View File

@@ -255,6 +255,20 @@ func Test_assert_fail_fails()
let exp = v:exception let exp = v:exception
endtry endtry
call assert_match("E856: assert_fails() second argument", exp) 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 endfunc
func Test_assert_fails_in_try_block() func Test_assert_fails_in_try_block()

View File

@@ -14,6 +14,12 @@
# include "testing.c.generated.h" # include "testing.c.generated.h"
#endif #endif
static char e_assert_fails_second_arg[]
= N_("E856: assert_fails() second argument must be a string or a list with one or two strings");
static char e_assert_fails_fourth_argument[]
= N_("E1115: assert_fails() fourth argument must be a number");
static char e_assert_fails_fifth_argument[]
= N_("E1116: assert_fails() fifth argument must be a string");
static char e_calling_test_garbagecollect_now_while_v_testing_is_not_set[] static char e_calling_test_garbagecollect_now_while_v_testing_is_not_set[]
= N_("E1142: Calling test_garbagecollect_now() while v:testing is not set"); = N_("E1142: Calling test_garbagecollect_now() while v:testing is not set");
@@ -480,7 +486,7 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
garray_T ga; garray_T ga;
int save_trylevel = trylevel; int save_trylevel = trylevel;
const int called_emsg_before = called_emsg; const int called_emsg_before = called_emsg;
bool wrong_arg = false; char *wrong_arg_msg = NULL;
// trylevel must be zero for a ":throw" command to be considered failed // trylevel must be zero for a ":throw" command to be considered failed
trylevel = 0; trylevel = 0;
@@ -509,7 +515,7 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
} else if (argvars[1].v_type == VAR_LIST) { } else if (argvars[1].v_type == VAR_LIST) {
const list_T *const list = argvars[1].vval.v_list; const list_T *const list = argvars[1].vval.v_list;
if (list == NULL || tv_list_len(list) < 1 || tv_list_len(list) > 2) { if (list == NULL || tv_list_len(list) < 1 || tv_list_len(list) > 2) {
wrong_arg = true; wrong_arg_msg = e_assert_fails_second_arg;
goto theend; goto theend;
} }
const typval_T *tv = TV_LIST_ITEM_TV(tv_list_first(list)); const typval_T *tv = TV_LIST_ITEM_TV(tv_list_first(list));
@@ -525,25 +531,32 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
} }
} }
} else { } else {
wrong_arg = true; wrong_arg_msg = e_assert_fails_second_arg;
goto theend; goto theend;
} }
if (!error_found && argvars[2].v_type != VAR_UNKNOWN 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 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) { && argvars[3].vval.v_number != emsg_assert_fails_lnum) {
error_found = true; error_found = true;
error_found_index = 3; error_found_index = 3;
} }
if (!error_found && argvars[4].v_type == VAR_STRING if (!error_found && argvars[4].v_type != VAR_UNKNOWN) {
&& argvars[4].vval.v_string != NULL 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, && !pattern_match(argvars[4].vval.v_string,
emsg_assert_fails_context, false)) { emsg_assert_fails_context, false)) {
error_found = true; error_found = true;
error_found_index = 4; error_found_index = 4;
} }
} }
}
if (error_found) { if (error_found) {
typval_T actual_tv; typval_T actual_tv;
@@ -576,9 +589,8 @@ theend:
emsg_assert_fails_used = false; emsg_assert_fails_used = false;
XFREE_CLEAR(emsg_assert_fails_msg); XFREE_CLEAR(emsg_assert_fails_msg);
set_vim_var_string(VV_ERRMSG, NULL, 0); set_vim_var_string(VV_ERRMSG, NULL, 0);
if (wrong_arg) { if (wrong_arg_msg != NULL) {
emsg(_( emsg(_(wrong_arg_msg));
"E856: assert_fails() second argument must be a string or a list with one or two strings"));
} }
} }