I think that assert_fails(command, exception_pattern[, message]) would be
more practical: I mean a function which executes command given as an
argument and intercepts exceptions from it, failing if there was no
exception or if exception did not match, so that your example is one line
only:
call assert_fails('commandthatfails', '\V\^Vim:E492:')
2016-01-15 17:31 GMT+03:00 Bram Moolenaar <[email protected]>:
>
> Patch 7.4.1092
> Problem: It is not simple to test for an exception and give a proper
> error
> message.
> Solution: Add assert_exception().
> Files: src/eval.c, runtime/doc/eval.txt
>
>
> *** ../vim-7.4.1091/src/eval.c 2016-01-09 18:20:41.601002765 +0100
> --- src/eval.c 2016-01-15 14:39:41.292690326 +0100
> ***************
> *** 475,480 ****
> --- 475,481 ----
> static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
> static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
> static void f_assert_equal __ARGS((typval_T *argvars, typval_T *rettv));
> + static void f_assert_exception __ARGS((typval_T *argvars, typval_T
> *rettv));
> static void f_assert_false __ARGS((typval_T *argvars, typval_T *rettv));
> static void f_assert_true __ARGS((typval_T *argvars, typval_T *rettv));
> #ifdef FEAT_FLOAT
> ***************
> *** 8088,8093 ****
> --- 8089,8095 ----
> {"asin", 1, 1, f_asin}, /* WJMc */
> #endif
> {"assert_equal", 2, 3, f_assert_equal},
> + {"assert_exception", 1, 2, f_assert_exception},
> {"assert_false", 1, 2, f_assert_false},
> {"assert_true", 1, 2, f_assert_true},
> #ifdef FEAT_FLOAT
> ***************
> *** 9270,9275 ****
> --- 9272,9306 ----
> }
>
> /*
> + * "assert_exception(string[, msg])" function
> + */
> + static void
> + f_assert_exception(argvars, rettv)
> + typval_T *argvars;
> + typval_T *rettv UNUSED;
> + {
> + garray_T ga;
> + char *error;
> +
> + error = (char *)get_tv_string_chk(&argvars[0]);
> + if (vimvars[VV_EXCEPTION].vv_str == NULL)
> + {
> + prepare_assert_error(&ga);
> + ga_concat(&ga, (char_u *)"v:exception is not set");
> + assert_error(&ga);
> + ga_clear(&ga);
> + }
> + else if (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);
> + assert_error(&ga);
> + ga_clear(&ga);
> + }
> + }
> +
> + /*
> * Common for assert_true() and assert_false().
> */
> static void
> *** ../vim-7.4.1091/runtime/doc/eval.txt 2016-01-07
> 21:24:57.329499581 +0100
> --- runtime/doc/eval.txt 2016-01-15 15:28:08.001417172 +0100
> ***************
> *** 1746,1754 ****
> 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 that {exp} equals
> {act}
> ! assert_false( {actual} [, {msg}]) none assert that {actual} is
> false
> ! assert_true( {actual} [, {msg}]) none assert that {actual} is
> true
> asin( {expr}) Float arc sine of {expr}
> atan( {expr}) Float arc tangent of {expr}
> atan2( {expr}, {expr}) Float arc tangent of {expr1} /
> {expr2}
> --- 1750,1759 ----
> 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_exception({error} [, {msg}]) none assert {error} is in
> v:exception
> ! assert_false( {actual} [, {msg}]) none assert {actual} is false
> ! assert_true( {actual} [, {msg}]) none assert {actual} is true
> asin( {expr}) Float arc sine of {expr}
> atan( {expr}) Float arc tangent of {expr}
> atan2( {expr}, {expr}) Float arc tangent of {expr1} /
> {expr2}
> ***************
> *** 2175,2181 ****
> returned.
>
> *assert_equal()*
> ! assert_equal({expected}, {actual}, [, {msg}])
> When {expected} and {actual} are not equal an error
> message is
> added to |v:errors|.
> There is no automatic conversion, the String "4" is
> different
> --- 2180,2186 ----
> returned.
>
> *assert_equal()*
> ! assert_equal({expected}, {actual} [, {msg}])
> When {expected} and {actual} are not equal an error
> message is
> added to |v:errors|.
> There is no automatic conversion, the String "4" is
> different
> ***************
> *** 2189,2206 ****
> < Will result in a string to be added to |v:errors|:
> test.vim line 12: Expected 'foo' but got 'bar' ~
>
> ! assert_false({actual}, [, {msg}])
> *assert_false()*
> When {actual} is not false an error message is added to
> ! |v:errors|, like with |assert_equal()|..
> A value is false when it is zero. When "{actual}" is not a
> number the assert fails.
> When {msg} is omitted an error in the form "Expected False
> but
> got {actual}" is produced.
>
> ! assert_true({actual}, [, {msg}])
> *assert_true()*
> When {actual} is not true an error message is added to
> ! |v:errors|, like with |assert_equal()|..
> ! A value is true when it is a non-zeron number. When
> {actual}
> is not a number the assert fails.
> When {msg} is omitted an error in the form "Expected True
> but
> got {actual}" is produced.
> --- 2194,2224 ----
> < Will result in a string to be added to |v:errors|:
> test.vim line 12: Expected 'foo' but got 'bar' ~
>
> ! assert_exception({error} [, {msg}])
> *assert_exception()*
> ! When v:exception does not contain the string {error} an
> error
> ! message is added to |v:errors|.
> ! This can be used to assert that a command throws an
> exception.
> ! Using the error number, followed by a colon, avoids
> problems
> ! with translations: >
> ! try
> ! commandthatfails
> ! call assert_false(1, 'command should have
> failed')
> ! catch
> ! call assert_exception('E492:')
> ! endtry
> !
> ! assert_false({actual} [, {msg}])
> *assert_false()*
> When {actual} is not false an error message is added to
> ! |v:errors|, like with |assert_equal()|.
> A value is false when it is zero. When "{actual}" is not a
> number the assert fails.
> When {msg} is omitted an error in the form "Expected False
> but
> got {actual}" is produced.
>
> ! assert_true({actual} [, {msg}])
> *assert_true()*
> When {actual} is not true an error message is added to
> ! |v:errors|, like with |assert_equal()|.
> ! A value is true when it is a non-zero number. When
> {actual}
> is not a number the assert fails.
> When {msg} is omitted an error in the form "Expected True
> but
> got {actual}" is produced.
> *** ../vim-7.4.1091/src/version.c 2016-01-15 15:16:58.336609048 +0100
> --- src/version.c 2016-01-15 15:20:51.614104253 +0100
> ***************
> *** 743,744 ****
> --- 743,746 ----
> { /* Add new patch number below this line */
> + /**/
> + 1092,
> /**/
>
> --
> Hanson's Treatment of Time:
> There are never enough hours in a day, but always too
> many days before Saturday.
>
> /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net
> \\\
> /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/
> \\\
> \\\ an exciting new programming language -- http://www.Zimbu.org
> ///
> \\\ help me help AIDS victims -- http://ICCF-Holland.org
> ///
>
> --
> --
> You received this message from the "vim_dev" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups
> "vim_dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.