0
0
mirror of https://github.com/vim/vim.git synced 2025-10-04 05:25:06 -04:00

patch 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 #5815)
This commit is contained in:
Bram Moolenaar
2020-03-20 18:20:51 +01:00
parent 98be7fecac
commit 8b63313510
18 changed files with 228 additions and 53 deletions

View File

@@ -87,6 +87,7 @@ func Test_blob_get_range()
call assert_equal(0z0011223344, b[:]) call assert_equal(0z0011223344, b[:])
call assert_equal(0z0011223344, b[:-1]) call assert_equal(0z0011223344, b[:-1])
call assert_equal(0z, b[5:6]) call assert_equal(0z, b[5:6])
call assert_equal(0z0011, b[-10:1])
endfunc endfunc
func Test_blob_get() func Test_blob_get()

View File

@@ -518,6 +518,15 @@ func Test_raw_pipe()
endif endif
endfor endfor
call assert_equal(1, found) call assert_equal(1, found)
" Try to use the job and channel where a number is expected. This is not
" related to testing the raw pipe. This test is here just to reuse the
" already created job/channel.
let ch = job_getchannel(job)
call assert_fails('let i = job + 1', 'E910:')
call assert_fails('let j = ch + 1', 'E913:')
call assert_fails('echo 2.0 == job', 'E911:')
call assert_fails('echo 2.0 == ch', 'E914:')
endfunc endfunc
func Test_raw_pipe_blob() func Test_raw_pipe_blob()
@@ -2025,3 +2034,5 @@ func Test_issue_5485()
call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))}) call WaitForAssert({-> assert_equal("local local", trim(g:Ch_reply))})
unlet $VAR1 unlet $VAR1
endfunc endfunc
" vim: shiftwidth=2 sts=2 expandtab

View File

@@ -195,35 +195,6 @@ func Test_highlight_completion()
call assert_equal([], getcompletion('A', 'highlight')) call assert_equal([], getcompletion('A', 'highlight'))
endfunc 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() func Test_getcompletion()
if !has('cmdline_compl') if !has('cmdline_compl')
return return
@@ -612,7 +583,7 @@ func Test_cmdline_complete_bang()
endif endif
endfunc endfunc
funct Test_cmdline_complete_languages() func Test_cmdline_complete_languages()
let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '') let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx') call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
@@ -636,10 +607,8 @@ endfunc
func Test_cmdline_complete_env_variable() func Test_cmdline_complete_env_variable()
let $X_VIM_TEST_COMPLETE_ENV = 'foo' let $X_VIM_TEST_COMPLETE_ENV = 'foo'
call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx') call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:) call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
unlet $X_VIM_TEST_COMPLETE_ENV unlet $X_VIM_TEST_COMPLETE_ENV
endfunc endfunc
@@ -781,17 +750,13 @@ func Test_cmdline_complete_various()
call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt') call feedkeys(":e `a1b2c\t\<C-B>\"\<CR>", 'xt')
call assert_equal('"e `a1b2c', @:) call assert_equal('"e `a1b2c', @:)
" completion for the expression register
call feedkeys(":\"\<C-R>=float2\t\"\<C-B>\"\<CR>", 'xt')
call assert_equal('"float2nr("', @=)
" completion for :language command with an invalid argument " completion for :language command with an invalid argument
call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt') call feedkeys(":language dummy \t\<C-B>\"\<CR>", 'xt')
call assert_equal("\"language dummy \t", @:) call assert_equal("\"language dummy \t", @:)
" completion for commands after a :global command " completion for commands after a :global command
call feedkeys(":g/a\\xb/call float2\t\<C-B>\"\<CR>", 'xt') call feedkeys(":g/a\\xb/clearj\t\<C-B>\"\<CR>", 'xt')
call assert_equal('"g/a\xb/call float2nr(', @:) call assert_equal('"g/a\xb/clearjumps', @:)
" completion with ambiguous user defined commands " completion with ambiguous user defined commands
com TCmd1 echo 'TCmd1' com TCmd1 echo 'TCmd1'
@@ -804,20 +769,6 @@ func Test_cmdline_complete_various()
" completion after a range followed by a pipe (|) character " completion after a range followed by a pipe (|) character
call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt') call feedkeys(":1,10 | chist\t\<C-B>\"\<CR>", 'xt')
call assert_equal('"1,10 | chistory', @:) call assert_equal('"1,10 | chistory', @:)
" 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
endfunc endfunc
func Test_cmdline_write_alternatefile() func Test_cmdline_write_alternatefile()

