blob: 26fe04db8ff1a9b0e08184e10da4a3cb0b7680e7 [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)))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020025 " matches with same score should not be reordered
26 let l = ['abc1', 'abc2', 'abc3']
27 call assert_equal(l, l->matchfuzzy('abc'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020028
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)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020033 call assert_equal(['onetwo', 'one_two', 'one two'], ['onetwo', 'one_two', 'one two']->matchfuzzy('onetwo'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020034 " 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'))
Bram Moolenaare9f9f162020-10-20 19:01:30 +020042 " 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'))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020044 " gap penalty
45 call assert_equal(['xxayybxxxx', 'xxayyybxxx', 'xxayyyybxx'], ['xxayyyybxx', 'xxayyybxxx', 'xxayybxxxx']->matchfuzzy('ab'))
Bram Moolenaardcdd42a2020-10-29 18:58:01 +010046 " path separator vs word separator
47 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 +020048
49 " match multiple words (separated by space)
50 call assert_equal(['foo bar baz'], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('baz foo'))
51 call assert_equal([], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzy('one two'))
52 call assert_equal([], ['foo bar']->matchfuzzy(" \t "))
53
54 " test for matching a sequence of words
55 call assert_equal(['bar foo'], ['foo bar', 'bar foo', 'foobar', 'barfoo']->matchfuzzy('bar foo', {'matchseq' : 1}))
56 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 +020057
58 %bw!
59 eval ['somebuf', 'anotherone', 'needle', 'yetanotherone']->map({_, v -> bufadd(v) + bufload(v)})
Bram Moolenaar22e7e862022-07-02 20:48:01 +010060 let l = getbufinfo()->map({_, v -> fnamemodify(v.name, ':t')})->matchfuzzy('ndl')
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020061 call assert_equal(1, len(l))
62 call assert_match('needle', l[0])
63
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020064 " Test for fuzzy matching dicts
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020065 let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
66 call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'text_cb' : {v -> v.val}}))
67 call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'key' : 'val'}))
68 call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> v.val}}))
69 call assert_equal([], matchfuzzy(l, 'day', {'key' : 'val'}))
70 call assert_fails("let x = matchfuzzy(l, 'cam', 'random')", 'E715:')
71 call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> []}}))
72 call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> 1}}))
73 call assert_fails("let x = matchfuzzy(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
74 call assert_equal([], matchfuzzy(l, 'cam'))
75 call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E921:')
76 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E730:')
77 call assert_fails("let x = matchfuzzy(l, 'cam', test_null_dict())", 'E715:')
78 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : test_null_string()})", 'E475:')
79 call assert_fails("let x = matchfuzzy(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020080 " matches with same score should not be reordered
81 let l = [#{text: 'abc', id: 1}, #{text: 'abc', id: 2}, #{text: 'abc', id: 3}]
82 call assert_equal(l, l->matchfuzzy('abc', #{key: 'text'}))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020083
84 let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
85 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : 'name'})", 'E730:')
86
87 " Test in latin1 encoding
88 let save_enc = &encoding
89 set encoding=latin1
90 call assert_equal(['abc'], matchfuzzy(['abc'], 'abc'))
91 let &encoding = save_enc
92endfunc
93
Bram Moolenaar8ded5b62020-10-23 16:50:30 +020094" Test for the matchfuzzypos() function
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020095func Test_matchfuzzypos()
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +010096 call assert_equal([['curl', 'world'], [[2,3], [2,3]], [128, 127]], matchfuzzypos(['world', 'curl'], 'rl'))
97 call assert_equal([['curl', 'world'], [[2,3], [2,3]], [128, 127]], matchfuzzypos(['world', 'one', 'curl'], 'rl'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +020098 call assert_equal([['hello', 'hello world hello world'],
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +010099 \ [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]], [275, 257]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200100 \ matchfuzzypos(['hello world hello world', 'hello', 'world'], 'hello'))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100101 call assert_equal([['aaaaaaa'], [[0, 1, 2]], [191]], matchfuzzypos(['aaaaaaa'], 'aaa'))
102 call assert_equal([['a b'], [[0, 3]], [219]], matchfuzzypos(['a b'], 'a b'))
103 call assert_equal([['a b'], [[0, 3]], [219]], matchfuzzypos(['a b'], 'a b'))
104 call assert_equal([['a b'], [[0]], [112]], matchfuzzypos(['a b'], ' a '))
105 call assert_equal([[], [], []], matchfuzzypos(['a b'], ' '))
106 call assert_equal([[], [], []], matchfuzzypos(['world', 'curl'], 'ab'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200107 let x = matchfuzzypos([repeat('a', 256)], repeat('a', 256))
108 call assert_equal(range(256), x[1][0])
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100109 call assert_equal([[], [], []], matchfuzzypos([repeat('a', 300)], repeat('a', 257)))
110 call assert_equal([[], [], []], matchfuzzypos([], 'abc'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200111
112 " match in a long string
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100113 call assert_equal([[repeat('x', 300) .. 'abc'], [[300, 301, 302]], [-135]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200114 \ matchfuzzypos([repeat('x', 300) .. 'abc'], 'abc'))
115
116 " preference for camel case match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100117 call assert_equal([['xabcxxaBc'], [[6, 7, 8]], [189]], matchfuzzypos(['xabcxxaBc'], 'abc'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200118 " preference for match after a separator (_ or space)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100119 call assert_equal([['xabx_ab'], [[5, 6]], [145]], matchfuzzypos(['xabx_ab'], 'ab'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200120 " preference for leading letter match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100121 call assert_equal([['abcxabc'], [[0, 1]], [150]], matchfuzzypos(['abcxabc'], 'ab'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200122 " preference for sequential match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100123 call assert_equal([['aobncedone'], [[7, 8, 9]], [158]], matchfuzzypos(['aobncedone'], 'one'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200124 " best recursive match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100125 call assert_equal([['xoone'], [[2, 3, 4]], [168]], matchfuzzypos(['xoone'], 'one'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200126
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200127 " match multiple words (separated by space)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100128 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 +0100129 call assert_equal([[], [], []], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('baz foo', {'matchseq': 1}))
130 call assert_equal([['foo bar baz'], [[0, 1, 2, 8, 9, 10]], [369]], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('foo baz'))
131 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 +0100132 call assert_equal([[], [], []], ['foo bar baz', 'foo', 'foo bar', 'baz bar']->matchfuzzypos('one two'))
133 call assert_equal([[], [], []], ['foo bar']->matchfuzzypos(" \t "))
134 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 +0200135
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200136 let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100137 call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [192]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200138 \ matchfuzzypos(l, 'cam', {'text_cb' : {v -> v.val}}))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100139 call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]], [192]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200140 \ matchfuzzypos(l, 'cam', {'key' : 'val'}))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100141 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> v.val}}))
142 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'key' : 'val'}))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200143 call assert_fails("let x = matchfuzzypos(l, 'cam', 'random')", 'E715:')
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100144 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> []}}))
145 call assert_equal([[], [], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> 1}}))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200146 call assert_fails("let x = matchfuzzypos(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100147 call assert_equal([[], [], []], matchfuzzypos(l, 'cam'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200148 call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E921:')
149 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E730:')
150 call assert_fails("let x = matchfuzzypos(l, 'cam', test_null_dict())", 'E715:')
151 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : test_null_string()})", 'E475:')
152 call assert_fails("let x = matchfuzzypos(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
153
154 let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
155 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : 'name'})", 'E730:')
156endfunc
157
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200158" Test for matchfuzzy() with multibyte characters
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200159func Test_matchfuzzy_mbyte()
160 CheckFeature multi_lang
161 call assert_equal(['ンヹㄇヺヴ'], matchfuzzy(['ンヹㄇヺヴ'], 'ヹヺ'))
162 " reverse the order of characters
163 call assert_equal([], matchfuzzy(['ンヹㄇヺヴ'], 'ヺヹ'))
164 call assert_equal(['αβΩxxx', 'xαxβxΩx'],
165 \ matchfuzzy(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
166 call assert_equal(['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
167 \ matchfuzzy(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
168
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200169 " match multiple words (separated by space)
170 call assert_equal(['세 마리의 작은 돼지'], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('돼지 마리의'))
171 call assert_equal([], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzy('파란 하늘'))
172
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200173 " preference for camel case match
174 call assert_equal(['oneĄwo', 'oneąwo'],
175 \ ['oneąwo', 'oneĄwo']->matchfuzzy('oneąwo'))
Bram Moolenaare9f9f162020-10-20 19:01:30 +0200176 " preference for complete match then match after separator (_ or space)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200177 call assert_equal(['ⅠⅡabㄟㄠ'] + sort(['ⅠⅡa_bㄟㄠ', 'ⅠⅡa bㄟㄠ']),
Bram Moolenaare9f9f162020-10-20 19:01:30 +0200178 \ ['ⅠⅡabㄟㄠ', 'ⅠⅡa bㄟㄠ', 'ⅠⅡa_bㄟㄠ']->matchfuzzy('ⅠⅡabㄟㄠ'))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200179 " preference for match after a separator (_ or space)
180 call assert_equal(['ㄓㄔabㄟㄠ', 'ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ'],
181 \ ['ㄓㄔa_bㄟㄠ', 'ㄓㄔa bㄟㄠ', 'ㄓㄔabㄟㄠ']->matchfuzzy('ㄓㄔabㄟㄠ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200182 " preference for leading letter match
183 call assert_equal(['ŗŝţũŵż', 'xŗŝţũŵż'],
184 \ ['xŗŝţũŵż', 'ŗŝţũŵż']->matchfuzzy('ŗŝţũŵż'))
185 " preference for sequential match
186 call assert_equal(['ㄞㄡㄤfffifl', 'ㄞaㄡbㄤcffdfiefl'],
187 \ ['ㄞaㄡbㄤcffdfiefl', 'ㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
188 " non-matching leading letter(s) penalty
189 call assert_equal(['xㄞㄡㄤfffifl', 'xxㄞㄡㄤfffifl'],
190 \ ['xxㄞㄡㄤfffifl', 'xㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
191 " total non-matching letter(s) penalty
192 call assert_equal(['ŗŝţ', 'ŗŝţx', 'ŗŝţxx'],
193 \ ['ŗŝţxx', 'ŗŝţ', 'ŗŝţx']->matchfuzzy('ŗŝţ'))
194endfunc
195
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200196" Test for matchfuzzypos() with multibyte characters
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200197func Test_matchfuzzypos_mbyte()
198 CheckFeature multi_lang
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100199 call assert_equal([['こんにちは世界'], [[0, 1, 2, 3, 4]], [273]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200200 \ matchfuzzypos(['こんにちは世界'], 'こんにちは'))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100201 call assert_equal([['ンヹㄇヺヴ'], [[1, 3]], [88]], matchfuzzypos(['ンヹㄇヺヴ'], 'ヹヺ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200202 " reverse the order of characters
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100203 call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇヺヴ'], 'ヺヹ'))
204 call assert_equal([['αβΩxxx', 'xαxβxΩx'], [[0, 1, 2], [1, 3, 5]], [222, 113]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200205 \ matchfuzzypos(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
206 call assert_equal([['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100207 \ [[0, 1], [0, 1], [0, 1], [0, 2]], [151, 148, 145, 110]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200208 \ matchfuzzypos(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100209 call assert_equal([['ααααααα'], [[0, 1, 2]], [191]],
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200210 \ matchfuzzypos(['ααααααα'], 'ααα'))
211
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100212 call assert_equal([[], [], []], matchfuzzypos(['ンヹㄇ', 'ŗŝţ'], 'fffifl'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200213 let x = matchfuzzypos([repeat('Ψ', 256)], repeat('Ψ', 256))
214 call assert_equal(range(256), x[1][0])
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100215 call assert_equal([[], [], []], matchfuzzypos([repeat('✓', 300)], repeat('✓', 257)))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200216
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200217 " match multiple words (separated by space)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100218 call assert_equal([['세 마리의 작은 돼지'], [[9, 10, 2, 3, 4]], [328]], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('돼지 마리의'))
219 call assert_equal([[], [], []], ['세 마리의 작은 돼지', '마리의', '마리의 작은', '작은 돼지']->matchfuzzypos('파란 하늘'))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200220
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200221 " match in a long string
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100222 call assert_equal([[repeat('ぶ', 300) .. 'ẼẼẼ'], [[300, 301, 302]], [-135]],
Bram Moolenaar8ded5b62020-10-23 16:50:30 +0200223 \ matchfuzzypos([repeat('ぶ', 300) .. 'ẼẼẼ'], 'ẼẼẼ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200224 " preference for camel case match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100225 call assert_equal([['xѳѵҁxxѳѴҁ'], [[6, 7, 8]], [189]], matchfuzzypos(['xѳѵҁxxѳѴҁ'], 'ѳѵҁ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200226 " preference for match after a separator (_ or space)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100227 call assert_equal([['xちだx_ちだ'], [[5, 6]], [145]], matchfuzzypos(['xちだx_ちだ'], 'ちだ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200228 " preference for leading letter match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100229 call assert_equal([['ѳѵҁxѳѵҁ'], [[0, 1]], [150]], matchfuzzypos(['ѳѵҁxѳѵҁ'], 'ѳѵ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200230 " preference for sequential match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100231 call assert_equal([['aンbヹcㄇdンヹㄇ'], [[7, 8, 9]], [158]], matchfuzzypos(['aンbヹcㄇdンヹㄇ'], 'ンヹㄇ'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200232 " best recursive match
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +0100233 call assert_equal([['xффйд'], [[2, 3, 4]], [168]], matchfuzzypos(['xффйд'], 'фйд'))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200234endfunc
235
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +0100236" Test for matchfuzzy() with limit
237func Test_matchfuzzy_limit()
238 let x = ['1', '2', '3', '2']
239 call assert_equal(['2', '2'], x->matchfuzzy('2'))
240 call assert_equal(['2', '2'], x->matchfuzzy('2', #{}))
241 call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 0}))
242 call assert_equal(['2'], x->matchfuzzy('2', #{limit: 1}))
243 call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 2}))
244 call assert_equal(['2', '2'], x->matchfuzzy('2', #{limit: 3}))
245 call assert_fails("call matchfuzzy(x, '2', #{limit: '2'})", 'E475:')
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +0100246
247 let l = [{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}]
248 call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}}))
249 call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{key: 'val'}))
250 call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}, limit: 0}))
251 call assert_equal([{'id': 5, 'val': 'crayon'}, {'id': 6, 'val': 'camera'}], l->matchfuzzy('c', #{key: 'val', limit: 0}))
252 call assert_equal([{'id': 5, 'val': 'crayon'}], l->matchfuzzy('c', #{text_cb: {v -> v.val}, limit: 1}))
253 call assert_equal([{'id': 5, 'val': 'crayon'}], l->matchfuzzy('c', #{key: 'val', limit: 1}))
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +0100254endfunc
255
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +0200256" vim: shiftwidth=2 sts=2 expandtab