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)

e24b5e0b0f

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 <yegappan@yahoo.com>
This commit is contained in:
zeertzjq
2023-05-05 12:40:31 +08:00
parent 5fb6b3431a
commit 050b24cbcc
4 changed files with 187 additions and 19 deletions

View File

@@ -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;

View File

@@ -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(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(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.

View File

@@ -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()

View File

@@ -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')