vim-patch:partial:8.2.0418: code in eval.c not sufficiently covered by tests

Problem:    Code in eval.c not sufficiently covered by tests.
Solution:   Add more tests. (Yegappan Lakshmanan, closes vim/vim#5815)
8b63313510

Only port test_expr.vim and the first hunk of test_cmdline.vim.
Add missing test from patch 7.4.1755.
Cherry-pick test_expr.vim change from patch 8.2.2060.
This commit is contained in:
zeertzjq
2022-10-20 21:50:27 +08:00
parent e6917306f6
commit be0f284ae1
2 changed files with 104 additions and 29 deletions

View File

@@ -349,35 +349,6 @@ func Test_highlight_completion()
call assert_equal([], getcompletion('A', 'highlight'))
endfunc
func Test_expr_completion()
if !has('cmdline_compl')
return
endif
for cmd in [
\ 'let a = ',
\ 'const a = ',
\ 'if',
\ 'elseif',
\ 'while',
\ 'for',
\ 'echo',
\ 'echon',
\ 'execute',
\ 'echomsg',
\ 'echoerr',
\ 'call',
\ 'return',
\ 'cexpr',
\ 'caddexpr',
\ 'cgetexpr',
\ 'lexpr',
\ 'laddexpr',
\ 'lgetexpr']
call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"' . cmd . ' getline(', getreg(':'))
endfor
endfunc
func Test_getcompletion()
if !has('cmdline_compl')
return

View File

@@ -1,5 +1,7 @@
" Tests for expressions.
source check.vim
func Test_equal()
let base = {}
func base.method()
@@ -76,6 +78,17 @@ func Test_strcharpart()
call assert_equal('', strcharpart('axb', -2, 2))
call assert_equal('a', strcharpart('axb', -1, 2))
call assert_equal('edit', "editor"[-10:3])
endfunc
func Test_getreg_empty_list()
call assert_equal('', getreg('x'))
call assert_equal([], getreg('x', 1, 1))
let x = getreg('x', 1, 1)
let y = x
call add(x, 'foo')
call assert_equal(['foo'], y)
endfunc
func Test_loop_over_null_list()
@@ -529,6 +542,7 @@ func Test_function_with_funcref()
call assert_fails("call function('foo()')", 'E475:')
call assert_fails("call function('foo()')", 'foo()')
call assert_fails("function('')", 'E129:')
endfunc
func Test_funcref()
@@ -587,3 +601,93 @@ func Test_eval_after_if()
if 0 | eval SetVal('a') | endif | call SetVal('b')
call assert_equal('b', s:val)
endfunc
" Test for command-line completion of expressions
func Test_expr_completion()
CheckFeature cmdline_compl
for cmd in [
\ 'let a = ',
\ 'const a = ',
\ 'if',
\ 'elseif',
\ 'while',
\ 'for',
\ 'echo',
\ 'echon',
\ 'execute',
\ 'echomsg',
\ 'echoerr',
\ 'call',
\ 'return',
\ 'cexpr',
\ 'caddexpr',
\ 'cgetexpr',
\ 'lexpr',
\ 'laddexpr',
\ 'lgetexpr']
call feedkeys(":" . cmd . " getl\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"' . cmd . ' getline(', getreg(':'))
endfor
" completion for the expression register
call feedkeys(":\"\<C-R>=float2\t\"\<C-B>\"\<CR>", 'xt')
call assert_equal('"float2nr("', @=)
" completion for window local variables
let w:wvar1 = 10
let w:wvar2 = 10
call feedkeys(":echo w:wvar\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"echo w:wvar1 w:wvar2', @:)
unlet w:wvar1 w:wvar2
" completion for tab local variables
let t:tvar1 = 10
let t:tvar2 = 10
call feedkeys(":echo t:tvar\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"echo t:tvar1 t:tvar2', @:)
unlet t:tvar1 t:tvar2
" completion for variables
let g:tvar1 = 1
let g:tvar2 = 2
call feedkeys(":let g:tv\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"let g:tvar1 g:tvar2', @:)
" completion for variables after a ||
call feedkeys(":echo 1 || g:tv\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"echo 1 || g:tvar1 g:tvar2', @:)
" completion for options
call feedkeys(":echo &compat\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"echo &compatible', @:)
call feedkeys(":echo 1 && &compat\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"echo 1 && &compatible', @:)
call feedkeys(":echo &g:equala\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"echo &g:equalalways', @:)
" completion for string
call feedkeys(":echo \"Hello\\ World\"\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"echo \"Hello\\ World\"\<C-A>", @:)
call feedkeys(":echo 'Hello World'\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"echo 'Hello World'\<C-A>", @:)
" completion for command after a |
call feedkeys(":echo 'Hello' | cwin\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"echo 'Hello' | cwindow", @:)
" completion for environment variable
let $X_VIM_TEST_COMPLETE_ENV = 'foo'
call feedkeys(":let $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
call assert_match('"let $X_VIM_TEST_COMPLETE_ENV', @:)
unlet $X_VIM_TEST_COMPLETE_ENV
endfunc
" Test for errors in expression evaluation
func Test_expr_eval_error()
call assert_fails("let i = 'abc' . []", 'E730:')
call assert_fails("let l = [] + 10", 'E745:')
call assert_fails("let v = 10 + []", 'E745:')
call assert_fails("let v = 10 / []", 'E745:')
call assert_fails("let v = -{}", 'E728:')
endfunc
" vim: shiftwidth=2 sts=2 expandtab