View File

@@ -234,3 +234,5 @@ func Test_numbersize()
" correctly. " correctly.
call assert_equal(64, v:numbersize) call assert_equal(64, v:numbersize)
endfunc endfunc
" vim: shiftwidth=2 sts=2 expandtab

View File

@@ -73,6 +73,8 @@ func Test_strcharpart()
call assert_equal('', strcharpart('axb', -2, 2)) call assert_equal('', strcharpart('axb', -2, 2))
call assert_equal('a', strcharpart('axb', -1, 2)) call assert_equal('a', strcharpart('axb', -1, 2))
call assert_equal('edit', "editor"[-10:3])
endfunc endfunc
func Test_getreg_empty_list() func Test_getreg_empty_list()
@@ -476,6 +478,7 @@ func Test_function_with_funcref()
call assert_fails("call function('foo()')", 'E475:') call assert_fails("call function('foo()')", 'E475:')
call assert_fails("call function('foo()')", 'foo()') call assert_fails("call function('foo()')", 'foo()')
call assert_fails("function('')", 'E129:')
endfunc endfunc
func Test_funcref() func Test_funcref()
@@ -533,3 +536,95 @@ func Test_eval_after_if()
if 0 | eval SetVal('a') | endif | call SetVal('b') if 0 | eval SetVal('a') | endif | call SetVal('b')
call assert_equal('b', s:val) call assert_equal('b', s:val)
endfunc endfunc
" Test for command-line completion of expressions
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
" 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

View File

@@ -1145,6 +1145,7 @@ func Test_col()
norm gg4|mx6|mY2| norm gg4|mx6|mY2|
call assert_equal(2, col('.')) call assert_equal(2, col('.'))
call assert_equal(7, col('$')) call assert_equal(7, col('$'))
call assert_equal(2, col('v'))
call assert_equal(4, col("'x")) call assert_equal(4, col("'x"))
call assert_equal(6, col("'Y")) call assert_equal(6, col("'Y"))
call assert_equal(2, [1, 2]->col()) call assert_equal(2, [1, 2]->col())
@@ -1155,6 +1156,19 @@ func Test_col()
call assert_equal(0, col([2, '$'])) call assert_equal(0, col([2, '$']))
call assert_equal(0, col([1, 100])) call assert_equal(0, col([1, 100]))
call assert_equal(0, col([1])) call assert_equal(0, col([1]))
" test for getting the visual start column
func T()
let g:Vcol = col('v')
return ''
endfunc
let g:Vcol = 0
xmap <expr> <F2> T()
exe "normal gg3|ve\<F2>"
call assert_equal(3, g:Vcol)
xunmap <F2>
delfunc T
bw! bw!
endfunc endfunc

View File

@@ -1,6 +1,6 @@
" This test is in a separate file, because it usually causes reports for memory " This test is in a separate file, because it usually causes reports for memory
" leaks under valgrind. That is because when fork/exec fails memory is not " leaks under valgrind. That is because when fork/exec fails memory is not
" freed. Since the process exists right away it's not a real leak. " freed. Since the process exits right away it's not a real leak.
source shared.vim source shared.vim
@@ -14,3 +14,5 @@ func Test_job_start_fails()
endif endif
endif endif
endfunc endfunc
" vim: shiftwidth=2 sts=2 expandtab

View File

@@ -63,6 +63,7 @@ function Test_lambda_fails()
call assert_equal(3, {a, b -> a + b}(1, 2)) call assert_equal(3, {a, b -> a + b}(1, 2))
call assert_fails('echo {a, a -> a + a}(1, 2)', 'E853:') call assert_fails('echo {a, a -> a + a}(1, 2)', 'E853:')
call assert_fails('echo {a, b -> a + b)}(1, 2)', 'E15:') call assert_fails('echo {a, b -> a + b)}(1, 2)', 'E15:')
echo assert_fails('echo 10->{a -> a + 2}', 'E107:')
endfunc endfunc
func Test_not_lamda() func Test_not_lamda()
@@ -309,3 +310,5 @@ func Test_lambda_error()
" This was causing a crash " This was causing a crash
call assert_fails('ec{@{->{d->()()', 'E15') call assert_fails('ec{@{->{d->()()', 'E15')
endfunc endfunc
" vim: shiftwidth=2 sts=2 expandtab

View File

