Merge pull request #5020 from jamessan/vim-7.4.1663

vim-patch:7.4.1663,7.4.1682,7.4.1703
This commit is contained in:
James McCoy 2016-07-24 12:29:06 -05:00 committed by GitHub
commit 2bb4c43f2f
5 changed files with 186 additions and 27 deletions

View File

@ -1,4 +1,4 @@
*eval.txt* For Vim version 7.4. Last change: 2016 Apr 12 *eval.txt* For Vim version 7.4. Last change: 2016 Mar 27
VIM REFERENCE MANUAL by Bram Moolenaar VIM REFERENCE MANUAL by Bram Moolenaar
@ -1784,10 +1784,13 @@ argidx() Number current index in the argument list
arglistid([{winnr} [, {tabnr}]]) Number argument list id arglistid([{winnr} [, {tabnr}]]) Number argument list id
argv({nr}) String {nr} entry of the argument list argv({nr}) String {nr} entry of the argument list
argv() List the argument list argv() List the argument list
assert_equal({exp}, {act} [, {msg}]) none assert {exp} equals {act} assert_equal({exp}, {act} [, {msg}]) none assert {exp} is equal to {act}
assert_exception( {error} [, {msg}]) none assert {error} is in v:exception assert_exception( {error} [, {msg}]) none assert {error} is in v:exception
assert_fails( {cmd} [, {error}]) none assert {cmd} fails assert_fails( {cmd} [, {error}]) none assert {cmd} fails
assert_false({actual} [, {msg}]) none assert {actual} is false assert_false({actual} [, {msg}]) none assert {actual} is false
assert_match( {pat}, {text} [, {msg}]) none assert {pat} matches {text}
assert_notequal( {exp}, {act} [, {msg}]) none assert {exp} is not equal {act}
assert_notmatch( {pat}, {text} [, {msg}]) none assert {pat} not matches {text}
assert_true({actual} [, {msg}]) none assert {actual} is true assert_true({actual} [, {msg}]) none assert {actual} is true
asin({expr}) Float arc sine of {expr} asin({expr}) Float arc sine of {expr}
atan({expr}) Float arc tangent of {expr} atan({expr}) Float arc tangent of {expr}
@ -2281,6 +2284,36 @@ assert_false({actual} [, {msg}]) *assert_false()*
When {msg} is omitted an error in the form "Expected False but When {msg} is omitted an error in the form "Expected False but
got {actual}" is produced. 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_notequal()*
assert_notequal({expected}, {actual} [, {msg}])
The opposite of `assert_equal()`: add an error message to
|v:errors| when {expected} and {actual} are equal.
*assert_notmatch()*
assert_notmatch({pattern}, {actual} [, {msg}])
The opposite of `assert_match()`: add an error message to
|v:errors| when {pattern} matches {actual}.
assert_true({actual} [, {msg}]) *assert_true()* assert_true({actual} [, {msg}]) *assert_true()*
When {actual} is not true an error message is added to When {actual} is not true an error message is added to
|v:errors|, like with |assert_equal()|. |v:errors|, like with |assert_equal()|.

View File

