comparison src/testdir/test_listdict.vim @ 29712:bdb31515f78b v9.0.0196

patch 9.0.0196: finding value in list may require a for loop Commit: https://github.com/vim/vim/commit/b218655d5a485f5b193fb18d7240837d42b89812 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sat Aug 13 13:09:20 2022 +0100 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 https://github.com/vim/vim/issues/10903)
author Bram Moolenaar <Bram@vim.org>
date Sat, 13 Aug 2022 14:15:05 +0200
parents 35e24d9de858
children cadc9851377d
comparison
equal deleted inserted replaced
29711:4069513e8995 29712:bdb31515f78b
1444 lockvar d 1444 lockvar d
1445 call assert_equal(1, islocked('d')) 1445 call assert_equal(1, islocked('d'))
1446 unlockvar d 1446 unlockvar d
1447 endfunc 1447 endfunc
1448 1448
1449 " Test for the indexof() function
1450 func Test_indexof()
1451 let l = [#{color: 'red'}, #{color: 'blue'}, #{color: 'green'}]
1452 call assert_equal(0, indexof(l, {i, v -> v.color == 'red'}))
1453 call assert_equal(2, indexof(l, {i, v -> v.color == 'green'}))
1454 call assert_equal(-1, indexof(l, {i, v -> v.color == 'grey'}))
1455 call assert_equal(1, indexof(l, "v:val.color == 'blue'"))
1456 call assert_equal(-1, indexof(l, "v:val.color == 'cyan'"))
1457
1458 let l = [#{n: 10}, #{n: 10}, #{n: 20}]
1459 call assert_equal(0, indexof(l, "v:val.n == 10", #{startidx: 0}))
1460 call assert_equal(1, indexof(l, "v:val.n == 10", #{startidx: -2}))
1461 call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: 4}))
1462 call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: -4}))
1463 call assert_equal(0, indexof(l, "v:val.n == 10", test_null_dict()))
1464
1465 call assert_equal(-1, indexof([], {i, v -> v == 'a'}))
1466 call assert_equal(-1, indexof(test_null_list(), {i, v -> v == 'a'}))
1467 call assert_equal(-1, indexof(l, test_null_string()))
1468 call assert_equal(-1, indexof(l, test_null_function()))
1469
1470 " failure cases
1471 call assert_fails('let i = indexof(l, "v:val == ''cyan''")', 'E735:')
1472 call assert_fails('let i = indexof(l, "color == ''cyan''")', 'E121:')
1473 call assert_fails('let i = indexof(l, {})', 'E1256:')
1474 call assert_fails('let i = indexof({}, "v:val == 2")', 'E1226:')
1475 call assert_fails('let i = indexof([], "v:val == 2", [])', 'E1206:')
1476
1477 func TestIdx(k, v)
1478 return a:v.n == 20
1479 endfunc
1480 call assert_equal(2, indexof(l, function("TestIdx")))
1481 delfunc TestIdx
1482 func TestIdx(k, v)
1483 return {}
1484 endfunc
1485 call assert_fails('let i = indexof(l, function("TestIdx"))', 'E728:')
1486 delfunc TestIdx
1487 func TestIdx(k, v)
1488 throw "IdxError"
1489 endfunc
1490 call assert_fails('let i = indexof(l, function("TestIdx"))', 'E605:')
1491 delfunc TestIdx
1492 endfunc
1493
1449 " vim: shiftwidth=2 sts=2 expandtab 1494 " vim: shiftwidth=2 sts=2 expandtab