From bc306ab5aa0d2ee7a4653cb1d47615a3f04478a1 Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 25 May 2016 16:59:52 -0400 Subject: [PATCH 1/2] vim-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. https://github.com/vim/vim/commit/a260b87d9da17f605666630f18c1ed909c2b8bae --- runtime/doc/eval.txt | 6 ++++++ src/nvim/eval.c | 37 +++++++++++++++++++++++++++++++++++++ src/nvim/version.c | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 8b23d2ff5f..bda7a4b910 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -1789,6 +1789,7 @@ 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_fails( {cmd} [, {error}]) none assert {cmd} fails 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} @@ -2263,6 +2264,11 @@ assert_exception({error} [, {msg}]) *assert_exception()* call assert_exception('E492:') endtry +assert_fails({cmd} [, {error}]) *assert_fails()* + Run {cmd} and add an error message to |v:errors| if it does + NOT produce an error. + When {error} is given it must match |v:errmsg|. + assert_false({actual} [, {msg}]) *assert_false()* When {actual} is not false an error message is added to |v:errors|, like with |assert_equal()|. diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 0d060e5b70..ec53de3269 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -6680,6 +6680,7 @@ static struct fst { { "asin", 1, 1, f_asin }, // WJMc { "assert_equal", 2, 3, f_assert_equal }, { "assert_exception", 1, 2, f_assert_exception }, + { "assert_fails", 1, 2, f_assert_fails }, { "assert_false", 1, 2, f_assert_false }, { "assert_true", 1, 2, f_assert_true }, { "atan", 1, 1, f_atan }, @@ -7667,6 +7668,42 @@ static void f_assert_exception(typval_T *argvars, typval_T *rettv) } } +/// "assert_fails(cmd [, error])" function +static void f_assert_fails(typval_T *argvars, typval_T *rettv) +{ + char_u *cmd = get_tv_string_chk(&argvars[0]); + garray_T ga; + + called_emsg = false; + suppress_errthrow = true; + emsg_silent = true; + do_cmdline_cmd((char *)cmd); + if (!called_emsg) { + prepare_assert_error(&ga); + ga_concat(&ga, (char_u *)"command did not fail: "); + ga_concat(&ga, cmd); + assert_error(&ga); + ga_clear(&ga); + } else if (argvars[1].v_type != VAR_UNKNOWN) { + char_u buf[NUMBUFLEN]; + char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf); + + if (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); + assert_error(&ga); + ga_clear(&ga); + } + } + + called_emsg = false; + suppress_errthrow = false; + emsg_silent = false; + emsg_on_display = false; + set_vim_var_string(VV_ERRMSG, NULL, 0); +} + // Common for assert_true() and assert_false(). static void assert_bool(typval_T *argvars, bool is_true) { diff --git a/src/nvim/version.c b/src/nvim/version.c index d34df421f3..7400385d2d 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -590,7 +590,7 @@ static int included_patches[] = { // 1099 NA // 1098 NA // 1097, - // 1096, + 1096, // 1095 NA // 1094, 1093, From ccef5c9c77543fb0cf1b31c82fcb566cca7eff4c Mon Sep 17 00:00:00 2001 From: James McCoy Date: Wed, 25 May 2016 17:13:23 -0400 Subject: [PATCH 2/2] vim-patch:7.4.1567 Problem: Crash in assert_fails(). Solution: Check for NULL. (Dominique Pelle) Add a test. https://github.com/vim/vim/commit/1abb502635c7f317e05a0cf3ea067101f9d684f5 --- src/nvim/eval.c | 3 ++- src/nvim/version.c | 2 +- test/functional/legacy/assert_spec.lua | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index ec53de3269..18d6dc1444 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -7688,7 +7688,8 @@ static void f_assert_fails(typval_T *argvars, typval_T *rettv) char_u buf[NUMBUFLEN]; char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf); - if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL) { + if (error == NULL + || 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); diff --git a/src/nvim/version.c b/src/nvim/version.c index 7400385d2d..e66bf44846 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -119,7 +119,7 @@ static int included_patches[] = { 1570, 1569, 1568, - // 1567, + 1567, // 1566 NA // 1565, // 1564, diff --git a/test/functional/legacy/assert_spec.lua b/test/functional/legacy/assert_spec.lua index 1ce665360d..63699387c1 100644 --- a/test/functional/legacy/assert_spec.lua +++ b/test/functional/legacy/assert_spec.lua @@ -142,4 +142,22 @@ describe('assert function:', function() }) end) end) + + -- assert_fails({cmd}, [, {error}]) + describe('assert_fails', function() + it('should change v:errors when error does not match v:errmsg', function() + execute([[call assert_fails('xxx', {})]]) + expected_errors({"Expected {} but got 'E731: using Dictionary as a String'"}) + end) + + it('should not change v:errors when cmd errors', function() + call('assert_fails', 'NonexistentCmd') + expected_empty() + end) + + it('should change v:errors when cmd succeeds', function() + call('assert_fails', 'call empty("")') + expected_errors({'command did not fail: call empty("")'}) + end) + end) end)