Skip to content

Commit b48e96f

Browse files
committed
patch 8.0.1510: cannot test if a command causes a beep
Problem: Cannot test if a command causes a beep. Solution: Add assert_beeps().
1 parent 2949595 commit b48e96f

9 files changed

Lines changed: 65 additions & 2 deletions

File tree

runtime/doc/eval.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,7 @@ argidx() Number current index in the argument list
20172017
arglistid([{winnr} [, {tabnr}]]) Number argument list id
20182018
argv({nr}) String {nr} entry of the argument list
20192019
argv() List the argument list
2020+
assert_beeps({cmd}) none assert {cmd} causes a beep
20202021
assert_equal({exp}, {act} [, {msg}])
20212022
none assert {exp} is equal to {act}
20222023
assert_exception({error} [, {msg}])
@@ -2568,6 +2569,11 @@ argv([{nr}]) The result is the {nr}th file in the argument list of the
25682569
< Without the {nr} argument a |List| with the whole |arglist| is
25692570
returned.
25702571

2572+
assert_beeps({cmd}) *assert_beeps()*
2573+
Run {cmd} and add an error message to |v:errors| if it does
2574+
NOT produce a beep or visual bell.
2575+
Also see |assert_fails()|.
2576+
25712577
*assert_equal()*
25722578
assert_equal({expected}, {actual} [, {msg}])
25732579
When {expected} and {actual} are not equal an error message is
@@ -2600,6 +2606,8 @@ assert_fails({cmd} [, {error}]) *assert_fails()*
26002606
Run {cmd} and add an error message to |v:errors| if it does
26012607
NOT produce an error.
26022608
When {error} is given it must match in |v:errmsg|.
2609+
Note that beeping is not considered an error, and some failing
2610+
commands only beep. Use |assert_beeps()| for those.
26032611

26042612
assert_false({actual} [, {msg}]) *assert_false()*
26052613
When {actual} is not false an error message is added to

src/eval.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8941,6 +8941,29 @@ assert_exception(typval_T *argvars)
89418941
}
89428942
}
89438943

8944+
void
8945+
assert_beeps(typval_T *argvars)
8946+
{
8947+
char_u *cmd = get_tv_string_chk(&argvars[0]);
8948+
garray_T ga;
8949+
8950+
called_vim_beep = FALSE;
8951+
suppress_errthrow = TRUE;
8952+
emsg_silent = FALSE;
8953+
do_cmdline_cmd(cmd);
8954+
if (!called_vim_beep)
8955+
{
8956+
prepare_assert_error(&ga);
8957+
ga_concat(&ga, (char_u *)"command did not beep: ");
8958+
ga_concat(&ga, cmd);
8959+
assert_error(&ga);
8960+
ga_clear(&ga);
8961+
}
8962+
8963+
suppress_errthrow = FALSE;
8964+
emsg_on_display = FALSE;
8965+
}
8966+
89448967
void
89458968
assert_fails(typval_T *argvars)
89468969
{

src/evalfunc.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ static void f_argc(typval_T *argvars, typval_T *rettv);
4444
static void f_argidx(typval_T *argvars, typval_T *rettv);
4545
static void f_arglistid(typval_T *argvars, typval_T *rettv);
4646
static void f_argv(typval_T *argvars, typval_T *rettv);
47+
static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
4748
static void f_assert_equal(typval_T *argvars, typval_T *rettv);
4849
static void f_assert_exception(typval_T *argvars, typval_T *rettv);
4950
static void f_assert_fails(typval_T *argvars, typval_T *rettv);
@@ -483,6 +484,7 @@ static struct fst
483484
#ifdef FEAT_FLOAT
484485
{"asin", 1, 1, f_asin}, /* WJMc */
485486
#endif
487+
{"assert_beeps", 1, 2, f_assert_beeps},
486488
{"assert_equal", 2, 3, f_assert_equal},
487489
{"assert_exception", 1, 2, f_assert_exception},
488490
{"assert_fails", 1, 2, f_assert_fails},
@@ -1274,6 +1276,15 @@ f_argv(typval_T *argvars, typval_T *rettv)
12741276
alist_name(&ARGLIST[idx]), -1);
12751277
}
12761278

1279+
/*
1280+
* "assert_beeps(cmd [, error])" function
1281+
*/
1282+
static void
1283+
f_assert_beeps(typval_T *argvars, typval_T *rettv UNUSED)
1284+
{
1285+
assert_beeps(argvars);
1286+
}
1287+
12771288
/*
12781289
* "assert_equal(expected, actual[, msg])" function
12791290
*/

src/globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ EXTERN dict_T globvardict; /* Dictionary with g: variables */
181181
EXTERN int did_emsg; /* set by emsg() when the message
182182
is displayed or thrown */
183183
#ifdef FEAT_EVAL
184+
EXTERN int called_vim_beep; /* set if vim_beep() is called */
184185
EXTERN int did_uncaught_emsg; /* emsg() was called and did not
185186
cause an exception */
186187
#endif

src/misc1.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3688,6 +3688,10 @@ beep_flush(void)
36883688
vim_beep(
36893689
unsigned val) /* one of the BO_ values, e.g., BO_OPER */
36903690
{
3691+
#ifdef FEAT_EVAL
3692+
called_vim_beep = TRUE;
3693+
#endif
3694+
36913695
if (emsg_silent == 0)
36923696
{
36933697
if (!((bo_flags & val) || (bo_flags & BO_ALL)))
@@ -3718,8 +3722,9 @@ vim_beep(
37183722
#endif
37193723
}
37203724

3721-
/* When 'verbose' is set and we are sourcing a script or executing a
3722-
* function give the user a hint where the beep comes from. */
3725+
/* When 'debug' contains "beep" produce a message. If we are sourcing
3726+
* a script or executing a function give the user a hint where the beep
3727+
* comes from. */
37233728
if (vim_strchr(p_debug, 'e') != NULL)
37243729
{
37253730
msg_source(HL_ATTR(HLF_W));

src/proto/eval.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ void assert_inrange(typval_T *argvars);
127127
void assert_bool(typval_T *argvars, int isTrue);
128128
void assert_report(typval_T *argvars);
129129
void assert_exception(typval_T *argvars);
130+
void assert_beeps(typval_T *argvars);
130131
void assert_fails(typval_T *argvars);
131132
void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T atype);
132133
int typval_compare(typval_T *typ1, typval_T *typ2, exptype_T type, int type_is, int ic, int evaluate);

src/testdir/test_assert.vim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ func Test_assert_fail_fails()
111111
call remove(v:errors, 0)
112112
endfunc
113113

114+
func Test_assert_beeps()
115+
new
116+
call assert_beeps('normal h')
117+
118+
call assert_beeps('normal 0')
119+
call assert_match("command did not beep: normal 0", v:errors[0])
120+
call remove(v:errors, 0)
121+
bwipe
122+
endfunc
123+
114124
func Test_assert_inrange()
115125
call assert_inrange(7, 7, 7)
116126
call assert_inrange(5, 7, 5)

src/testdir/test_normal.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,8 @@ endfunc
21772177

21782178
func! Test_normal45_drop()
21792179
if !has('dnd')
2180+
" The ~ register does not exist
2181+
call assert_beeps('norm! "~')
21802182
return
21812183
endif
21822184

src/version.c

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

772772
static int included_patches[] =
773773
{ /* Add new patch number below this line */
774+
/**/
775+
1510,
774776
/**/
775777
1509,
776778
/**/

0 commit comments

Comments
 (0)