diff options
author | zeertzjq <zeertzjq@outlook.com> | 2022-11-05 14:17:06 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-05 14:17:06 +0800 |
commit | 86fe2c8d94d212af098002ce04c87fb73fdf9c52 (patch) | |
tree | f48445ed4f2cb998a56ef56709ef4a87c8411e0d /src/nvim/testing.c | |
parent | a86295cd5c2bf15a11eb05e226fd8e226154f6a6 (diff) | |
parent | 6956971ec790e636b16eeaec798c826515da9834 (diff) | |
download | rneovim-86fe2c8d94d212af098002ce04c87fb73fdf9c52.tar.gz rneovim-86fe2c8d94d212af098002ce04c87fb73fdf9c52.tar.bz2 rneovim-86fe2c8d94d212af098002ce04c87fb73fdf9c52.zip |
Merge pull request #20941 from zeertzjq/vim-8.2.1183
vim-patch:8.2.{partial:1183,1184,1199,1479,1484,1631,1632}: assert_fails() patches
Diffstat (limited to 'src/nvim/testing.c')
-rw-r--r-- | src/nvim/testing.c | 103 |
1 files changed, 91 insertions, 12 deletions
diff --git a/src/nvim/testing.c b/src/nvim/testing.c index 45134db14f..9dd41224da 100644 --- a/src/nvim/testing.c +++ b/src/nvim/testing.c @@ -14,6 +14,12 @@ # include "testing.c.generated.h" #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[] = N_("E1142: Calling test_garbagecollect_now() while v:testing is not set"); @@ -68,7 +74,7 @@ static void ga_concat_esc(garray_T *gap, const char_u *p, int clen) case '\\': ga_concat(gap, "\\\\"); break; default: - if (*p < ' ') { + if (*p < ' ' || *p == 0x7f) { vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p); ga_concat(gap, (char *)buf); } else { @@ -124,7 +130,10 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_s bool did_copy = false; int omitted = 0; - if (opt_msg_tv->v_type != VAR_UNKNOWN) { + if (opt_msg_tv->v_type != VAR_UNKNOWN + && !(opt_msg_tv->v_type == VAR_STRING + && (opt_msg_tv->vval.v_string == NULL + || *opt_msg_tv->vval.v_string == NUL))) { tofree = (char_u *)encode_tv2echo(opt_msg_tv, NULL); ga_concat(gap, (char *)tofree); xfree(tofree); @@ -244,13 +253,12 @@ static int assert_match_common(typval_T *argvars, assert_type_T atype) { char buf1[NUMBUFLEN]; char buf2[NUMBUFLEN]; + const int called_emsg_before = called_emsg; const char *const pat = tv_get_string_buf_chk(&argvars[0], buf1); const char *const text = tv_get_string_buf_chk(&argvars[1], buf2); - if (pat == NULL || text == NULL) { - emsg(_(e_invarg)); - } else if (pattern_match((char *)pat, (char *)text, false) - != (atype == ASSERT_MATCH)) { + if (called_emsg == called_emsg_before + && pattern_match(pat, text, false) != (atype == ASSERT_MATCH)) { garray_T ga; prepare_assert_error(&ga); fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1], atype); @@ -351,11 +359,12 @@ static int assert_equalfile(typval_T *argvars) { char buf1[NUMBUFLEN]; char buf2[NUMBUFLEN]; + const int called_emsg_before = called_emsg; const char *const fname1 = tv_get_string_buf_chk(&argvars[0], buf1); const char *const fname2 = tv_get_string_buf_chk(&argvars[1], buf2); garray_T ga; - if (fname1 == NULL || fname2 == NULL) { + if (called_emsg > called_emsg_before) { return 0; } @@ -477,11 +486,13 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) garray_T ga; int save_trylevel = trylevel; const int called_emsg_before = called_emsg; + char *wrong_arg_msg = NULL; // trylevel must be zero for a ":throw" command to be considered failed trylevel = 0; suppress_errthrow = true; emsg_silent = true; + emsg_assert_fails_used = true; do_cmdline_cmd(cmd); if (called_emsg == called_emsg_before) { @@ -493,13 +504,75 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) rettv->vval.v_number = 1; } else if (argvars[1].v_type != VAR_UNKNOWN) { char buf[NUMBUFLEN]; - const char *const error = tv_get_string_buf_chk(&argvars[1], buf); + const char *expected; + bool error_found = false; + int error_found_index = 1; + char *actual = emsg_assert_fails_msg == NULL ? "[unknown]" : emsg_assert_fails_msg; + + if (argvars[1].v_type == VAR_STRING) { + expected = tv_get_string_buf_chk(&argvars[1], buf); + error_found = expected == NULL || strstr(actual, expected) == NULL; + } else if (argvars[1].v_type == VAR_LIST) { + const list_T *const list = argvars[1].vval.v_list; + if (list == NULL || tv_list_len(list) < 1 || tv_list_len(list) > 2) { + wrong_arg_msg = e_assert_fails_second_arg; + goto theend; + } + const typval_T *tv = TV_LIST_ITEM_TV(tv_list_first(list)); + expected = tv_get_string_buf_chk(tv, buf); + if (!pattern_match(expected, actual, false)) { + error_found = true; + } else if (tv_list_len(list) == 2) { + tv = TV_LIST_ITEM_TV(tv_list_last(list)); + actual = get_vim_var_str(VV_ERRMSG); + expected = tv_get_string_buf_chk(tv, buf); + if (!pattern_match(expected, actual, false)) { + error_found = true; + } + } + } else { + wrong_arg_msg = e_assert_fails_second_arg; + goto theend; + } - if (error == NULL - || strstr(get_vim_var_str(VV_ERRMSG), error) == NULL) { + if (!error_found && argvars[2].v_type != VAR_UNKNOWN + && argvars[3].v_type != VAR_UNKNOWN) { + 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_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; + } + } + } + + if (error_found) { + typval_T actual_tv; prepare_assert_error(&ga); - fill_assert_error(&ga, &argvars[2], NULL, &argvars[1], - get_vim_var_tv(VV_ERRMSG), ASSERT_OTHER); + if (error_found_index == 3) { + actual_tv.v_type = VAR_NUMBER; + actual_tv.vval.v_number = emsg_assert_fails_lnum; + } else if (error_found_index == 4) { + actual_tv.v_type = VAR_STRING; + actual_tv.vval.v_string = emsg_assert_fails_context; + } else { + actual_tv.v_type = VAR_STRING; + actual_tv.vval.v_string = actual; + } + fill_assert_error(&ga, &argvars[2], NULL, + &argvars[error_found_index], &actual_tv, ASSERT_OTHER); ga_concat(&ga, ": "); assert_append_cmd_or_arg(&ga, argvars, cmd); assert_error(&ga); @@ -508,11 +581,17 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } } +theend: trylevel = save_trylevel; suppress_errthrow = false; emsg_silent = false; emsg_on_display = false; + emsg_assert_fails_used = false; + XFREE_CLEAR(emsg_assert_fails_msg); set_vim_var_string(VV_ERRMSG, NULL, 0); + if (wrong_arg_msg != NULL) { + emsg(_(wrong_arg_msg)); + } } // "assert_false(actual[, msg])" function |