@ -3297,6 +3297,26 @@ char_u *get_user_var_name(expand_T *xp, int idx)
} }
/// 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 = 0;
regmatch_T regmatch;
// avoid 'l' flag in 'cpoptions'
char_u *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(&regmatch, text, (colnr_T)0);
vim_regfree(regmatch.regprog);
}
p_cpo = save_cpo;
return matches;
}
/* /*
* types for expressions. * types for expressions.
*/ */
@ -3572,9 +3592,7 @@ static int eval4(char_u **arg, typval_T *rettv, int evaluate)
long n1, n2; long n1, n2;
char_u *s1, *s2; char_u *s1, *s2;
char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN]; char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
regmatch_T regmatch;
int ic; int ic;
char_u *save_cpo;
/* /*
* Get the first variable. * Get the first variable.
@ -3783,19 +3801,10 @@ static int eval4(char_u **arg, typval_T *rettv, int evaluate)
case TYPE_MATCH: case TYPE_MATCH:
case TYPE_NOMATCH: case TYPE_NOMATCH:
/* avoid 'l' flag in 'cpoptions' */ n1 = pattern_match(s2, s1, ic);
save_cpo = p_cpo; if (type == TYPE_NOMATCH) {
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(&regmatch, s1, (colnr_T)0);
vim_regfree(regmatch.regprog);
if (type == TYPE_NOMATCH)
n1 = !n1; n1 = !n1;
} }
p_cpo = save_cpo;
break; break;
case TYPE_UNKNOWN: break; /* avoid gcc warning */ case TYPE_UNKNOWN: break; /* avoid gcc warning */
@ -6697,6 +6706,9 @@ static struct fst {
{ "assert_exception", 1, 2, f_assert_exception }, { "assert_exception", 1, 2, f_assert_exception },
{ "assert_fails", 1, 2, f_assert_fails }, { "assert_fails", 1, 2, f_assert_fails },
{ "assert_false", 1, 2, f_assert_false }, { "assert_false", 1, 2, f_assert_false },
{ "assert_match", 2, 3, f_assert_match },
{ "assert_notequal", 2, 3, f_assert_notequal },
{ "assert_notmatch", 2, 3, f_assert_notmatch },
{ "assert_true", 1, 2, f_assert_true }, { "assert_true", 1, 2, f_assert_true },
{ "atan", 1, 1, f_atan }, { "atan", 1, 1, f_atan },
{ "atan2", 2, 2, f_atan2 }, { "atan2", 2, 2, f_atan2 },
@ -7606,7 +7618,7 @@ static void prepare_assert_error(garray_T *gap)
// Fill "gap" with information about an assert error. // Fill "gap" with information about an assert error.
static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv,
char_u *exp_str, typval_T *exp_tv, char_u *exp_str, typval_T *exp_tv,
typval_T *got_tv) typval_T *got_tv, assert_type_T atype)
{ {
char_u *tofree; char_u *tofree;
@ -7614,8 +7626,12 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv,
tofree = (char_u *) encode_tv2string(opt_msg_tv, NULL); tofree = (char_u *) encode_tv2string(opt_msg_tv, NULL);
ga_concat(gap, tofree); ga_concat(gap, tofree);
xfree(tofree); xfree(tofree);
} else {
if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH) {
ga_concat(gap, (char_u *)"Pattern ");
} else { } else {
ga_concat(gap, (char_u *)"Expected "); ga_concat(gap, (char_u *)"Expected ");
}
if (exp_str == NULL) { if (exp_str == NULL) {
tofree = (char_u *) encode_tv2string(exp_tv, NULL); tofree = (char_u *) encode_tv2string(exp_tv, NULL);
ga_concat(gap, tofree); ga_concat(gap, tofree);
@ -7623,8 +7639,16 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv,
} else { } else {
ga_concat(gap, exp_str); ga_concat(gap, exp_str);
} }
tofree = (char_u *) encode_tv2string(got_tv, NULL); tofree = (char_u *)encode_tv2string(got_tv, NULL);
if (atype == ASSERT_MATCH) {
ga_concat(gap, (char_u *)" does not match ");
} else if (atype == ASSERT_NOTMATCH) {
ga_concat(gap, (char_u *)" does match ");
} else if (atype == ASSERT_NOTEQUAL) {
ga_concat(gap, (char_u *)" differs from ");
} else {
ga_concat(gap, (char_u *)" but got "); ga_concat(gap, (char_u *)" but got ");
}
ga_concat(gap, tofree); ga_concat(gap, tofree);
xfree(tofree); xfree(tofree);
} }
@ -7643,20 +7667,32 @@ static void assert_error(garray_T *gap)
gap->ga_data, gap->ga_len); gap->ga_data, gap->ga_len);
} }
// "assert_equal(expected, actual[, msg])" function static void assert_equal_common(typval_T *argvars, assert_type_T atype)
static void f_assert_equal(typval_T *argvars, typval_T *rettv)
{ {
garray_T ga; garray_T ga;
if (!tv_equal(&argvars[0], &argvars[1], false, false)) { if (tv_equal(&argvars[0], &argvars[1], false, false)
!= (atype == ASSERT_EQUAL)) {
prepare_assert_error(&ga); prepare_assert_error(&ga);
fill_assert_error(&ga, &argvars[2], NULL, fill_assert_error(&ga, &argvars[2], NULL,
&argvars[0], &argvars[1]); &argvars[0], &argvars[1], atype);
assert_error(&ga); assert_error(&ga);
ga_clear(&ga); ga_clear(&ga);
} }
} }
// "assert_equal(expected, actual[, msg])" function
static void f_assert_equal(typval_T *argvars, typval_T *rettv)
{
assert_equal_common(argvars, ASSERT_EQUAL);
}
// "assert_notequal(expected, actual[, msg])" function
static void f_assert_notequal(typval_T *argvars, typval_T *rettv)
{
assert_equal_common(argvars, ASSERT_NOTEQUAL);
}
/// "assert_exception(string[, msg])" function /// "assert_exception(string[, msg])" function
static void f_assert_exception(typval_T *argvars, typval_T *rettv) static void f_assert_exception(typval_T *argvars, typval_T *rettv)
{ {
@ -7672,7 +7708,7 @@ static void f_assert_exception(typval_T *argvars, typval_T *rettv)
&& strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL) { && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL) {
prepare_assert_error(&ga); prepare_assert_error(&ga);
fill_assert_error(&ga, &argvars[1], NULL, &argvars[0], fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
&vimvars[VV_EXCEPTION].vv_tv); &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
assert_error(&ga); assert_error(&ga);
ga_clear(&ga); ga_clear(&ga);
} }
@ -7702,7 +7738,7 @@ static void f_assert_fails(typval_T *argvars, typval_T *rettv)
|| strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL) { || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL) {
prepare_assert_error(&ga); prepare_assert_error(&ga);
fill_assert_error(&ga, &argvars[2], NULL, &argvars[1], fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
&vimvars[VV_ERRMSG].vv_tv); &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
assert_error(&ga); assert_error(&ga);
ga_clear(&ga); ga_clear(&ga);
} }
@ -7732,7 +7768,7 @@ static void assert_bool(typval_T *argvars, bool is_true)
prepare_assert_error(&ga); prepare_assert_error(&ga);
fill_assert_error(&ga, &argvars[1], fill_assert_error(&ga, &argvars[1],
(char_u *)(is_true ? "True" : "False"), (char_u *)(is_true ? "True" : "False"),
NULL, &argvars[0]); NULL, &argvars[0], ASSERT_OTHER);
assert_error(&ga); assert_error(&ga);
ga_clear(&ga); ga_clear(&ga);
} }
@ -7744,6 +7780,36 @@ static void f_assert_false(typval_T *argvars, typval_T *rettv)
assert_bool(argvars, false); assert_bool(argvars, false);
} }
static void assert_match_common(typval_T *argvars, assert_type_T atype)
{
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 (pat == NULL || text == NULL) {
EMSG(_(e_invarg));
} else if (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);
assert_error(&ga);
ga_clear(&ga);
}
}
/// "assert_match(pattern, actual[, msg])" function
static void f_assert_match(typval_T *argvars, typval_T *rettv)
{
assert_match_common(argvars, ASSERT_MATCH);
}
/// "assert_notmatch(pattern, actual[, msg])" function
static void f_assert_notmatch(typval_T *argvars, typval_T *rettv)
{
assert_match_common(argvars, ASSERT_NOTMATCH);
}
// "assert_true(actual[, msg])" function // "assert_true(actual[, msg])" function
static void f_assert_true(typval_T *argvars, typval_T *rettv) static void f_assert_true(typval_T *argvars, typval_T *rettv)
{ {

View File

@ -162,4 +162,14 @@ typedef struct list_stack_S {
/// Convert a hashitem pointer to a dictitem pointer /// Convert a hashitem pointer to a dictitem pointer
#define HI2DI(hi) HIKEY2DI((hi)->hi_key) #define HI2DI(hi) HIKEY2DI((hi)->hi_key)
/// Type of assert_* check being performed
typedef enum
{
ASSERT_EQUAL,
ASSERT_NOTEQUAL,
ASSERT_MATCH,
ASSERT_NOTMATCH,
ASSERT_OTHER,
} assert_type_T;
#endif // NVIM_EVAL_DEFS_H #endif // NVIM_EVAL_DEFS_H

View File

@ -90,7 +90,10 @@ static int included_patches[] = {
1728, 1728,
1716, 1716,
1712, 1712,
1703,
1695, 1695,
1682,
1663,
1654, 1654,
1652, 1652,
1649, 1649,

View File

@ -64,6 +64,20 @@ describe('assert function:', function()
end) end)
end) end)
-- assert_notequal({expected}, {actual}[, {msg}])
describe('assert_notequal', function()
it('should not change v:errors when expected differs from actual', function()
call('assert_notequal', 'foo', 4)
call('assert_notequal', {1, 2, 3}, 'foo')
expected_empty()
end)
it('should change v:errors when expected is equal to actual', function()
call('assert_notequal', 'foo', 'foo')
expected_errors({"Expected 'foo' differs from 'foo'"})
end)
end)
-- assert_false({actual}, [, {msg}]) -- assert_false({actual}, [, {msg}])
describe('assert_false', function() describe('assert_false', function()
it('should not change v:errors when actual is false', function() it('should not change v:errors when actual is false', function()
@ -155,10 +169,43 @@ describe('assert function:', function()
end) end)
end) end)
-- assert_match({pat}, {text}[, {msg}])
describe('assert_match', function()
it('should not change v:errors when pat matches text', function()
call('assert_match', '^f.*b.*r$', 'foobar')
expected_empty()
end)
it('should change v:errors when pat does not match text', function()
call('assert_match', 'bar.*foo', 'foobar')
expected_errors({"Pattern 'bar.*foo' does not match 'foobar'"})
end)
it('should set v:errors to msg when given and match fails', function()
call('assert_match', 'bar.*foo', 'foobar', 'wrong')
expected_errors({"'wrong'"})
end)
end)
-- assert_notmatch({pat}, {text}[, {msg}])
describe('assert_notmatch', function()
it('should not change v:errors when pat does not match text', function()
call('assert_notmatch', 'foo', 'bar')
call('assert_notmatch', '^foobar$', 'foobars')
expected_empty()
end)
it('should change v:errors when pat matches text', function()
call('assert_notmatch', 'foo', 'foobar')
expected_errors({"Pattern 'foo' does match 'foobar'"})
end)
end)
-- assert_fails({cmd}, [, {error}]) -- assert_fails({cmd}, [, {error}])
describe('assert_fails', function() describe('assert_fails', function()
it('should change v:errors when error does not match v:errmsg', function() it('should change v:errors when error does not match v:errmsg', function()
execute([[call assert_fails('xxx', {})]]) execute([[call assert_fails('xxx', {})]])
execute([[call assert_match("Expected {} but got 'E731:", v:errors[0])]])
expected_errors({"Expected {} but got 'E731: using Dictionary as a String'"}) expected_errors({"Expected {} but got 'E731: using Dictionary as a String'"})
end) end)