Skip to content

Commit a260b87

Browse files
committed
patch 7.4.1096
Problem: Need several lines to verify a command produces an error. Solution: Add assert_fails(). (suggested by Nikolay Pavlov) Make the quickfix alloc test actually work.
1 parent 3d6d5cc commit a260b87

6 files changed

Lines changed: 71 additions & 28 deletions

File tree

runtime/doc/eval.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,7 @@ argv( {nr}) String {nr} entry of the argument list
17521752
argv( ) List the argument list
17531753
assert_equal( {exp}, {act} [, {msg}]) none assert {exp} equals {act}
17541754
assert_exception({error} [, {msg}]) none assert {error} is in v:exception
1755+
assert_fails( {cmd} [, {error}]) none assert {cmd} fails
17551756
assert_false( {actual} [, {msg}]) none assert {actual} is false
17561757
assert_true( {actual} [, {msg}]) none assert {actual} is true
17571758
asin( {expr}) Float arc sine of {expr}
@@ -2207,6 +2208,11 @@ assert_exception({error} [, {msg}]) *assert_exception()*
22072208
call assert_exception('E492:')
22082209
endtry
22092210
2211+
assert_fails({cmd} [, {error}]) *assert_fails()*
2212+
Run {cmd} and add an error message to |v:errors| if it does
2213+
NOT produce an error.
2214+
When {error} is given it must match |v:errmsg|.
2215+
22102216
assert_false({actual} [, {msg}]) *assert_false()*
22112217
When {actual} is not false an error message is added to
22122218
|v:errors|, like with |assert_equal()|.

src/alloc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ typedef enum {
1717
aid_qf_namebuf,
1818
aid_qf_errmsg,
1919
aid_qf_pattern,
20+
aid_last,
2021
} alloc_id_T;

src/eval.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ 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));
478478
static void f_assert_exception __ARGS((typval_T *argvars, typval_T *rettv));
479+
static void f_assert_fails __ARGS((typval_T *argvars, typval_T *rettv));
479480
static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
480481
static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
481482
#ifdef FEAT_FLOAT
@@ -8090,6 +8091,7 @@ static struct fst
80908091
#endif
80918092
{"assert_equal", 2, 3, f_assert_equal},
80928093
{"assert_exception", 1, 2, f_assert_exception},
8094+
{"assert_fails", 1, 2, f_assert_fails},
80938095
{"assert_false", 1, 2, f_assert_false},
80948096
{"assert_true", 1, 2, f_assert_true},
80958097
#ifdef FEAT_FLOAT
@@ -9009,8 +9011,11 @@ f_alloc_fail(argvars, rettv)
90099011
else
90109012
{
90119013
alloc_fail_id = argvars[0].vval.v_number;
9014+
if (alloc_fail_id >= aid_last)
9015+
EMSG(_(e_invarg));
90129016
alloc_fail_countdown = argvars[1].vval.v_number;
90139017
alloc_fail_repeat = argvars[2].vval.v_number;
9018+
did_outofmem_msg = FALSE;
90149019
}
90159020
}
90169021

@@ -9300,6 +9305,51 @@ f_assert_exception(argvars, rettv)
93009305
}
93019306
}
93029307

9308+
/*
9309+
* "assert_fails(cmd [, error])" function
9310+
*/
9311+
static void
9312+
f_assert_fails(argvars, rettv)
9313+
typval_T *argvars;
9314+
typval_T *rettv UNUSED;
9315+
{
9316+
char_u *cmd = get_tv_string_chk(&argvars[0]);
9317+
garray_T ga;
9318+
9319+
called_emsg = FALSE;
9320+
suppress_errthrow = TRUE;
9321+
emsg_silent = TRUE;
9322+
do_cmdline_cmd(cmd);
9323+
if (!called_emsg)
9324+
{
9325+
prepare_assert_error(&ga);
9326+
ga_concat(&ga, (char_u *)"command did not fail: ");
9327+
ga_concat(&ga, cmd);
9328+
assert_error(&ga);
9329+
ga_clear(&ga);
9330+
}
9331+
else if (argvars[1].v_type != VAR_UNKNOWN)
9332+
{
9333+
char_u buf[NUMBUFLEN];
9334+
char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9335+
9336+
if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9337+
{
9338+
prepare_assert_error(&ga);
9339+
fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9340+
&vimvars[VV_ERRMSG].vv_tv);
9341+
assert_error(&ga);
9342+
ga_clear(&ga);
9343+
}
9344+
}
9345+
9346+
called_emsg = FALSE;
9347+
suppress_errthrow = FALSE;
9348+
emsg_silent = FALSE;
9349+
emsg_on_display = FALSE;
9350+
set_vim_var_string(VV_ERRMSG, NULL, 0);
9351+
}
9352+
93039353
/*
93049354
* Common for assert_true() and assert_false().
93059355
*/

