Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 1 | " Tests for fuzzy matching |
| 2 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 3 | " Test for matchfuzzy() |
| 4 | func Test_matchfuzzy() |
| 5 | call assert_fails('call matchfuzzy(10, "abc")', 'E686:') |
| 6 | call assert_fails('call matchfuzzy(["abc"], [])', 'E730:') |
| 7 | call assert_fails("let x = matchfuzzy(test_null_list(), 'foo')", 'E686:') |
| 8 | call assert_fails('call matchfuzzy(["abc"], test_null_string())', 'E475:') |
| 9 | call assert_equal([], matchfuzzy([], 'abc')) |
| 10 | call assert_equal([], matchfuzzy(['abc'], '')) |
| 11 | call assert_equal(['abc'], matchfuzzy(['abc', 10], 'ac')) |
| 12 | call assert_equal([], matchfuzzy([10, 20], 'ac')) |
| 13 | call assert_equal(['abc'], matchfuzzy(['abc'], 'abc')) |
| 14 | call assert_equal(['crayon', 'camera'], matchfuzzy(['camera', 'crayon'], 'cra')) |
| 15 | call assert_equal(['aabbaa', 'aaabbbaaa', 'aaaabbbbaaaa', 'aba'], matchfuzzy(['aba', 'aabbaa', 'aaabbbaaa', 'aaaabbbbaaaa'], 'aa')) |
| 16 | call assert_equal(['one'], matchfuzzy(['one', 'two'], 'one')) |
| 17 | call assert_equal(['oneTwo', 'onetwo'], matchfuzzy(['onetwo', 'oneTwo'], 'oneTwo')) |
Bram Moolenaar | e9f9f16 | 2020-10-20 19:01:30 +0200 | [diff] [blame] | 18 | call assert_equal(['onetwo', 'one_two'], matchfuzzy(['onetwo', 'one_two'], 'oneTwo')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 19 | call assert_equal(['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], matchfuzzy(['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], 'aa')) |
| 20 | call assert_equal(256, matchfuzzy([repeat('a', 256)], repeat('a', 256))[0]->len()) |
| 21 | call assert_equal([], matchfuzzy([repeat('a', 300)], repeat('a', 257))) |
glepnir | 5a04999 | 2024-12-26 15:38:39 +0100 | [diff] [blame] | 22 | " full match has highest score |
| 23 | call assert_equal(['Cursor', 'lCursor'], matchfuzzy(["hello", "lCursor", "Cursor"], "Cursor")) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 24 | " matches with same score should not be reordered |
| 25 | let l = ['abc1', 'abc2', 'abc3'] |
| 26 | call assert_equal(l, l->matchfuzzy('abc')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 27 | |
| 28 | " Tests for match preferences |
| 29 | " preference for camel case match |
| 30 | call assert_equal(['oneTwo', 'onetwo'], ['onetwo', 'oneTwo']->matchfuzzy('onetwo')) |
| 31 | " preference for match after a separator (_ or space) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 32 | call assert_equal(['onetwo', 'one_two', 'one two'], ['onetwo', 'one_two', 'one two']->matchfuzzy('onetwo')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 33 | " preference for leading letter match |
| 34 | call assert_equal(['onetwo', 'xonetwo'], ['xonetwo', 'onetwo']->matchfuzzy('onetwo')) |
| 35 | " preference for sequential match |
| 36 | call assert_equal(['onetwo', 'oanbectdweo'], ['oanbectdweo', 'onetwo']->matchfuzzy('onetwo')) |
| 37 | " non-matching leading letter(s) penalty |
| 38 | call assert_equal(['xonetwo', 'xxonetwo'], ['xxonetwo', 'xonetwo']->matchfuzzy('onetwo')) |
| 39 | " total non-matching letter(s) penalty |
| 40 | call assert_equal(['one', 'onex', 'onexx'], ['onexx', 'one', 'onex']->matchfuzzy('one')) |
Bram Moolenaar | e9f9f16 | 2020-10-20 19:01:30 +0200 | [diff] [blame] | 41 | " prefer complete matches over separator matches |
| 42 | call assert_equal(['.vim/vimrc', '.vim/vimrc_colors', '.vim/v_i_m_r_c'], ['.vim/vimrc', '.vim/vimrc_colors', '.vim/v_i_m_r_c']->matchfuzzy('vimrc')) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 43 | " gap penalty |
| 44 | call assert_equal(['xxayybxxxx', 'xxayyybxxx', 'xxayyyybxx'], ['xxayyyybxx', 'xxayyybxxx', 'xxayybxxxx']->matchfuzzy('ab')) |
Bram Moolenaar | dcdd42a | 2020-10-29 18:58:01 +0100 | [diff] [blame] | 45 | " path separator vs word separator |
| 46 | call assert_equal(['color/setup.vim', 'color\\setup.vim', 'color setup.vim', 'color_setup.vim', 'colorsetup.vim'], matchfuzzy(['colorsetup.vim', 'color setup.vim', 'color/setup.vim', 'color_setup.vim', 'color\\setup.vim'], 'setup.vim')) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 47 | |
| 48 | " match multiple words (separated by space) |
| 49 | call assert_equal(['foo bar baz'], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('baz foo')) |
| 50 | call assert_equal([], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('one two')) |
| 51 | call assert_equal([], ['foo bar']->matchfuzzy(" \t ")) |
| 52 | |
| 53 | " test for matching a sequence of words |
| 54 | call assert_equal(['bar foo'], ['foo bar', 'bar foo', 'foobar', 'barfoo']->matchfuzzy('bar foo', {'matchseq' : 1})) |
| 55 | call assert_equal([#{text: 'two one'}], [#{text: 'one two'}, #{text: 'two one'}]->matchfuzzy('two one', #{key: 'text', matchseq: v:true})) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 56 | |
| 57 | %bw! |
| 58 | eval ['somebuf', 'anotherone', 'needle', 'yetanotherone']->map({_, v -> bufadd(v) + bufload(v)}) |
Bram Moolenaar | 22e7e86 | 2022-07-02 20:48:01 +0100 | [diff] [blame] | 59 | let l = getbufinfo()->map({_, v -> fnamemodify(v.name, ':t')})->matchfuzzy('ndl') |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 60 | call assert_equal(1, len(l)) |
| 61 | call assert_match('needle', l[0]) |
| 62 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 63 | " Test for fuzzy matching dicts |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 64 | let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}] |
| 65 | call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'text_cb' : {v -> v.val}})) |
| 66 | call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'key' : 'val'})) |
| 67 | call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> v.val}})) |
| 68 | call assert_equal([], matchfuzzy(l, 'day', {'key' : 'val'})) |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 69 | call assert_fails("let x = matchfuzzy(l, 'cam', 'random')", 'E1206:') |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 70 | call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> []}})) |
| 71 | call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> 1}})) |
| 72 | call assert_fails("let x = matchfuzzy(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:') |
| 73 | call assert_equal([], matchfuzzy(l, 'cam')) |
| 74 | call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E921:') |
zeertzjq | c4815c1 | 2025-03-18 20:28:00 +0100 | [diff] [blame] | 75 | call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : 123})", 'E475: Invalid value for argument key: 123') |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 76 | call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E730:') |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 77 | call assert_fails("let x = matchfuzzy(l, 'cam', test_null_dict())", 'E1297:') |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 78 | call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : test_null_string()})", 'E475:') |
| 79 | call assert_fails("let x = matchfuzzy(l, 'foo', {'text_cb' : test_null_function()})", 'E475:') |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 80 | " matches with same score should not be reordered |
| 81 | let l = [#{text: 'abc', id: 1}, #{text: 'abc', id: 2}, #{text: 'abc', id: 3}] |
| 82 | call assert_equal(l, l->matchfuzzy('abc', #{key: 'text'})) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 83 | |
| 84 | let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}] |
| 85 | call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : 'name'})", 'E730:') |
| 86 | |
zeertzjq | 8562773 | 2025-03-17 21:06:02 +0100 | [diff] [blame] | 87 | " camelcase |
zeertzjq | 8562773 | 2025-03-17 21:06:02 +0100 | [diff] [blame] | 88 | call assert_equal(['lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'Cursor', 'CurSearch', 'CursorLine'], |
| 89 | \ matchfuzzy(['Cursor', 'lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'CurSearch', 'CursorLine'], 'Cur')) |
zeertzjq | c4815c1 | 2025-03-18 20:28:00 +0100 | [diff] [blame] | 90 | call assert_equal(['lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'Cursor', 'CurSearch', 'CursorLine'], |
| 91 | \ matchfuzzy(['Cursor', 'lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'CurSearch', 'CursorLine'], 'Cur', {"camelcase": v:true})) |
| 92 | call assert_equal(['Cursor', 'CurSearch', 'CursorLine', 'lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor'], |
| 93 | \ matchfuzzy(['Cursor', 'lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'CurSearch', 'CursorLine'], 'Cur', {"camelcase": v:false})) |
| 94 | call assert_equal(['things', 'sThings', 'thisThings'], |
| 95 | \ matchfuzzy(['things','sThings', 'thisThings'], 'thin', {'camelcase': v:false})) |
| 96 | call assert_fails("let x = matchfuzzy([], 'foo', {'camelcase': []})", 'E475: Invalid value for argument camelcase') |
glepnir | 28e40a7 | 2025-03-16 21:24:22 +0100 | [diff] [blame] | 97 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 98 | " Test in latin1 encoding |
| 99 | let save_enc = &encoding |
| 100 | set encoding=latin1 |
| 101 | call assert_equal(['abc'], matchfuzzy(['abc'], 'abc')) |
| 102 | let &encoding = save_enc |
| 103 | endfunc |
| 104 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 105 | " Test for the matchfuzzypos() function |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 106 | func Test_matchfuzzypos() |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 107 | call assert_equal([['curl', 'world'], [[2,3], [2,3]], [178, 177]], matchfuzzypos(['world', 'curl'], 'rl')) |
| 108 | call assert_equal([['curl', 'world'], [[2,3], [2,3]], [178, 177]], matchfuzzypos(['world', 'one', 'curl'], 'rl')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 109 | call assert_equal([['hello', 'hello world hello world'], |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 110 | \ [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], [500, 382]], |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 111 | \ matchfuzzypos(['hello world hello world', 'hello', 'world'], 'hello')) |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 112 | call assert_equal([['aaaaaaa'], [[0, 1, 2]], [266]], matchfuzzypos(['aaaaaaa'], 'aaa')) |
| 113 | call assert_equal([['a b'], [[0, 3]], [269]], matchfuzzypos(['a b'], 'a b')) |
| 114 | call assert_equal([['a b'], [[0, 3]], [269]], matchfuzzypos(['a b'], 'a b')) |
| 115 | call assert_equal([['a b'], [[0]], [137]], matchfuzzypos(['a b'], ' a ')) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 116 | call assert_equal([[], [], []], matchfuzzypos(['a b'], ' ')) |
| 117 | call assert_equal([[], [], []], matchfuzzypos(['world', 'curl'], 'ab')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 118 | let x = matchfuzzypos([repeat('a', 256)], repeat('a', 256)) |
| 119 | call assert_equal(range(256), x[1][0]) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 120 | call assert_equal([[], [], []], matchfuzzypos([repeat('a', 300)], repeat('a', 257))) |
| 121 | call assert_equal([[], [], []], matchfuzzypos([], 'abc')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 122 | |
| 123 | " match in a long string |
Girish Palya | 328332b | 2025-06-09 20:43:03 +0200 | [diff] [blame] | 124 | call assert_equal([[repeat('x', 300) .. 'abc'], [[300, 301, 302]], [155]], |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 125 | \ matchfuzzypos([repeat('x', 300) .. 'abc'], 'abc')) |
| 126 | |
| 127 | " preference for camel case match |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 128 | call assert_equal([['xabcxxaBc'], [[6, 7, 8]], [269]], matchfuzzypos(['xabcxxaBc'], 'abc')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 129 | " preference for match after a separator (_ or space) |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 130 | call assert_equal([['xabx_ab'], [[5, 6]], [195]], matchfuzzypos(['xabx_ab'], 'ab')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 131 | " preference for leading letter match |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 132 | call assert_equal([['abcxabc'], [[0, 1]], [200]], matchfuzzypos(['abcxabc'], 'ab')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 133 | " preference for sequential match |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 134 | call assert_equal([['aobncedone'], [[7, 8, 9]], [233]], matchfuzzypos(['aobncedone'], 'one')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 135 | " best recursive match |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 136 | call assert_equal([['xoone'], [[2, 3, 4]], [243]], matchfuzzypos(['xoone'], 'one')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 137 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 138 | " match multiple words (separated by space) |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 139 | call assert_equal([['foo bar baz'], [[8, 9, 10, 0, 1, 2]], [519]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('baz foo')) |
zeertzjq | 9af2bc0 | 2022-05-11 14:15:37 +0100 | [diff] [blame] | 140 | call assert_equal([[], [], []], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('baz foo', {'matchseq': 1})) |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 141 | call assert_equal([['foo bar baz'], [[0, 1, 2, 8, 9, 10]], [519]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('foo baz')) |
| 142 | call assert_equal([['foo bar baz'], [[0, 1, 2, 3, 4, 5, 10]], [476]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('foo baz', {'matchseq': 1})) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 143 | call assert_equal([[], [], []], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('one two')) |
| 144 | call assert_equal([[], [], []], ['foo bar']->matchfuzzypos(" \t ")) |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 145 | call assert_equal([['grace'], [[1, 2, 3, 4, 2, 3, 4, 0, 1, 2, 3, 4]], [1057]], ['grace']->matchfuzzypos('race ace grace')) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 146 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 147 | let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}] |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 148 | call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [267]], |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 149 | \ matchfuzzypos(l, 'cam', {'text_cb' : {v -> v.val}})) |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 150 | call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [267]], |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 151 | \ matchfuzzypos(l, 'cam', {'key' : 'val'})) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 152 | call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> v.val}})) |
| 153 | call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'key' : 'val'})) |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 154 | call assert_fails("let x = matchfuzzypos(l, 'cam', 'random')", 'E1206:') |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 155 | call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> []}})) |
| 156 | call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> 1}})) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 157 | call assert_fails("let x = matchfuzzypos(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:') |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 158 | call assert_equal([[], [], []], matchfuzzypos(l, 'cam')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 159 | call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E921:') |
zeertzjq | c4815c1 | 2025-03-18 20:28:00 +0100 | [diff] [blame] | 160 | call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : 123})", 'E475: Invalid value for argument key: 123') |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 161 | call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E730:') |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 162 | call assert_fails("let x = matchfuzzypos(l, 'cam', test_null_dict())", 'E1297:') |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 163 | call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : test_null_string()})", 'E475:') |
| 164 | call assert_fails("let x = matchfuzzypos(l, 'foo', {'text_cb' : test_null_function()})", 'E475:') |
| 165 | |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 166 | " case match |
| 167 | call assert_equal([['Match', 'match'], [[0, 1], [0, 1]], [202, 177]], matchfuzzypos(['match', 'Match'], 'Ma')) |
| 168 | call assert_equal([['match', 'Match'], [[0, 1], [0, 1]], [202, 177]], matchfuzzypos(['Match', 'match'], 'ma')) |
| 169 | " CamelCase has high weight even case match |
| 170 | call assert_equal(['MyTestCase', 'mytestcase'], matchfuzzy(['mytestcase', 'MyTestCase'], 'mtc')) |
| 171 | call assert_equal(['MyTestCase', 'mytestcase'], matchfuzzy(['MyTestCase', 'mytestcase'], 'mtc')) |
| 172 | call assert_equal(['MyTest', 'Mytest', 'mytest', ],matchfuzzy(['Mytest', 'mytest', 'MyTest'], 'MyT')) |
| 173 | call assert_equal(['CamelCaseMatchIngAlg', 'camelCaseMatchingAlg', 'camelcasematchingalg'], |
| 174 | \ matchfuzzy(['CamelCaseMatchIngAlg', 'camelcasematchingalg', 'camelCaseMatchingAlg'], 'CamelCase')) |
| 175 | call assert_equal(['CamelCaseMatchIngAlg', 'camelCaseMatchingAlg', 'camelcasematchingalg'], |
| 176 | \ matchfuzzy(['CamelCaseMatchIngAlg', 'camelcasematchingalg', 'camelCaseMatchingAlg'], 'CamelcaseM')) |
| 177 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 178 | let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}] |
| 179 | call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : 'name'})", 'E730:') |
glepnir | 28e40a7 | 2025-03-16 21:24:22 +0100 | [diff] [blame] | 180 | |
zeertzjq | 8562773 | 2025-03-17 21:06:02 +0100 | [diff] [blame] | 181 | " camelcase |
glepnir | 28e40a7 | 2025-03-16 21:24:22 +0100 | [diff] [blame] | 182 | call assert_equal([['lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'Cursor', 'CurSearch', 'CursorLine'], [[1, 2, 3], [2, 3, 4], [2, 3, 4], [6, 7, 8], [0, 1, 2], [0, 1, 2], [0, 1, 2]], [318, 311, 308, 303, 267, 264, 263]], |
zeertzjq | 8562773 | 2025-03-17 21:06:02 +0100 | [diff] [blame] | 183 | \ matchfuzzypos(['Cursor', 'lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'CurSearch', 'CursorLine'], 'Cur')) |
zeertzjq | c4815c1 | 2025-03-18 20:28:00 +0100 | [diff] [blame] | 184 | call assert_equal([['lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'Cursor', 'CurSearch', 'CursorLine'], [[1, 2, 3], [2, 3, 4], [2, 3, 4], [6, 7, 8], [0, 1, 2], [0, 1, 2], [0, 1, 2]], [318, 311, 308, 303, 267, 264, 263]], |
| 185 | \ matchfuzzypos(['Cursor', 'lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'CurSearch', 'CursorLine'], 'Cur', {"camelcase": v:true})) |
glepnir | 28e40a7 | 2025-03-16 21:24:22 +0100 | [diff] [blame] | 186 | call assert_equal([['Cursor', 'CurSearch', 'CursorLine', 'lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor'], [[0, 1, 2], [0, 1, 2], [0, 1, 2], [1, 2, 3], [2, 3, 4], [2, 3, 4], [6, 7, 8]], [267, 264, 263, 246, 239, 236, 231]], |
| 187 | \ matchfuzzypos(['Cursor', 'lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'CurSearch', 'CursorLine'], 'Cur', {"camelcase": v:false})) |
| 188 | call assert_equal([['things', 'sThings', 'thisThings'], [[0, 1, 2, 3], [1, 2, 3, 4], [0, 1, 2, 7]], [333, 287, 279]], |
| 189 | \ matchfuzzypos(['things','sThings', 'thisThings'], 'thin', {'camelcase': v:false})) |
zeertzjq | c4815c1 | 2025-03-18 20:28:00 +0100 | [diff] [blame] | 190 | call assert_fails("let x = matchfuzzypos([], 'foo', {'camelcase': []})", 'E475: Invalid value for argument camelcase') |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 191 | endfunc |
| 192 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 193 | " Test for matchfuzzy() with multibyte characters |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 194 | func Test_matchfuzzy_mbyte() |
| 195 | CheckFeature multi_lang |
| 196 | call assert_equal(['ンヹㄇヺヴ'], matchfuzzy(['ンヹㄇヺヴ'], 'ヹヺ')) |
| 197 | " reverse the order of characters |
| 198 | call assert_equal([], matchfuzzy(['ンヹㄇヺヴ'], 'ヺヹ')) |
| 199 | call assert_equal(['αβΩxxx', 'xαxβxΩx'], |
| 200 | \ matchfuzzy(['αβΩxxx', 'xαxβxΩx'], 'αβΩ')) |
| 201 | call assert_equal(['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'], |
| 202 | \ matchfuzzy(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ')) |
| 203 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 204 | " match multiple words (separated by space) |
| 205 | call assert_equal(['세 마리의 작은 돼지'], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('돼지 마리의')) |
| 206 | call assert_equal([], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('파란 하늘')) |
| 207 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 208 | " preference for camel case match |
| 209 | call assert_equal(['oneĄwo', 'oneąwo'], |
| 210 | \ ['oneąwo', 'oneĄwo']->matchfuzzy('oneąwo')) |
Bram Moolenaar | e9f9f16 | 2020-10-20 19:01:30 +0200 | [diff] [blame] | 211 | " preference for complete match then match after separator (_ or space) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 212 | call assert_equal(['ⅠⅡabㄟㄠ'] + sort(['ⅠⅡa_bㄟㄠ', 'ⅠⅡa bㄟㄠ']), |
Bram Moolenaar | e9f9f16 | 2020-10-20 19:01:30 +0200 | [diff] [blame] | 213 | \ ['ⅠⅡabㄟㄠ', 'ⅠⅡa bㄟㄠ', 'ⅠⅡa_bㄟㄠ']->matchfuzzy('ⅠⅡabㄟㄠ')) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 214 | " preference for match after a separator (_ or space) |
| 215 | call assert_equal(['ㄓㄔabㄟㄠ', 'ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ'], |
| 216 | \ ['ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ', 'ㄓㄔabㄟㄠ']->matchfuzzy('ㄓㄔabㄟㄠ')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 217 | " preference for leading letter match |
| 218 | call assert_equal(['ŗŝţũŵż', 'xŗŝţũŵż'], |
| 219 | \ ['xŗŝţũŵż', 'ŗŝţũŵż']->matchfuzzy('ŗŝţũŵż')) |
| 220 | " preference for sequential match |
| 221 | call assert_equal(['ㄞㄡㄤfffifl', 'ㄞaㄡbㄤcffdfiefl'], |
| 222 | \ ['ㄞaㄡbㄤcffdfiefl', 'ㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl')) |
| 223 | " non-matching leading letter(s) penalty |
| 224 | call assert_equal(['xㄞㄡㄤfffifl', 'xxㄞㄡㄤfffifl'], |
| 225 | \ ['xxㄞㄡㄤfffifl', 'xㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl')) |
| 226 | " total non-matching letter(s) penalty |
| 227 | call assert_equal(['ŗŝţ', 'ŗŝţx', 'ŗŝţxx'], |
| 228 | \ ['ŗŝţxx', 'ŗŝţ', 'ŗŝţx']->matchfuzzy('ŗŝţ')) |
| 229 | endfunc |
| 230 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 231 | " Test for matchfuzzypos() with multibyte characters |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 232 | func Test_matchfuzzypos_mbyte() |
| 233 | CheckFeature multi_lang |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 234 | call assert_equal([['こんにちは世界'], [[0, 1, 2, 3, 4]], [273]], |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 235 | \ matchfuzzypos(['こんにちは世界'], 'こんにちは')) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 236 | call assert_equal([['ンヹㄇヺヴ'], [[1, 3]], [88]], matchfuzzypos(['ンヹㄇヺヴ'], 'ヹヺ')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 237 | " reverse the order of characters |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 238 | call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇヺヴ'], 'ヺヹ')) |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 239 | call assert_equal([['αβΩxxx', 'xαxβxΩx'], [[0, 1, 2], [1, 3, 5]], [252, 143]], |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 240 | \ matchfuzzypos(['αβΩxxx', 'xαxβxΩx'], 'αβΩ')) |
| 241 | call assert_equal([['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'], |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 242 | \ [[0, 1], [0, 1], [0, 1], [0, 2]], [176, 173, 170, 135]], |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 243 | \ matchfuzzypos(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ')) |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 244 | call assert_equal([['ααααααα'], [[0, 1, 2]], [216]], |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 245 | \ matchfuzzypos(['ααααααα'], 'ααα')) |
| 246 | |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 247 | call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇ', 'ŗŝţ'], 'fffifl')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 248 | let x = matchfuzzypos([repeat('Ψ', 256)], repeat('Ψ', 256)) |
| 249 | call assert_equal(range(256), x[1][0]) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 250 | call assert_equal([[], [], []], matchfuzzypos([repeat('✓', 300)], repeat('✓', 257))) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 251 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 252 | " match multiple words (separated by space) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 253 | call assert_equal([['세 마리의 작은 돼지'], [[9, 10, 2, 3, 4]], [328]], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('돼지 마리의')) |
| 254 | call assert_equal([[], [], []], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('파란 하늘')) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 255 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 256 | " match in a long string |
Girish Palya | 328332b | 2025-06-09 20:43:03 +0200 | [diff] [blame] | 257 | call assert_equal([[repeat('ぶ', 300) .. 'ẼẼẼ'], [[300, 301, 302]], [105]], |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 258 | \ matchfuzzypos([repeat('ぶ', 300) .. 'ẼẼẼ'], 'ẼẼẼ')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 259 | " preference for camel case match |
glepnir | 9dfc7e5 | 2025-01-21 22:33:13 +0100 | [diff] [blame] | 260 | call assert_equal([['xѳѵҁxxѳѴҁ'], [[6, 7, 8]], [219]], matchfuzzypos(['xѳѵҁxxѳѴҁ'], 'ѳѵҁ')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 261 | " preference for match after a separator (_ or space) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 262 | call assert_equal([['xちだx_ちだ'], [[5, 6]], [145]], matchfuzzypos(['xちだx_ちだ'], 'ちだ')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 263 | " preference for leading letter match |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 264 | call assert_equal([['ѳѵҁxѳѵҁ'], [[0, 1]], [150]], matchfuzzypos(['ѳѵҁxѳѵҁ'], 'ѳѵ')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 265 | " preference for sequential match |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 266 | call assert_equal([['aンbヹcㄇdンヹㄇ'], [[7, 8, 9]], [158]], matchfuzzypos(['aンbヹcㄇdンヹㄇ'], 'ンヹㄇ')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 267 | " best recursive match |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 268 | call assert_equal([['xффйд'], [[2, 3, 4]], [168]], matchfuzzypos(['xффйд'], 'фйд')) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 269 | endfunc |
| 270 | |
Yasuhiro Matsumoto | 9029a6e | 2022-04-16 12:35:35 +0100 | [diff] [blame] | 271 | " Test for matchfuzzy() with limit |
| 272 | func Test_matchfuzzy_limit() |
| 273 | let x = ['1', '2', '3', '2'] |
| 274 | call assert_equal(['2', '2'], x->matchfuzzy('2')) |
| 275 | call assert_equal(['2', '2'], x->matchfuzzy('2', #{})) |
| 276 | call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 0})) |
| 277 | call assert_equal(['2'], x->matchfuzzy('2', #{limit: 1})) |
| 278 | call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 2})) |
| 279 | call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 3})) |
| 280 | call assert_fails("call matchfuzzy(x, '2', #{limit: '2'})", 'E475:') |
zeertzjq | c4815c1 | 2025-03-18 20:28:00 +0100 | [diff] [blame] | 281 | call assert_fails("call matchfuzzy(x, '2', #{limit: []})", 'E475:') |
Kazuyuki Miyagi | 47f1a55 | 2022-06-17 18:30:03 +0100 | [diff] [blame] | 282 | |
| 283 | let l = [{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}] |
| 284 | call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}})) |
| 285 | call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{key: 'val'})) |
| 286 | call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}, limit: 0})) |
| 287 | call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{key: 'val', limit: 0})) |
| 288 | call assert_equal([{'id': 5, 'val': 'crayon'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}, limit: 1})) |
| 289 | call assert_equal([{'id': 5, 'val': 'crayon'}], l->matchfuzzy('c', #{key: 'val', limit: 1})) |
Yasuhiro Matsumoto | 9029a6e | 2022-04-16 12:35:35 +0100 | [diff] [blame] | 290 | endfunc |
| 291 | |
Bram Moolenaar | caf642c | 2023-04-29 21:38:04 +0100 | [diff] [blame] | 292 | " This was using uninitialized memory |
| 293 | func Test_matchfuzzy_initialized() |
| 294 | CheckRunVimInTerminal |
| 295 | |
| 296 | " This can take a very long time (esp. when using valgrind). Run in a |
| 297 | " separate Vim instance and kill it after two seconds. We only check for |
| 298 | " memory errors. |
| 299 | let lines =<< trim END |
| 300 | lvimgrep [ss [fg* |
| 301 | END |
| 302 | call writefile(lines, 'XTest_matchfuzzy', 'D') |
| 303 | |
| 304 | let buf = RunVimInTerminal('-u NONE -X -Z', {}) |
| 305 | call term_sendkeys(buf, ":source XTest_matchfuzzy\n") |
| 306 | call TermWait(buf, 2000) |
| 307 | |
| 308 | let job = term_getjob(buf) |
| 309 | if job_status(job) == "run" |
| 310 | call job_stop(job, "int") |
| 311 | call TermWait(buf, 50) |
| 312 | endif |
| 313 | |
| 314 | " clean up |
| 315 | call StopVimInTerminal(buf) |
| 316 | endfunc |
| 317 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 318 | " vim: shiftwidth=2 sts=2 expandtab |