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