robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqvim-patch:9.2.0647: matchfuzzypos() false exact match for long equal-length candidates (#40257)3aea851bfe

master
21.3 KiB376 linesraw
1" Tests for fuzzy matching
2
3source shared.vim
4source check.vim
5source term_util.vim
6
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(v:_null_list, 'foo')", 'E686:')
12  call assert_fails('call matchfuzzy(["abc"], v:_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'], matchfuzzy(['onetwo', 'oneTwo'], 'oneTwo'))
22  call assert_equal([], matchfuzzy(['onetwo', 'one_two'], 'oneTwo'))
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([repeat('a', 300)], matchfuzzy([repeat('a', 300)], repeat('a', 257)))
26  " full match has highest score
27  call assert_equal(['Cursor', 'lCursor'], matchfuzzy(["hello", "lCursor", "Cursor"], "Cursor"))
28  " matches with same score should not be reordered
29  let l = ['abc1', 'abc2', 'abc3']
30  call assert_equal(l, l->matchfuzzy('abc'))
31
32  " Tests for match preferences
33  call assert_equal(['onetwo', 'oneTwo'], ['onetwo', 'oneTwo']->matchfuzzy('onetwo'))
34  " preference for match after a separator (_ or space)
35  call assert_equal(['onetwo', 'one_two', 'one two'], ['onetwo', 'one_two', 'one two']->matchfuzzy('onetwo'))
36  " preference for leading letter match
37  call assert_equal(['onetwo', 'xonetwo'], ['xonetwo', 'onetwo']->matchfuzzy('onetwo'))
38  " preference for sequential match
39  call assert_equal(['onetwo', 'oanbectdweo'], ['oanbectdweo', 'onetwo']->matchfuzzy('onetwo'))
40  " non-matching leading letter(s) penalty
41  call assert_equal(['xonetwo', 'xxonetwo'], ['xxonetwo', 'xonetwo']->matchfuzzy('onetwo'))
42  " total non-matching letter(s) penalty
43  call assert_equal(['one', 'onex', 'onexx'], ['onexx', 'one', 'onex']->matchfuzzy('one'))
44  " prefer complete matches over separator matches
45  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'))
46  " gap penalty
47  call assert_equal(['xxayybxxxx', 'xxayyybxxx', 'xxayyyybxx'], ['xxayyyybxx', 'xxayyybxxx', 'xxayybxxxx']->matchfuzzy('ab'))
48  " path separator vs word separator
49  call assert_equal(['color/setup.vim', 'color setup.vim', 'color_setup.vim', 'colorsetup.vim', 'color\\setup.vim'], matchfuzzy(['colorsetup.vim', 'color setup.vim', 'color/setup.vim', 'color_setup.vim', 'color\\setup.vim'], 'setup.vim'))
50
51  " match multiple words (separated by space)
52  call assert_equal(['foo bar baz'], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('baz foo'))
53  call assert_equal([], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('one two'))
54  call assert_equal([], ['foo bar']->matchfuzzy(" \t "))
55
56  " test for matching a sequence of words
57  call assert_equal(['bar foo'], ['foo bar', 'bar foo', 'foobar', 'barfoo']->matchfuzzy('bar foo', {'matchseq' : 1}))
58  call assert_equal([#{text: 'two one'}], [#{text: 'one two'}, #{text: 'two one'}]->matchfuzzy('two one', #{key: 'text', matchseq: v:true}))
59
60  %bw!
61  eval ['somebuf', 'anotherone', 'needle', 'yetanotherone']->map({_, v -> bufadd(v) + bufload(v)})
62  let l = getbufinfo()->map({_, v -> fnamemodify(v.name, ':t')})->matchfuzzy('ndl')
63  call assert_equal(1, len(l))
64  call assert_match('needle', l[0])
65  %bw!
66
67  " Test for fuzzy matching dicts
68  let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
69  call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'text_cb' : {v -> v.val}}))
70  call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'key' : 'val'}))
71  call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> v.val}}))
72  call assert_equal([], matchfuzzy(l, 'day', {'key' : 'val'}))
73  call assert_fails("let x = matchfuzzy(l, 'cam', 'random')", 'E1206:')
74  call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> []}}))
75  call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> 1}}))
76  call assert_fails("let x = matchfuzzy(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
77  call assert_equal([], matchfuzzy(l, 'cam'))
78  " Nvim's callback implementation is different, so E6000 is expected instead,
79  " call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E921:')
80  call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E6000:')
81  call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : 123})", 'E475: Invalid value for argument key: 123')
82  call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E730:')
83  call assert_fails("let x = matchfuzzy(l, 'cam', v:_null_dict)", 'E1297:')
84  call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : v:_null_string})", 'E475:')
85  " Nvim doesn't have null functions
86  " call assert_fails("let x = matchfuzzy(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
87  " matches with same score should not be reordered
88  let l = [#{text: 'abc', id: 1}, #{text: 'abc', id: 2}, #{text: 'abc', id: 3}]
89  call assert_equal(l, l->matchfuzzy('abc', #{key: 'text'}))
90
91  let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
92  call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : 'name'})", 'E730:')
93
94  " camelcase
95  call assert_equal(['Cursor', 'CurSearch', 'CursorLine', 'lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor'],
96        \ matchfuzzy(['Cursor', 'lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'CurSearch', 'CursorLine'], 'Cur'))
97  call assert_equal(['things', 'sThings', 'thisThings'],
98        \ matchfuzzy(['things','sThings', 'thisThings'], 'thin'))
99  call assert_equal(['MyTestCase', 'mytestcase'], matchfuzzy(['mytestcase', 'MyTestCase'], 'mtc'))
100  call assert_equal(['MyTestCase', 'mytestcase'], matchfuzzy(['MyTestCase', 'mytestcase'], 'mtc'))
101  call assert_equal(['MyTest'], matchfuzzy(['Mytest', 'mytest', 'MyTest'], 'MyT'))
102  call assert_equal(['CamelCaseMatchIngAlg'],
103      \ matchfuzzy(['CamelCaseMatchIngAlg', 'camelCaseMatchingAlg', 'camelcasematchingalg'], 'CamelCase'))
104  call assert_equal(['SomeWord', 'PrefixSomeWord'], matchfuzzy(['PrefixSomeWord', 'SomeWord'], 'somewor'))
105
106  " Test in latin1 encoding
107  let save_enc = &encoding
108  " Nvim supports utf-8 encoding only
109  " set encoding=latin1
110  call assert_equal(['abc'], matchfuzzy(['abc'], 'abc'))
111  let &encoding = save_enc
112endfunc
113
114" Test for the matchfuzzypos() function
115func Test_matchfuzzypos()
116  call assert_equal([['curl', 'world'], [[2,3], [2,3]], [990, 985]], matchfuzzypos(['world', 'curl'], 'rl'))
117  call assert_equal([['curl', 'world'], [[2,3], [2,3]], [990, 985]], matchfuzzypos(['world', 'one', 'curl'], 'rl'))
118  call assert_equal([['hello', 'hello world hello world'],
119        \ [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], [2147483647, 4810]],
120        \ matchfuzzypos(['hello world hello world', 'hello', 'world'], 'hello'))
121  call assert_equal([['aaaaaaa'], [[0, 1, 2]], [2880]], matchfuzzypos(['aaaaaaa'], 'aaa'))
122  call assert_equal([['a  b'], [[0, 3]], [1670]], matchfuzzypos(['a  b'], 'a  b'))
123  call assert_equal([['a  b'], [[0, 3]], [1670]], matchfuzzypos(['a  b'], 'a    b'))
124  call assert_equal([['a  b'], [[0]], [885]], matchfuzzypos(['a  b'], '  a  '))
125  call assert_equal([[], [], []], matchfuzzypos(['a  b'], '  '))
126  call assert_equal([[], [], []], matchfuzzypos(['world', 'curl'], 'ab'))
127  let x = matchfuzzypos([repeat('a', 256)], repeat('a', 256))
128  call assert_equal(range(256), x[1][0])
129
130  " fuzzy matches limited to 1024 chars
131  let matches = matchfuzzypos([repeat('a', 1030)], repeat('a', 1025))
132  call assert_equal([repeat('a', 1030)], matches[0])
133  call assert_equal(1024, len(matches[1][0]))
134  call assert_equal(1023, matches[1][0][1023])
135
136  call assert_equal([[], [], []], matchfuzzypos([], 'abc'))
137  call assert_equal([[], [], []], matchfuzzypos(['abc'], ''))
138
139  " match in a long string
140  call assert_equal([[repeat('x', 300) .. 'abc'], [[300, 301, 302]], [500]],
141        \ matchfuzzypos([repeat('x', 300) .. 'abc'], 'abc'))
142
143  " preference for camel case match
144  call assert_equal([['xabcxxaBc'], [[7, 8]], [1665]], matchfuzzypos(['xabcxxaBc'], 'bc'))
145  " preference for match after a separator (_ or space)
146  call assert_equal([['xabx_ab'], [[5, 6]], [1775]], matchfuzzypos(['xabx_ab'], 'ab'))
147  " preference for leading letter match
148  call assert_equal([['abcxabc'], [[0, 1]], [1875]], matchfuzzypos(['abcxabc'], 'ab'))
149  " preference for sequential match
150  call assert_equal([['aobncedone'], [[7, 8, 9]], [1965]], matchfuzzypos(['aobncedone'], 'one'))
151  " best recursive match
152  call assert_equal([['xoone'], [[2, 3, 4]], [1990]], matchfuzzypos(['xoone'], 'one'))
153
154  " match multiple words (separated by space)
155  call assert_equal([['foo bar baz'], [[8, 9, 10, 0, 1, 2]], [5620]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('baz foo'))
156  call assert_equal([[], [], []], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('baz foo', {'matchseq': 1}))
157  call assert_equal([['foo bar baz'], [[0, 1, 2, 8, 9, 10]], [5620]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('foo baz'))
158  call assert_equal([['foo bar baz'], [[0, 1, 2, 3, 4, 9, 10]], [6660]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('foo baz', {'matchseq': 1}))
159  call assert_equal([[], [], []], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('one two'))
160  call assert_equal([[], [], []], ['foo bar']->matchfuzzypos(" \t "))
161  call assert_equal([['grace'], [[1, 2, 3, 4, 2, 3, 4, 0, 1, 2, 3, 4]], [2147483647]], ['grace']->matchfuzzypos('race ace grace'))
162
163  let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
164  call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [2885]],
165        \ matchfuzzypos(l, 'cam', {'text_cb' : {v -> v.val}}))
166  call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [2885]],
167        \ matchfuzzypos(l, 'cam', {'key' : 'val'}))
168  call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> v.val}}))
169  call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'key' : 'val'}))
170  call assert_fails("let x = matchfuzzypos(l, 'cam', 'random')", 'E1206:')
171  call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> []}}))
172  call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> 1}}))
173  call assert_fails("let x = matchfuzzypos(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
174  call assert_equal([[], [], []], matchfuzzypos(l, 'cam'))
175  " Nvim's callback implementation is different, so E6000 is expected instead,
176  " call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E921:')
177  call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E6000:')
178  call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : 123})", 'E475: Invalid value for argument key: 123')
179  call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E730:')
180  call assert_fails("let x = matchfuzzypos(l, 'cam', v:_null_dict)", 'E1297:')
181  call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : v:_null_string})", 'E475:')
182  " Nvim doesn't have null functions
183  " call assert_fails("let x = matchfuzzypos(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
184
185  " case match
186  call assert_equal([['Match'], [[0, 1]], [1885]], matchfuzzypos(['match', 'Match'], 'Ma'))
187  call assert_equal([['match', 'Match'], [[0, 1], [0, 1]], [1885, 1885]], matchfuzzypos(['Match', 'match'], 'ma'))
188
189  let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
190  call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : 'name'})", 'E730:')
191
192  " camelcase
193  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]], [2885, 2870, 2865, 2680, 2670, 2655, 2655]],
194        \ matchfuzzypos(['Cursor', 'lCursor', 'shCurlyIn', 'shCurlyError', 'TracesCursor', 'CurSearch', 'CursorLine'], 'Cur'))
195  call assert_equal([['things', 'sThings', 'thisThings'], [[0, 1, 2, 3], [1, 2, 3, 4], [0, 1, 6, 7]], [3890, 3685, 3670]],
196        \ matchfuzzypos(['things','sThings', 'thisThings'], 'thin'))
197endfunc
198
199" Test for matchfuzzy() with multibyte characters
200func Test_matchfuzzy_mbyte()
201  CheckFeature multi_lang
202  call assert_equal(['ンヹㄇヺヴ'], matchfuzzy(['ンヹㄇヺヴ'], 'ヹヺ'))
203  " reverse the order of characters
204  call assert_equal([], matchfuzzy(['ンヹㄇヺヴ'], 'ヺヹ'))
205  call assert_equal(['αβΩxxx', 'xαxβxΩx'],
206        \ matchfuzzy(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
207  call assert_equal(['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
208        \ matchfuzzy(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
209
210  " match multiple words (separated by space)
211  call assert_equal(['세 마리의 작은 돼지'], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('돼지 마리의'))
212  call assert_equal([], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('파란 하늘'))
213
214  " camel case match
215  call assert_equal(['oneąwo', 'oneĄwo'],
216        \ ['oneĄwo', 'oneąwo']->matchfuzzy('oneąwo'))
217  call assert_equal(['oneĄwo'],
218        \ ['oneĄwo', 'oneąwo']->matchfuzzy('Ąwo'))
219  " prefer camelcase when searched first character matches upper case
220  call assert_equal(['oneĄwo', 'oneąwo'],
221        \ ['oneĄwo', 'oneąwo']->matchfuzzy('ąw'))
222  " preference for complete match then match after separator (_ or space)
223  call assert_equal(['ⅠⅡabㄟㄠ'] + sort(['ⅠⅡa_bㄟㄠ', 'ⅠⅡa bㄟㄠ']),
224          \ ['ⅠⅡabㄟㄠ', 'ⅠⅡa bㄟㄠ', 'ⅠⅡa_bㄟㄠ']->matchfuzzy('ⅠⅡabㄟㄠ'))
225  " preference for match after a separator (_ or space)
226  call assert_equal(['ㄓㄔabㄟㄠ', 'ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ'],
227        \ ['ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ', 'ㄓㄔabㄟㄠ']->matchfuzzy('ㄓㄔabㄟㄠ'))
228  " preference for leading letter match
229  call assert_equal(['ŗŝţũŵż', 'xŗŝţũŵż'],
230        \ ['xŗŝţũŵż', 'ŗŝţũŵż']->matchfuzzy('ŗŝţũŵż'))
231  " preference for sequential match
232  call assert_equal(['ㄞㄡㄤfffifl', 'ㄞaㄡbㄤcffdfiefl'],
233        \ ['ㄞaㄡbㄤcffdfiefl', 'ㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
234  " non-matching leading letter(s) penalty
235  call assert_equal(['xㄞㄡㄤfffifl', 'xxㄞㄡㄤfffifl'],
236        \ ['xxㄞㄡㄤfffifl', 'xㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
237  " total non-matching letter(s) penalty
238  call assert_equal(['ŗŝţ', 'ŗŝţx', 'ŗŝţxx'],
239        \ ['ŗŝţxx', 'ŗŝţ', 'ŗŝţx']->matchfuzzy('ŗŝţ'))
240endfunc
241
242" Test for matchfuzzypos() with multibyte characters
243func Test_matchfuzzypos_mbyte()
244  CheckFeature multi_lang
245  call assert_equal([['こんにちは世界'], [[0, 1, 2, 3, 4]], [4890]],
246        \ matchfuzzypos(['こんにちは世界'], 'こんにちは'))
247  call assert_equal([['ンヹㄇヺヴ'], [[1, 3]], [-20]], matchfuzzypos(['ンヹㄇヺヴ'], 'ヹヺ'))
248  " reverse the order of characters
249  call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇヺヴ'], 'ヺヹ'))
250  call assert_equal([['αβΩxxx', 'xαxβxΩx'], [[0, 1, 2], [1, 3, 5]], [2885, 670]],
251        \ matchfuzzypos(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
252  call assert_equal([['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
253        \ [[0, 1], [0, 1], [0, 1], [0, 2]], [1880, 1865, 1850, 890]],
254        \ matchfuzzypos(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
255  call assert_equal([['ααααααα'], [[0, 1, 2]], [2880]],
256        \ matchfuzzypos(['ααααααα'], 'ααα'))
257
258  call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇ', 'ŗŝţ'], 'fffifl'))
259  let x = matchfuzzypos([repeat('Ψ', 256)], repeat('Ψ', 256))
260  call assert_equal(range(256), x[1][0])
261  call assert_equal([repeat('✓', 300)], matchfuzzypos([repeat('✓', 300)], repeat('✓', 257))[0])
262
263  " match multiple words (separated by space)
264  call assert_equal([['세 마리의 작은 돼지'], [[9, 10, 2, 3, 4]], [4515]], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('돼지 마리의'))
265  call assert_equal([[], [], []], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('파란 하늘'))
266
267  " match in a long string
268  call assert_equal([[repeat('ぶ', 300) .. 'ẼẼẼ'], [[300, 301, 302]], [500]],
269        \ matchfuzzypos([repeat('ぶ', 300) .. 'ẼẼẼ'], 'ẼẼẼ'))
270  " preference for match after a separator (_ or space)
271  call assert_equal([['xちだx_ちだ'], [[5, 6]], [1775]], matchfuzzypos(['xちだx_ちだ'], 'ちだ'))
272  " preference for leading letter match
273  call assert_equal([['ѳѵҁxѳѵҁ'], [[0, 1]], [1875]], matchfuzzypos(['ѳѵҁxѳѵҁ'], 'ѳѵ'))
274  " preference for sequential match
275  call assert_equal([['aンbヹcㄇdンヹㄇ'], [[7, 8, 9]], [1965]], matchfuzzypos(['aンbヹcㄇdンヹㄇ'], 'ンヹㄇ'))
276  " best recursive match
277  call assert_equal([['xффйд'], [[2, 3, 4]], [1990]], matchfuzzypos(['xффйд'], 'фйд'))
278endfunc
279
280" Test for matchfuzzy() with limit
281func Test_matchfuzzy_limit()
282  let x = ['1', '2', '3', '2']
283  call assert_equal(['2', '2'], x->matchfuzzy('2'))
284  call assert_equal(['2', '2'], x->matchfuzzy('2', #{}))
285  call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 0}))
286  call assert_equal(['2'], x->matchfuzzy('2', #{limit: 1}))
287  call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 2}))
288  call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 3}))
289  call assert_fails("call matchfuzzy(x, '2', #{limit: '2'})", 'E475:')
290  call assert_fails("call matchfuzzy(x, '2', #{limit: []})", 'E475:')
291
292  let l = [{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}]
293  call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}}))
294  call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{key: 'val'}))
295  call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}, limit: 0}))
296  call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{key: 'val', limit: 0}))
297  call assert_equal([{'id': 5, 'val': 'crayon'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}, limit: 1}))
298  call assert_equal([{'id': 5, 'val': 'crayon'}], l->matchfuzzy('c', #{key: 'val', limit: 1}))
299endfunc
300
301" This was using uninitialized memory
302func Test_matchfuzzy_initialized()
303  CheckRunVimInTerminal
304
305  " This can take a very long time (esp. when using valgrind).  Run in a
306  " separate Vim instance and kill it after two seconds.  We only check for
307  " memory errors.
308  let lines =<< trim END
309      lvimgrep [ss [fg*
310  END
311  call writefile(lines, 'XTest_matchfuzzy', 'D')
312
313  let buf = RunVimInTerminal('-u NONE -X -Z', {})
314  call term_sendkeys(buf, ":source XTest_matchfuzzy\n")
315  " Use term_wait directly rather than the TermWait wrapper; otherwise,
316  " retries become very slow.
317  call term_wait(buf, 2000)
318
319  let job = term_getjob(buf)
320  if job_status(job) == "run"
321    call job_stop(job, "int")
322    " The search might or might not have been completed. If the search is
323    " finished and Vim receives a SIGINT, then that will trigger a message
324    " next time Vim is active:
325    "   Type  :qa  and press <Enter> to exit Vim
326    " If we do not send something here to trigger displaying the message, before
327    " TermWait(), then the exit sequence sent afterward does not work.
328    call term_sendkeys(buf, "\<C-O>")
329    call TermWait(buf, 50)
330  endif
331
332  " clean up
333  call StopVimInTerminal(buf)
334endfunc
335
336func Test_matchfuzzy_long_multiword_no_overflow()
337  let word = repeat('a', 100)
338  let pat_ok = repeat(word . ' ', 9) . word
339  call assert_equal([word], matchfuzzy([word], pat_ok))
340
341  let pat_overflow = repeat(word . ' ', 14) . word
342  call assert_equal([[], [], []], matchfuzzypos([word], pat_overflow))
343endfunc
344
345func Test_matchfuzzy_oversized_candidate()
346  let str = repeat('a', 1024) .. 'z'
347  call assert_equal([], matchfuzzy([str], 'az'))
348  call assert_equal([[], [], []], matchfuzzypos([str], 'az'))
349
350  call assert_equal([str], matchfuzzy([str], 'a'))
351  let r = matchfuzzypos([str], 'a')
352  call assert_equal([str], r[0])
353  call assert_equal([0], r[1][0])
354
355  let edge = 'a' .. repeat('x', 1022) .. 'a'
356  call assert_equal([edge], matchfuzzy([edge], 'aa'))
357  let e = matchfuzzypos([edge], 'aa')
358  call assert_equal([edge], e[0])
359  call assert_equal([0, 1023], e[1][0])
360
361  let cand = 'x' .. repeat('a', 1024)
362  call assert_equal([], matchfuzzy([cand], repeat('a', 1024)))
363  call assert_equal([[], [], []], matchfuzzypos([cand], repeat('a', 1024)))
364endfunc
365
366func Test_matchfuzzy_long_candidate_mbyte()
367  let ok = repeat('好', 1023) .. '界'
368  call assert_equal([ok], matchfuzzy([ok], '好界'))
369  call assert_notequal([], matchfuzzypos([ok], '好界')[0])
370
371  let toolong = repeat('好', 1024) .. '界'
372  call assert_equal([], matchfuzzy([toolong], '好界'))
373  call assert_equal([[], [], []], matchfuzzypos([toolong], '好界'))
374endfunc
375
376" vim: shiftwidth=2 sts=2 expandtab