robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
91b9516d1b
master
1" Test findfile() and finddir() 2 3source check.vim 4source vim9.vim 5 6let s: files = [ 'Xfinddir1/foo' , 7 \'Xfinddir1/bar' , 8 \'Xfinddir1/Xdir2/foo' , 9 \'Xfinddir1/Xdir2/foobar' , 10 \'Xfinddir1/Xdir2/Xdir3/bar' , 11 \'Xfinddir1/Xdir2/Xdir3/barfoo' ] 12 13func CreateFiles () 14call mkdir ( 'Xfinddir1/Xdir2/Xdir3/Xdir2' , 'p' ) 15for f in s: files 16call writefile ([], f ) 17endfor 18endfunc 19 20func CleanFiles () 21" Safer to delete each file even if it's more verbose 22" than doing a recursive delete('Xfinddir1', 'rf'). 23for f in s: files 24call delete ( f ) 25endfor 26 27call delete ( 'Xfinddir1/Xdir2/Xdir3/Xdir2' , 'd' ) 28call delete ( 'Xfinddir1/Xdir2/Xdir3' , 'd' ) 29call delete ( 'Xfinddir1/Xdir2' , 'd' ) 30call delete ( 'Xfinddir1' , 'd' ) 31endfunc 32 33" Test findfile({name} [, {path} [, {count}]]) 34func Test_findfile () 35let save_path = & path 36let save_shellslash = & shellslash 37let save_dir = getcwd () 38set shellslash 39call CreateFiles () 40cd Xfinddir1 41e Xdir2/foo 42 43" With ,, in path, findfile() searches in current directory. 44set path = ,, 45call assert_equal ( 'foo' , findfile ( 'foo' )) 46call assert_equal ( 'bar' , findfile ( 'bar' )) 47call assert_equal ( '' , findfile ( 'foobar' )) 48 49" Directories should not be found (finddir() finds them). 50call assert_equal ( '' , findfile ( 'Xdir2' )) 51 52" With . in 'path', findfile() searches relatively to current file. 53set path = . 54call assert_equal ( 'Xdir2/foo' , findfile ( 'foo' )) 55call assert_equal ( '' , findfile ( 'bar' )) 56call assert_equal ( 'Xdir2/foobar' , 'foobar' ->findfile ()) 57 58" Empty {path} 2nd argument is the same as no 2nd argument. 59call assert_equal ( 'Xdir2/foo' , findfile ( 'foo' , '' )) 60call assert_equal ( '' , findfile ( 'bar' , '' )) 61 62" Test with * 63call assert_equal ( 'Xdir2/foo' , findfile ( 'foo' , '*' )) 64call assert_equal ( '' , findfile ( 'bar' , '*' )) 65call assert_equal ( 'Xdir2/Xdir3/bar' , findfile ( 'bar' , '*/*' )) 66call assert_equal ( 'Xdir2/Xdir3/bar' , findfile ( 'bar' , 'Xdir2/*' )) 67call assert_equal ( 'Xdir2/Xdir3/bar' , findfile ( 'bar' , 'Xdir*/Xdir3' )) 68call assert_equal ( 'Xdir2/Xdir3/bar' , findfile ( 'bar' , '*2/*3' )) 69 70" Test with ** 71call assert_equal ( 'bar' , findfile ( 'bar' , '**' )) 72call assert_equal ( 'Xdir2/Xdir3/bar' , findfile ( 'bar' , '**/Xdir3' )) 73call assert_equal ( 'Xdir2/Xdir3/bar' , findfile ( 'bar' , 'Xdir2/**' )) 74 75call assert_equal ( 'Xdir2/Xdir3/barfoo' , findfile ( 'barfoo' , '**2' )) 76call assert_equal ( '' , findfile ( 'barfoo' , '**1' )) 77call assert_equal ( 'Xdir2/foobar' , findfile ( 'foobar' , '**1' )) 78 79" Test with {count} 3rd argument. 80call assert_equal ( 'bar' , findfile ( 'bar' , '**' , 0 )) 81call assert_equal ( 'bar' , findfile ( 'bar' , '**' , 1 )) 82call assert_equal ( 'Xdir2/Xdir3/bar' , findfile ( 'bar' , '**' , 2 )) 83call assert_equal ( '' , findfile ( 'bar' , '**' , 3 )) 84call assert_equal ([ 'bar' , 'Xdir2/Xdir3/bar' ], findfile ( 'bar' , '**' , -1 )) 85 86" Test upwards search. 87cd Xdir2/Xdir3 88call assert_equal ( 'bar' , findfile ( 'bar' , ';' )) 89call assert_match ( '.*/Xfinddir1/Xdir2/foo' , findfile ( 'foo' , ';' )) 90call assert_match ( '.*/Xfinddir1/Xdir2/foo' , findfile ( 'foo' , ';' , 1 )) 91call assert_match ( '.*/Xfinddir1/foo' , findfile ( 'foo' , ';' , 2 )) 92call assert_match ( '.*/Xfinddir1/foo' , findfile ( 'foo' , ';' , 2 )) 93call assert_match ( '.*/Xfinddir1/Xdir2/foo' , findfile ( 'foo' , 'Xdir2;' , 1 )) 94call assert_equal ( '' , findfile ( 'foo' , 'Xdir2;' , 2 )) 95 96" List l should have at least 2 values (possibly more if foo file 97" happens to be found upwards above Xfinddir1). 98let l = findfile ( 'foo' , ';' , -1 ) 99call assert_match ( '.*/Xfinddir1/Xdir2/foo' , l [ 0 ]) 100call assert_match ( '.*/Xfinddir1/foo' , l [ 1 ]) 101 102" Test upwards search with stop-directory. 103cd Xdir2 104let l = findfile ( 'bar' , ';' . save_dir . '/Xfinddir1/Xdir2/Xdir3/' , -1 ) 105call assert_equal ( 1 , len ( l )) 106call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 107let l = findfile ( 'bar' , ';' . save_dir . '/Xfinddir1/Xdir2/Xdir3' , -1 ) 108call assert_equal ( 1 , len ( l )) 109call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 110let l = findfile ( 'bar' , ';../' , -1 ) 111call assert_equal ( 1 , len ( l )) 112call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 113let l = findfile ( 'bar' , ';..' , -1 ) 114call assert_equal ( 1 , len ( l )) 115call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 116 117let l = findfile ( 'bar' , ';' . save_dir . '/Xfinddir1/Xdir2/' , -1 ) 118call assert_equal ( 1 , len ( l )) 119call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 120let l = findfile ( 'bar' , ';' . save_dir . '/Xfinddir1/Xdir2' , -1 ) 121call assert_equal ( 1 , len ( l )) 122call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 123let l = findfile ( 'bar' , ';../../' , -1 ) 124call assert_equal ( 1 , len ( l )) 125call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 126let l = findfile ( 'bar' , ';../..' , -1 ) 127call assert_equal ( 1 , len ( l )) 128call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 129 130let l = findfile ( 'bar' , ';' . save_dir . '/Xfinddir1/' , -1 ) 131call assert_equal ( 2 , len ( l )) 132call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 133call assert_match ( '.*/Xfinddir1/bar' , l [ 1 ]) 134let l = findfile ( 'bar' , ';' . save_dir . '/Xfinddir1' , -1 ) 135call assert_equal ( 2 , len ( l )) 136call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 137call assert_match ( '.*/Xfinddir1/bar' , l [ 1 ]) 138let l = findfile ( 'bar' , ';../../../' , -1 ) 139call assert_equal ( 2 , len ( l )) 140call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 141call assert_match ( '.*/Xfinddir1/bar' , l [ 1 ]) 142let l = findfile ( 'bar' , ';../../..' , -1 ) 143call assert_equal ( 2 , len ( l )) 144call assert_match ( '.*/Xfinddir1/Xdir2/Xdir3/bar' , l [ 0 ]) 145call assert_match ( '.*/Xfinddir1/bar' , l [ 1 ]) 146 147" Test combined downwards and upwards search from Xdir2/. 148cd ../.. 149call assert_equal ( 'Xdir3/bar' , findfile ( 'bar' , '**;' , 1 )) 150call assert_match ( '.*/Xfinddir1/bar' , findfile ( 'bar' , '**;' , 2 )) 151 152bwipe ! 153call chdir ( save_dir ) 154call CleanFiles () 155let & path = save_path 156let & shellslash = save_shellslash 157endfunc 158 159func Test_findfile_error () 160call assert_fails ( 'call findfile([])' , 'E730:' ) 161call assert_fails ( 'call findfile("x", [])' , 'E730:' ) 162call assert_fails ( 'call findfile("x", "", [])' , 'E745:' ) 163call assert_fails ( 'call findfile("x", "**x")' , 'E343:' ) 164call assert_fails ( 'call findfile("x", repeat("x", 5000))' , 'E854:' ) 165endfunc 166 167" Test finddir({name} [, {path} [, {count}]]) 168func Test_finddir () 169let save_path = & path 170let save_shellslash = & shellslash 171let save_dir = getcwd () 172set path = ,, 173set shellslash 174call CreateFiles () 175cd Xfinddir1 176 177call assert_equal ( 'Xdir2' , finddir ( 'Xdir2' )) 178call assert_equal ( '' , 'Xdir3' ->finddir ()) 179 180" Files should not be found (findfile() finds them). 181call assert_equal ( '' , finddir ( 'foo' )) 182 183call assert_equal ( 'Xdir2' , finddir ( 'Xdir2' , '**' )) 184call assert_equal ( 'Xdir2/Xdir3' , finddir ( 'Xdir3' , '**' )) 185 186call assert_equal ( 'Xdir2' , finddir ( 'Xdir2' , '**' , 1 )) 187call assert_equal ( 'Xdir2/Xdir3/Xdir2' , finddir ( 'Xdir2' , '**' , 2 )) 188call assert_equal ([ 'Xdir2' , 189 \'Xdir2/Xdir3/Xdir2' ], finddir ( 'Xdir2' , '**' , -1 )) 190 191call assert_equal ( 'Xdir2' , finddir ( 'Xdir2' , '**1' )) 192call assert_equal ( 'Xdir2' , finddir ( 'Xdir2' , '**0' )) 193call assert_equal ( 'Xdir2/Xdir3' , finddir ( 'Xdir3' , '**1' )) 194call assert_equal ( '' , finddir ( 'Xdir3' , '**0' )) 195 196" Test upwards dir search. 197cd Xdir2/Xdir3 198call assert_match ( '.*/Xfinddir1' , finddir ( 'Xfinddir1' , ';' )) 199 200" Test upwards search with stop-directory. 201call assert_match ( '.*/Xfinddir1' , finddir ( 'Xfinddir1' , ';' . save_dir . '/' )) 202call assert_equal ( '' , finddir ( 'Xfinddir1' , ';' . save_dir . '/Xfinddir1/' )) 203 204" Test combined downwards and upwards dir search from Xdir2/. 205cd .. 206call assert_match ( '.*/Xfinddir1' , finddir ( 'Xfinddir1' , '**;' , 1 )) 207call assert_equal ( 'Xdir3/Xdir2' , finddir ( 'Xdir2' , '**;' , 1 )) 208call assert_match ( '.*/Xfinddir1/Xdir2' , finddir ( 'Xdir2' , '**;' , 2 )) 209call assert_equal ( 'Xdir3' , finddir ( 'Xdir3' , '**;' , 1 )) 210 211call chdir ( save_dir ) 212call CleanFiles () 213let & path = save_path 214let & shellslash = save_shellslash 215endfunc 216 217func Test_finddir_error () 218call assert_fails ( 'call finddir([])' , 'E730:' ) 219call assert_fails ( 'call finddir("x", [])' , 'E730:' ) 220call assert_fails ( 'call finddir("x", "", [])' , 'E745:' ) 221call assert_fails ( 'call finddir("x", "**x")' , 'E343:' ) 222call assert_fails ( 'call finddir("x", repeat("x", 5000))' , 'E854:' ) 223endfunc 224 225func Test_findfile_with_suffixesadd () 226let save_path = & path 227let save_dir = getcwd () 228set path = ,, 229call mkdir ( 'Xfinddir1' , 'pR' ) 230cd Xfinddir1 231 232call writefile ([], 'foo.c' , 'D' ) 233call writefile ([], 'bar.cpp' , 'D' ) 234call writefile ([], 'baz.cc' , 'D' ) 235call writefile ([], 'foo.o' , 'D' ) 236call writefile ([], 'bar.o' , 'D' ) 237call writefile ([], 'baz.o' , 'D' ) 238 239set suffixesadd = .c,.cpp 240call assert_equal ( 'foo.c' , findfile ( 'foo' )) 241call assert_equal ( './foo.c' , findfile ( './foo' )) 242call assert_equal ( 'bar.cpp' , findfile ( 'bar' )) 243call assert_equal ( './bar.cpp' , findfile ( './bar' )) 244call assert_equal ( '' , findfile ( 'baz' )) 245call assert_equal ( '' , findfile ( './baz' )) 246set suffixesadd += .cc 247call assert_equal ( 'baz.cc' , findfile ( 'baz' )) 248call assert_equal ( './baz.cc' , findfile ( './baz' )) 249 250set suffixesadd & 251call chdir ( save_dir ) 252let & path = save_path 253endfunc 254 255" Test for the :find, :sfind and :tabfind commands 256func Test_find_cmd () 257new 258let save_path = & path 259let save_dir = getcwd () 260set path = .,./**/* 261call CreateFiles () 262cd Xfinddir1 263 264" Test for :find 265find foo 266call assert_equal ( 'foo' , expand ( '%:.' )) 2672 find foo 268call assert_equal ( 'Xdir2/foo' , expand ( '%:.' )) 269call assert_fails ( '3find foo' , 'E347:' ) 270 271" Test for :sfind 272enew 273sfind barfoo 274call assert_equal ( 'Xdir2/Xdir3/barfoo' , expand ( '%:.' )) 275call assert_equal ( 3 , winnr ( '$' )) 276close 277call assert_fails ( 'sfind baz' , 'E345:' ) 278call assert_equal ( 2 , winnr ( '$' )) 279 280" Test for :tabfind 281enew 282tabfind foobar 283call assert_equal ( 'Xdir2/foobar' , expand ( '%:.' )) 284call assert_equal ( 2 , tabpagenr ( '$' )) 285tabclose 286call assert_fails ( 'tabfind baz' , 'E345:' ) 287call assert_equal ( 1 , tabpagenr ( '$' )) 288 289call chdir ( save_dir ) 290exe 'cd ' . save_dir 291call CleanFiles () 292let & path = save_path 293close 294 295call assert_fails ( 'find' , 'E471:' ) 296call assert_fails ( 'sfind' , 'E471:' ) 297call assert_fails ( 'tabfind' , 'E471:' ) 298endfunc 299 300func Test_find_non_existing_path () 301new 302let save_path = & path 303let save_dir = getcwd () 304call mkdir ( 'dir1/dir2' , 'p' ) 305call writefile ([], 'dir1/file.txt' ) 306call writefile ([], 'dir1/dir2/base.txt' ) 307call chdir ( 'dir1/dir2' ) 308e base.txt 309set path = ../include 310 311call assert_fails ( ':find file.txt' , 'E345:' ) 312 313call chdir ( save_dir ) 314bw ! 315call delete ( 'dir1/dir2/base.txt' , 'rf' ) 316call delete ( 'dir1/dir2' , 'rf' ) 317call delete ( 'dir1/file.txt' , 'rf' ) 318call delete ( 'dir1' , 'rf' ) 319let & path = save_path 320endfunc 321 322" Test for 'findfunc' 323func Test_findfunc () 324CheckUnix 325call assert_equal ( '' , & findfunc ) 326call writefile ([ 'aFile' ], 'Xfindfunc1.c' , 'D' ) 327call writefile ([ 'bFile' ], 'Xfindfunc2.c' , 'D' ) 328call writefile ([ 'cFile' ], 'Xfindfunc3.c' , 'D' ) 329 330" basic tests 331func FindFuncBasic ( pat , cmdcomplete ) 332let fnames = [ 'Xfindfunc1.c' , 'Xfindfunc2.c' , 'Xfindfunc3.c' ] 333return fnames ->copy () ->filter ( 'v:val =~? a:pat' ) 334endfunc 335 336set findfunc = FindFuncBasic 337find Xfindfunc3 338call assert_match ( 'Xfindfunc3\.c' , @%) 339bw ! 3402 find Xfind 341call assert_match ( 'Xfindfunc2\.c' , @%) 342bw ! 343call assert_fails ( '4find Xfind' , 'E347: No more file "Xfind" found in path' ) 344call assert_fails ( 'find foobar' , 'E345: Can''t find file "foobar" in path' ) 345 346sfind Xfindfunc2.c 347call assert_match ( 'Xfindfunc2\.c' , @%) 348call assert_equal ( 2 , winnr ( '$' )) 349 %bw ! 350call assert_fails ( 'sfind foobar' , 'E345: Can''t find file "foobar" in path' ) 351 352tabfind Xfindfunc3.c 353call assert_match ( 'Xfindfunc3\.c' , @%) 354call assert_equal ( 2 , tabpagenr ()) 355 %bw ! 356call assert_fails ( 'tabfind foobar' , 'E345: Can''t find file "foobar" in path' ) 357 358" Test garbage collection 359call test_garbagecollect_now () 360find Xfindfunc2 361call assert_match ( 'Xfindfunc2\.c' , @%) 362bw ! 363delfunc FindFuncBasic 364call test_garbagecollect_now () 365call assert_fails ( 'find Xfindfunc2' , 'E117: Unknown function: FindFuncBasic' ) 366 367" 'findfunc' with dicts in the returned list 368func FindFuncDict ( pat , cmdcomplete ) 369return [ 370 \#{ word : 'Xfindfunc1.c' , abbr : 'Xff1.c' }, 371 \#{ word : 'Xfindfunc2.c' }, 372 \'Xfindfunc3.c' , 373"\ invalid values 374 \#{ abbr : 'XXX' }, 375 \v: _null_dict , 376 \v: _null_string , 377 \] 378endfunc 379 380set findfunc = FindFuncDict 381find Xfind 382call assert_match ( 'Xfindfunc1\.c' , @%) 383bw ! 3842 find Xfind 385call assert_match ( 'Xfindfunc2\.c' , @%) 386bw ! 3873 find Xfind 388call assert_match ( 'Xfindfunc3\.c' , @%) 389bw ! 390" These invalid values should not crash 3914 find Xfind 3925 find Xfind 3936 find Xfind 394call assert_fails ( '7find Xfind' , 'E347: No more file "Xfind" found in path' ) 395call assert_equal ( '' , @%) 396 %bw ! 397delfunc FindFuncDict 398 399" Buffer-local option 400func GlobalFindFunc ( pat , cmdcomplete ) 401return [ 'global' ] 402endfunc 403func LocalFindFunc ( pat , cmdcomplete ) 404return [ 'local' ] 405endfunc 406set findfunc = GlobalFindFunc 407new 408setlocal findfunc = LocalFindFunc 409find xxxx 410call assert_equal ( 'local' , @%) 411wincmd w 412find xxxx 413call assert_equal ( 'global' , @%) 414aboveleft new 415call assert_equal ( "GlobalFindFunc" , & findfunc ) 416wincmd k 417aboveleft new 418call assert_equal ( "GlobalFindFunc" , & findfunc ) 419 %bw ! 420delfunc GlobalFindFunc 421delfunc LocalFindFunc 422 423" Assign an expression 424set findfunc = [] 425call assert_fails ( 'find xxxx' , 'E117: Unknown function: []' ) 426 427" Error cases 428 429" Function that doesn't take any arguments 430func FindFuncNoArg () 431endfunc 432set findfunc = FindFuncNoArg 433call assert_fails ( 'find Xfindfunc1.c' , 'E118: Too many arguments for function: FindFuncNoArg' ) 434delfunc FindFuncNoArg 435 436" Syntax error in the function 437func FindFuncSyntaxError ( pat , cmdcomplete ) 438return l 439endfunc 440set findfunc = FindFuncSyntaxError 441call assert_fails ( 'find Xfindfunc1.c' , 'E121: Undefined variable: l' ) 442delfunc FindFuncSyntaxError 443 444" Find function throws an error 445func FindFuncWithThrow ( pat , cmdcomplete ) 446throw 'find error' 447endfunc 448set findfunc = FindFuncWithThrow 449call assert_fails ( 'find Xfindfunc1.c' , 'find error' ) 450delfunc FindFuncWithThrow 451 452" Try using a null function 453"call assert_fails('let &findfunc = test_null_function()', 'E129: Function name required') 454 455" Try to create a new window from the find function 456func FindFuncNewWindow ( pat , cmdexpand ) 457new 458return [ "foo" ] 459endfunc 460set findfunc = FindFuncNewWindow 461call assert_fails ( 'find Xfindfunc1.c' , 'E565: Not allowed to change text or change window' ) 462delfunc FindFuncNewWindow 463 464" Try to modify the current buffer from the find function 465func FindFuncModifyBuf ( pat , cmdexpand ) 466call setline ( 1 , [ 'abc' ]) 467return [ "foo" ] 468endfunc 469set findfunc = FindFuncModifyBuf 470call assert_fails ( 'find Xfindfunc1.c' , 'E565: Not allowed to change text or change window' ) 471delfunc FindFuncModifyBuf 472 473" Return the wrong type from the function 474func FindFuncWrongRet ( pat , cmdexpand ) 475return 'foo' 476endfunc 477set findfunc = FindFuncWrongRet 478call assert_fails ( 'find Xfindfunc1.c' , "E1514: 'findfunc' did not return a List type" ) 479delfunc FindFuncWrongRet 480 481set findfunc & 482endfunc 483 484" Test for using a script-local function for 'findfunc' 485func Test_findfunc_scriptlocal_func () 486func ! s: FindFuncScript ( pat , cmdexpand ) 487let g: FindFuncArg = a: pat 488return [ 'xxx' ] 489endfunc 490 491set findfunc = s:FindFuncScript 492call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & findfunc ) 493call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & g: findfunc ) 494new |only 495let g: FindFuncArg = '' 496find abc 497call assert_equal ( 'abc' , g: FindFuncArg ) 498bw ! 499 500set findfunc = <SID>FindFuncScript 501call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & findfunc ) 502call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & g: findfunc ) 503new |only 504let g: FindFuncArg = '' 505find abc 506call assert_equal ( 'abc' , g: FindFuncArg ) 507bw ! 508 509let & findfunc = 's:FindFuncScript' 510call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & g: findfunc ) 511new |only 512let g: FindFuncArg = '' 513find abc 514call assert_equal ( 'abc' , g: FindFuncArg ) 515bw ! 516 517let & findfunc = '<SID>FindFuncScript' 518call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & g: findfunc ) 519new |only 520let g: FindFuncArg = '' 521find abc 522call assert_equal ( 'abc' , g: FindFuncArg ) 523bw ! 524 525set findfunc = 526setglobal findfunc=s:FindFuncScript 527setlocal findfunc = 528call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & findfunc ) 529call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & g: findfunc ) 530call assert_equal ( '' , & l: findfunc ) 531new |only 532let g: FindFuncArg = '' 533find abc 534call assert_equal ( 'abc' , g: FindFuncArg ) 535bw ! 536 537new |only 538set findfunc = 539setglobal findfunc= 540setlocal findfunc = s:FindFuncScript 541call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & findfunc ) 542call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & l: findfunc ) 543call assert_equal ( '' , & g: findfunc ) 544let g: FindFuncArg = '' 545find abc 546call assert_equal ( 'abc' , g: FindFuncArg ) 547bw ! 548 549new |only 550set findfunc = 551setlocal findfunc = NoSuchFunc 552setglobal findfunc=s:FindFuncScript 553call assert_equal ( 'NoSuchFunc' , & findfunc ) 554call assert_equal ( 'NoSuchFunc' , & l: findfunc ) 555call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & g: findfunc ) 556new |only 557call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & findfunc ) 558call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & g: findfunc ) 559call assert_equal ( '' , & l: findfunc ) 560let g: FindFuncArg = '' 561find abc 562call assert_equal ( 'abc' , g: FindFuncArg ) 563bw ! 564 565new |only 566set findfunc = 567setlocal findfunc = NoSuchFunc 568set findfunc = s:FindFuncScript 569call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & findfunc ) 570call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & g: findfunc ) 571call assert_equal ( '' , & l: findfunc ) 572let g: FindFuncArg = '' 573find abc 574call assert_equal ( 'abc' , g: FindFuncArg ) 575new |only 576call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & findfunc ) 577call assert_equal ( expand ( '<SID>' ) .. 'FindFuncScript' , & g: findfunc ) 578call assert_equal ( '' , & l: findfunc ) 579let g: FindFuncArg = '' 580find abc 581call assert_equal ( 'abc' , g: FindFuncArg ) 582bw ! 583 584set findfunc = 585delfunc s:FindFuncScript 586endfunc 587 588" Test for expanding the argument to the :find command using 'findfunc' 589func Test_findfunc_expand_arg () 590let s: fnames = [ 'Xfindfunc1.c' , 'Xfindfunc2.c' , 'Xfindfunc3.c' ] 591 592" 'findfunc' that accepts a regular expression 593func FindFuncRegexp ( pat , cmdcomplete ) 594return s: fnames ->copy () ->filter ( 'v:val =~? a:pat' ) 595endfunc 596 597" 'findfunc' that accepts a glob 598func FindFuncGlob ( pat_arg , cmdcomplete ) 599let pat = glob2regpat ( a: cmdcomplete ?$ '*{a:pat_arg}*' : a:pat_arg) 600return s: fnames ->copy () ->filter ( 'v:val =~? pat' ) 601endfunc 602 603for regexp in [ v: true , v: false ] 604let & findfunc = regexp ? 'FindFuncRegexp' : 'FindFuncGlob' 605 606call feedkeys ( ":find \<Tab>\<C-B>\"\<CR>" , "xt" ) 607call assert_equal ( '"find Xfindfunc1.c' , @:) 608 609call feedkeys ( ":find Xfind\<Tab>\<Tab>\<C-B>\"\<CR>" , "xt" ) 610call assert_equal ( '"find Xfindfunc2.c' , @:) 611 612call assert_equal ( s: fnames , getcompletion ( 'find ' , 'cmdline' )) 613call assert_equal ( s: fnames , getcompletion ( 'find Xfind' , 'cmdline' )) 614 615let pat = regexp ? 'X.*1\.c' : 'X*1.c' 616call feedkeys ( $ ":find {pat}\<Tab>\<C-B>\"\<CR>", "xt") 617call assert_equal ( '"find Xfindfunc1.c' , @:) 618call assert_equal ([ 'Xfindfunc1.c' ], getcompletion ( $ 'find {pat}', 'cmdline')) 619 620call feedkeys ( ":find 3\<Tab>\<C-B>\"\<CR>" , "xt" ) 621call assert_equal ( '"find Xfindfunc3.c' , @:) 622call assert_equal ([ 'Xfindfunc3.c' ], getcompletion ( $ 'find3', 'cmdline')) 623 624call feedkeys ( ":find Xfind\<C-A>\<C-B>\"\<CR>" , "xt" ) 625call assert_equal ( '"find Xfindfunc1.c Xfindfunc2.c Xfindfunc3.c' , @:) 626 627call feedkeys ( ":find abc\<Tab>\<C-B>\"\<CR>" , "xt" ) 628call assert_equal ( '"find abc' , @:) 629call assert_equal ([], getcompletion ( 'find abc' , 'cmdline' )) 630endfor 631 632set findfunc & 633delfunc ! FindFuncRegexp 634delfunc ! FindFuncGlob 635unlet s: fnames 636endfunc 637 638" Test for different ways of setting the 'findfunc' option 639func Test_findfunc_callback () 640new 641func FindFunc1 ( pat , cmdexpand ) 642let g: FindFunc1Args = [ a: pat , a: cmdexpand ] 643return [ 'findfunc1' ] 644endfunc 645 646let lines =<< trim END 647#" Test for using a function name 648LET &findfunc = 'g:FindFunc1' 649LET g:FindFunc1Args = [] 650find abc1 651call assert_equal(['abc1', v:false], g:FindFunc1Args) 652 653#" Test for using a function() 654set findfunc=function('g:FindFunc1') 655LET g:FindFunc1Args = [] 656find abc2 657call assert_equal(['abc2', v:false], g:FindFunc1Args) 658 659#" Using a funcref variable to set 'findfunc' 660VAR Fn = function('g:FindFunc1') 661LET &findfunc = Fn 662LET g:FindFunc1Args = [] 663find abc3 664call assert_equal(['abc3', v:false], g:FindFunc1Args) 665 666#" Using a string(funcref_variable) to set 'findfunc' 667LET Fn = function('g:FindFunc1') 668LET &findfunc = string(Fn) 669LET g:FindFunc1Args = [] 670find abc4 671call assert_equal(['abc4', v:false], g:FindFunc1Args) 672 673#" Test for using a funcref() 674set findfunc=funcref('g:FindFunc1') 675LET g:FindFunc1Args = [] 676find abc5 677call assert_equal(['abc5', v:false], g:FindFunc1Args) 678 679#" Using a funcref variable to set 'findfunc' 680LET Fn = funcref('g:FindFunc1') 681LET &findfunc = Fn 682LET g:FindFunc1Args = [] 683find abc6 684call assert_equal(['abc6', v:false], g:FindFunc1Args) 685 686#" Using a string(funcref_variable) to set 'findfunc' 687LET Fn = funcref('g:FindFunc1') 688LET &findfunc = string(Fn) 689LET g:FindFunc1Args = [] 690find abc7 691call assert_equal(['abc7', v:false], g:FindFunc1Args) 692 693#" Test for using a lambda function using set 694VAR optval = "LSTART pat, cmdexpand LMIDDLE FindFunc1(pat, cmdexpand) LEND" 695LET optval = substitute(optval, ' ', '\\ ', 'g') 696exe "set findfunc=" .. optval 697LET g:FindFunc1Args = [] 698find abc8 699call assert_equal(['abc8', v:false], g:FindFunc1Args) 700 701#" Test for using a lambda function using LET 702LET &findfunc = LSTART pat, _ LMIDDLE FindFunc1(pat, v:false) LEND 703LET g:FindFunc1Args = [] 704find abc9 705call assert_equal(['abc9', v:false], g:FindFunc1Args) 706 707#" Set 'findfunc' to a string(lambda expression) 708LET &findfunc = 'LSTART pat, _ LMIDDLE FindFunc1(pat, v:false) LEND' 709LET g:FindFunc1Args = [] 710find abc10 711call assert_equal(['abc10', v:false], g:FindFunc1Args) 712 713#" Set 'findfunc' to a variable with a lambda expression 714VAR Lambda = LSTART pat, _ LMIDDLE FindFunc1(pat, v:false) LEND 715LET &findfunc = Lambda 716LET g:FindFunc1Args = [] 717find abc11 718call assert_equal(['abc11', v:false], g:FindFunc1Args) 719 720#" Set 'findfunc' to a string(variable with a lambda expression) 721LET Lambda = LSTART pat, _ LMIDDLE FindFunc1(pat, v:false) LEND 722LET &findfunc = string(Lambda) 723LET g:FindFunc1Args = [] 724find abc12 725call assert_equal(['abc12', v:false], g:FindFunc1Args) 726 727#" Try to use 'findfunc' after the function is deleted 728func g:TmpFindFunc(pat, cmdexpand) 729let g:TmpFindFunc1Args = [a:pat, a:cmdexpand] 730endfunc 731LET &findfunc = function('g:TmpFindFunc') 732delfunc g:TmpFindFunc 733call test_garbagecollect_now() 734LET g:TmpFindFunc1Args = [] 735call assert_fails('find abc13', 'E117:') 736call assert_equal([], g:TmpFindFunc1Args) 737 738#" Try to use a function with three arguments for 'findfunc' 739func g:TmpFindFunc2(x, y, z) 740let g:TmpFindFunc2Args = [a:x, a:y, a:z] 741endfunc 742set findfunc=TmpFindFunc2 743LET g:TmpFindFunc2Args = [] 744call assert_fails('find abc14', 'E119:') 745call assert_equal([], g:TmpFindFunc2Args) 746delfunc TmpFindFunc2 747 748#" Try to use a function with zero arguments for 'findfunc' 749func g:TmpFindFunc3() 750let g:TmpFindFunc3Called = v:true 751endfunc 752set findfunc=TmpFindFunc3 753LET g:TmpFindFunc3Called = v:false 754call assert_fails('find abc15', 'E118:') 755call assert_equal(v:false, g:TmpFindFunc3Called) 756delfunc TmpFindFunc3 757 758#" Try to use a lambda function with three arguments for 'findfunc' 759LET &findfunc = LSTART a, b, c LMIDDLE FindFunc1(a, v:false) LEND 760LET g:FindFunc1Args = [] 761call assert_fails('find abc16', 'E119:') 762call assert_equal([], g:FindFunc1Args) 763 764#" Test for clearing the 'findfunc' option 765set findfunc='' 766set findfunc& 767call assert_fails("set findfunc=function('abc')", "E700:") 768call assert_fails("set findfunc=funcref('abc')", "E700:") 769 770#" set 'findfunc' to a non-existing function 771LET &findfunc = function('g:FindFunc1') 772call assert_fails("set findfunc=function('NonExistingFunc')", 'E700:') 773call assert_fails("LET &findfunc = function('NonExistingFunc')", 'E700:') 774LET g:FindFunc1Args = [] 775find abc17 776call assert_equal(['abc17', v:false], g:FindFunc1Args) 777END 778call CheckTransLegacySuccess ( lines ) 779 780" Test for using a script-local function name 781func s: FindFunc2 ( pat , cmdexpand ) 782let g: FindFunc2Args = [ a: pat , a: cmdexpand ] 783return [ 'findfunc2' ] 784endfunc 785set findfunc = s:FindFunc2 786let g: FindFunc2Args = [] 787find abc18 788call assert_equal ([ 'abc18' , v: false ], g: FindFunc2Args ) 789 790let & findfunc = 's:FindFunc2' 791let g: FindFunc2Args = [] 792find abc19 793call assert_equal ([ 'abc19' , v: false ], g: FindFunc2Args ) 794delfunc s:FindFunc2 795 796" Using Vim9 lambda expression in legacy context should fail 797set findfunc = (pat,\ cmdexpand)\ =>\ FindFunc1(pat,\ v:false) 798let g: FindFunc1Args = [] 799call assert_fails ( 'find abc20' , 'E117:' ) 800call assert_equal ([], g: FindFunc1Args ) 801 802" set 'findfunc' to a partial with dict. 803func SetFindFunc () 804let operator = { 'execute' : function ( 'FindFuncExecute' )} 805let & findfunc = operator . execute 806endfunc 807func FindFuncExecute ( pat , cmdexpand ) dict 808return [ 'findfuncexecute' ] 809endfunc 810call SetFindFunc () 811call test_garbagecollect_now () 812set findfunc = 813delfunc SetFindFunc 814delfunc FindFuncExecute 815 816func FindFunc2 ( pat , cmdexpand ) 817let g: FindFunc2Args = [ a: pat , a: cmdexpand ] 818return [ 'findfunc2' ] 819endfunc 820 821" Vim9 tests 822let lines =<< trim END 823vim9script 824 825def g:Vim9findFunc(pat: string, cmdexpand: bool): list<string> 826g:FindFunc1Args = [pat, cmdexpand] 827return ['vim9findfunc'] 828enddef 829 830# Test for using a def function with findfunc 831set findfunc=function('g:Vim9findFunc') 832g:FindFunc1Args = [] 833find abc21 834assert_equal(['abc21', false], g:FindFunc1Args) 835 836# Test for using a global function name 837&findfunc = g:FindFunc2 838g:FindFunc2Args = [] 839find abc22 840assert_equal(['abc22', false], g:FindFunc2Args) 841bw! 842 843# Test for using a script-local function name 844def LocalFindFunc(pat: string, cmdexpand: bool): list<string> 845g:LocalFindFuncArgs = [pat, cmdexpand] 846return ['localfindfunc'] 847enddef 848&findfunc = LocalFindFunc 849g:LocalFindFuncArgs = [] 850find abc23 851assert_equal(['abc23', false], g:LocalFindFuncArgs) 852bw! 853END 854call CheckScriptSuccess ( lines ) 855 856" setting 'findfunc' to a script local function outside of a script context 857" should fail 858let cleanup =<< trim END 859call writefile([execute('messages')], 'Xtest.out') 860qall 861END 862call writefile ( cleanup , 'Xverify.vim' , 'D' ) 863call RunVim ([], [], "-c \"set findfunc=s:abc\" -S Xverify.vim" ) 864call assert_match ( 'E81: Using <SID> not in a' , readfile ( 'Xtest.out' )[ 0 ]) 865call delete ( 'Xtest.out' ) 866 867" cleanup 868set findfunc & 869delfunc FindFunc1 870delfunc FindFunc2 871unlet g: FindFunc1Args g: FindFunc2Args 872 %bw ! 873endfunc 874 875" Test using environment variables with spaces 876func Test_path_env_variable_with_whitespaces () 877let save_path = & path 878defer execute('let &path = save_path') 879 880let $ testdir = 'Xpath with some whites' 881call mkdir ( $ testdir , 'R' ) 882 883" Check direct usage yields the same result that autocomplete 884call feedkeys ( ':set path=$testdir' .. "\<C-A>\<CR>" , 'xt' ) 885let auto_testpath = & path 886" include autocomplete suffix 887exe "set path=$testdir" .. "/" 888call assert_equal ( auto_testpath , & path ) 889 890" Check a file can be found using environment variables 891let expanded_test_path = expand ( '$testdir/test.txt' ) 892call writefile ([ 'testing...' ], expanded_test_path ) 893 894" hinting an environment variable path 895call assert_equal ( expanded_test_path , findfile ( 'test.txt' , $ test_dir )) 896 897" using 'path' option with an environment variable 898set path = $testdir 899call assert_equal ( expanded_test_path , findfile ( 'test.txt' )) 900 901" using :find instead of findfile() 902find test.txt 903call assert_equal ( expanded_test_path , expand ( '%:.' )) 904enew 905endfunc 906 907" vim: shiftwidth=2 sts=2 expandtab