@@ -275,6 +275,17 @@ func Test_let_errors()
let s = "var" let s = "var"
let var = 1 let var = 1
call assert_fails('let {s}.1 = 2', 'E18:') call assert_fails('let {s}.1 = 2', 'E18:')
call assert_fails('let a[1] = 5', 'E121:')
let l = [[1,2]]
call assert_fails('let l[:][0] = [5]', 'E708:')
let d = {'k' : 4}
call assert_fails('let d.# = 5', 'E713:')
call assert_fails('let d.m += 5', 'E734:')
let l = [1, 2]
call assert_fails('let l[2] = 0', 'E684:')
call assert_fails('let l[0:1] = [1, 2, 3]', 'E710:')
call assert_fails('let l[-2:-3] = [3, 4]', 'E684:')
call assert_fails('let l[0:4] = [5, 6]', 'E711:')
" This test works only when the language is English " This test works only when the language is English
if v:lang == "C" || v:lang =~ '^[Ee]n' if v:lang == "C" || v:lang =~ '^[Ee]n'

View File

@@ -31,6 +31,7 @@ func Test_list_slice()
call assert_equal([1, 'as''d', [1, 2, function('strlen')]], l[:-2]) call assert_equal([1, 'as''d', [1, 2, function('strlen')]], l[:-2])
call assert_equal([1, 'as''d', [1, 2, function('strlen')], {'a': 1}], l[0:8]) call assert_equal([1, 'as''d', [1, 2, function('strlen')], {'a': 1}], l[0:8])
call assert_equal([], l[8:-1]) call assert_equal([], l[8:-1])
call assert_equal([], l[0:-10])
endfunc endfunc
" List identity " List identity
@@ -104,6 +105,8 @@ func Test_list_range_assign()
let l = [0] let l = [0]
let l[:] = [1, 2] let l[:] = [1, 2]
call assert_equal([1, 2], l) call assert_equal([1, 2], l)
let l[-4:-1] = [5, 6]
call assert_equal([5, 6], l)
endfunc endfunc
" Test removing items in list " Test removing items in list
@@ -648,6 +651,12 @@ func Test_listdict_compare()
call assert_true(d == d) call assert_true(d == d)
call assert_false(l != deepcopy(l)) call assert_false(l != deepcopy(l))
call assert_false(d != deepcopy(d)) call assert_false(d != deepcopy(d))
" comparison errors
call assert_fails('echo [1, 2] =~ {}', 'E691:')
call assert_fails('echo [1, 2] =~ [1, 2]', 'E692:')
call assert_fails('echo {} =~ 5', 'E735:')
call assert_fails('echo {} =~ {}', 'E736:')
endfunc endfunc
" compare complex recursively linked list and dict " compare complex recursively linked list and dict
@@ -842,4 +851,17 @@ func Test_deep_nested_dict()
unlet deep_dict unlet deep_dict
endfunc endfunc
" List and dict indexing tests
func Test_listdict_index()
call assert_fails('echo function("min")[0]', 'E695:')
call assert_fails('echo v:true[0]', 'E909:')
let d = {'k' : 10}
call assert_fails('echo d.', 'E15:')
call assert_fails('echo d[1:2]', 'E719:')
call assert_fails("let v = [4, 6][{-> 1}]", 'E729:')
call assert_fails("let v = range(5)[2:[]]", 'E730:')
call assert_fails("let v = range(5)[2:{-> 2}(]", 'E116:')
call assert_fails("let v = range(5)[2:3", 'E111:')
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab

View File

@@ -81,6 +81,15 @@ func Test_setpos()
call assert_equal([0, 1, 21341234, 0], getpos("'a")) call assert_equal([0, 1, 21341234, 0], getpos("'a"))
call assert_equal(4, virtcol("'a")) call assert_equal(4, virtcol("'a"))
" Test with invalid buffer number, line number and column number
call cursor(2, 2)
call setpos('.', [-1, 1, 1, 0])
call assert_equal([2, 2], [line('.'), col('.')])
call setpos('.', [0, -1, 1, 0])
call assert_equal([2, 2], [line('.'), col('.')])
call setpos('.', [0, 1, -1, 0])
call assert_equal([2, 2], [line('.'), col('.')])
bwipe! bwipe!
call win_gotoid(twowin) call win_gotoid(twowin)
bwipe! bwipe!

View File

@@ -35,6 +35,7 @@ func Test_list_method()
call assert_equal(v:t_list, l->type()) call assert_equal(v:t_list, l->type())
call assert_equal([1, 2, 3], [1, 1, 2, 3, 3]->uniq()) call assert_equal([1, 2, 3], [1, 1, 2, 3, 3]->uniq())
call assert_fails('eval l->values()', 'E715:') call assert_fails('eval l->values()', 'E715:')
call assert_fails('echo []->len', 'E107:')
endfunc endfunc
func Test_dict_method() func Test_dict_method()
@@ -152,3 +153,5 @@ endfunc
func Test_method_not_supported() func Test_method_not_supported()
call assert_fails('eval 123->changenr()', 'E276:') call assert_fails('eval 123->changenr()', 'E276:')
endfunc endfunc
" vim: shiftwidth=2 sts=2 expandtab

View File

@@ -420,6 +420,7 @@ func Test_normal10_expand()
" Test expand(`=...`) i.e. backticks expression expansion " Test expand(`=...`) i.e. backticks expression expansion
call assert_equal('5', expand('`=2+3`')) call assert_equal('5', expand('`=2+3`'))
call assert_equal('3.14', expand('`=3.14`'))
" clean up " clean up
bw! bw!

View File

@@ -21,6 +21,12 @@ endfunc
func Test_unlet_fails() func Test_unlet_fails()
call assert_fails('unlet v:["count"]', 'E46:') call assert_fails('unlet v:["count"]', 'E46:')
call assert_fails('unlet $', 'E475:') call assert_fails('unlet $', 'E475:')
let v = {}
call assert_fails('unlet v[:]', 'E719:')
let l = []
call assert_fails("unlet l['k'", 'E111:')
let d = {'k' : 1}
call assert_fails("unlet d.k2", 'E716:')
endfunc endfunc
func Test_unlet_env() func Test_unlet_env()

View File

@@ -549,4 +549,25 @@ func Test_command_list()
call assert_equal("\nNo user-defined commands found", execute('command')) call assert_equal("\nNo user-defined commands found", execute('command'))
endfunc endfunc
" Test for a custom user completion returning the wrong value type
func Test_usercmd_custom()
func T1(a, c, p)
return "a\nb\n"
endfunc
command -nargs=* -complete=customlist,T1 TCmd1
call feedkeys(":T1 \<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"T1 ', @:)
delcommand TCmd1
delfunc T1
func T2(a, c, p)
return ['a', 'b', 'c']
endfunc
command -nargs=* -complete=customlist,T2 TCmd2
call feedkeys(":T2 \<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"T2 ', @:)
delcommand TCmd2
delfunc T2
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab

View File

@@ -1737,6 +1737,8 @@ func Test_compound_assignment_operators()
call assert_equal(4.2, x) call assert_equal(4.2, x)
call assert_fails('let x %= 0.5', 'E734') call assert_fails('let x %= 0.5', 'E734')
call assert_fails('let x .= "f"', 'E734') call assert_fails('let x .= "f"', 'E734')
let x = !3.14
call assert_equal(0.0, x)
endif endif
" Test for environment variable " Test for environment variable
@@ -2083,6 +2085,20 @@ func Test_sfile_in_function()
delfunc Xfunc delfunc Xfunc
endfunc endfunc
" Test for errors in converting to float from various types {{{1
func Test_float_conversion_errors()
if has('float')
call assert_fails('let x = 4.0 % 2.0', 'E804')
call assert_fails('echo 1.1[0]', 'E806')
call assert_fails('echo sort([function("min"), 1], "f")', 'E891:')
call assert_fails('echo 3.2 == "vim"', 'E892:')
call assert_fails('echo sort([[], 1], "f")', 'E893:')
call assert_fails('echo sort([{}, 1], "f")', 'E894:')
call assert_fails('echo 3.2 == v:true', 'E362:')
call assert_fails('echo 3.2 == v:none', 'E907:')
endif
endfunc
"------------------------------------------------------------------------------- "-------------------------------------------------------------------------------
" Modelines {{{1 " Modelines {{{1
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

View File

@@ -546,6 +546,11 @@ func Test_window_contents()
call assert_equal(59, line("w0")) call assert_equal(59, line("w0"))
call assert_equal('59 ', s3) call assert_equal('59 ', s3)
%d
call setline(1, ['one', 'two', 'three'])
call assert_equal(1, line('w0'))
call assert_equal(3, line('w$'))
bwipeout! bwipeout!
call test_garbagecollect_now() call test_garbagecollect_now()
endfunc endfunc

View File

@@ -738,6 +738,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
418,
/**/ /**/
417, 417,
/**/ /**/