-*eval.txt* For Vim version 7.4. Last change: 2016 Mar 26
+*eval.txt* For Vim version 7.4. Last change: 2016 Mar 27
VIM REFERENCE MANUAL by Bram Moolenaar
assert_exception( {error} [, {msg}]) none assert {error} is in v:exception
assert_fails( {cmd} [, {error}]) none assert {cmd} fails
assert_false( {actual} [, {msg}]) none assert {actual} is false
+assert_match( {pat}, {text} [, {msg}]) none assert {pat} matches {text}
assert_true( {actual} [, {msg}]) none assert {actual} is true
asin( {expr}) Float arc sine of {expr}
atan( {expr}) Float arc tangent of {expr}
When {msg} is omitted an error in the form "Expected False but
got {actual}" is produced.
+ *assert_match()*
+assert_match({pattern}, {actual} [, {msg}])
+ When {pattern} does not match {actual} an error message is
+ added to |v:errors|.
+
+ {pattern} is used as with |=~|: The matching is always done
+ like 'magic' was set and 'cpoptions' is empty, no matter what
+ the actual value of 'magic' or 'cpoptions' is.
+
+ {actual} is used as a string, automatic conversion applies.
+ Use "^" and "$" to match with the start and end of the text.
+ Use both to match the whole text.
+
+ When {msg} is omitted an error in the form "Pattern {pattern}
+ does not match {actual}" is produced.
+ Example: >
+ assert_match('^f.*o$', 'foobar')
+< Will result in a string to be added to |v:errors|:
+ test.vim line 12: Pattern '^f.*o$' does not match 'foobar' ~
+
assert_true({actual} [, {msg}]) *assert_true()*
When {actual} is not true an error message is added to
|v:errors|, like with |assert_equal()|.
static void f_assert_exception(typval_T *argvars, typval_T *rettv);
static void f_assert_fails(typval_T *argvars, typval_T *rettv);
static void f_assert_false(typval_T *argvars, typval_T *rettv);
+static void f_assert_match(typval_T *argvars, typval_T *rettv);
static void f_assert_true(typval_T *argvars, typval_T *rettv);
#ifdef FEAT_FLOAT
static void f_asin(typval_T *argvars, typval_T *rettv);
#endif /* FEAT_CMDL_COMPL */
+/*
+ * Return TRUE if "pat" matches "text".
+ * Does not use 'cpo' and always uses 'magic'.
+ */
+ static int
+pattern_match(char_u *pat, char_u *text, int ic)
+{
+ int matches = FALSE;
+ char_u *save_cpo;
+ regmatch_T regmatch;
+
+ /* avoid 'l' flag in 'cpoptions' */
+ save_cpo = p_cpo;
+ p_cpo = (char_u *)"";
+ regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
+ if (regmatch.regprog != NULL)
+ {
+ regmatch.rm_ic = ic;
+ matches = vim_regexec_nl(®match, text, (colnr_T)0);
+ vim_regfree(regmatch.regprog);
+ }
+ p_cpo = save_cpo;
+ return matches;
+}
+
/*
* types for expressions.
*/
long n1, n2;
char_u *s1, *s2;
char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
- regmatch_T regmatch;
int ic;
- char_u *save_cpo;
/*
* Get the first variable.
case TYPE_MATCH:
case TYPE_NOMATCH:
- /* avoid 'l' flag in 'cpoptions' */
- save_cpo = p_cpo;
- p_cpo = (char_u *)"";
- regmatch.regprog = vim_regcomp(s2,
- RE_MAGIC + RE_STRING);
- regmatch.rm_ic = ic;
- if (regmatch.regprog != NULL)
- {
- n1 = vim_regexec_nl(®match, s1, (colnr_T)0);
- vim_regfree(regmatch.regprog);
- if (type == TYPE_NOMATCH)
- n1 = !n1;
- }
- p_cpo = save_cpo;
+ n1 = pattern_match(s2, s1, ic);
+ if (type == TYPE_NOMATCH)
+ n1 = !n1;
break;
case TYPE_UNKNOWN: break; /* avoid gcc warning */
{"assert_exception", 1, 2, f_assert_exception},
{"assert_fails", 1, 2, f_assert_fails},
{"assert_false", 1, 2, f_assert_false},
+ {"assert_match", 2, 3, f_assert_match},
{"assert_true", 1, 2, f_assert_true},
#ifdef FEAT_FLOAT
{"atan", 1, 1, f_atan},
}
static void prepare_assert_error(garray_T*gap);
-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);
+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);
static void assert_error(garray_T *gap);
static void assert_bool(typval_T *argvars, int isTrue);
typval_T *opt_msg_tv,
char_u *exp_str,
typval_T *exp_tv,
- typval_T *got_tv)
+ typval_T *got_tv,
+ int is_match)
{
char_u numbuf[NUMBUFLEN];
char_u *tofree;
}
else
{
- ga_concat(gap, (char_u *)"Expected ");
+ if (is_match)
+ ga_concat(gap, (char_u *)"Pattern ");
+ else
+ ga_concat(gap, (char_u *)"Expected ");
if (exp_str == NULL)
{
ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
}
else
ga_concat_esc(gap, exp_str);
- ga_concat(gap, (char_u *)" but got ");
+ if (is_match)
+ ga_concat(gap, (char_u *)" does not match ");
+ else
+ ga_concat(gap, (char_u *)" but got ");
ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
vim_free(tofree);
}
if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
{
prepare_assert_error(&ga);
- fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
+ fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
+ FALSE);
assert_error(&ga);
ga_clear(&ga);
}
{
prepare_assert_error(&ga);
fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
- &vimvars[VV_EXCEPTION].vv_tv);
+ &vimvars[VV_EXCEPTION].vv_tv, FALSE);
assert_error(&ga);
ga_clear(&ga);
}
{
prepare_assert_error(&ga);
fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
- &vimvars[VV_ERRMSG].vv_tv);
+ &vimvars[VV_ERRMSG].vv_tv, FALSE);
assert_error(&ga);
ga_clear(&ga);
}
prepare_assert_error(&ga);
fill_assert_error(&ga, &argvars[1],
(char_u *)(isTrue ? "True" : "False"),
- NULL, &argvars[0]);
+ NULL, &argvars[0], FALSE);
assert_error(&ga);
ga_clear(&ga);
}
assert_bool(argvars, FALSE);
}
+/*
+ * "assert_match(pattern, actual[, msg])" function
+ */
+ static void
+f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
+{
+ garray_T ga;
+ char_u buf1[NUMBUFLEN];
+ char_u buf2[NUMBUFLEN];
+ char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
+ char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
+
+ if (!pattern_match(pat, text, FALSE))
+ {
+ prepare_assert_error(&ga);
+ fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
+ TRUE);
+ assert_error(&ga);
+ ga_clear(&ga);
+ }
+}
+
/*
* "assert_true(actual[, msg])" function
*/