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