blob: cba08446b5bcaf4871922f49174bdceba9077b6d [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)))
glepnir5a049992024-12-26 15:38:39 +010026 " full match has highest score
27 call assert_equal(['Cursor', 'lCursor'], matchfuzzy(["hello", "lCursor", "Cursor"], "Cursor"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020028 " matches with same score should not be reordered
29 let l = ['abc1', 'abc2', 'abc3']
30 call assert_equal(l, l->matchfuzzy('abc'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020031
32 " Tests for match preferences
33 " preference for camel case match
34 call assert_equal(['oneTwo', 'onetwo'], ['onetwo', 'oneTwo']->matchfuzzy('onetwo'))
35 " preference for match after a separator (_ or space)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020036 call assert_equal(['onetwo', 'one_two', 'one two'], ['onetwo', 'one_two', 'one two']->matchfuzzy('onetwo'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020037 " preference for leading letter match
38 call assert_equal(['onetwo', 'xonetwo'], ['xonetwo', 'onetwo']->matchfuzzy('onetwo'))
39 " preference for sequential match
40 call assert_equal(['onetwo', 'oanbectdweo'], ['oanbectdweo', 'onetwo']->matchfuzzy('onetwo'))
41 " non-matching leading letter(s) penalty
42 call assert_equal(['xonetwo', 'xxonetwo'], ['xxonetwo', 'xonetwo']->matchfuzzy('onetwo'))
43 " total non-matching letter(s) penalty
44 call assert_equal(['one', 'onex', 'onexx'], ['onexx', 'one', 'onex']->matchfuzzy('one'))
Bram Moolenaare9f9f162020-10-20 19:01:30 +020045 " prefer complete matches over separator matches
46 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 +020047 " gap penalty
48 call assert_equal(['xxayybxxxx', 'xxayyybxxx', 'xxayyyybxx'], ['xxayyyybxx', 'xxayyybxxx', 'xxayybxxxx']->matchfuzzy('ab'))
Bram Moolenaardcdd42a2020-10-29 18:58:01 +010049 " path separator vs word separator
50 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 +020051
52 " match multiple words (separated by space)
53 call assert_equal(['foo bar baz'], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('baz foo'))
54 call assert_equal([], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('one two'))
55 call assert_equal([], ['foo bar']->matchfuzzy(" \t "))
56
57 " test for matching a sequence of words
58 call assert_equal(['bar foo'], ['foo bar', 'bar foo', 'foobar', 'barfoo']->matchfuzzy('bar foo', {'matchseq' : 1}))
59 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 +020060
61 %bw!
62 eval ['somebuf', 'anotherone', 'needle', 'yetanotherone']->map({_, v -> bufadd(v) + bufload(v)})
Bram Moolenaar22e7e862022-07-02 20:48:01 +010063 let l = getbufinfo()->map({_, v -> fnamemodify(v.name, ':t')})->matchfuzzy('ndl')
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020064 call assert_equal(1, len(l))
65 call assert_match('needle', l[0])
66
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020067 " Test for fuzzy matching dicts
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020068 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'}))
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +010073 call assert_fails("let x = matchfuzzy(l, 'cam', 'random')", 'E1206:')
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020074 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 call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E921:')
79 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E730:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +010080 call assert_fails("let x = matchfuzzy(l, 'cam', test_null_dict())", 'E1297:')
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020081 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : test_null_string()})", 'E475:')
82 call assert_fails("let x = matchfuzzy(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020083 " matches with same score should not be reordered
84 let l = [#{text: 'abc', id: 1}, #{text: 'abc', id: 2}, #{text: 'abc', id: 3}]
85 call assert_equal(l, l->matchfuzzy('abc', #{key: 'text'}))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020086
87 let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
88 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : 'name'})", 'E730:')
89
90 " Test in latin1 encoding
91 let save_enc = &encoding
92 set encoding=latin1
93 call assert_equal(['abc'], matchfuzzy(['abc'], 'abc'))
94 let &encoding = save_enc
95endfunc
96
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020097" Test for the matchfuzzypos() function
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020098func Test_matchfuzzypos()
glepnir9dfc7e52025-01-21 22:33:13 +010099 call assert_equal([['curl', 'world'], [[2,3], [2,3]], [178, 177]], matchfuzzypos(['world', 'curl'], 'rl'))
100 call assert_equal([['curl', 'world'], [[2,3], [2,3]], [178, 177]], matchfuzzypos(['world', 'one', 'curl'], 'rl'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200101 call assert_equal([['hello', 'hello world hello world'],
glepnir9dfc7e52025-01-21 22:33:13 +0100102 \ [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], [500, 382]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200103 \ matchfuzzypos(['hello world hello world', 'hello', 'world'], 'hello'))
glepnir9dfc7e52025-01-21 22:33:13 +0100104 call assert_equal([['aaaaaaa'], [[0, 1, 2]], [266]], matchfuzzypos(['aaaaaaa'], 'aaa'))
105 call assert_equal([['a b'], [[0, 3]], [269]], matchfuzzypos(['a b'], 'a b'))
106 call assert_equal([['a b'], [[0, 3]], [269]], matchfuzzypos(['a b'], 'a b'))
107 call assert_equal([['a b'], [[0]], [137]], matchfuzzypos(['a b'], ' a '))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100108 call assert_equal([[], [], []], matchfuzzypos(['a b'], ' '))
109 call assert_equal([[], [], []], matchfuzzypos(['world', 'curl'], 'ab'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200110 let x = matchfuzzypos([repeat('a', 256)], repeat('a', 256))
111 call assert_equal(range(256), x[1][0])
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100112 call assert_equal([[], [], []], matchfuzzypos([repeat('a', 300)], repeat('a', 257)))
113 call assert_equal([[], [], []], matchfuzzypos([], 'abc'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200114
115 " match in a long string
glepnir9dfc7e52025-01-21 22:33:13 +0100116 call assert_equal([[repeat('x', 300) .. 'abc'], [[300, 301, 302]], [-60]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200117 \ matchfuzzypos([repeat('x', 300) .. 'abc'], 'abc'))
118
119 " preference for camel case match
glepnir9dfc7e52025-01-21 22:33:13 +0100120 call assert_equal([['xabcxxaBc'], [[6, 7, 8]], [269]], matchfuzzypos(['xabcxxaBc'], 'abc'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200121 " preference for match after a separator (_ or space)
glepnir9dfc7e52025-01-21 22:33:13 +0100122 call assert_equal([['xabx_ab'], [[5, 6]], [195]], matchfuzzypos(['xabx_ab'], 'ab'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200123 " preference for leading letter match
glepnir9dfc7e52025-01-21 22:33:13 +0100124 call assert_equal([['abcxabc'], [[0, 1]], [200]], matchfuzzypos(['abcxabc'], 'ab'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200125 " preference for sequential match
glepnir9dfc7e52025-01-21 22:33:13 +0100126 call assert_equal([['aobncedone'], [[7, 8, 9]], [233]], matchfuzzypos(['aobncedone'], 'one'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200127 " best recursive match
glepnir9dfc7e52025-01-21 22:33:13 +0100128 call assert_equal([['xoone'], [[2, 3, 4]], [243]], matchfuzzypos(['xoone'], 'one'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200129
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200130 " match multiple words (separated by space)
glepnir9dfc7e52025-01-21 22:33:13 +0100131 call assert_equal([['foo bar baz'], [[8, 9, 10, 0, 1, 2]], [519]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('baz foo'))
zeertzjq9af2bc02022-05-11 14:15:37 +0100132 call assert_equal([[], [], []], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('baz foo', {'matchseq': 1}))
glepnir9dfc7e52025-01-21 22:33:13 +0100133 call assert_equal([['foo bar baz'], [[0, 1, 2, 8, 9, 10]], [519]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('foo baz'))
134 call assert_equal([['foo bar baz'], [[0, 1, 2, 3, 4, 5, 10]], [476]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('foo baz', {'matchseq': 1}))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100135 call assert_equal([[], [], []], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('one two'))
136 call assert_equal([[], [], []], ['foo bar']->matchfuzzypos(" \t "))
glepnir9dfc7e52025-01-21 22:33:13 +0100137 call assert_equal([['grace'], [[1, 2, 3, 4, 2, 3, 4, 0, 1, 2, 3, 4]], [1057]], ['grace']->matchfuzzypos('race ace grace'))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200138
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200139 let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
glepnir9dfc7e52025-01-21 22:33:13 +0100140 call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [267]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200141 \ matchfuzzypos(l, 'cam', {'text_cb' : {v -> v.val}}))
glepnir9dfc7e52025-01-21 22:33:13 +0100142 call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [267]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200143 \ matchfuzzypos(l, 'cam', {'key' : 'val'}))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100144 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> v.val}}))
145 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'key' : 'val'}))
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100146 call assert_fails("let x = matchfuzzypos(l, 'cam', 'random')", 'E1206:')
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100147 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> []}}))
148 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> 1}}))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200149 call assert_fails("let x = matchfuzzypos(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100150 call assert_equal([[], [], []], matchfuzzypos(l, 'cam'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200151 call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E921:')
152 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E730:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100153 call assert_fails("let x = matchfuzzypos(l, 'cam', test_null_dict())", 'E1297:')
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200154 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : test_null_string()})", 'E475:')
155 call assert_fails("let x = matchfuzzypos(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
156
glepnir9dfc7e52025-01-21 22:33:13 +0100157 " case match
158 call assert_equal([['Match', 'match'], [[0, 1], [0, 1]], [202, 177]], matchfuzzypos(['match', 'Match'], 'Ma'))
159 call assert_equal([['match', 'Match'], [[0, 1], [0, 1]], [202, 177]], matchfuzzypos(['Match', 'match'], 'ma'))
160 " CamelCase has high weight even case match
161 call assert_equal(['MyTestCase', 'mytestcase'], matchfuzzy(['mytestcase', 'MyTestCase'], 'mtc'))
162 call assert_equal(['MyTestCase', 'mytestcase'], matchfuzzy(['MyTestCase', 'mytestcase'], 'mtc'))
163 call assert_equal(['MyTest', 'Mytest', 'mytest', ],matchfuzzy(['Mytest', 'mytest', 'MyTest'], 'MyT'))
164 call assert_equal(['CamelCaseMatchIngAlg', 'camelCaseMatchingAlg', 'camelcasematchingalg'],
165 \ matchfuzzy(['CamelCaseMatchIngAlg', 'camelcasematchingalg', 'camelCaseMatchingAlg'], 'CamelCase'))
166 call assert_equal(['CamelCaseMatchIngAlg', 'camelCaseMatchingAlg', 'camelcasematchingalg'],
167 \ matchfuzzy(['CamelCaseMatchIngAlg', 'camelcasematchingalg', 'camelCaseMatchingAlg'], 'CamelcaseM'))
168
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200169 let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
170 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : 'name'})", 'E730:')
171endfunc
172
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200173" Test for matchfuzzy() with multibyte characters
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200174func Test_matchfuzzy_mbyte()
175 CheckFeature multi_lang
176 call assert_equal(['ンヹㄇヺヴ'], matchfuzzy(['ンヹㄇヺヴ'], 'ヹヺ'))
177 " reverse the order of characters
178 call assert_equal([], matchfuzzy(['ンヹㄇヺヴ'], 'ヺヹ'))
179 call assert_equal(['αβΩxxx', 'xαxβxΩx'],
180 \ matchfuzzy(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
181 call assert_equal(['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
182 \ matchfuzzy(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
183
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200184 " match multiple words (separated by space)
185 call assert_equal(['세 마리의 작은 돼지'], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('돼지 마리의'))
186 call assert_equal([], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('파란 하늘'))
187
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200188 " preference for camel case match
189 call assert_equal(['oneĄwo', 'oneąwo'],
190 \ ['oneąwo', 'oneĄwo']->matchfuzzy('oneąwo'))
Bram Moolenaare9f9f162020-10-20 19:01:30 +0200191 " preference for complete match then match after separator (_ or space)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200192 call assert_equal(['ⅠⅡabㄟㄠ'] + sort(['ⅠⅡa_bㄟㄠ', 'ⅠⅡa bㄟㄠ']),
Bram Moolenaare9f9f162020-10-20 19:01:30 +0200193 \ ['ⅠⅡabㄟㄠ', 'ⅠⅡa bㄟㄠ', 'ⅠⅡa_bㄟㄠ']->matchfuzzy('ⅠⅡabㄟㄠ'))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200194 " preference for match after a separator (_ or space)
195 call assert_equal(['ㄓㄔabㄟㄠ', 'ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ'],
196 \ ['ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ', 'ㄓㄔabㄟㄠ']->matchfuzzy('ㄓㄔabㄟㄠ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200197 " preference for leading letter match
198 call assert_equal(['ŗŝţũŵż', 'xŗŝţũŵż'],
199 \ ['xŗŝţũŵż', 'ŗŝţũŵż']->matchfuzzy('ŗŝţũŵż'))
200 " preference for sequential match
201 call assert_equal(['ㄞㄡㄤfffifl', 'ㄞaㄡbㄤcffdfiefl'],
202 \ ['ㄞaㄡbㄤcffdfiefl', 'ㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
203 " non-matching leading letter(s) penalty
204 call assert_equal(['xㄞㄡㄤfffifl', 'xxㄞㄡㄤfffifl'],
205 \ ['xxㄞㄡㄤfffifl', 'xㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
206 " total non-matching letter(s) penalty
207 call assert_equal(['ŗŝţ', 'ŗŝţx', 'ŗŝţxx'],
208 \ ['ŗŝţxx', 'ŗŝţ', 'ŗŝţx']->matchfuzzy('ŗŝţ'))
209endfunc
210
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200211" Test for matchfuzzypos() with multibyte characters
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200212func Test_matchfuzzypos_mbyte()
213 CheckFeature multi_lang
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100214 call assert_equal([['こんにちは世界'], [[0, 1, 2, 3, 4]], [273]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200215 \ matchfuzzypos(['こんにちは世界'], 'こんにちは'))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100216 call assert_equal([['ンヹㄇヺヴ'], [[1, 3]], [88]], matchfuzzypos(['ンヹㄇヺヴ'], 'ヹヺ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200217 " reverse the order of characters
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100218 call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇヺヴ'], 'ヺヹ'))
glepnir9dfc7e52025-01-21 22:33:13 +0100219 call assert_equal([['αβΩxxx', 'xαxβxΩx'], [[0, 1, 2], [1, 3, 5]], [252, 143]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200220 \ matchfuzzypos(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
221 call assert_equal([['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
glepnir9dfc7e52025-01-21 22:33:13 +0100222 \ [[0, 1], [0, 1], [0, 1], [0, 2]], [176, 173, 170, 135]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200223 \ matchfuzzypos(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
glepnir9dfc7e52025-01-21 22:33:13 +0100224 call assert_equal([['ααααααα'], [[0, 1, 2]], [216]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200225 \ matchfuzzypos(['ααααααα'], 'ααα'))
226
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100227 call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇ', 'ŗŝţ'], 'fffifl'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200228 let x = matchfuzzypos([repeat('Ψ', 256)], repeat('Ψ', 256))
229 call assert_equal(range(256), x[1][0])
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100230 call assert_equal([[], [], []], matchfuzzypos([repeat('✓', 300)], repeat('✓', 257)))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200231
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200232 " match multiple words (separated by space)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100233 call assert_equal([['세 마리의 작은 돼지'], [[9, 10, 2, 3, 4]], [328]], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('돼지 마리의'))
234 call assert_equal([[], [], []], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('파란 하늘'))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200235
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200236 " match in a long string
glepnir9dfc7e52025-01-21 22:33:13 +0100237 call assert_equal([[repeat('ぶ', 300) .. 'ẼẼẼ'], [[300, 301, 302]], [-110]],
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200238 \ matchfuzzypos([repeat('ぶ', 300) .. 'ẼẼẼ'], 'ẼẼẼ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200239 " preference for camel case match
glepnir9dfc7e52025-01-21 22:33:13 +0100240 call assert_equal([['xѳѵҁxxѳѴҁ'], [[6, 7, 8]], [219]], matchfuzzypos(['xѳѵҁxxѳѴҁ'], 'ѳѵҁ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200241 " preference for match after a separator (_ or space)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100242 call assert_equal([['xちだx_ちだ'], [[5, 6]], [145]], matchfuzzypos(['xちだx_ちだ'], 'ちだ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200243 " preference for leading letter match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100244 call assert_equal([['ѳѵҁxѳѵҁ'], [[0, 1]], [150]], matchfuzzypos(['ѳѵҁxѳѵҁ'], 'ѳѵ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200245 " preference for sequential match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100246 call assert_equal([['aンbヹcㄇdンヹㄇ'], [[7, 8, 9]], [158]], matchfuzzypos(['aンbヹcㄇdンヹㄇ'], 'ンヹㄇ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200247 " best recursive match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100248 call assert_equal([['xффйд'], [[2, 3, 4]], [168]], matchfuzzypos(['xффйд'], 'фйд'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200249endfunc
250
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +0100251" Test for matchfuzzy() with limit
252func Test_matchfuzzy_limit()
253 let x = ['1', '2', '3', '2']
254 call assert_equal(['2', '2'], x->matchfuzzy('2'))
255 call assert_equal(['2', '2'], x->matchfuzzy('2', #{}))
256 call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 0}))
257 call assert_equal(['2'], x->matchfuzzy('2', #{limit: 1}))
258 call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 2}))
259 call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 3}))
260 call assert_fails("call matchfuzzy(x, '2', #{limit: '2'})", 'E475:')
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +0100261
262 let l = [{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}]
263 call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}}))
264 call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{key: 'val'}))
265 call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}, limit: 0}))
266 call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{key: 'val', limit: 0}))
267 call assert_equal([{'id': 5, 'val': 'crayon'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}, limit: 1}))
268 call assert_equal([{'id': 5, 'val': 'crayon'}], l->matchfuzzy('c', #{key: 'val', limit: 1}))
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +0100269endfunc
270
Bram Moolenaarcaf642c2023-04-29 21:38:04 +0100271" This was using uninitialized memory
272func Test_matchfuzzy_initialized()
273 CheckRunVimInTerminal
274
275 " This can take a very long time (esp. when using valgrind). Run in a
276 " separate Vim instance and kill it after two seconds. We only check for
277 " memory errors.
278 let lines =<< trim END
279 lvimgrep [ss [fg*
280 END
281 call writefile(lines, 'XTest_matchfuzzy', 'D')
282
283 let buf = RunVimInTerminal('-u NONE -X -Z', {})
284 call term_sendkeys(buf, ":source XTest_matchfuzzy\n")
285 call TermWait(buf, 2000)
286
287 let job = term_getjob(buf)
288 if job_status(job) == "run"
289 call job_stop(job, "int")
290 call TermWait(buf, 50)
291 endif
292
293 " clean up
294 call StopVimInTerminal(buf)
295endfunc
296
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200297" vim: shiftwidth=2 sts=2 expandtab