diff options
| author | zeertzjq <zeertzjq@outlook.com> | 2023-02-27 08:14:10 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-27 08:14:10 +0800 |
| commit | d52a3f7c715cf4e3f2996dc16b405e1a63b7301d (patch) | |
| tree | 6ae8e0d31bc4b08ffbd931061255e6cb1ade485d /src/nvim/testdir | |
| parent | 1e37703a74eeebfa14c401db865157c39f1215bf (diff) | |
| parent | 1f8cef53deb4b91a7249affca77bfb89ce0949b4 (diff) | |
| download | rneovim-d52a3f7c715cf4e3f2996dc16b405e1a63b7301d.tar.gz rneovim-d52a3f7c715cf4e3f2996dc16b405e1a63b7301d.tar.bz2 rneovim-d52a3f7c715cf4e3f2996dc16b405e1a63b7301d.zip | |
Merge pull request #22419 from zeertzjq/vim-9.0.0196
vim-patch:9.0.{0196,partial:0202,0204}: indexof()
Diffstat (limited to 'src/nvim/testdir')
| -rw-r--r-- | src/nvim/testdir/test_blob.vim | 28 | ||||
| -rw-r--r-- | src/nvim/testdir/test_listdict.vim | 52 |
2 files changed, 80 insertions, 0 deletions
diff --git a/src/nvim/testdir/test_blob.vim b/src/nvim/testdir/test_blob.vim index 046acb81e1..cf03d2011c 100644 --- a/src/nvim/testdir/test_blob.vim +++ b/src/nvim/testdir/test_blob.vim @@ -364,4 +364,32 @@ 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(b, {_, v -> "v == 0xad"})) + + 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..37c1ee8307 100644 --- a/src/nvim/testdir/test_listdict.vim +++ b/src/nvim/testdir/test_listdict.vim @@ -1067,4 +1067,56 @@ 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)) + + let s = ["a", "b", "c"] + call assert_equal(2, indexof(s, {_, v -> v == 'c'})) + call assert_equal(-1, indexof(s, {_, v -> v == 'd'})) + call assert_equal(-1, indexof(s, {_, v -> "v == 'd'"})) + + call assert_equal(-1, indexof([], {i, v -> v == 'a'})) + call assert_equal(-1, indexof([1, 2, 3], {_, v -> "v == 2"})) + 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 |