mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
vim-patch:7.4.1703
Problem: Can't assert for not equal and not matching.
Solution: Add assert_notmatch() and assert_notequal().
b50e5f5686
This commit is contained in:
@@ -1784,11 +1784,13 @@ argidx() Number current index in the argument list
|
||||
arglistid([{winnr} [, {tabnr}]]) Number argument list id
|
||||
argv({nr}) String {nr} entry of 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_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_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
|
||||
asin({expr}) Float arc sine of {expr}
|
||||
atan({expr}) Float arc tangent of {expr}
|
||||
@@ -2302,6 +2304,16 @@ assert_match({pattern}, {actual} [, {msg}])
|
||||
< 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()*
|
||||
When {actual} is not true an error message is added to
|
||||
|v:errors|, like with |assert_equal()|.
|
||||
|
||||
@@ -6707,6 +6707,8 @@ static struct fst {
|
||||
{ "assert_fails", 1, 2, f_assert_fails },
|
||||
{ "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 },
|
||||
{ "atan", 1, 1, f_atan },
|
||||
{ "atan2", 2, 2, f_atan2 },
|
||||
@@ -7616,7 +7618,7 @@ static void prepare_assert_error(garray_T *gap)
|
||||
// Fill "gap" with information about an assert error.
|
||||
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, bool is_match)
|
||||
typval_T *got_tv, assert_type_T atype)
|
||||
{
|
||||
char_u *tofree;
|
||||
|
||||
@@ -7625,7 +7627,7 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv,
|
||||
ga_concat(gap, tofree);
|
||||
xfree(tofree);
|
||||
} else {
|
||||
if (is_match) {
|
||||
if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH) {
|
||||
ga_concat(gap, (char_u *)"Pattern ");
|
||||
} else {
|
||||
ga_concat(gap, (char_u *)"Expected ");
|
||||
@@ -7638,8 +7640,12 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv,
|
||||
ga_concat(gap, exp_str);
|
||||
}
|
||||
tofree = (char_u *) encode_tv2string(got_tv, NULL);
|
||||
if (is_match) {
|
||||
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 ");
|
||||
}
|
||||
@@ -7661,20 +7667,32 @@ static void assert_error(garray_T *gap)
|
||||
gap->ga_data, gap->ga_len);
|
||||
}
|
||||
|
||||
// "assert_equal(expected, actual[, msg])" function
|
||||
static void f_assert_equal(typval_T *argvars, typval_T *rettv)
|
||||
static void assert_equal_common(typval_T *argvars, assert_type_T atype)
|
||||
{
|
||||
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);
|
||||
fill_assert_error(&ga, &argvars[2], NULL,
|
||||
&argvars[0], &argvars[1], false);
|
||||
&argvars[0], &argvars[1], atype);
|
||||
assert_error(&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
|
||||
static void f_assert_exception(typval_T *argvars, typval_T *rettv)
|
||||
{
|
||||
@@ -7690,7 +7708,7 @@ static void f_assert_exception(typval_T *argvars, typval_T *rettv)
|
||||
&& strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL) {
|
||||
prepare_assert_error(&ga);
|
||||
fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
|
||||
&vimvars[VV_EXCEPTION].vv_tv, false);
|
||||
&vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
|
||||
assert_error(&ga);
|
||||
ga_clear(&ga);
|
||||
}
|
||||
@@ -7720,7 +7738,7 @@ static void f_assert_fails(typval_T *argvars, typval_T *rettv)
|
||||
|| strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL) {
|
||||
prepare_assert_error(&ga);
|
||||
fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
|
||||
&vimvars[VV_ERRMSG].vv_tv, false);
|
||||
&vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
|
||||
assert_error(&ga);
|
||||
ga_clear(&ga);
|
||||
}
|
||||
@@ -7750,7 +7768,7 @@ static void assert_bool(typval_T *argvars, bool is_true)
|
||||
prepare_assert_error(&ga);
|
||||
fill_assert_error(&ga, &argvars[1],
|
||||
(char_u *)(is_true ? "True" : "False"),
|
||||
NULL, &argvars[0], false);
|
||||
NULL, &argvars[0], ASSERT_OTHER);
|
||||
assert_error(&ga);
|
||||
ga_clear(&ga);
|
||||
}
|
||||
@@ -7762,8 +7780,7 @@ static void f_assert_false(typval_T *argvars, typval_T *rettv)
|
||||
assert_bool(argvars, false);
|
||||
}
|
||||
|
||||
/// "assert_match(pattern, actual[, msg])" function
|
||||
static void f_assert_match(typval_T *argvars, typval_T *rettv)
|
||||
static void assert_match_common(typval_T *argvars, assert_type_T atype)
|
||||
{
|
||||
char_u buf1[NUMBUFLEN];
|
||||
char_u buf2[NUMBUFLEN];
|
||||
@@ -7772,15 +7789,27 @@ static void f_assert_match(typval_T *argvars, typval_T *rettv)
|
||||
|
||||
if (pat == NULL || text == NULL) {
|
||||
EMSG(_(e_invarg));
|
||||
} else if (!pattern_match(pat, text, false)) {
|
||||
} 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], true);
|
||||
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
|
||||
static void f_assert_true(typval_T *argvars, typval_T *rettv)
|
||||
{
|
||||
|
||||
@@ -162,4 +162,14 @@ typedef struct list_stack_S {
|
||||
/// Convert a hashitem pointer to a dictitem pointer
|
||||
#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
|
||||
|
||||
@@ -90,6 +90,7 @@ static int included_patches[] = {
|
||||
1728,
|
||||
1716,
|
||||
1712,
|
||||
1703,
|
||||
1695,
|
||||
1682,
|
||||
1663,
|
||||
|
||||
@@ -64,6 +64,20 @@ describe('assert function:', function()
|
||||
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}])
|
||||
describe('assert_false', function()
|
||||
it('should not change v:errors when actual is false', function()
|
||||
@@ -173,6 +187,20 @@ describe('assert function:', function()
|
||||
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}])
|
||||
describe('assert_fails', function()
|
||||
it('should change v:errors when error does not match v:errmsg', function()
|
||||
|
||||
Reference in New Issue
Block a user