@@ -475,6 +475,7 @@ static void f_assert_equal(typval_T *argvars, typval_T *rettv);
475475static void f_assert_exception(typval_T *argvars, typval_T *rettv);
476476static void f_assert_fails(typval_T *argvars, typval_T *rettv);
477477static void f_assert_false(typval_T *argvars, typval_T *rettv);
478+ static void f_assert_match(typval_T *argvars, typval_T *rettv);
478479static void f_assert_true(typval_T *argvars, typval_T *rettv);
479480#ifdef FEAT_FLOAT
480481static void f_asin(typval_T *argvars, typval_T *rettv);
@@ -4105,6 +4106,31 @@ get_user_var_name(expand_T *xp, int idx)
41054106
41064107#endif /* FEAT_CMDL_COMPL */
41074108
4109+ /*
4110+ * Return TRUE if "pat" matches "text".
4111+ * Does not use 'cpo' and always uses 'magic'.
4112+ */
4113+ static int
4114+ pattern_match(char_u *pat, char_u *text, int ic)
4115+ {
4116+ int matches = FALSE;
4117+ char_u *save_cpo;
4118+ regmatch_T regmatch;
4119+
4120+ /* avoid 'l' flag in 'cpoptions' */
4121+ save_cpo = p_cpo;
4122+ p_cpo = (char_u *)"";
4123+ regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4124+ if (regmatch.regprog != NULL)
4125+ {
4126+ regmatch.rm_ic = ic;
4127+ matches = vim_regexec_nl(®match, text, (colnr_T)0);
4128+ vim_regfree(regmatch.regprog);
4129+ }
4130+ p_cpo = save_cpo;
4131+ return matches;
4132+ }
4133+
41084134/*
41094135 * types for expressions.
41104136 */
@@ -4403,9 +4429,7 @@ eval4(char_u **arg, typval_T *rettv, int evaluate)
44034429 long n1, n2;
44044430 char_u *s1, *s2;
44054431 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4406- regmatch_T regmatch;
44074432 int ic;
4408- char_u *save_cpo;
44094433
44104434 /*
44114435 * Get the first variable.
@@ -4646,20 +4670,9 @@ eval4(char_u **arg, typval_T *rettv, int evaluate)
46464670
46474671 case TYPE_MATCH:
46484672 case TYPE_NOMATCH:
4649- /* avoid 'l' flag in 'cpoptions' */
4650- save_cpo = p_cpo;
4651- p_cpo = (char_u *)"";
4652- regmatch.regprog = vim_regcomp(s2,
4653- RE_MAGIC + RE_STRING);
4654- regmatch.rm_ic = ic;
4655- if (regmatch.regprog != NULL)
4656- {
4657- n1 = vim_regexec_nl(®match, s1, (colnr_T)0);
4658- vim_regfree(regmatch.regprog);
4659- if (type == TYPE_NOMATCH)
4660- n1 = !n1;
4661- }
4662- p_cpo = save_cpo;
4673+ n1 = pattern_match(s2, s1, ic);
4674+ if (type == TYPE_NOMATCH)
4675+ n1 = !n1;
46634676 break;
46644677
46654678 case TYPE_UNKNOWN: break; /* avoid gcc warning */
@@ -8154,6 +8167,7 @@ static struct fst
81548167 {"assert_exception", 1, 2, f_assert_exception},
81558168 {"assert_fails", 1, 2, f_assert_fails},
81568169 {"assert_false", 1, 2, f_assert_false},
8170+ {"assert_match", 2, 3, f_assert_match},
81578171 {"assert_true", 1, 2, f_assert_true},
81588172#ifdef FEAT_FLOAT
81598173 {"atan", 1, 1, f_atan},
@@ -9295,7 +9309,7 @@ f_argv(typval_T *argvars, typval_T *rettv)
92959309}
92969310
92979311static void prepare_assert_error(garray_T*gap);
9298- static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9312+ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, int is_match );
92999313static void assert_error(garray_T *gap);
93009314static void assert_bool(typval_T *argvars, int isTrue);
93019315
@@ -9370,7 +9384,8 @@ fill_assert_error(
93709384 typval_T *opt_msg_tv,
93719385 char_u *exp_str,
93729386 typval_T *exp_tv,
9373- typval_T *got_tv)
9387+ typval_T *got_tv,
9388+ int is_match)
93749389{
93759390 char_u numbuf[NUMBUFLEN];
93769391 char_u *tofree;
@@ -9382,15 +9397,21 @@ fill_assert_error(
93829397 }
93839398 else
93849399 {
9385- ga_concat(gap, (char_u *)"Expected ");
9400+ if (is_match)
9401+ ga_concat(gap, (char_u *)"Pattern ");
9402+ else
9403+ ga_concat(gap, (char_u *)"Expected ");
93869404 if (exp_str == NULL)
93879405 {
93889406 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
93899407 vim_free(tofree);
93909408 }
93919409 else
93929410 ga_concat_esc(gap, exp_str);
9393- ga_concat(gap, (char_u *)" but got ");
9411+ if (is_match)
9412+ ga_concat(gap, (char_u *)" does not match ");
9413+ else
9414+ ga_concat(gap, (char_u *)" but got ");
93949415 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
93959416 vim_free(tofree);
93969417 }
@@ -9421,7 +9442,8 @@ f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
94219442 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
94229443 {
94239444 prepare_assert_error(&ga);
9424- fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9445+ fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9446+ FALSE);
94259447 assert_error(&ga);
94269448 ga_clear(&ga);
94279449 }
@@ -9449,7 +9471,7 @@ f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
94499471 {
94509472 prepare_assert_error(&ga);
94519473 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9452- &vimvars[VV_EXCEPTION].vv_tv);
9474+ &vimvars[VV_EXCEPTION].vv_tv, FALSE );
94539475 assert_error(&ga);
94549476 ga_clear(&ga);
94559477 }
@@ -9486,7 +9508,7 @@ f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
94869508 {
94879509 prepare_assert_error(&ga);
94889510 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9489- &vimvars[VV_ERRMSG].vv_tv);
9511+ &vimvars[VV_ERRMSG].vv_tv, FALSE );
94909512 assert_error(&ga);
94919513 ga_clear(&ga);
94929514 }
@@ -9518,7 +9540,7 @@ assert_bool(typval_T *argvars, int isTrue)
95189540 prepare_assert_error(&ga);
95199541 fill_assert_error(&ga, &argvars[1],
95209542 (char_u *)(isTrue ? "True" : "False"),
9521- NULL, &argvars[0]);
9543+ NULL, &argvars[0], FALSE );
95229544 assert_error(&ga);
95239545 ga_clear(&ga);
95249546 }
@@ -9533,6 +9555,28 @@ f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
95339555 assert_bool(argvars, FALSE);
95349556}
95359557
9558+ /*
9559+ * "assert_match(pattern, actual[, msg])" function
9560+ */
9561+ static void
9562+ f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9563+ {
9564+ garray_T ga;
9565+ char_u buf1[NUMBUFLEN];
9566+ char_u buf2[NUMBUFLEN];
9567+ char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9568+ char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9569+
9570+ if (!pattern_match(pat, text, FALSE))
9571+ {
9572+ prepare_assert_error(&ga);
9573+ fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9574+ TRUE);
9575+ assert_error(&ga);
9576+ ga_clear(&ga);
9577+ }
9578+ }
9579+
95369580/*
95379581 * "assert_true(actual[, msg])" function
95389582 */
0 commit comments