annotate src/testdir/test_matchfuzzy.vim @ 22647:0dd527d9c62d v8.2.1872

patch 8.2.1872: matchfuzzy() does not prefer sequential matches Commit: https://github.com/vim/vim/commit/e9f9f16387554e5b34ba42ce00c42c28dd66af58 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Oct 20 19:01:30 2020 +0200 patch 8.2.1872: matchfuzzy() does not prefer sequential matches Problem: Matchfuzzy() does not prefer sequential matches. Solution: Give sequential matches a higher bonus. (Christian Brabandt, closes #7140)
author Bram Moolenaar <Bram@vim.org>
date Tue, 20 Oct 2020 19:15:04 +0200
parents 0491b9cafd44
children f8bf2c122452
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22355
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 " Tests for fuzzy matching
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
3 source shared.vim
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 source check.vim
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
6 " Test for matchfuzzy()
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
7 func Test_matchfuzzy()
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
8 call assert_fails('call matchfuzzy(10, "abc")', 'E686:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
9 call assert_fails('call matchfuzzy(["abc"], [])', 'E730:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 call assert_fails("let x = matchfuzzy(test_null_list(), 'foo')", 'E686:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 call assert_fails('call matchfuzzy(["abc"], test_null_string())', 'E475:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 call assert_equal([], matchfuzzy([], 'abc'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13 call assert_equal([], matchfuzzy(['abc'], ''))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
14 call assert_equal(['abc'], matchfuzzy(['abc', 10], 'ac'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15 call assert_equal([], matchfuzzy([10, 20], 'ac'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 call assert_equal(['abc'], matchfuzzy(['abc'], 'abc'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17 call assert_equal(['crayon', 'camera'], matchfuzzy(['camera', 'crayon'], 'cra'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18 call assert_equal(['aabbaa', 'aaabbbaaa', 'aaaabbbbaaaa', 'aba'], matchfuzzy(['aba', 'aabbaa', 'aaabbbaaa', 'aaaabbbbaaaa'], 'aa'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 call assert_equal(['one'], matchfuzzy(['one', 'two'], 'one'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
20 call assert_equal(['oneTwo', 'onetwo'], matchfuzzy(['onetwo', 'oneTwo'], 'oneTwo'))
22647
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
21 call assert_equal(['onetwo', 'one_two'], matchfuzzy(['onetwo', 'one_two'], 'oneTwo'))
22355
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
22 call assert_equal(['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], matchfuzzy(['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'], 'aa'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23 call assert_equal(256, matchfuzzy([repeat('a', 256)], repeat('a', 256))[0]->len())
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 call assert_equal([], matchfuzzy([repeat('a', 300)], repeat('a', 257)))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26 " Tests for match preferences
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 " preference for camel case match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 call assert_equal(['oneTwo', 'onetwo'], ['onetwo', 'oneTwo']->matchfuzzy('onetwo'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 " preference for match after a separator (_ or space)
22647
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
30 if has("win32")
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
31 call assert_equal(['onetwo', 'one two', 'one_two'], ['onetwo', 'one_two', 'one two']->matchfuzzy('onetwo'))
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
32 else
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
33 call assert_equal(['onetwo', 'one_two', 'one two'], ['onetwo', 'one_two', 'one two']->matchfuzzy('onetwo'))
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
34 endif
22355
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 " preference for leading letter match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36 call assert_equal(['onetwo', 'xonetwo'], ['xonetwo', 'onetwo']->matchfuzzy('onetwo'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
37 " preference for sequential match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 call assert_equal(['onetwo', 'oanbectdweo'], ['oanbectdweo', 'onetwo']->matchfuzzy('onetwo'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39 " non-matching leading letter(s) penalty
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 call assert_equal(['xonetwo', 'xxonetwo'], ['xxonetwo', 'xonetwo']->matchfuzzy('onetwo'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 " total non-matching letter(s) penalty
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 call assert_equal(['one', 'onex', 'onexx'], ['onexx', 'one', 'onex']->matchfuzzy('one'))
22647
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
43 " prefer complete matches over separator matches
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
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'))
22355
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 %bw!
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47 eval ['somebuf', 'anotherone', 'needle', 'yetanotherone']->map({_, v -> bufadd(v) + bufload(v)})
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 let l = getbufinfo()->map({_, v -> v.name})->matchfuzzy('ndl')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
49 call assert_equal(1, len(l))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 call assert_match('needle', l[0])
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'text_cb' : {v -> v.val}}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 call assert_equal([{'id' : 6, 'val' : 'camera'}], matchfuzzy(l, 'cam', {'key' : 'val'}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55 call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> v.val}}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 call assert_equal([], matchfuzzy(l, 'day', {'key' : 'val'}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 call assert_fails("let x = matchfuzzy(l, 'cam', 'random')", 'E715:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> []}}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 call assert_equal([], matchfuzzy(l, 'day', {'text_cb' : {v -> 1}}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 call assert_fails("let x = matchfuzzy(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 call assert_equal([], matchfuzzy(l, 'cam'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62 call assert_fails("let x = matchfuzzy(l, 'cam', {'text_cb' : []})", 'E921:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : []})", 'E730:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 call assert_fails("let x = matchfuzzy(l, 'cam', test_null_dict())", 'E715:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : test_null_string()})", 'E475:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 call assert_fails("let x = matchfuzzy(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
68 let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
69 call assert_fails("let x = matchfuzzy(l, 'foo', {'key' : 'name'})", 'E730:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
70
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
71 " Test in latin1 encoding
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
72 let save_enc = &encoding
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
73 set encoding=latin1
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
74 call assert_equal(['abc'], matchfuzzy(['abc'], 'abc'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
75 let &encoding = save_enc
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
76 endfunc
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
77
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
78 " Test for the fuzzymatchpos() function
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
79 func Test_matchfuzzypos()
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
80 call assert_equal([['curl', 'world'], [[2,3], [2,3]]], matchfuzzypos(['world', 'curl'], 'rl'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
81 call assert_equal([['curl', 'world'], [[2,3], [2,3]]], matchfuzzypos(['world', 'one', 'curl'], 'rl'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
82 call assert_equal([['hello', 'hello world hello world'],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
83 \ [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
84 \ matchfuzzypos(['hello world hello world', 'hello', 'world'], 'hello'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
85 call assert_equal([['aaaaaaa'], [[0, 1, 2]]], matchfuzzypos(['aaaaaaa'], 'aaa'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
86 call assert_equal([[], []], matchfuzzypos(['world', 'curl'], 'ab'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
87 let x = matchfuzzypos([repeat('a', 256)], repeat('a', 256))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
88 call assert_equal(range(256), x[1][0])
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
89 call assert_equal([[], []], matchfuzzypos([repeat('a', 300)], repeat('a', 257)))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
90 call assert_equal([[], []], matchfuzzypos([], 'abc'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
91
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
92 " match in a long string
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
93 call assert_equal([[repeat('x', 300) .. 'abc'], [[300, 301, 302]]],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
94 \ matchfuzzypos([repeat('x', 300) .. 'abc'], 'abc'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
95
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
96 " preference for camel case match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
97 call assert_equal([['xabcxxaBc'], [[6, 7, 8]]], matchfuzzypos(['xabcxxaBc'], 'abc'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
98 " preference for match after a separator (_ or space)
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
99 call assert_equal([['xabx_ab'], [[5, 6]]], matchfuzzypos(['xabx_ab'], 'ab'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
100 " preference for leading letter match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
101 call assert_equal([['abcxabc'], [[0, 1]]], matchfuzzypos(['abcxabc'], 'ab'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
102 " preference for sequential match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
103 call assert_equal([['aobncedone'], [[7, 8, 9]]], matchfuzzypos(['aobncedone'], 'one'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
104 " best recursive match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
105 call assert_equal([['xoone'], [[2, 3, 4]]], matchfuzzypos(['xoone'], 'one'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
106
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
107 let l = [{'id' : 5, 'val' : 'crayon'}, {'id' : 6, 'val' : 'camera'}]
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
108 call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]]],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
109 \ matchfuzzypos(l, 'cam', {'text_cb' : {v -> v.val}}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
110 call assert_equal([[{'id' : 6, 'val' : 'camera'}], [[0, 1, 2]]],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
111 \ matchfuzzypos(l, 'cam', {'key' : 'val'}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
112 call assert_equal([[], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> v.val}}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
113 call assert_equal([[], []], matchfuzzypos(l, 'day', {'key' : 'val'}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
114 call assert_fails("let x = matchfuzzypos(l, 'cam', 'random')", 'E715:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
115 call assert_equal([[], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> []}}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
116 call assert_equal([[], []], matchfuzzypos(l, 'day', {'text_cb' : {v -> 1}}))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
117 call assert_fails("let x = matchfuzzypos(l, 'day', {'text_cb' : {a, b -> 1}})", 'E119:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
118 call assert_equal([[], []], matchfuzzypos(l, 'cam'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
119 call assert_fails("let x = matchfuzzypos(l, 'cam', {'text_cb' : []})", 'E921:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
120 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : []})", 'E730:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
121 call assert_fails("let x = matchfuzzypos(l, 'cam', test_null_dict())", 'E715:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
122 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : test_null_string()})", 'E475:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
123 call assert_fails("let x = matchfuzzypos(l, 'foo', {'text_cb' : test_null_function()})", 'E475:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
124
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
125 let l = [{'id' : 5, 'name' : 'foo'}, {'id' : 6, 'name' : []}, {'id' : 7}]
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
126 call assert_fails("let x = matchfuzzypos(l, 'foo', {'key' : 'name'})", 'E730:')
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
127 endfunc
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
128
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
129 func Test_matchfuzzy_mbyte()
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
130 CheckFeature multi_lang
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
131 call assert_equal(['ンヹㄇヺヴ'], matchfuzzy(['ンヹㄇヺヴ'], 'ヹヺ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
132 " reverse the order of characters
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
133 call assert_equal([], matchfuzzy(['ンヹㄇヺヴ'], 'ヺヹ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
134 call assert_equal(['αβΩxxx', 'xαxβxΩx'],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
135 \ matchfuzzy(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
136 call assert_equal(['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
137 \ matchfuzzy(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
138
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
139 " preference for camel case match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
140 call assert_equal(['oneĄwo', 'oneąwo'],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
141 \ ['oneąwo', 'oneĄwo']->matchfuzzy('oneąwo'))
22647
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
142 " preference for complete match then match after separator (_ or space)
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
143 if has("win32")
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
144 " order is different between Windows and Unix :(
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
145 " It's important that the complete match is first
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
146 call assert_equal(['ⅠⅡabㄟㄠ', 'ⅠⅡa bㄟㄠ', 'ⅠⅡa_bㄟㄠ'],
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
147 \ ['ⅠⅡabㄟㄠ', 'ⅠⅡa_bㄟㄠ', 'ⅠⅡa bㄟㄠ']->matchfuzzy('ⅠⅡabㄟㄠ'))
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
148 else
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
149 call assert_equal(['ⅠⅡabㄟㄠ'] + sort(['ⅠⅡa_bㄟㄠ', 'ⅠⅡa bㄟㄠ']),
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
150 \ ['ⅠⅡabㄟㄠ', 'ⅠⅡa bㄟㄠ', 'ⅠⅡa_bㄟㄠ']->matchfuzzy('ⅠⅡabㄟㄠ'))
0dd527d9c62d patch 8.2.1872: matchfuzzy() does not prefer sequential matches
Bram Moolenaar <Bram@vim.org>
parents: 22355
diff changeset
151 endif
22355
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
152 " preference for leading letter match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
153 call assert_equal(['ŗŝţũŵż', 'xŗŝţũŵż'],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
154 \ ['xŗŝţũŵż', 'ŗŝţũŵż']->matchfuzzy('ŗŝţũŵż'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
155 " preference for sequential match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
156 call assert_equal(['ㄞㄡㄤfffifl', 'ㄞaㄡbㄤcffdfiefl'],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
157 \ ['ㄞaㄡbㄤcffdfiefl', 'ㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
158 " non-matching leading letter(s) penalty
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
159 call assert_equal(['xㄞㄡㄤfffifl', 'xxㄞㄡㄤfffifl'],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
160 \ ['xxㄞㄡㄤfffifl', 'xㄞㄡㄤfffifl']->matchfuzzy('ㄞㄡㄤfffifl'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
161 " total non-matching letter(s) penalty
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
162 call assert_equal(['ŗŝţ', 'ŗŝţx', 'ŗŝţxx'],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
163 \ ['ŗŝţxx', 'ŗŝţ', 'ŗŝţx']->matchfuzzy('ŗŝţ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
164 endfunc
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
165
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
166 func Test_matchfuzzypos_mbyte()
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
167 CheckFeature multi_lang
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
168 call assert_equal([['こんにちは世界'], [[0, 1, 2, 3, 4]]],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
169 \ matchfuzzypos(['こんにちは世界'], 'こんにちは'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
170 call assert_equal([['ンヹㄇヺヴ'], [[1, 3]]], matchfuzzypos(['ンヹㄇヺヴ'], 'ヹヺ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
171 " reverse the order of characters
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
172 call assert_equal([[], []], matchfuzzypos(['ンヹㄇヺヴ'], 'ヺヹ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
173 call assert_equal([['αβΩxxx', 'xαxβxΩx'], [[0, 1, 2], [1, 3, 5]]],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
174 \ matchfuzzypos(['αβΩxxx', 'xαxβxΩx'], 'αβΩ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
175 call assert_equal([['ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ', 'πbπ'],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
176 \ [[0, 1], [0, 1], [0, 1], [0, 2]]],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
177 \ matchfuzzypos(['πbπ', 'ππbbππ', 'πππbbbπππ', 'ππππbbbbππππ'], 'ππ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
178 call assert_equal([['ααααααα'], [[0, 1, 2]]],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
179 \ matchfuzzypos(['ααααααα'], 'ααα'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
180
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
181 call assert_equal([[], []], matchfuzzypos(['ンヹㄇ', 'ŗŝţ'], 'fffifl'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
182 let x = matchfuzzypos([repeat('Ψ', 256)], repeat('Ψ', 256))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
183 call assert_equal(range(256), x[1][0])
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
184 call assert_equal([[], []], matchfuzzypos([repeat('✓', 300)], repeat('✓', 257)))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
185
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
186 " match in a long string
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
187 call assert_equal([[repeat('♪', 300) .. '✗✗✗'], [[300, 301, 302]]],
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
188 \ matchfuzzypos([repeat('♪', 300) .. '✗✗✗'], '✗✗✗'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
189 " preference for camel case match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
190 call assert_equal([['xѳѵҁxxѳѴҁ'], [[6, 7, 8]]], matchfuzzypos(['xѳѵҁxxѳѴҁ'], 'ѳѵҁ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
191 " preference for match after a separator (_ or space)
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
192 call assert_equal([['xちだx_ちだ'], [[5, 6]]], matchfuzzypos(['xちだx_ちだ'], 'ちだ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
193 " preference for leading letter match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
194 call assert_equal([['ѳѵҁxѳѵҁ'], [[0, 1]]], matchfuzzypos(['ѳѵҁxѳѵҁ'], 'ѳѵ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
195 " preference for sequential match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
196 call assert_equal([['aンbヹcㄇdンヹㄇ'], [[7, 8, 9]]], matchfuzzypos(['aンbヹcㄇdンヹㄇ'], 'ンヹㄇ'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
197 " best recursive match
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
198 call assert_equal([['xффйд'], [[2, 3, 4]]], matchfuzzypos(['xффйд'], 'фйд'))
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
199 endfunc
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
200
0491b9cafd44 patch 8.2.1726: fuzzy matching only works on strings
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
201 " vim: shiftwidth=2 sts=2 expandtab