diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-02-26 23:13:12 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2023-02-27 07:34:59 +0800 |
commit | 0972d7a12468d6914a70e453af85c307b167c55b (patch) | |
tree | 917696cbca5316c5eb87e0bab6cb57d14ab2dde9 /src/nvim/testdir | |
parent | 1e37703a74eeebfa14c401db865157c39f1215bf (diff) | |
download | rneovim-0972d7a12468d6914a70e453af85c307b167c55b.tar.gz rneovim-0972d7a12468d6914a70e453af85c307b167c55b.tar.bz2 rneovim-0972d7a12468d6914a70e453af85c307b167c55b.zip |
vim-patch:9.0.0196: finding value in list may require a for loop
Problem: Finding value in list may require a for loop.
Solution: Add indexof(). (Yegappan Lakshmanan, closes vim/vim#10903)
https://github.com/vim/vim/commit/b218655d5a485f5b193fb18d7240837d42b89812
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Diffstat (limited to 'src/nvim/testdir')
-rw-r--r-- | src/nvim/testdir/test_blob.vim | 27 | ||||
-rw-r--r-- | src/nvim/testdir/test_listdict.vim | 46 |
2 files changed, 73 insertions, 0 deletions
diff --git a/src/nvim/testdir/test_blob.vim b/src/nvim/testdir/test_blob.vim index 046acb81e1..4c5632c81f 100644 --- a/src/nvim/testdir/test_blob.vim +++ b/src/nvim/testdir/test_blob.vim @@ -364,4 +364,31 @@ func Test_blob2string() call assert_equal(v, string(b)) endfunc +" Test for the indexof() function +func Test_indexof() + let b = 0zdeadbeef + call assert_equal(0, indexof(b, {i, v -> v == 0xde})) + call assert_equal(3, indexof(b, {i, v -> v == 0xef})) + call assert_equal(-1, indexof(b, {i, v -> v == 0x1})) + call assert_equal(1, indexof(b, "v:val == 0xad")) + call assert_equal(-1, indexof(b, "v:val == 0xff")) + + call assert_equal(-1, indexof(0z, "v:val == 0x0")) + call assert_equal(-1, indexof(v:_null_blob, "v:val == 0xde")) + call assert_equal(-1, indexof(b, v:_null_string)) + " Nvim doesn't have null functions + " call assert_equal(-1, indexof(b, test_null_function())) + + let b = 0z01020102 + call assert_equal(1, indexof(b, "v:val == 0x02", #{startidx: 0})) + call assert_equal(2, indexof(b, "v:val == 0x01", #{startidx: -2})) + call assert_equal(-1, indexof(b, "v:val == 0x01", #{startidx: 5})) + call assert_equal(0, indexof(b, "v:val == 0x01", #{startidx: -5})) + call assert_equal(0, indexof(b, "v:val == 0x01", v:_null_dict)) + + " failure cases + call assert_fails('let i = indexof(b, "val == 0xde")', 'E121:') + call assert_fails('let i = indexof(b, {})', 'E1256:') +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/testdir/test_listdict.vim b/src/nvim/testdir/test_listdict.vim index 9ecd83265a..4c51119d16 100644 --- a/src/nvim/testdir/test_listdict.vim +++ b/src/nvim/testdir/test_listdict.vim @@ -1067,4 +1067,50 @@ func Test_null_dict() call assert_equal({}, {}) endfunc +" Test for the indexof() function +func Test_indexof() + let l = [#{color: 'red'}, #{color: 'blue'}, #{color: 'green'}] + call assert_equal(0, indexof(l, {i, v -> v.color == 'red'})) + call assert_equal(2, indexof(l, {i, v -> v.color == 'green'})) + call assert_equal(-1, indexof(l, {i, v -> v.color == 'grey'})) + call assert_equal(1, indexof(l, "v:val.color == 'blue'")) + call assert_equal(-1, indexof(l, "v:val.color == 'cyan'")) + + let l = [#{n: 10}, #{n: 10}, #{n: 20}] + call assert_equal(0, indexof(l, "v:val.n == 10", #{startidx: 0})) + call assert_equal(1, indexof(l, "v:val.n == 10", #{startidx: -2})) + call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: 4})) + call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: -4})) + call assert_equal(0, indexof(l, "v:val.n == 10", v:_null_dict)) + + call assert_equal(-1, indexof([], {i, v -> v == 'a'})) + call assert_equal(-1, indexof(v:_null_list, {i, v -> v == 'a'})) + call assert_equal(-1, indexof(l, v:_null_string)) + " Nvim doesn't have null functions + " call assert_equal(-1, indexof(l, test_null_function())) + + " failure cases + call assert_fails('let i = indexof(l, "v:val == ''cyan''")', 'E735:') + call assert_fails('let i = indexof(l, "color == ''cyan''")', 'E121:') + call assert_fails('let i = indexof(l, {})', 'E1256:') + call assert_fails('let i = indexof({}, "v:val == 2")', 'E1226:') + call assert_fails('let i = indexof([], "v:val == 2", [])', 'E1206:') + + func TestIdx(k, v) + return a:v.n == 20 + endfunc + call assert_equal(2, indexof(l, function("TestIdx"))) + delfunc TestIdx + func TestIdx(k, v) + return {} + endfunc + call assert_fails('let i = indexof(l, function("TestIdx"))', 'E728:') + delfunc TestIdx + func TestIdx(k, v) + throw "IdxError" + endfunc + call assert_fails('let i = indexof(l, function("TestIdx"))', 'E605:') + delfunc TestIdx +endfunc + " vim: shiftwidth=2 sts=2 expandtab |