neovim/test/old/testdir/test_method.vim

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

175 lines
5.9 KiB
VimL
Raw Normal View History

" Tests for ->method()
vim-patch:8.2.0155: MinGW warnings; tests without +float #15908 Problem: Warnings from MinGW compiler. (John Marriott) Json test fails when building without +float feature. Solution: Init variables. Fix Json parsing. Skip a few tests that require the +float feature. https://github.com/vim/vim/commit/a5d5953d59730d9bf9c00a727c4aeb56f6ffc944 Omit vim9 changes. vim9 internal implementation is N/A, similar to the `vimscript-*` features. N/A patches for version.c: vim-patch:8.1.0737: compiler warning for uninitialized variable Problem: Compiler warning for uninitialized variable. Solution: Add initialization. (John Marriott) https://github.com/vim/vim/commit/e519dfd7139d504ada44031a986482ac4fb1229a vim-patch:8.1.1385: signed/unsigned compiler warning Problem: Signed/unsigned compiler warning. Solution: Use STRLEN() instead of strlen(). https://github.com/vim/vim/commit/71de720c2c117137185a6fc233b35aab37f0d4bc vim-patch:8.2.0165: Coverity warning for using NULL pointer Problem: Coverity warning for using NULL pointer. Solution: Add missing "else". https://github.com/vim/vim/commit/5b18c248d3fe4961076dbc59c960ef60c80650f0 vim-patch:8.2.0166: Coverity warning for using uninitialized variable Problem: Coverity warning for using uninitialized variable. Solution: Check for failure. https://github.com/vim/vim/commit/07da94b0f07beb15c7e26b78837def5a91e102dc vim-patch:8.2.0167: Coverity warning for ignoring return value Problem: Coverity warning for ignoring return value. Solution: Check the return value and jump if failed. https://github.com/vim/vim/commit/58ceca5cae75ed839b20a89c5fa9998f02552f58 "src/testdir/test_vim9*.vim" files are N/A because vim9 code is currently unsupported. vim-patch:8.2.0168: Coverity warning for assigning NULL to an option Problem: Coverity warning for assigning NULL to an option. Solution: Use empty string instead of NULL. https://github.com/vim/vim/commit/97a2af39cd3249c6cbe5a5c59bc24167632d39ad vim-patch:8.2.0169: Coverity warning for dead code Problem: Coverity warning for dead code. Solution: Check if inside try-finally. https://github.com/vim/vim/commit/8cbd6dfc0c9d84c5be8414dfdea3b28b72dfddb6 vim-patch:8.2.0170: Coverity warning for ignoring return value Problem: Coverity warning for ignoring return value. Solution: Check the return value and return if failed. https://github.com/vim/vim/commit/a6d536829a2c3151f3d0faa0ecdc7b8230fb11ec vim-patch:8.2.0172: Coverity warning for not restoring character Problem: Coverity warning for not restoring character. Solution: Restore the character also in case of failure. https://github.com/vim/vim/commit/4549ece47cc8d6487d8e64ae37361fea87e3ad39 vim-patch:8.2.0254: compiler warning for checking size_t to be negative Problem: Compiler warning for checking size_t to be negative. Solution: Only check for zero. (Zoltan Arpadffy) https://github.com/vim/vim/commit/ae8d2de3a9c0436a543c7d46071104af31ff6db7 vim-patch:8.2.0326: compiler warning for using uninitialized variable Problem: Compiler warning for using uninitialized variable. (Yegappan Lakshmanan) Solution: Do not jump to failed but return. https://github.com/vim/vim/commit/d5aec0ced17f8f60761128bff32e54ad2d1d57ef vim-patch:8.2.3387: compiler warning for non-static function Problem: Compiler warning for non-static function. Solution: Make the function static. (Dominique Pellé, closes vim/vim#8816) https://github.com/vim/vim/commit/de05ae71589f46c5d27642c33fe5eb21b22aaf5d
2021-10-05 10:58:06 -07:00
source check.vim
func Test_list_method()
let l = [1, 2, 3]
call assert_equal([1, 2, 3, 4], [1, 2, 3]->add(4))
eval l->assert_equal(l)
eval l->assert_equal(l, 'wrong')
eval l->assert_notequal([3, 2, 1])
eval l->assert_notequal([3, 2, 1], 'wrong')
call assert_equal(l, l->copy())
call assert_equal(l, l->deepcopy())
call assert_equal(1, l->count(2))
call assert_false(l->empty())
call assert_true([]->empty())
call assert_equal(579, ['123', '+', '456']->join()->eval())
call assert_equal([1, 2, 3, 4, 5], [1, 2, 3]->extend([4, 5]))
call assert_equal([1, 3], [1, 2, 3]->filter('v:val != 2'))
call assert_equal(2, l->get(1))
call assert_equal(1, l->index(2))
call assert_equal([0, 1, 2, 3], [1, 2, 3]->insert(0))
call assert_fails('eval l->items()', 'E715:')
call assert_equal('1 2 3', l->join())
call assert_fails('eval l->keys()', 'E715:')
call assert_equal(3, l->len())
call assert_equal([2, 3, 4], [1, 2, 3]->map('v:val + 1'))
call assert_equal(3, l->max())
call assert_equal(1, l->min())
call assert_equal(2, [1, 2, 3]->remove(1))
call assert_equal([1, 2, 3, 1, 2, 3], l->repeat(2))
call assert_equal([3, 2, 1], [1, 2, 3]->reverse())
call assert_equal([1, 2, 3, 4], [4, 2, 3, 1]->sort())
call assert_equal('[1, 2, 3]', l->string())
call assert_equal(v:t_list, l->type())
call assert_equal([1, 2, 3], [1, 1, 2, 3, 3]->uniq())
call assert_fails('eval l->values()', 'E715:')
call assert_fails('echo []->len', 'E107:')
endfunc
func Test_dict_method()
let d = #{one: 1, two: 2, three: 3}
call assert_equal(d, d->copy())
call assert_equal(d, d->deepcopy())
call assert_equal(1, d->count(2))
call assert_false(d->empty())
call assert_true({}->empty())
call assert_equal(#{one: 1, two: 2, three: 3, four: 4}, d->extend(#{four: 4}))
call assert_equal(#{one: 1, two: 2, three: 3}, d->filter('v:val != 4'))
call assert_equal(2, d->get('two'))
call assert_fails("let x = d->index(2)", 'E897:')
call assert_fails("let x = d->insert(0)", 'E899:')
call assert_true(d->has_key('two'))
call assert_equal([['one', 1], ['two', 2], ['three', 3]], d->items())
call assert_fails("let x = d->join()", 'E714:')
call assert_equal(['one', 'two', 'three'], d->keys())
call assert_equal(3, d->len())
call assert_equal(#{one: 2, two: 3, three: 4}, d->map('v:val + 1'))
call assert_equal(#{one: 1, two: 2, three: 3}, d->map('v:val - 1'))
call assert_equal(3, d->max())
call assert_equal(1, d->min())
call assert_equal(2, d->remove("two"))
let d.two = 2
call assert_fails('let x = d->repeat(2)', 'E731:')
call assert_fails('let x = d->reverse()', 'E899:')
call assert_fails('let x = d->sort()', 'E686:')
call assert_equal("{'one': 1, 'two': 2, 'three': 3}", d->string())
call assert_equal(v:t_dict, d->type())
call assert_fails('let x = d->uniq()', 'E686:')
call assert_equal([1, 2, 3], d->values())
endfunc
func Test_string_method()
eval '1 2 3'->split()->assert_equal(['1', '2', '3'])
eval '1 2 3'->split()->map({i, v -> str2nr(v)})->assert_equal([1, 2, 3])
eval 'ABC'->str2list()->assert_equal([65, 66, 67])
eval 'ABC'->strlen()->assert_equal(3)
eval "a\rb\ec"->strtrans()->assert_equal('a^Mb^[c')
eval "aあb"->strwidth()->assert_equal(4)
eval 'abc'->substitute('b', 'x', '')->assert_equal('axc')
eval 'abc'->printf('the %s arg')->assert_equal('the abc arg')
endfunc
func Test_method_append()
new
eval ['one', 'two', 'three']->append(1)
call assert_equal(['', 'one', 'two', 'three'], getline(1, '$'))
%del
let bnr = bufnr('')
wincmd w
eval ['one', 'two', 'three']->appendbufline(bnr, 1)
call assert_equal(['', 'one', 'two', 'three'], getbufline(bnr, 1, '$'))
exe 'bwipe! ' .. bnr
endfunc
func Test_method_funcref()
func Concat(one, two, three)
return a:one .. a:two .. a:three
endfunc
let FuncRef = function('Concat')
eval 'foo'->FuncRef('bar', 'tail')->assert_equal('foobartail')
" not enough arguments
call assert_fails("eval 'foo'->FuncRef('bar')", 'E119:')
" too many arguments
call assert_fails("eval 'foo'->FuncRef('bar', 'tail', 'four')", 'E118:')
let Partial = function('Concat', ['two'])
eval 'one'->Partial('three')->assert_equal('onetwothree')
" not enough arguments
call assert_fails("eval 'one'->Partial()", 'E119:')
" too many arguments
call assert_fails("eval 'one'->Partial('three', 'four')", 'E118:')
delfunc Concat
endfunc
func Test_method_float()
vim-patch:8.2.0155: MinGW warnings; tests without +float #15908 Problem: Warnings from MinGW compiler. (John Marriott) Json test fails when building without +float feature. Solution: Init variables. Fix Json parsing. Skip a few tests that require the +float feature. https://github.com/vim/vim/commit/a5d5953d59730d9bf9c00a727c4aeb56f6ffc944 Omit vim9 changes. vim9 internal implementation is N/A, similar to the `vimscript-*` features. N/A patches for version.c: vim-patch:8.1.0737: compiler warning for uninitialized variable Problem: Compiler warning for uninitialized variable. Solution: Add initialization. (John Marriott) https://github.com/vim/vim/commit/e519dfd7139d504ada44031a986482ac4fb1229a vim-patch:8.1.1385: signed/unsigned compiler warning Problem: Signed/unsigned compiler warning. Solution: Use STRLEN() instead of strlen(). https://github.com/vim/vim/commit/71de720c2c117137185a6fc233b35aab37f0d4bc vim-patch:8.2.0165: Coverity warning for using NULL pointer Problem: Coverity warning for using NULL pointer. Solution: Add missing "else". https://github.com/vim/vim/commit/5b18c248d3fe4961076dbc59c960ef60c80650f0 vim-patch:8.2.0166: Coverity warning for using uninitialized variable Problem: Coverity warning for using uninitialized variable. Solution: Check for failure. https://github.com/vim/vim/commit/07da94b0f07beb15c7e26b78837def5a91e102dc vim-patch:8.2.0167: Coverity warning for ignoring return value Problem: Coverity warning for ignoring return value. Solution: Check the return value and jump if failed. https://github.com/vim/vim/commit/58ceca5cae75ed839b20a89c5fa9998f02552f58 "src/testdir/test_vim9*.vim" files are N/A because vim9 code is currently unsupported. vim-patch:8.2.0168: Coverity warning for assigning NULL to an option Problem: Coverity warning for assigning NULL to an option. Solution: Use empty string instead of NULL. https://github.com/vim/vim/commit/97a2af39cd3249c6cbe5a5c59bc24167632d39ad vim-patch:8.2.0169: Coverity warning for dead code Problem: Coverity warning for dead code. Solution: Check if inside try-finally. https://github.com/vim/vim/commit/8cbd6dfc0c9d84c5be8414dfdea3b28b72dfddb6 vim-patch:8.2.0170: Coverity warning for ignoring return value Problem: Coverity warning for ignoring return value. Solution: Check the return value and return if failed. https://github.com/vim/vim/commit/a6d536829a2c3151f3d0faa0ecdc7b8230fb11ec vim-patch:8.2.0172: Coverity warning for not restoring character Problem: Coverity warning for not restoring character. Solution: Restore the character also in case of failure. https://github.com/vim/vim/commit/4549ece47cc8d6487d8e64ae37361fea87e3ad39 vim-patch:8.2.0254: compiler warning for checking size_t to be negative Problem: Compiler warning for checking size_t to be negative. Solution: Only check for zero. (Zoltan Arpadffy) https://github.com/vim/vim/commit/ae8d2de3a9c0436a543c7d46071104af31ff6db7 vim-patch:8.2.0326: compiler warning for using uninitialized variable Problem: Compiler warning for using uninitialized variable. (Yegappan Lakshmanan) Solution: Do not jump to failed but return. https://github.com/vim/vim/commit/d5aec0ced17f8f60761128bff32e54ad2d1d57ef vim-patch:8.2.3387: compiler warning for non-static function Problem: Compiler warning for non-static function. Solution: Make the function static. (Dominique Pellé, closes vim/vim#8816) https://github.com/vim/vim/commit/de05ae71589f46c5d27642c33fe5eb21b22aaf5d
2021-10-05 10:58:06 -07:00
CheckFeature float
eval 1.234->string()->assert_equal('1.234')
eval -1.234->string()->assert_equal('-1.234')
endfunc
func Test_method_syntax()
eval [1, 2, 3] ->sort( )
eval [1, 2, 3]
\ ->sort(
\ )
call assert_fails('eval [1, 2, 3]-> sort()', 'E15:')
call assert_fails('eval [1, 2, 3]->sort ()', 'E274:')
call assert_fails('eval [1, 2, 3]-> sort ()', 'E15:')
endfunc
func Test_method_lambda()
eval "text"->{x -> x .. " extended"}()->assert_equal('text extended')
eval "text"->{x, y -> x .. " extended " .. y}('more')->assert_equal('text extended more')
call assert_fails('eval "text"->{x -> x .. " extended"} ()', 'E274:')
" todo: lambda accepts more arguments than it consumes
" call assert_fails('eval "text"->{x -> x .. " extended"}("more")', 'E99:')
" Nvim doesn't include test_refcount().
" let l = [1, 2, 3]
" eval l->{x -> x}()
" call assert_equal(1, test_refcount(l))
endfunc
func Test_method_not_supported()
call assert_fails('eval 123->changenr()', 'E276:')
call assert_fails('echo "abc"->invalidfunc()', 'E117:')
" Test for too many or too few arguments to a method
call assert_fails('let n="abc"->len(2)', 'E118:')
call assert_fails('let n=10->setwinvar()', 'E119:')
endfunc
" Test for passing optional arguments to methods
func Test_method_args()
let v:errors = []
let n = 10->assert_inrange(1, 5, "Test_assert_inrange")
if v:errors[0] !~ 'Test_assert_inrange'
call assert_report(v:errors[0])
else
" Test passed
let v:errors = []
endif
endfunc
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker