From 0a3a2132d43be3a376214f75157d4d2b1340311e Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 4 May 2023 21:00:46 +0800 Subject: vim-patch:8.2.1953: Vim9: extra "unknown" error after other error Problem: Vim9: extra "unknown" error after other error. Solution: Restore did_emsg count after EXEC instruction. (closes vim/vim#7254) Improve error message from assert_fails() https://github.com/vim/vim/commit/631e8f93458b102091d54c502f489c03e454d4da Co-authored-by: Bram Moolenaar --- src/nvim/testing.c | 13 +++++++++++-- test/old/testdir/test_assert.vim | 12 ++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/nvim/testing.c b/src/nvim/testing.c index 867a8ffd3a..27d5334fc5 100644 --- a/src/nvim/testing.c +++ b/src/nvim/testing.c @@ -142,7 +142,7 @@ static void ga_concat_shorten_esc(garray_T *gap, const char *str) } /// Fill "gap" with information about an assert error. -static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char *exp_str, +static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, const char *exp_str, typval_T *exp_tv_arg, typval_T *got_tv_arg, assert_type_T atype) { char *tofree; @@ -220,7 +220,13 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char *exp_str ga_concat_shorten_esc(gap, tofree); xfree(tofree); } else { + if (atype != ASSERT_INRANGE) { + ga_concat(gap, "'"); + } ga_concat_shorten_esc(gap, exp_str); + if (atype != ASSERT_INRANGE) { + ga_concat(gap, "'"); + } } if (atype != ASSERT_NOTEQUAL) { @@ -534,6 +540,7 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } else if (argvars[1].v_type != VAR_UNKNOWN) { char buf[NUMBUFLEN]; const char *expected; + const char *expected_str = NULL; bool error_found = false; int error_found_index = 1; char *actual = emsg_assert_fails_msg == NULL ? "[unknown]" : emsg_assert_fails_msg; @@ -551,12 +558,14 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) expected = tv_get_string_buf_chk(tv, buf); if (!pattern_match(expected, actual, false)) { error_found = true; + expected_str = expected; } else if (tv_list_len(list) == 2) { tv = TV_LIST_ITEM_TV(tv_list_last(list)); actual = get_vim_var_str(VV_ERRMSG); expected = tv_get_string_buf_chk(tv, buf); if (!pattern_match(expected, actual, false)) { error_found = true; + expected_str = expected; } } } else { @@ -600,7 +609,7 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) actual_tv.v_type = VAR_STRING; actual_tv.vval.v_string = actual; } - fill_assert_error(&ga, &argvars[2], NULL, + fill_assert_error(&ga, &argvars[2], expected_str, &argvars[error_found_index], &actual_tv, ASSERT_OTHER); ga_concat(&ga, ": "); assert_append_cmd_or_arg(&ga, argvars, cmd); diff --git a/test/old/testdir/test_assert.vim b/test/old/testdir/test_assert.vim index 62b2e5d7ab..5fd0103d27 100644 --- a/test/old/testdir/test_assert.vim +++ b/test/old/testdir/test_assert.vim @@ -6,11 +6,11 @@ func Test_assert_false() call assert_equal(0, v:false->assert_false()) call assert_equal(1, assert_false(123)) - call assert_match("Expected False but got 123", v:errors[0]) + call assert_match("Expected 'False' but got 123", v:errors[0]) call remove(v:errors, 0) call assert_equal(1, 123->assert_false()) - call assert_match("Expected False but got 123", v:errors[0]) + call assert_match("Expected 'False' but got 123", v:errors[0]) call remove(v:errors, 0) endfunc @@ -21,11 +21,11 @@ func Test_assert_true() call assert_equal(0, v:true->assert_true()) call assert_equal(1, assert_true(0)) - call assert_match("Expected True but got 0", v:errors[0]) + call assert_match("Expected 'True' but got 0", v:errors[0]) call remove(v:errors, 0) call assert_equal(1, 0->assert_true()) - call assert_match("Expected True but got 0", v:errors[0]) + call assert_match("Expected 'True' but got 0", v:errors[0]) call remove(v:errors, 0) endfunc @@ -228,11 +228,11 @@ func Test_assert_fail_fails() call remove(v:errors, 0) call assert_equal(1, assert_fails('xxx', ['E9876'])) - call assert_match("Expected \\['E9876'\\] but got 'E492:", v:errors[0]) + call assert_match("Expected 'E9876' but got 'E492:", v:errors[0]) call remove(v:errors, 0) call assert_equal(1, assert_fails('xxx', ['E492:', 'E9876'])) - call assert_match("Expected \\['E492:', 'E9876'\\] but got 'E492:", v:errors[0]) + call assert_match("Expected 'E9876' but got 'E492:", v:errors[0]) call remove(v:errors, 0) call assert_equal(1, assert_fails('echo', '', 'echo command')) -- cgit From 17c8e39f8803daa6f0e6106ce1c087240cef4771 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 4 May 2023 21:49:48 +0800 Subject: vim-patch:9.0.0213: using freed memory with error in assert argument Problem: Using freed memory with error in assert argument. Solution: Make a copy of the error. https://github.com/vim/vim/commit/249e1b903a9c0460d618f6dcc59aeb8c03b24b20 Co-authored-by: Bram Moolenaar --- src/nvim/testing.c | 5 ++++- test/old/testdir/test_assert.vim | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/nvim/testing.c b/src/nvim/testing.c index 27d5334fc5..25149f1b38 100644 --- a/src/nvim/testing.c +++ b/src/nvim/testing.c @@ -510,6 +510,7 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) const int save_trylevel = trylevel; const int called_emsg_before = called_emsg; const char *wrong_arg_msg = NULL; + char *tofree = NULL; if (tv_check_for_string_or_number_arg(argvars, 0) == FAIL || tv_check_for_opt_string_or_list_arg(argvars, 1) == FAIL @@ -560,8 +561,9 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) error_found = true; expected_str = expected; } else if (tv_list_len(list) == 2) { + // make a copy, an error in pattern_match() may free it + tofree = actual = xstrdup(get_vim_var_str(VV_ERRMSG)); tv = TV_LIST_ITEM_TV(tv_list_last(list)); - actual = get_vim_var_str(VV_ERRMSG); expected = tv_get_string_buf_chk(tv, buf); if (!pattern_match(expected, actual, false)) { error_found = true; @@ -632,6 +634,7 @@ theend: msg_reset_scroll(); lines_left = Rows; XFREE_CLEAR(emsg_assert_fails_msg); + xfree(tofree); set_vim_var_string(VV_ERRMSG, NULL, 0); if (wrong_arg_msg != NULL) { emsg(_(wrong_arg_msg)); diff --git a/test/old/testdir/test_assert.vim b/test/old/testdir/test_assert.vim index 5fd0103d27..27f0491634 100644 --- a/test/old/testdir/test_assert.vim +++ b/test/old/testdir/test_assert.vim @@ -277,6 +277,10 @@ func Test_assert_fail_fails() let exp = v:exception endtry call assert_match("E1174: String required for argument 5", exp) + + call assert_equal(1, assert_fails('c0', ['', '\1'])) + call assert_match("Expected '\\\\\\\\1' but got 'E939: Positive count required: c0': c0", v:errors[0]) + call remove(v:errors, 0) endfunc func Test_assert_fails_in_try_block() -- cgit From 5fb6b3431a52932dcc50bcc930f8b2c9b71a1bb0 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 5 May 2023 12:36:47 +0800 Subject: vim-patch:9.0.0404: crash when passing invalid arguments to assert_fails() Problem: Crash when passing invalid arguments to assert_fails(). Solution: Check for NULL string. https://github.com/vim/vim/commit/1540d334a04d874c2aa9d26b82dbbcd4bc5a78de Co-authored-by: Bram Moolenaar --- src/nvim/testing.c | 11 +++++++++++ test/old/testdir/test_assert.vim | 19 +++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/nvim/testing.c b/src/nvim/testing.c index 25149f1b38..92747822b4 100644 --- a/src/nvim/testing.c +++ b/src/nvim/testing.c @@ -531,6 +531,11 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) no_wait_return++; do_cmdline_cmd(cmd); + + // reset here for any errors reported below + trylevel = save_trylevel; + suppress_errthrow = false; + if (called_emsg == called_emsg_before) { prepare_assert_error(&ga); ga_concat(&ga, "command did not fail: "); @@ -557,6 +562,9 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } const typval_T *tv = TV_LIST_ITEM_TV(tv_list_first(list)); expected = tv_get_string_buf_chk(tv, buf); + if (expected == NULL) { + goto theend; + } if (!pattern_match(expected, actual, false)) { error_found = true; expected_str = expected; @@ -565,6 +573,9 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) tofree = actual = xstrdup(get_vim_var_str(VV_ERRMSG)); tv = TV_LIST_ITEM_TV(tv_list_last(list)); expected = tv_get_string_buf_chk(tv, buf); + if (expected == NULL) { + goto theend; + } if (!pattern_match(expected, actual, false)) { error_found = true; expected_str = expected; diff --git a/test/old/testdir/test_assert.vim b/test/old/testdir/test_assert.vim index 27f0491634..6926d73af1 100644 --- a/test/old/testdir/test_assert.vim +++ b/test/old/testdir/test_assert.vim @@ -264,6 +264,21 @@ func Test_assert_fail_fails() endtry call assert_match("E1222: String or List required for argument 2", exp) + try + call assert_equal(0, assert_fails('xxx', [#{one: 1}])) + catch + let exp = v:exception + endtry + call assert_match("E731: Using a Dictionary as a String", exp) + + let exp = '' + try + call assert_equal(0, assert_fails('xxx', ['E492', #{one: 1}])) + catch + let exp = v:exception + endtry + call assert_match("E731: Using a Dictionary as a String", exp) + try call assert_equal(1, assert_fails('xxx', 'E492', '', 'burp')) catch @@ -278,8 +293,8 @@ func Test_assert_fail_fails() endtry call assert_match("E1174: String required for argument 5", exp) - call assert_equal(1, assert_fails('c0', ['', '\1'])) - call assert_match("Expected '\\\\\\\\1' but got 'E939: Positive count required: c0': c0", v:errors[0]) + call assert_equal(1, assert_fails('c0', ['', '\(.\)\1'])) + call assert_match("Expected '\\\\\\\\(.\\\\\\\\)\\\\\\\\1' but got 'E939: Positive count required: c0': c0", v:errors[0]) call remove(v:errors, 0) endfunc -- cgit From 050b24cbccbe6e08f31fd33a854b91f7e8920834 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 5 May 2023 12:40:31 +0800 Subject: vim-patch:9.0.0543: insufficient testing for assert and test functions Problem: Insufficient testing for assert and test functions. Solution: Add a few more tests. (Yegappan Lakshmanan, closes vim/vim#11190) https://github.com/vim/vim/commit/e24b5e0b0f5ab015215ef2761baa98ccb1ba8606 Cherry-pick E1219 from patch 8.2.3229. Cherry-pick test_assert.vim change from patch 9.0.0491. Cherry-pick the whole Test_refcount() function and skip it. Co-authored-by: Yegappan Lakshmanan --- src/nvim/eval/typval.c | 4 +- test/old/testdir/test_assert.vim | 82 ++++++++++++++++++++----- test/old/testdir/test_options.vim | 6 +- test/old/testdir/test_vimscript.vim | 116 ++++++++++++++++++++++++++++++++++++ 4 files changed, 188 insertions(+), 20 deletions(-) diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 5f0c082ada..5755178b18 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -56,6 +56,8 @@ static const char e_list_required_for_argument_nr[] = N_("E1211: List required for argument %d"); static const char e_bool_required_for_argument_nr[] = N_("E1212: Bool required for argument %d"); +static const char e_float_or_number_required_for_argument_nr[] + = N_("E1219: Float or Number required for argument %d"); static const char e_string_or_number_required_for_argument_nr[] = N_("E1220: String or Number required for argument %d"); static const char e_string_or_list_required_for_argument_nr[] @@ -4171,7 +4173,7 @@ int tv_check_for_float_or_nr_arg(const typval_T *const args, const int idx) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE { if (args[idx].v_type != VAR_FLOAT && args[idx].v_type != VAR_NUMBER) { - semsg(_(e_number_required_for_argument_nr), idx + 1); + semsg(_(e_float_or_number_required_for_argument_nr), idx + 1); return FAIL; } return OK; diff --git a/test/old/testdir/test_assert.vim b/test/old/testdir/test_assert.vim index 6926d73af1..9b65e36e16 100644 --- a/test/old/testdir/test_assert.vim +++ b/test/old/testdir/test_assert.vim @@ -257,6 +257,20 @@ func Test_assert_fail_fails() endtry call assert_match("E856: \"assert_fails()\" second argument", exp) + try + call assert_equal(1, assert_fails('xxx', v:_null_list)) + catch + let exp = v:exception + endtry + call assert_match("E856: \"assert_fails()\" second argument", exp) + + try + call assert_equal(1, assert_fails('xxx', [])) + catch + let exp = v:exception + endtry + call assert_match("E856: \"assert_fails()\" second argument", exp) + try call assert_equal(1, assert_fails('xxx', #{one: 1})) catch @@ -296,6 +310,15 @@ func Test_assert_fail_fails() call assert_equal(1, assert_fails('c0', ['', '\(.\)\1'])) call assert_match("Expected '\\\\\\\\(.\\\\\\\\)\\\\\\\\1' but got 'E939: Positive count required: c0': c0", v:errors[0]) call remove(v:errors, 0) + + " Test for matching the line number and the script name in an error message + call writefile(['', 'call Xnonexisting()'], 'Xassertfails.vim', 'D') + call assert_fails('source Xassertfails.vim', 'E117:', '', 10) + call assert_match("Expected 10 but got 2", v:errors[0]) + call remove(v:errors, 0) + call assert_fails('source Xassertfails.vim', 'E117:', '', 2, 'Xabc') + call assert_match("Expected 'Xabc' but got .*Xassertfails.vim", v:errors[0]) + call remove(v:errors, 0) endfunc func Test_assert_fails_in_try_block() @@ -320,6 +343,12 @@ func Test_assert_beeps() bwipe endfunc +func Test_assert_nobeep() + call assert_equal(1, assert_nobeep('normal! cr')) + call assert_match("command did beep: normal! cr", v:errors[0]) + call remove(v:errors, 0) +endfunc + func Test_assert_inrange() call assert_equal(0, assert_inrange(7, 7, 7)) call assert_equal(0, assert_inrange(5, 7, 5)) @@ -341,21 +370,29 @@ func Test_assert_inrange() call assert_fails('call assert_inrange(1, 1)', 'E119:') - if has('float') - call assert_equal(0, assert_inrange(7.0, 7, 7)) - call assert_equal(0, assert_inrange(7, 7.0, 7)) - call assert_equal(0, assert_inrange(7, 7, 7.0)) - call assert_equal(0, assert_inrange(5, 7, 5.0)) - call assert_equal(0, assert_inrange(5, 7, 6.0)) - call assert_equal(0, assert_inrange(5, 7, 7.0)) - - call assert_equal(1, assert_inrange(5, 7, 4.0)) - call assert_match("Expected range 5.0 - 7.0, but got 4.0", v:errors[0]) - call remove(v:errors, 0) - call assert_equal(1, assert_inrange(5, 7, 8.0)) - call assert_match("Expected range 5.0 - 7.0, but got 8.0", v:errors[0]) - call remove(v:errors, 0) - endif + call assert_equal(0, assert_inrange(7.0, 7, 7)) + call assert_equal(0, assert_inrange(7, 7.0, 7)) + call assert_equal(0, assert_inrange(7, 7, 7.0)) + call assert_equal(0, assert_inrange(5, 7, 5.0)) + call assert_equal(0, assert_inrange(5, 7, 6.0)) + call assert_equal(0, assert_inrange(5, 7, 7.0)) + + call assert_equal(1, assert_inrange(5, 7, 4.0)) + call assert_match("Expected range 5.0 - 7.0, but got 4.0", v:errors[0]) + call remove(v:errors, 0) + call assert_equal(1, assert_inrange(5, 7, 8.0)) + call assert_match("Expected range 5.0 - 7.0, but got 8.0", v:errors[0]) + call remove(v:errors, 0) + + " Use a custom message + call assert_equal(1, assert_inrange(5, 7, 8.0, "Higher")) + call assert_match("Higher", v:errors[0]) + call remove(v:errors, 0) + + " Invalid arguments + call assert_fails("call assert_inrange([], 2, 3)", 'E1219:') + call assert_fails("call assert_inrange(1, [], 3)", 'E1219:') + call assert_fails("call assert_inrange(1, 2, [])", 'E1219:') endfunc func Test_assert_with_msg() @@ -393,6 +430,21 @@ func Test_mouse_position() let &mouse = save_mouse endfunc +" Test for the test_alloc_fail() function +func Test_test_alloc_fail() + throw 'Skipped: Nvim does not support test_alloc_fail()' + call assert_fails('call test_alloc_fail([], 1, 1)', 'E474:') + call assert_fails('call test_alloc_fail(10, [], 1)', 'E474:') + call assert_fails('call test_alloc_fail(10, 1, [])', 'E474:') + call assert_fails('call test_alloc_fail(999999, 1, 1)', 'E474:') +endfunc + +" Test for the test_option_not_set() function +func Test_test_option_not_set() + throw 'Skipped: Nvim does not support test_option_not_set()' + call assert_fails('call test_option_not_set("Xinvalidopt")', 'E475:') +endfunc + " Must be last. func Test_zz_quit_detected() " Verify that if a test function ends Vim the test script detects this. diff --git a/test/old/testdir/test_options.vim b/test/old/testdir/test_options.vim index 8fc86a99e3..cf8bf1903f 100644 --- a/test/old/testdir/test_options.vim +++ b/test/old/testdir/test_options.vim @@ -894,8 +894,6 @@ endfunc func Test_shortmess_F2() e file1 e file2 - " Accommodate Nvim default. - set shortmess-=F call assert_match('file1', execute('bn', '')) call assert_match('file2', execute('bn', '')) set shortmess+=F @@ -913,12 +911,12 @@ func Test_shortmess_F2() " call assert_false(test_getvalue('need_fileinfo')) call assert_true(empty(execute('bn', ''))) " call assert_false(test_getvalue('need_fileinfo')) - " Accommodate Nvim default. - set shortmess-=F + set shortmess-=F " Accommodate Nvim default. call assert_match('file1', execute('bn', '')) call assert_match('file2', execute('bn', '')) bwipe bwipe + " call assert_fails('call test_getvalue("abc")', 'E475:') endfunc func Test_local_scrolloff() diff --git a/test/old/testdir/test_vimscript.vim b/test/old/testdir/test_vimscript.vim index 1c5310e883..c8085cc396 100644 --- a/test/old/testdir/test_vimscript.vim +++ b/test/old/testdir/test_vimscript.vim @@ -7035,6 +7035,122 @@ func Test_unlet_env() call assert_equal('', $TESTVAR) endfunc +func Test_refcount() + throw 'Skipped: Nvim does not support test_refcount()' + " Immediate values + call assert_equal(-1, test_refcount(1)) + call assert_equal(-1, test_refcount('s')) + call assert_equal(-1, test_refcount(v:true)) + call assert_equal(0, test_refcount([])) + call assert_equal(0, test_refcount({})) + call assert_equal(0, test_refcount(0zff)) + call assert_equal(0, test_refcount({-> line('.')})) + call assert_equal(-1, test_refcount(0.1)) + if has('job') + call assert_equal(0, test_refcount(job_start([&shell, &shellcmdflag, 'echo .']))) + endif + + " No refcount types + let x = 1 + call assert_equal(-1, test_refcount(x)) + let x = 's' + call assert_equal(-1, test_refcount(x)) + let x = v:true + call assert_equal(-1, test_refcount(x)) + let x = 0.1 + call assert_equal(-1, test_refcount(x)) + + " Check refcount + let x = [] + call assert_equal(1, test_refcount(x)) + + let x = {} + call assert_equal(1, x->test_refcount()) + + let x = 0zff + call assert_equal(1, test_refcount(x)) + + let X = {-> line('.')} + call assert_equal(1, test_refcount(X)) + let Y = X + call assert_equal(2, test_refcount(X)) + + if has('job') + let job = job_start([&shell, &shellcmdflag, 'echo .']) + call assert_equal(1, test_refcount(job)) + call assert_equal(1, test_refcount(job_getchannel(job))) + call assert_equal(1, test_refcount(job)) + endif + + " Function arguments, copying and unassigning + func ExprCheck(x, i) + let i = a:i + 1 + call assert_equal(i, test_refcount(a:x)) + let Y = a:x + call assert_equal(i + 1, test_refcount(a:x)) + call assert_equal(test_refcount(a:x), test_refcount(Y)) + let Y = 0 + call assert_equal(i, test_refcount(a:x)) + endfunc + call ExprCheck([], 0) + call ExprCheck({}, 0) + call ExprCheck(0zff, 0) + call ExprCheck({-> line('.')}, 0) + if has('job') + call ExprCheck(job, 1) + call ExprCheck(job_getchannel(job), 1) + call job_stop(job) + endif + delfunc ExprCheck + + " Regarding function + func Func(x) abort + call assert_equal(2, test_refcount(function('Func'))) + call assert_equal(0, test_refcount(funcref('Func'))) + endfunc + call assert_equal(1, test_refcount(function('Func'))) + call assert_equal(0, test_refcount(function('Func', [1]))) + call assert_equal(0, test_refcount(funcref('Func'))) + call assert_equal(0, test_refcount(funcref('Func', [1]))) + let X = function('Func') + let Y = X + call assert_equal(1, test_refcount(X)) + let X = function('Func', [1]) + let Y = X + call assert_equal(2, test_refcount(X)) + let X = funcref('Func') + let Y = X + call assert_equal(2, test_refcount(X)) + let X = funcref('Func', [1]) + let Y = X + call assert_equal(2, test_refcount(X)) + unlet X + unlet Y + call Func(1) + delfunc Func + + " Function with dict + func DictFunc() dict + call assert_equal(3, test_refcount(self)) + endfunc + let d = {'Func': function('DictFunc')} + call assert_equal(1, test_refcount(d)) + call assert_equal(0, test_refcount(d.Func)) + call d.Func() + unlet d + delfunc DictFunc + + if has('channel') + call assert_equal(-1, test_refcount(test_null_job())) + call assert_equal(-1, test_refcount(test_null_channel())) + endif + call assert_equal(-1, test_refcount(test_null_function())) + call assert_equal(-1, test_refcount(test_null_partial())) + call assert_equal(-1, test_refcount(test_null_blob())) + call assert_equal(-1, test_refcount(test_null_list())) + call assert_equal(-1, test_refcount(test_null_dict())) +endfunc + " Test for missing :endif, :endfor, :endwhile and :endtry {{{1 func Test_missing_end() call writefile(['if 2 > 1', 'echo ">"'], 'Xscript') -- cgit From 49a2bb91170f49101fa67b0cf4cbfec5885edcf4 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 5 May 2023 12:53:39 +0800 Subject: vim-patch:9.0.0846: using assert_fails() may cause hit-enter prompt Problem: Using assert_fails() may cause hit-enter prompt. Solution: Set no_wait_return. (closes vim/vim#11522) https://github.com/vim/vim/commit/f220643c260d55d21a841a3c4032daadc41bc50b Co-authored-by: Bram Moolenaar --- src/nvim/testing.c | 3 +-- test/old/testdir/test_assert.vim | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/nvim/testing.c b/src/nvim/testing.c index 92747822b4..d9c376dd6a 100644 --- a/src/nvim/testing.c +++ b/src/nvim/testing.c @@ -522,14 +522,13 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) return; } - const char *const cmd = tv_get_string_chk(&argvars[0]); - // trylevel must be zero for a ":throw" command to be considered failed trylevel = 0; suppress_errthrow = true; in_assert_fails = true; no_wait_return++; + const char *const cmd = tv_get_string_chk(&argvars[0]); do_cmdline_cmd(cmd); // reset here for any errors reported below diff --git a/test/old/testdir/test_assert.vim b/test/old/testdir/test_assert.vim index 9b65e36e16..e20084515e 100644 --- a/test/old/testdir/test_assert.vim +++ b/test/old/testdir/test_assert.vim @@ -327,6 +327,12 @@ func Test_assert_fails_in_try_block() endtry endfunc +func Test_assert_fails_in_timer() + " should not cause a hit-enter prompt, which isn't actually checked here + call timer_start(0, {-> assert_fails('call', 'E471:')}) + sleep 10m +endfunc + func Test_assert_beeps() new call assert_equal(0, assert_beeps('normal h')) -- cgit From 49c3eb01ab53c4f6bb3f372590878d46ef0c163a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 5 May 2023 12:54:55 +0800 Subject: vim-patch:9.0.0854: no proper test for what 9.0.0846 fixes Problem: No proper test for what 9.0.0846 fixes. Solution: Run test in a terminal so that the hit-enter prompt can show up. (closes vim/vim#11523) https://github.com/vim/vim/commit/7265851b2b4e5a63c0a02a9057dee237502ee557 --- test/old/testdir/test_assert.vim | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/test/old/testdir/test_assert.vim b/test/old/testdir/test_assert.vim index e20084515e..087eea3bcf 100644 --- a/test/old/testdir/test_assert.vim +++ b/test/old/testdir/test_assert.vim @@ -1,5 +1,8 @@ " Test that the methods used for testing work. +source check.vim +source term_util.vim + func Test_assert_false() call assert_equal(0, assert_false(0)) call assert_equal(0, assert_false(v:false)) @@ -327,10 +330,21 @@ func Test_assert_fails_in_try_block() endtry endfunc +" Test that assert_fails() in a timer does not cause a hit-enter prompt. +" Requires using a terminal, in regular tests the hit-enter prompt won't be +" triggered. func Test_assert_fails_in_timer() - " should not cause a hit-enter prompt, which isn't actually checked here - call timer_start(0, {-> assert_fails('call', 'E471:')}) - sleep 10m + CheckRunVimInTerminal + + let buf = RunVimInTerminal('', {'rows': 6}) + let cmd = ":call timer_start(0, {-> assert_fails('call', 'E471:')})" + call term_sendkeys(buf, cmd) + call WaitForAssert({-> assert_equal(cmd, term_getline(buf, 6))}) + call term_sendkeys(buf, "\") + call TermWait(buf, 100) + call assert_match('E471: Argument required', term_getline(buf, 6)) + + call StopVimInTerminal(buf) endfunc func Test_assert_beeps() -- cgit From c11417b3d74f02568e37ea3370a7c24141d4f18a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 5 May 2023 12:59:43 +0800 Subject: vim-patch:9.0.1507: assert message is confusing with boolean result Problem: Assert message is confusing with boolean result. assert_inrange() replaces message instead of adding it. Solution: Don't put quotes around expected boolean value. Append message for assert_inrange(). (closes vim/vim#12342, closes vim/vim#12341) https://github.com/vim/vim/commit/53f5e51628b56ef9171671cd6e9970374036a084 Move assert_type_T to testing.c and remove ASSERT_INRANGE. --- src/nvim/eval.h | 10 ---------- src/nvim/testing.c | 37 +++++++++++++++++++------------------ test/old/testdir/test_assert.vim | 13 ++++++++----- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/nvim/eval.h b/src/nvim/eval.h index e9cdb108a8..5fbfc4702c 100644 --- a/src/nvim/eval.h +++ b/src/nvim/eval.h @@ -229,16 +229,6 @@ typedef struct { Callback callback; } timer_T; -/// Type of assert_* check being performed -typedef enum { - ASSERT_EQUAL, - ASSERT_NOTEQUAL, - ASSERT_MATCH, - ASSERT_NOTMATCH, - ASSERT_INRANGE, - ASSERT_OTHER, -} assert_type_T; - /// types for expressions. typedef enum { EXPR_UNKNOWN = 0, diff --git a/src/nvim/testing.c b/src/nvim/testing.c index d9c376dd6a..430d6713ff 100644 --- a/src/nvim/testing.c +++ b/src/nvim/testing.c @@ -30,6 +30,16 @@ #include "nvim/types.h" #include "nvim/vim.h" +/// Type of assert_* check being performed +typedef enum { + ASSERT_EQUAL, + ASSERT_NOTEQUAL, + ASSERT_MATCH, + ASSERT_NOTMATCH, + ASSERT_FAILS, + ASSERT_OTHER, +} assert_type_T; + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "testing.c.generated.h" #endif @@ -220,11 +230,11 @@ static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, const char *e ga_concat_shorten_esc(gap, tofree); xfree(tofree); } else { - if (atype != ASSERT_INRANGE) { + if (atype == ASSERT_FAILS) { ga_concat(gap, "'"); } ga_concat_shorten_esc(gap, exp_str); - if (atype != ASSERT_INRANGE) { + if (atype == ASSERT_FAILS) { ga_concat(gap, "'"); } } @@ -622,7 +632,7 @@ void f_assert_fails(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) actual_tv.vval.v_string = actual; } fill_assert_error(&ga, &argvars[2], expected_str, - &argvars[error_found_index], &actual_tv, ASSERT_OTHER); + &argvars[error_found_index], &actual_tv, ASSERT_FAILS); ga_concat(&ga, ": "); assert_append_cmd_or_arg(&ga, argvars, cmd); assert_error(&ga); @@ -672,16 +682,9 @@ static int assert_inrange(typval_T *argvars) if (factual < flower || factual > fupper) { garray_T ga; prepare_assert_error(&ga); - if (argvars[3].v_type != VAR_UNKNOWN) { - char *const tofree = encode_tv2string(&argvars[3], NULL); - ga_concat(&ga, tofree); - xfree(tofree); - } else { - char msg[80]; - vim_snprintf(msg, sizeof(msg), "Expected range %g - %g, but got %g", - flower, fupper, factual); - ga_concat(&ga, msg); - } + char expected_str[200]; + vim_snprintf(expected_str, sizeof(expected_str), "range %g - %g,", flower, fupper); + fill_assert_error(&ga, &argvars[3], expected_str, NULL, &argvars[2], ASSERT_OTHER); assert_error(&ga); ga_clear(&ga); return 1; @@ -697,13 +700,11 @@ static int assert_inrange(typval_T *argvars) if (actual < lower || actual > upper) { garray_T ga; prepare_assert_error(&ga); - - char msg[55]; - vim_snprintf(msg, sizeof(msg), + char expected_str[200]; + vim_snprintf(expected_str, sizeof(expected_str), "range %" PRIdVARNUMBER " - %" PRIdVARNUMBER ",", lower, upper); // -V576 - fill_assert_error(&ga, &argvars[3], msg, NULL, &argvars[2], - ASSERT_INRANGE); + fill_assert_error(&ga, &argvars[3], expected_str, NULL, &argvars[2], ASSERT_OTHER); assert_error(&ga); ga_clear(&ga); return 1; diff --git a/test/old/testdir/test_assert.vim b/test/old/testdir/test_assert.vim index 087eea3bcf..57d11d0e3a 100644 --- a/test/old/testdir/test_assert.vim +++ b/test/old/testdir/test_assert.vim @@ -9,11 +9,11 @@ func Test_assert_false() call assert_equal(0, v:false->assert_false()) call assert_equal(1, assert_false(123)) - call assert_match("Expected 'False' but got 123", v:errors[0]) + call assert_match("Expected False but got 123", v:errors[0]) call remove(v:errors, 0) call assert_equal(1, 123->assert_false()) - call assert_match("Expected 'False' but got 123", v:errors[0]) + call assert_match("Expected False but got 123", v:errors[0]) call remove(v:errors, 0) endfunc @@ -24,11 +24,11 @@ func Test_assert_true() call assert_equal(0, v:true->assert_true()) call assert_equal(1, assert_true(0)) - call assert_match("Expected 'True' but got 0", v:errors[0]) + call assert_match("Expected True but got 0", v:errors[0]) call remove(v:errors, 0) call assert_equal(1, 0->assert_true()) - call assert_match("Expected 'True' but got 0", v:errors[0]) + call assert_match("Expected True but got 0", v:errors[0]) call remove(v:errors, 0) endfunc @@ -405,8 +405,11 @@ func Test_assert_inrange() call remove(v:errors, 0) " Use a custom message + call assert_equal(1, assert_inrange(5, 7, 8, "Higher")) + call assert_match("Higher: Expected range 5 - 7, but got 8", v:errors[0]) + call remove(v:errors, 0) call assert_equal(1, assert_inrange(5, 7, 8.0, "Higher")) - call assert_match("Higher", v:errors[0]) + call assert_match("Higher: Expected range 5.0 - 7.0, but got 8.0", v:errors[0]) call remove(v:errors, 0) " Invalid arguments -- cgit