blob: 44cedf6c0e7348226e3540dcfd5bb326d902735b [file] [log] [blame]
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02001" 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'))
Bram Moolenaare9f9f162020-10-20 19:01:30 +020021 call assert_equal(['onetwo', 'one_two'], matchfuzzy(['onetwo', 'one_two'], 'oneTwo'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020022 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
26 " Tests for match preferences
27 " preference for camel case match
28 call assert_equal(['oneTwo', 'onetwo'], ['onetwo', 'oneTwo']->matchfuzzy('onetwo'))
29 " preference for match after a separator (_ or space)
Bram Moolenaare9f9f162020-10-20 19:01:30 +020030 if has("win32")
31 call assert_equal(['onetwo', 'one two', 'one_two'], ['onetwo', 'one_two', 'one two']->matchfuzzy('onetwo'))
32 else
33 call assert_equal(['onetwo', 'one_two', 'one two'], ['onetwo', 'one_two', 'one two']->matchfuzzy('onetwo'))
34 endif
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 Moolenaar4f73b8e2020-09-22 20:33:50 +020045
46 %bw!
47 eval ['somebuf', 'anotherone', 'needle', 'yetanotherone']->map({_, v -> bufadd(v) + bufload(v)})
48 let l = getbufinfo()->map({_, v -> v.name})->matchfuzzy('ndl')
49 call assert_equal(1, len(l))
50 call assert_match('needle', l[0])
51
52 let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
53 call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'text_cb' : {v -> v.val}}))
54 call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'key' : 'val'}))
55 call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> v.val}}))
56 call assert_equal([], matchfuzzy(l, 'day', {'key' : 'val'}))
57 call assert_fails("let x = matchfuzzy(l, 'cam', 'random')", 'E715:')
58 call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> []}}))
59 call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> 1}}))
60 call assert_fails("let x = matchfuzzy(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
61 call assert_equal([], matchfuzzy(l, 'cam'))
62 call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E921:')
63 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E730:')
64 call assert_fails("let x = matchfuzzy(l, 'cam', test_null_dict())", 'E715:')
65 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : test_null_string()})", 'E475:')
66 call assert_fails("let x = matchfuzzy(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
67
68 let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
69 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : 'name'})", 'E730:')
70
71 " Test in latin1 encoding
72 let save_enc = &encoding
73 set encoding=latin1
74 call assert_equal(['abc'], matchfuzzy(['abc'], 'abc'))
75 let &encoding = save_enc
76endfunc
77
78" Test for the fuzzymatchpos() function
79func Test_matchfuzzypos()
80 call assert_equal([['curl', 'world'], [[2,3], [2,3]]], matchfuzzypos(['world', 'curl'], 'rl'))
81 call assert_equal([['curl', 'world'], [[2,3], [2,3]]], matchfuzzypos(['world', 'one', 'curl'], 'rl'))
82 call assert_equal([['hello', 'hello world hello world'],
83 \ [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]],
84 \ matchfuzzypos(['hello world hello world', 'hello', 'world'], 'hello'))
85 call assert_equal([['aaaaaaa'], [[0, 1, 2]]], matchfuzzypos(['aaaaaaa'], 'aaa'))
86 call assert_equal([[], []], matchfuzzypos(['world', 'curl'], 'ab'))
87 let x = matchfuzzypos([repeat('a', 256)], repeat('a', 256))
88 call assert_equal(range(256), x[1][0])
89 call assert_equal([[], []], matchfuzzypos([repeat('a', 300)], repeat('a', 257)))
90 call assert_equal([[], []], matchfuzzypos([], 'abc'))
91
92 " match in a long string
93 call assert_equal([[repeat('x', 300) .. 'abc'], [[300, 301, 302]]],
94 \ matchfuzzypos([repeat('x', 300) .. 'abc'], 'abc'))
95
96 " preference for camel case match
97 call assert_equal([['xabcxxaBc'], [[6, 7, 8]]], matchfuzzypos(['xabcxxaBc'], 'abc'))
98 " preference for match after a separator (_ or space)
99 call assert_equal([['xabx_ab'], [[5, 6]]], matchfuzzypos(['xabx_ab'], 'ab'))
100 " preference for leading letter match
101 call assert_equal([['abcxabc'], [[0, 1]]], matchfuzzypos(['abcxabc'], 'ab'))
102 " preference for sequential match
103 call assert_equal([['aobncedone'], [[7, 8, 9]]], matchfuzzypos(['aobncedone'], 'one'))
104 " best recursive match
105 call assert_equal([['xoone'], [[2, 3, 4]]], matchfuzzypos(['xoone'], 'one'))
106
107 let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
108 call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]]],
109 \ matchfuzzypos(l, 'cam', {'text_cb' : {v -> v.val}}))
110 call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]]],
111 \ matchfuzzypos(l, 'cam', {'key' : 'val'}))
112 call assert_equal([[], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> v.val}}))
113 call assert_equal([[], []], matchfuzzypos(l, 'day', {'key' : 'val'}))
114 call assert_fails("let x = matchfuzzypos(l, 'cam', 'random')", 'E715:')
115 call assert_equal([[], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> []}}))
116 call assert_equal([[], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> 1}}))
117 call assert_fails("let x = matchfuzzypos(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
118 call assert_equal([[], []], matchfuzzypos(l, 'cam'))
119 call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E921:')
120 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E730:')
121 call assert_fails("let x = matchfuzzypos(l, 'cam', test_null_dict())", 'E715:')
122 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : test_null_string()})", 'E475:')
123 call assert_fails("let x = matchfuzzypos(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
124
125 let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
126 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : 'name'})", 'E730:')
127endfunc
128
129func Test_matchfuzzy_mbyte()
130 CheckFeature multi_lang
131 call assert_equal(['ンヹㄇヺヴ'], matchfuzzy(['ンヹㄇヺヴ'], 'ヹヺ'))
132 " reverse the order of characters
133 call assert_equal([], matchfuzzy(['ンヹㄇヺヴ'], 'ヺヹ'))
134 call assert_equal(['αβΩxxx', 'xαxβxΩx'],
135 \ matchfuzzy(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
136 call assert_equal(['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
137 \ matchfuzzy(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
138
139 " preference for camel case match
140 call assert_equal(['oneĄwo', 'oneąwo'],
141 \ ['oneąwo', 'oneĄwo']->matchfuzzy('oneąwo'))
Bram Moolenaare9f9f162020-10-20 19:01:30 +0200142 " preference for complete match then match after separator (_ or space)
143 if has("win32")
144 " order is different between Windows and Unix :(
145 " It's important that the complete match is first
146 call assert_equal(['ⅠⅡabㄟㄠ', 'ⅠⅡa bㄟㄠ', 'ⅠⅡa_bㄟㄠ'],
147 \ ['ⅠⅡabㄟㄠ', 'ⅠⅡa_bㄟㄠ', 'ⅠⅡa bㄟㄠ']->matchfuzzy('ⅠⅡabㄟㄠ'))
148 else
149 call assert_equal(['ⅠⅡabㄟㄠ'] + sort(['ⅠⅡa_bㄟㄠ', 'ⅠⅡa bㄟㄠ']),
150 \ ['ⅠⅡabㄟㄠ', 'ⅠⅡa bㄟㄠ', 'ⅠⅡa_bㄟㄠ']->matchfuzzy('ⅠⅡabㄟㄠ'))
151 endif
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200152 " preference for leading letter match
153 call assert_equal(['ŗŝţũŵż', 'xŗŝţũŵż'],
154 \ ['xŗŝţũŵż', 'ŗŝţũŵż']->matchfuzzy('ŗŝţũŵż'))
155 " preference for sequential match
156 call assert_equal(['ㄞㄡㄤfffifl', 'ㄞaㄡbㄤcffdfiefl'],
157 \ ['ㄞaㄡbㄤcffdfiefl', 'ㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
158 " non-matching leading letter(s) penalty
159 call assert_equal(['xㄞㄡㄤfffifl', 'xxㄞㄡㄤfffifl'],
160 \ ['xxㄞㄡㄤfffifl', 'xㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
161 " total non-matching letter(s) penalty
162 call assert_equal(['ŗŝţ', 'ŗŝţx', 'ŗŝţxx'],
163 \ ['ŗŝţxx', 'ŗŝţ', 'ŗŝţx']->matchfuzzy('ŗŝţ'))
164endfunc
165
166func Test_matchfuzzypos_mbyte()
167 CheckFeature multi_lang
168 call assert_equal([['こんにちは世界'], [[0, 1, 2, 3, 4]]],
169 \ matchfuzzypos(['こんにちは世界'], 'こんにちは'))
170 call assert_equal([['ンヹㄇヺヴ'], [[1, 3]]], matchfuzzypos(['ンヹㄇヺヴ'], 'ヹヺ'))
171 " reverse the order of characters
172 call assert_equal([[], []], matchfuzzypos(['ンヹㄇヺヴ'], 'ヺヹ'))
173 call assert_equal([['αβΩxxx', 'xαxβxΩx'], [[0, 1, 2], [1, 3, 5]]],
174 \ matchfuzzypos(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
175 call assert_equal([['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
176 \ [[0, 1], [0, 1], [0, 1], [0, 2]]],
177 \ matchfuzzypos(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
178 call assert_equal([['ααααααα'], [[0, 1, 2]]],
179 \ matchfuzzypos(['ααααααα'], 'ααα'))
180
181 call assert_equal([[], []], matchfuzzypos(['ンヹㄇ', 'ŗŝţ'], 'fffifl'))
182 let x = matchfuzzypos([repeat('Ψ', 256)], repeat('Ψ', 256))
183 call assert_equal(range(256), x[1][0])
184 call assert_equal([[], []], matchfuzzypos([repeat('✓', 300)], repeat('✓', 257)))
185
186 " match in a long string
187 call assert_equal([[repeat('♪', 300) .. '✗✗✗'], [[300, 301, 302]]],
188 \ matchfuzzypos([repeat('♪', 300) .. '✗✗✗'], '✗✗✗'))
189 " preference for camel case match
190 call assert_equal([['xѳѵҁxxѳѴҁ'], [[6, 7, 8]]], matchfuzzypos(['xѳѵҁxxѳѴҁ'], 'ѳѵҁ'))
191 " preference for match after a separator (_ or space)
192 call assert_equal([['xちだx_ちだ'], [[5, 6]]], matchfuzzypos(['xちだx_ちだ'], 'ちだ'))
193 " preference for leading letter match
194 call assert_equal([['ѳѵҁxѳѵҁ'], [[0, 1]]], matchfuzzypos(['ѳѵҁxѳѵҁ'], 'ѳѵ'))
195 " preference for sequential match
196 call assert_equal([['aンbヹcㄇdンヹㄇ'], [[7, 8, 9]]], matchfuzzypos(['aンbヹcㄇdンヹㄇ'], 'ンヹㄇ'))
197 " best recursive match
198 call assert_equal([['xффйд'], [[2, 3, 4]]], matchfuzzypos(['xффйд'], 'фйд'))
199endfunc
200
201" vim: shiftwidth=2 sts=2 expandtab