src/misc2.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,13 +798,17 @@ vim_mem_profile_dump()
798798
#endif /* MEM_PROFILE */
799799

800800
#ifdef FEAT_EVAL
801+
static int alloc_does_fail __ARGS((long_u size));
802+
801803
static int
802-
alloc_does_fail()
804+
alloc_does_fail(size)
805+
long_u size;
803806
{
804807
if (alloc_fail_countdown == 0)
805808
{
806809
if (--alloc_fail_repeat <= 0)
807810
alloc_fail_id = 0;
811+
do_outofmem_msg(size);
808812
return TRUE;
809813
}
810814
--alloc_fail_countdown;
@@ -844,7 +848,7 @@ alloc_id(size, id)
844848
alloc_id_T id UNUSED;
845849
{
846850
#ifdef FEAT_EVAL
847-
if (alloc_fail_id == id && alloc_does_fail())
851+
if (alloc_fail_id == id && alloc_does_fail((long_u)size))
848852
return NULL;
849853
#endif
850854
return (lalloc((long_u)size, TRUE));
@@ -1008,7 +1012,7 @@ lalloc_id(size, message, id)
10081012
alloc_id_T id UNUSED;
10091013
{
10101014
#ifdef FEAT_EVAL
1011-
if (alloc_fail_id == id && alloc_does_fail())
1015+
if (alloc_fail_id == id && alloc_does_fail(size))
10121016
return NULL;
10131017
#endif
10141018
return (lalloc((long_u)size, message));

src/testdir/test_quickfix.vim

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -279,39 +279,19 @@ endfunction
279279

280280
function Test_nomem()
281281
call alloc_fail(GetAllocId('qf_dirname_start'), 0, 0)
282-
try
283-
vimgrep vim runtest.vim
284-
catch
285-
call assert_true(v:exception =~ 'E342')
286-
endtry
282+
call assert_fails('vimgrep vim runtest.vim', 'E342:')
287283

288284
call alloc_fail(GetAllocId('qf_dirname_now'), 0, 0)
289-
try
290-
vimgrep vim runtest.vim
291-
catch
292-
call assert_true(v:exception =~ 'E342')
293-
endtry
285+
call assert_fails('vimgrep vim runtest.vim', 'E342:')
294286

295287
call alloc_fail(GetAllocId('qf_namebuf'), 0, 0)
296-
try
297-
cfile runtest.vim
298-
catch
299-
call assert_true(v:exception =~ 'E342')
300-
endtry
288+
call assert_fails('cfile runtest.vim', 'E342:')
301289

302290
call alloc_fail(GetAllocId('qf_errmsg'), 0, 0)
303-
try
304-
cfile runtest.vim
305-
catch
306-
call assert_true(v:exception =~ 'E342')
307-
endtry
291+
call assert_fails('cfile runtest.vim', 'E342:')
308292

309293
call alloc_fail(GetAllocId('qf_pattern'), 0, 0)
310-
try
311-
cfile runtest.vim
312-
catch
313-
call assert_true(v:exception =~ 'E342')
314-
endtry
294+
call assert_fails('cfile runtest.vim', 'E342:')
315295

316296
endfunc
317297

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+
1096,
744746
/**/
745747
1095,
746748
/**/

0 commit comments

Comments
 (0)