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