blob: 43eca8ff08af62852dba5df77493d81251e75264 [file] [log] [blame]
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02001" Tests for fuzzy matching
2
3source shared.vim
4source check.vim
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01005source term_util.vim
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02006
7" Test for matchfuzzy()
8func 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 Moolenaare9f9f162020-10-20 19:01:30 +020022 call assert_equal(['onetwo', 'one_two'], matchfuzzy(['onetwo', 'one_two'], 'oneTwo'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020023 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 Moolenaar8ded5b62020-10-23 16:50:30 +020026 " matches with same score should not be reordered
27 let l = ['abc1', 'abc2', 'abc3']
28 call assert_equal(l, l->matchfuzzy('abc'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020029
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 Moolenaar8ded5b62020-10-23 16:50:30 +020034 call assert_equal(['onetwo', 'one_two', 'one two'], ['onetwo', 'one_two', 'one two']->matchfuzzy('onetwo'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020035 " 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 Moolenaare9f9f162020-10-20 19:01:30 +020043 " 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 Moolenaar8ded5b62020-10-23 16:50:30 +020045 " gap penalty
46 call assert_equal(['xxayybxxxx', 'xxayyybxxx', 'xxayyyybxx'], ['xxayyyybxx', 'xxayyybxxx', 'xxayybxxxx']->matchfuzzy('ab'))
Bram Moolenaardcdd42a2020-10-29 18:58:01 +010047 " 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 Moolenaar8ded5b62020-10-23 16:50:30 +020049
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 Moolenaar4f73b8e2020-09-22 20:33:50 +020058
59 %bw!
60 eval ['somebuf', 'anotherone', 'needle', 'yetanotherone']->map({_, v -> bufadd(v) + bufload(v)})
Bram Moolenaar22e7e862022-07-02 20:48:01 +010061 let l = getbufinfo()->map({_, v -> fnamemodify(v.name, ':t')})->matchfuzzy('ndl')
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020062 call assert_equal(1, len(l))
63 call assert_match('needle', l[0])
64
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020065 " Test for fuzzy matching dicts
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020066 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 Lakshmanan04c4c572022-08-30 19:48:24 +010071 call assert_fails("let x = matchfuzzy(l, 'cam', 'random')", 'E1206:')
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020072 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 Lakshmanan04c4c572022-08-30 19:48:24 +010078 call assert_fails("let x = matchfuzzy(l, 'cam', test_null_dict())", 'E1297:')
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020079 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 Moolenaar8ded5b62020-10-23 16:50:30 +020081 " 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 Moolenaar4f73b8e2020-09-22 20:33:50 +020084
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
93endfunc
94
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020095" Test for the matchfuzzypos() function
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020096func Test_matchfuzzypos()
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +010097 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 Moolenaar4f73b8e2020-09-22 20:33:50 +020099 call assert_equal([['hello', 'hello world hello world'],
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100100 \ [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], [275, 257]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200101 \ matchfuzzypos(['hello world hello world', 'hello', 'world'], 'hello'))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100102 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 Moolenaar4f73b8e2020-09-22 20:33:50 +0200108 let x = matchfuzzypos([repeat('a', 256)], repeat('a', 256))
109 call assert_equal(range(256), x[1][0])
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100110 call assert_equal([[], [], []], matchfuzzypos([repeat('a', 300)], repeat('a', 257)))
111 call assert_equal([[], [], []], matchfuzzypos([], 'abc'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200112
113 " match in a long string
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100114 call assert_equal([[repeat('x', 300) .. 'abc'], [[300, 301, 302]], [-135]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200115 \ matchfuzzypos([repeat('x', 300) .. 'abc'], 'abc'))
116
117 " preference for camel case match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100118 call assert_equal([['xabcxxaBc'], [[6, 7, 8]], [189]], matchfuzzypos(['xabcxxaBc'], 'abc'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200119 " preference for match after a separator (_ or space)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100120 call assert_equal([['xabx_ab'], [[5, 6]], [145]], matchfuzzypos(['xabx_ab'], 'ab'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200121 " preference for leading letter match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100122 call assert_equal([['abcxabc'], [[0, 1]], [150]], matchfuzzypos(['abcxabc'], 'ab'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200123 " preference for sequential match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100124 call assert_equal([['aobncedone'], [[7, 8, 9]], [158]], matchfuzzypos(['aobncedone'], 'one'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200125 " best recursive match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100126 call assert_equal([['xoone'], [[2, 3, 4]], [168]], matchfuzzypos(['xoone'], 'one'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200127
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200128 " match multiple words (separated by space)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100129 call assert_equal([['foo bar baz'], [[8, 9, 10, 0, 1, 2]], [369]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('baz foo'))
zeertzjq9af2bc02022-05-11 14:15:37 +0100130 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 Moolenaar9d19e4f2021-01-02 18:31:32 +0100133 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 Moolenaar8ded5b62020-10-23 16:50:30 +0200136
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200137 let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100138 call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [192]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200139 \ matchfuzzypos(l, 'cam', {'text_cb' : {v -> v.val}}))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100140 call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [192]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200141 \ matchfuzzypos(l, 'cam', {'key' : 'val'}))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100142 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> v.val}}))
143 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'key' : 'val'}))
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100144 call assert_fails("let x = matchfuzzypos(l, 'cam', 'random')", 'E1206:')
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100145 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> []}}))
146 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> 1}}))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200147 call assert_fails("let x = matchfuzzypos(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100148 call assert_equal([[], [], []], matchfuzzypos(l, 'cam'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200149 call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E921:')
150 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E730:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100151 call assert_fails("let x = matchfuzzypos(l, 'cam', test_null_dict())", 'E1297:')
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200152 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:')
157endfunc
158
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200159" Test for matchfuzzy() with multibyte characters
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200160func 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 Moolenaar8ded5b62020-10-23 16:50:30 +0200170 " match multiple words (separated by space)
171 call assert_equal(['세 마리의 작은 돼지'], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('돼지 마리의'))
172 call assert_equal([], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('파란 하늘'))
173
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200174 " preference for camel case match
175 call assert_equal(['oneĄwo', 'oneąwo'],
176 \ ['oneąwo', 'oneĄwo']->matchfuzzy('oneąwo'))
Bram Moolenaare9f9f162020-10-20 19:01:30 +0200177 " preference for complete match then match after separator (_ or space)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200178 call assert_equal(['ⅠⅡabㄟㄠ'] + sort(['ⅠⅡa_bㄟㄠ', 'ⅠⅡa bㄟㄠ']),
Bram Moolenaare9f9f162020-10-20 19:01:30 +0200179 \ ['ⅠⅡabㄟㄠ', 'ⅠⅡa bㄟㄠ', 'ⅠⅡa_bㄟㄠ']->matchfuzzy('ⅠⅡabㄟㄠ'))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200180 " 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 Moolenaar4f73b8e2020-09-22 20:33:50 +0200183 " 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('ŗŝţ'))
195endfunc
196
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200197" Test for matchfuzzypos() with multibyte characters
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200198func Test_matchfuzzypos_mbyte()
199 CheckFeature multi_lang
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100200 call assert_equal([['こんにちは世界'], [[0, 1, 2, 3, 4]], [273]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200201 \ matchfuzzypos(['こんにちは世界'], 'こんにちは'))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100202 call assert_equal([['ンヹㄇヺヴ'], [[1, 3]], [88]], matchfuzzypos(['ンヹㄇヺヴ'], 'ヹヺ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200203 " reverse the order of characters
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100204 call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇヺヴ'], 'ヺヹ'))
205 call assert_equal([['αβΩxxx', 'xαxβxΩx'], [[0, 1, 2], [1, 3, 5]], [222, 113]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200206 \ matchfuzzypos(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
207 call assert_equal([['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100208 \ [[0, 1], [0, 1], [0, 1], [0, 2]], [151, 148, 145, 110]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200209 \ matchfuzzypos(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100210 call assert_equal([['ααααααα'], [[0, 1, 2]], [191]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200211 \ matchfuzzypos(['ααααααα'], 'ααα'))
212
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100213 call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇ', 'ŗŝţ'], 'fffifl'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200214 let x = matchfuzzypos([repeat('Ψ', 256)], repeat('Ψ', 256))
215 call assert_equal(range(256), x[1][0])
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100216 call assert_equal([[], [], []], matchfuzzypos([repeat('✓', 300)], repeat('✓', 257)))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200217
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200218 " match multiple words (separated by space)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100219 call assert_equal([['세 마리의 작은 돼지'], [[9, 10, 2, 3, 4]], [328]], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('돼지 마리의'))
220 call assert_equal([[], [], []], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('파란 하늘'))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200221
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200222 " match in a long string
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100223 call assert_equal([[repeat('ぶ', 300) .. 'ẼẼẼ'], [[300, 301, 302]], [-135]],
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200224 \ matchfuzzypos([repeat('ぶ', 300) .. 'ẼẼẼ'], 'ẼẼẼ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200225 " preference for camel case match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100226 call assert_equal([['xѳѵҁxxѳѴҁ'], [[6, 7, 8]], [189]], matchfuzzypos(['xѳѵҁxxѳѴҁ'], 'ѳѵҁ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200227 " preference for match after a separator (_ or space)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100228 call assert_equal([['xちだx_ちだ'], [[5, 6]], [145]], matchfuzzypos(['xちだx_ちだ'], 'ちだ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200229 " preference for leading letter match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100230 call assert_equal([['ѳѵҁxѳѵҁ'], [[0, 1]], [150]], matchfuzzypos(['ѳѵҁxѳѵҁ'], 'ѳѵ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200231 " preference for sequential match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100232 call assert_equal([['aンbヹcㄇdンヹㄇ'], [[7, 8, 9]], [158]], matchfuzzypos(['aンbヹcㄇdンヹㄇ'], 'ンヹㄇ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200233 " best recursive match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100234 call assert_equal([['xффйд'], [[2, 3, 4]], [168]], matchfuzzypos(['xффйд'], 'фйд'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200235endfunc
236
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +0100237" Test for matchfuzzy() with limit
238func 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 Miyagi47f1a552022-06-17 18:30:03 +0100247
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 Matsumoto9029a6e2022-04-16 12:35:35 +0100255endfunc
256
Bram Moolenaarcaf642c2023-04-29 21:38:04 +0100257" This was using uninitialized memory
258func 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)
281endfunc
282
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200283" vim: shiftwidth=2 sts=2 expandtab