Skip to content

Commit a803c7f

Browse files
committed
patch 7.4.1092
Problem: It is not simple to test for an exception and give a proper error message. Solution: Add assert_exception().
1 parent b01f357 commit a803c7f

3 files changed

Lines changed: 56 additions & 9 deletions

File tree

runtime/doc/eval.txt

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,9 +1750,10 @@ arglistid( [{winnr} [, {tabnr}]])
17501750
Number argument list id
17511751
argv( {nr}) String {nr} entry of the argument list
17521752
argv( ) List the argument list
1753-
assert_equal( {exp}, {act} [, {msg}]) none assert that {exp} equals {act}
1754-
assert_false( {actual} [, {msg}]) none assert that {actual} is false
1755-
assert_true( {actual} [, {msg}]) none assert that {actual} is true
1753+
assert_equal( {exp}, {act} [, {msg}]) none assert {exp} equals {act}
1754+
assert_exception({error} [, {msg}]) none assert {error} is in v:exception
1755+
assert_false( {actual} [, {msg}]) none assert {actual} is false
1756+
assert_true( {actual} [, {msg}]) none assert {actual} is true
17561757
asin( {expr}) Float arc sine of {expr}
17571758
atan( {expr}) Float arc tangent of {expr}
17581759
atan2( {expr}, {expr}) Float arc tangent of {expr1} / {expr2}
@@ -2179,7 +2180,7 @@ argv([{nr}]) The result is the {nr}th file in the argument list of the
21792180
returned.
21802181

21812182
*assert_equal()*
2182-
assert_equal({expected}, {actual}, [, {msg}])
2183+
assert_equal({expected}, {actual} [, {msg}])
21832184
When {expected} and {actual} are not equal an error message is
21842185
added to |v:errors|.
21852186
There is no automatic conversion, the String "4" is different
@@ -2193,18 +2194,31 @@ assert_equal({expected}, {actual}, [, {msg}])
21932194
< Will result in a string to be added to |v:errors|:
21942195
test.vim line 12: Expected 'foo' but got 'bar' ~
21952196

2196-
assert_false({actual}, [, {msg}]) *assert_false()*
2197+
assert_exception({error} [, {msg}]) *assert_exception()*
2198+
When v:exception does not contain the string {error} an error
2199+
message is added to |v:errors|.
2200+
This can be used to assert that a command throws an exception.
2201+
Using the error number, followed by a colon, avoids problems
2202+
with translations: >
2203+
try
2204+
commandthatfails
2205+
call assert_false(1, 'command should have failed')
2206+
catch
2207+
call assert_exception('E492:')
2208+
endtry
2209+
2210+
assert_false({actual} [, {msg}]) *assert_false()*
21972211
When {actual} is not false an error message is added to
2198-
|v:errors|, like with |assert_equal()|..
2212+
|v:errors|, like with |assert_equal()|.
21992213
A value is false when it is zero. When "{actual}" is not a
22002214
number the assert fails.
22012215
When {msg} is omitted an error in the form "Expected False but
22022216
got {actual}" is produced.
22032217

2204-
assert_true({actual}, [, {msg}]) *assert_true()*
2218+
assert_true({actual} [, {msg}]) *assert_true()*
22052219
When {actual} is not true an error message is added to
2206-
|v:errors|, like with |assert_equal()|..
2207-
A value is true when it is a non-zeron number. When {actual}
2220+
|v:errors|, like with |assert_equal()|.
2221+
A value is true when it is a non-zero number. When {actual}
22082222
is not a number the assert fails.
22092223
When {msg} is omitted an error in the form "Expected True but
22102224
got {actual}" is produced.

src/eval.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
475475
static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
476476
static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
477477
static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
478+
static void f_assert_exception __ARGS((typval_T *argvars, typval_T *rettv));
478479
static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
479480
static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
480481
#ifdef FEAT_FLOAT
@@ -8088,6 +8089,7 @@ static struct fst
80888089
{"asin", 1, 1, f_asin}, /* WJMc */
80898090
#endif
80908091
{"assert_equal", 2, 3, f_assert_equal},
8092+
{"assert_exception", 1, 2, f_assert_exception},
80918093
{"assert_false", 1, 2, f_assert_false},
80928094
{"assert_true", 1, 2, f_assert_true},
80938095
#ifdef FEAT_FLOAT
@@ -9269,6 +9271,35 @@ f_assert_equal(argvars, rettv)
92699271
}
92709272
}
92719273

9274+
/*
9275+
* "assert_exception(string[, msg])" function
9276+
*/
9277+
static void
9278+
f_assert_exception(argvars, rettv)
9279+
typval_T *argvars;
9280+
typval_T *rettv UNUSED;
9281+
{
9282+
garray_T ga;
9283+
char *error;
9284+
9285+
error = (char *)get_tv_string_chk(&argvars[0]);
9286+
if (vimvars[VV_EXCEPTION].vv_str == NULL)
9287+
{
9288+
prepare_assert_error(&ga);
9289+
ga_concat(&ga, (char_u *)"v:exception is not set");
9290+
assert_error(&ga);
9291+
ga_clear(&ga);
9292+
}
9293+
else if (strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
9294+
{
9295+
prepare_assert_error(&ga);
9296+
fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9297+
&vimvars[VV_EXCEPTION].vv_tv);
9298+
assert_error(&ga);
9299+
ga_clear(&ga);
9300+
}
9301+
}
9302+
92729303
/*
92739304
* Common for assert_true() and assert_false().
92749305
*/

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ static char *(features[]) =
741741

742742
static int included_patches[] =
743743
{ /* Add new patch number below this line */
744+
/**/
745+
1092,
744746
/**/
745747
1091,
746748
/**/

0 commit comments

Comments
 (0)