1" Tests for fuzzy matching
2
3source shared.vim
4source check.vim
5
6" Test for matchfuzzy()
7func Test_matchfuzzy()
8  call assert_fails('call matchfuzzy(10, "abc")', 'E686:')
9  call assert_fails('call matchfuzzy(["abc"], [])', 'E730:')
10  call assert_fails("let x = matchfuzzy(test_null_list(), 'foo')", 'E686:')
11  call assert_fails('call matchfuzzy(["abc"], test_null_string())', 'E475:')
12  call assert_equal([], matchfuzzy([], 'abc'))
13  call assert_equal([], matchfuzzy(['abc'], ''))
14  call assert_equal(['abc'], matchfuzzy(['abc', 10], 'ac'))
15  call assert_equal([], matchfuzzy([10, 20], 'ac'))
16  call assert_equal(['abc'], matchfuzzy(['abc'], 'abc'))
17  call assert_equal(['crayon', 'camera'], matchfuzzy(['camera', 'crayon'], 'cra'))
18  call assert_equal(['aabbaa', 'aaabbbaaa', 'aaaabbbbaaaa', 'aba'], matchfuzzy(['aba', 'aabbaa', 'aaabbbaaa', 'aaaabbbbaaaa'], 'aa'))
19  call assert_equal(['one'], matchfuzzy(['one', 'two'], 'one'))
20  call assert_equal(['oneTwo', 'onetwo'], matchfuzzy(['onetwo', 'oneTwo'], 'oneTwo'))
21  call assert_equal(['onetwo', 'one_two'], matchfuzzy(['onetwo', 'one_two'], 'oneTwo'))
22  call assert_equal(['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], matchfuzzy(['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], 'aa'))
23  call assert_equal(256, matchfuzzy([repeat('a', 256)], repeat('a', 256))[0]->len())
24  call assert_equal([], matchfuzzy([repeat('a', 300)], repeat('a', 257)))
25  " matches with same score should not be reordered
26  let l = ['abc1', 'abc2', 'abc3']
27  call assert_equal(l, l->matchfuzzy('abc'))
28
29  " Tests for match preferences
30  " preference for camel case match
31  call assert_equal(['oneTwo', 'onetwo'], ['onetwo', 'oneTwo']->matchfuzzy('onetwo'))
32  " preference for match after a separator (_ or space)
33  call assert_equal(['onetwo', 'one_two', 'one two'], ['onetwo', 'one_two', 'one two']->matchfuzzy('onetwo'))
34  " preference for leading letter match
35  call assert_equal(['onetwo', 'xonetwo'], ['xonetwo', 'onetwo']->matchfuzzy('onetwo'))
36  " preference for sequential match
37  call assert_equal(['onetwo', 'oanbectdweo'], ['oanbectdweo', 'onetwo']->matchfuzzy('onetwo'))
38  " non-matching leading letter(s) penalty
39  call assert_equal(['xonetwo', 'xxonetwo'], ['xxonetwo', 'xonetwo']->matchfuzzy('onetwo'))
40  " total non-matching letter(s) penalty
41  call assert_equal(['one', 'onex', 'onexx'], ['onexx', 'one', 'onex']->matchfuzzy('one'))
42  " prefer complete matches over separator matches
43  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'))
44  " gap penalty
45  call assert_equal(['xxayybxxxx', 'xxayyybxxx', 'xxayyyybxx'], ['xxayyyybxx', 'xxayyybxxx', 'xxayybxxxx']->matchfuzzy('ab'))
46
47  " match multiple words (separated by space)
48  call assert_equal(['foo bar baz'], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('baz foo'))
49  call assert_equal([], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('one two'))
50  call assert_equal([], ['foo bar']->matchfuzzy(" \t "))
51
52  " test for matching a sequence of words
53  call assert_equal(['bar foo'], ['foo bar', 'bar foo', 'foobar', 'barfoo']->matchfuzzy('bar foo', {'matchseq' : 1}))
54  call assert_equal([#{text: 'two one'}], [#{text: 'one two'}, #{text: 'two one'}]->matchfuzzy('two one', #{key: 'text', matchseq: v:true}))
55
56  %bw!
57  eval ['somebuf', 'anotherone', 'needle', 'yetanotherone']->map({_, v -> bufadd(v) + bufload(v)})
58  let l = getbufinfo()->map({_, v -> v.name})->matchfuzzy('ndl')
59  call assert_equal(1, len(l))
60  call assert_match('needle', l[0])
61
62  " Test for fuzzy matching dicts
63  let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
64  call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'text_cb' : {v -> v.val}}))
65  call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'key' : 'val'}))
66  call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> v.val}}))
67  call assert_equal([], matchfuzzy(l, 'day', {'key' : 'val'}))
68  call assert_fails("let x = matchfuzzy(l, 'cam', 'random')", 'E715:')
69  call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> []}}))
70  call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> 1}}))
71  call assert_fails("let x = matchfuzzy(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
72  call assert_equal([], matchfuzzy(l, 'cam'))
73  call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E921:')
74  call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E730:')
75  call assert_fails("let x = matchfuzzy(l, 'cam', test_null_dict())", 'E715:')
76  call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : test_null_string()})", 'E475:')
77  call assert_fails("let x = matchfuzzy(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
78  " matches with same score should not be reordered
79  let l = [#{text: 'abc', id: 1}, #{text: 'abc', id: 2}, #{text: 'abc', id: 3}]
80  call assert_equal(l, l->matchfuzzy('abc', #{key: 'text'}))
81
82  let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
83  call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : 'name'})", 'E730:')
84
85  " Test in latin1 encoding
86  let save_enc = &encoding
87  set encoding=latin1
88  call assert_equal(['abc'], matchfuzzy(['abc'], 'abc'))
89  let &encoding = save_enc
90endfunc
91
92" Test for the matchfuzzypos() function
93func Test_matchfuzzypos()
94  call assert_equal([['curl', 'world'], [[2,3], [2,3]]], matchfuzzypos(['world', 'curl'], 'rl'))
95  call assert_equal([['curl', 'world'], [[2,3], [2,3]]], matchfuzzypos(['world', 'one', 'curl'], 'rl'))
96  call assert_equal([['hello', 'hello world hello world'],
97        \ [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]],
98        \ matchfuzzypos(['hello world hello world', 'hello', 'world'], 'hello'))
99  call assert_equal([['aaaaaaa'], [[0, 1, 2]]], matchfuzzypos(['aaaaaaa'], 'aaa'))
100  call assert_equal([['a  b'], [[0, 3]]], matchfuzzypos(['a  b'], 'a  b'))
101  call assert_equal([['a  b'], [[0, 3]]], matchfuzzypos(['a  b'], 'a    b'))
102  call assert_equal([['a  b'], [[0]]], matchfuzzypos(['a  b'], '  a  '))
103  call assert_equal([[], []], matchfuzzypos(['a  b'], '  '))
104  call assert_equal([[], []], matchfuzzypos(['world', 'curl'], 'ab'))
105  let x = matchfuzzypos([repeat('a', 256)], repeat('a', 256))
106  call assert_equal(range(256), x[1][0])
107  call assert_equal([[], []], matchfuzzypos([repeat('a', 300)], repeat('a', 257)))
108  call assert_equal([[], []], matchfuzzypos([], 'abc'))
109
110  " match in a long string
111  call assert_equal([[repeat('x', 300) .. 'abc'], [[300, 301, 302]]],
112        \ matchfuzzypos([repeat('x', 300) .. 'abc'], 'abc'))
113
114  " preference for camel case match
115  call assert_equal([['xabcxxaBc'], [[6, 7, 8]]], matchfuzzypos(['xabcxxaBc'], 'abc'))
116  " preference for match after a separator (_ or space)
117  call assert_equal([['xabx_ab'], [[5, 6]]], matchfuzzypos(['xabx_ab'], 'ab'))
118  " preference for leading letter match
119  call assert_equal([['abcxabc'], [[0, 1]]], matchfuzzypos(['abcxabc'], 'ab'))
120  " preference for sequential match
121  call assert_equal([['aobncedone'], [[7, 8, 9]]], matchfuzzypos(['aobncedone'], 'one'))
122  " best recursive match
123  call assert_equal([['xoone'], [[2, 3, 4]]], matchfuzzypos(['xoone'], 'one'))
124
125  " match multiple words (separated by space)
126  call assert_equal([['foo bar baz'], [[8, 9, 10, 0, 1, 2]]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('baz foo'))
127  call assert_equal([[], []], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('one two'))
128  call assert_equal([[], []], ['foo bar']->matchfuzzypos(" \t "))
129  call assert_equal([['grace'], [[1, 2, 3, 4, 2, 3, 4, 0, 1, 2, 3, 4]]], ['grace']->matchfuzzypos('race ace grace'))
130
131  let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
132  call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]]],
133        \ matchfuzzypos(l, 'cam', {'text_cb' : {v -> v.val}}))
134  call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]]],
135        \ matchfuzzypos(l, 'cam', {'key' : 'val'}))
136  call assert_equal([[], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> v.val}}))
137  call assert_equal([[], []], matchfuzzypos(l, 'day', {'key' : 'val'}))
138  call assert_fails("let x = matchfuzzypos(l, 'cam', 'random')", 'E715:')
139  call assert_equal([[], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> []}}))
140  call assert_equal([[], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> 1}}))
141  call assert_fails("let x = matchfuzzypos(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
142  call assert_equal([[], []], matchfuzzypos(l, 'cam'))
143  call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E921:')
144  call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E730:')
145  call assert_fails("let x = matchfuzzypos(l, 'cam', test_null_dict())", 'E715:')
146  call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : test_null_string()})", 'E475:')
147  call assert_fails("let x = matchfuzzypos(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
148
149  let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
150  call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : 'name'})", 'E730:')
151endfunc
152
153" Test for matchfuzzy() with multibyte characters
154func Test_matchfuzzy_mbyte()
155  CheckFeature multi_lang
156  call assert_equal(['ンヹㄇヺヴ'], matchfuzzy(['ンヹㄇヺヴ'], 'ヹヺ'))
157  " reverse the order of characters
158  call assert_equal([], matchfuzzy(['ンヹㄇヺヴ'], 'ヺヹ'))
159  call assert_equal(['αβΩxxx', 'xαxβxΩx'],
160        \ matchfuzzy(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
161  call assert_equal(['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
162        \ matchfuzzy(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
163
164  " match multiple words (separated by space)
165  call assert_equal(['세 마리의 작은 돼지'], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('돼지 마리의'))
166  call assert_equal([], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('파란 하늘'))
167
168  " preference for camel case match
169  call assert_equal(['oneĄwo', 'oneąwo'],
170        \ ['oneąwo', 'oneĄwo']->matchfuzzy('oneąwo'))
171  " preference for complete match then match after separator (_ or space)
172  call assert_equal(['ⅠⅡabㄟㄠ'] + sort(['ⅠⅡa_bㄟㄠ', 'ⅠⅡa bㄟㄠ']),
173          \ ['ⅠⅡabㄟㄠ', 'ⅠⅡa bㄟㄠ', 'ⅠⅡa_bㄟㄠ']->matchfuzzy('ⅠⅡabㄟㄠ'))
174  " preference for match after a separator (_ or space)
175  call assert_equal(['ㄓㄔabㄟㄠ', 'ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ'],
176        \ ['ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ', 'ㄓㄔabㄟㄠ']->matchfuzzy('ㄓㄔabㄟㄠ'))
177  " preference for leading letter match
178  call assert_equal(['ŗŝţũŵż', 'xŗŝţũŵż'],
179        \ ['xŗŝţũŵż', 'ŗŝţũŵż']->matchfuzzy('ŗŝţũŵż'))
180  " preference for sequential match
181  call assert_equal(['ㄞㄡㄤfffifl', 'ㄞaㄡbㄤcffdfiefl'],
182        \ ['ㄞaㄡbㄤcffdfiefl', 'ㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
183  " non-matching leading letter(s) penalty
184  call assert_equal(['xㄞㄡㄤfffifl', 'xxㄞㄡㄤfffifl'],
185        \ ['xxㄞㄡㄤfffifl', 'xㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
186  " total non-matching letter(s) penalty
187  call assert_equal(['ŗŝţ', 'ŗŝţx', 'ŗŝţxx'],
188        \ ['ŗŝţxx', 'ŗŝţ', 'ŗŝţx']->matchfuzzy('ŗŝţ'))
189endfunc
190
191" Test for matchfuzzypos() with multibyte characters
192func Test_matchfuzzypos_mbyte()
193  CheckFeature multi_lang
194  call assert_equal([['こんにちは世界'], [[0, 1, 2, 3, 4]]],
195        \ matchfuzzypos(['こんにちは世界'], 'こんにちは'))
196  call assert_equal([['ンヹㄇヺヴ'], [[1, 3]]], matchfuzzypos(['ンヹㄇヺヴ'], 'ヹヺ'))
197  " reverse the order of characters
198  call assert_equal([[], []], matchfuzzypos(['ンヹㄇヺヴ'], 'ヺヹ'))
199  call assert_equal([['αβΩxxx', 'xαxβxΩx'], [[0, 1, 2], [1, 3, 5]]],
200        \ matchfuzzypos(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
201  call assert_equal([['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
202        \ [[0, 1], [0, 1], [0, 1], [0, 2]]],
203        \ matchfuzzypos(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
204  call assert_equal([['ααααααα'], [[0, 1, 2]]],
205        \ matchfuzzypos(['ααααααα'], 'ααα'))
206
207  call assert_equal([[], []], matchfuzzypos(['ンヹㄇ', 'ŗŝţ'], 'fffifl'))
208  let x = matchfuzzypos([repeat('Ψ', 256)], repeat('Ψ', 256))
209  call assert_equal(range(256), x[1][0])
210  call assert_equal([[], []], matchfuzzypos([repeat('✓', 300)], repeat('✓', 257)))
211
212  " match multiple words (separated by space)
213  call assert_equal([['세 마리의 작은 돼지'], [[9, 10, 2, 3, 4]]], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('돼지 마리의'))
214  call assert_equal([[], []], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('파란 하늘'))
215
216  " match in a long string
217  call assert_equal([[repeat('ぶ', 300) .. 'ẼẼẼ'], [[300, 301, 302]]],
218        \ matchfuzzypos([repeat('ぶ', 300) .. 'ẼẼẼ'], 'ẼẼẼ'))
219  " preference for camel case match
220  call assert_equal([['xѳѵҁxxѳѴҁ'], [[6, 7, 8]]], matchfuzzypos(['xѳѵҁxxѳѴҁ'], 'ѳѵҁ'))
221  " preference for match after a separator (_ or space)
222  call assert_equal([['xちだx_ちだ'], [[5, 6]]], matchfuzzypos(['xちだx_ちだ'], 'ちだ'))
223  " preference for leading letter match
224  call assert_equal([['ѳѵҁxѳѵҁ'], [[0, 1]]], matchfuzzypos(['ѳѵҁxѳѵҁ'], 'ѳѵ'))
225  " preference for sequential match
226  call assert_equal([['aンbヹcㄇdンヹㄇ'], [[7, 8, 9]]], matchfuzzypos(['aンbヹcㄇdンヹㄇ'], 'ンヹㄇ'))
227  " best recursive match
228  call assert_equal([['xффйд'], [[2, 3, 4]]], matchfuzzypos(['xффйд'], 'фйд'))
229endfunc
230
231" vim: shiftwidth=2 sts=2 expandtab
232