robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
715c28d67f
master
1" Test for the gf and gF (goto file) commands 2 3" This is a test if a URL is recognized by "gf", with the cursor before and 4" after the "://". Also test ":\\". 5func Test_gf_url () 6enew ! 7call append ( 0 , [ 8 \"first test for URL://machine.name/tmp/vimtest2a and other text" , 9 \"second test for URL://machine.name/tmp/vimtest2b. And other text" , 10 \"third test for URL:\\\\machine.name\\vimtest2c and other text" , 11 \"fourth test for URL:\\\\machine.name\\tmp\\vimtest2d, and other text" , 12 \"fifth test for URL://machine.name/tmp?q=vim&opt=yes and other text" , 13 \"sixth test for URL://machine.name:1234?q=vim and other text" , 14 \]) 15call cursor ( 1 , 1 ) 16call search ( "^first" ) 17call search ( "tmp" ) 18call assert_equal ( "URL://machine.name/tmp/vimtest2a" , expand ( "<cfile>" )) 19call search ( "^second" ) 20call search ( "URL" ) 21call assert_equal ( "URL://machine.name/tmp/vimtest2b" , expand ( "<cfile>" )) 22set isf = @,48-57,/,.,-,_,+,,,$,~,\ 23call search ( "^third" ) 24call search ( "name" ) 25call assert_equal ( "URL:\\\\machine.name\\vimtest2c" , expand ( "<cfile>" )) 26call search ( "^fourth" ) 27call search ( "URL" ) 28call assert_equal ( "URL:\\\\machine.name\\tmp\\vimtest2d" , expand ( "<cfile>" )) 29 30call search ( "^fifth" ) 31call search ( "URL" ) 32call assert_equal ( "URL://machine.name/tmp?q=vim&opt=yes" , expand ( "<cfile>" )) 33 34call search ( "^sixth" ) 35call search ( "URL" ) 36call assert_equal ( "URL://machine.name:1234?q=vim" , expand ( "<cfile>" )) 37 38 %d 39call setline ( 1 , "demo://remote_file" ) 40wincmd f 41call assert_equal ( 'demo://remote_file' , @%) 42call assert_equal ( 2 , winnr ( '$' )) 43close ! 44 45set isf & vim 46enew ! 47endfunc 48 49func Test_gF () 50new 51call setline ( 1 , [ '111' , '222' , '333' , '444' ]) 52w ! Xfile 53close 54new 55set isfname -= : 56call setline ( 1 , [ 'one' , 'Xfile:3' , 'three' ]) 572 58call assert_fails ( 'normal gF' , 'E37:' ) 59call assert_equal ( 2 , getcurpos ()[ 1 ]) 60w ! Xfile2 61normal gF 62call assert_equal ( 'Xfile' , bufname ( '%' )) 63call assert_equal ( 3 , getcurpos ()[ 1 ]) 64 65enew ! 66call setline ( 1 , [ 'one' , 'the Xfile line 2, and more' , 'three' ]) 67w ! Xfile2 68normal 2GfX 69normal gF 70call assert_equal ( 'Xfile' , bufname ( '%' )) 71call assert_equal ( 2 , getcurpos ()[ 1 ]) 72 73" jumping to the file/line with CTRL-W_F 74 %bw ! 75edit Xfile1 76call setline ( 1 , [ 'one' , 'Xfile:4' , 'three' ]) 77exe "normal 2G\<C-W>F" 78call assert_equal ( 'Xfile' , bufname ( '%' )) 79call assert_equal ( 4 , getcurpos ()[ 1 ]) 80 81set isfname & 82call delete ( 'Xfile' ) 83call delete ( 'Xfile2' ) 84 %bw ! 85endfunc 86 87" Test for invoking 'gf' on a ${VAR} variable 88func Test_gf () 89set isfname = @,48-57,/,.,-,_,+,,,$,:,~,{,} 90 91call writefile ([ "Test for gf command" ], "Xtest1" ) 92if has ( "unix" ) 93call writefile ([ " ${CDIR}/Xtest1" ], "Xtestgf" ) 94else 95call writefile ([ " $TDIR/Xtest1" ], "Xtestgf" ) 96endif 97new Xtestgf 98if has ( "unix" ) 99let $ CDIR = "." 100/ CDIR 101else 102if has ( "amiga" ) 103let $ TDIR = "/testdir" 104else 105let $ TDIR = "." 106endif 107/ TDIR 108endif 109 110normal gf 111call assert_equal ( 'Xtest1' , fnamemodify ( bufname ( '' ), ":t" )) 112close ! 113 114call delete ( 'Xtest1' ) 115call delete ( 'Xtestgf' ) 116endfunc 117 118func Test_gf_visual () 119call writefile ([ 'one' , 'two' , 'three' , 'four' ], "Xtest_gf_visual" ) 120new 121call setline ( 1 , 'XXXtest_gf_visualXXX' ) 122set hidden 123 124" Visually select Xtest_gf_visual and use gf to go to that file 125norm ! ttvtXgf 126call assert_equal ( 'Xtest_gf_visual' , bufname ( '%' )) 127 128" if multiple lines are selected, then gf should fail 129call setline ( 1 , [ "one" , "two" ]) 130normal VGgf 131call assert_equal ( 'Xtest_gf_visual' , @%) 132 133" following line number is used for gF 134bwipe ! 135new 136call setline ( 1 , 'XXXtest_gf_visual:3XXX' ) 137norm ! 0ttvt:gF 138call assert_equal ( 'Xtest_gf_visual' , bufname ( '%' )) 139call assert_equal ( 3 , getcurpos ()[ 1 ]) 140 141" do not include the NUL at the end 142call writefile ([ 'x' ], 'X' ) 143let save_enc = & enc 144" for enc in ['latin1', 'utf-8'] 145for enc in [ 'utf-8' ] 146exe "set enc=" .. enc 147new 148call setline ( 1 , 'X' ) 149set nomodified 150exe "normal \<C-V>$gf" 151call assert_equal ( 'X' , bufname ()) 152bwipe ! 153endfor 154let & enc = save_enc 155call delete ( 'X' ) 156 157" line number in visual area is used for file name 158if has ( 'unix' ) 159bwipe ! 160call writefile ([], "Xtest_gf_visual:3" ) 161new 162call setline ( 1 , 'XXXtest_gf_visual:3XXX' ) 163norm ! 0ttvtXgF 164call assert_equal ( 'Xtest_gf_visual:3' , bufname ( '%' )) 165call delete ( 'Xtest_gf_visual:3' ) 166endif 167 168bwipe ! 169call delete ( 'Xtest_gf_visual' ) 170set hidden & 171endfunc 172 173func Test_gf_error () 174new 175call assert_fails ( 'normal gf' , 'E446:' ) 176call assert_fails ( 'normal gF' , 'E446:' ) 177call setline ( 1 , '/doesnotexist' ) 178call assert_fails ( 'normal gf' , 'E447:' ) 179call assert_fails ( 'normal gF' , 'E447:' ) 180call assert_fails ( 'normal [f' , 'E447:' ) 181 182" gf is not allowed when text is locked 183au InsertCharPre <buffer> normal ! gF<CR> 184let caught_e565 = 0 185try 186call feedkeys ( "ix\<esc>" , 'xt' ) 187catch /^Vim\%((\a\+)\)\=:E565/ " catch E565 188let caught_e565 = 1 189endtry 190call assert_equal ( 1 , caught_e565 ) 191au ! InsertCharPre 192 193bwipe ! 194 195" gf is not allowed when buffer is locked 196new 197augroup Test_gf 198au ! 199au OptionSet diff norm ! gf 200augroup END 201call setline ( 1 , [ 'Xfile1' , 'line2' , 'line3' , 'line4' ]) 202call Ntest_override ( 'starting' , 1 ) 203call assert_fails ( 'diffthis' , 'E788:' ) 204call Ntest_override ( 'starting' , 0 ) 205augroup Test_gf 206au ! 207augroup END 208bw ! 209endfunc 210 211" If a file is not found by 'gf', then 'includeexpr' should be used to locate 212" the file. 213func Test_gf_includeexpr () 214new 215let g: Inc_fname = '' 216func IncFunc () 217let g: Inc_fname = v: fname 218return v: fname 219endfunc 220setlocal includeexpr = IncFunc () 221call setline ( 1 , 'somefile.java' ) 222call assert_fails ( 'normal gf' , 'E447:' ) 223call assert_equal ( 'somefile.java' , g: Inc_fname ) 224close ! 225delfunc IncFunc 226endfunc 227 228" Test for using a script-local function for 'includeexpr' 229func Test_includeexpr_scriptlocal_func () 230func ! s: IncludeFunc () 231let g: IncludeFname = v: fname 232return '' 233endfunc 234set includeexpr = s :IncludeFunc() 235call assert_equal ( expand ( '<SID>' ) .. 'IncludeFunc()' , & includeexpr ) 236call assert_equal ( expand ( '<SID>' ) .. 'IncludeFunc()' , & g: includeexpr ) 237new |only 238call setline ( 1 , 'TestFile1' ) 239let g: IncludeFname = '' 240call assert_fails ( 'normal! gf' , 'E447:' ) 241call assert_equal ( 'TestFile1' , g: IncludeFname ) 242bw ! 243set includeexpr = <SID>IncludeFunc() 244call assert_equal ( expand ( '<SID>' ) .. 'IncludeFunc()' , & includeexpr ) 245call assert_equal ( expand ( '<SID>' ) .. 'IncludeFunc()' , & g: includeexpr ) 246new |only 247call setline ( 1 , 'TestFile2' ) 248let g: IncludeFname = '' 249call assert_fails ( 'normal! gf' , 'E447:' ) 250call assert_equal ( 'TestFile2' , g: IncludeFname ) 251bw ! 252setlocal includeexpr = 253setglobal includeexpr=s:IncludeFunc() 254call assert_equal ( expand ( '<SID>' ) .. 'IncludeFunc()' , & g: includeexpr ) 255call assert_equal ( '' , & includeexpr ) 256new 257call assert_equal ( expand ( '<SID>' ) .. 'IncludeFunc()' , & includeexpr ) 258call setline ( 1 , 'TestFile3' ) 259let g: IncludeFname = '' 260call assert_fails ( 'normal! gf' , 'E447:' ) 261call assert_equal ( 'TestFile3' , g: IncludeFname ) 262bw ! 263setlocal includeexpr = 264setglobal includeexpr=<SID>IncludeFunc() 265call assert_equal ( expand ( '<SID>' ) .. 'IncludeFunc()' , & g: includeexpr ) 266call assert_equal ( '' , & includeexpr ) 267new 268call assert_equal ( expand ( '<SID>' ) .. 'IncludeFunc()' , & includeexpr ) 269call setline ( 1 , 'TestFile4' ) 270let g: IncludeFname = '' 271call assert_fails ( 'normal! gf' , 'E447:' ) 272call assert_equal ( 'TestFile4' , g: IncludeFname ) 273bw ! 274set includeexpr & 275delfunc s:IncludeFunc 276bw ! 277endfunc 278 279" Check that expanding directories can handle more than 255 entries. 280func Test_gf_subdirs_wildcard () 281let cwd = getcwd () 282let dir = 'Xtestgf_dir' 283call mkdir ( dir ) 284call chdir ( dir ) 285for i in range ( 300 ) 286call mkdir ( i ) 287call writefile ([], i .. '/' .. i , 'S' ) 288endfor 289set path = ./** 290 291new |only 292call setline ( 1 , '99' ) 293w ! Xtest1 294normal gf 295call assert_equal ( '99' , fnamemodify ( bufname ( '' ), ":t" )) 296 297call chdir ( cwd ) 298call delete ( dir , 'rf' ) 299set path & 300endfunc 301 302" Test for 'switchbuf' with gf and gF commands 303func Test_gf_switchbuf () 304call writefile ( repeat ([ "aaa" ], 10 ), "Xtest1" , 'D' ) 305edit Xtest1 306new 307call setline ( 1 , [ 'Xtest1' ]) 308 309" Test for 'useopen' 310set switchbuf = useopen 311call cursor ( 1 , 1 ) 312exe "normal \<C-W>f" 313call assert_equal ([ 2 , 2 ], [ winnr (), winnr ( '$' )]) 314close 315 316" If the file is opened in another tabpage, then it should not be considered 317tabedit Xtest1 318tabfirst 319exe "normal \<C-W>f" 320call assert_equal ([ 1 , 2 ], [ winnr (), winnr ( '$' )]) 321call assert_equal ([ 1 , 2 ], [ tabpagenr (), tabpagenr ( '$' )]) 322close 323 324" Test for 'usetab' 325set switchbuf = usetab 326exe "normal \<C-W>f" 327call assert_equal ([ 1 , 1 ], [ winnr (), winnr ( '$' )]) 328call assert_equal ([ 2 , 2 ], [ tabpagenr (), tabpagenr ( '$' )]) 329 %bw ! 330 331" Test for CTRL-W_F with 'useopen' 332set isfname -= : 333call setline ( 1 , [ 'Xtest1:5' ]) 334set switchbuf = useopen 335split +1 Xtest1 336wincmd b 337exe "normal \<C-W>F" 338call assert_equal ([ 1 , 2 ], [ winnr (), winnr ( '$' )]) 339call assert_equal ( 5 , line ( '.' )) 340close 341 342" If the file is opened in another tabpage, then it should not be considered 343tabedit +1 Xtest1 344tabfirst 345exe "normal \<C-W>F" 346call assert_equal ([ 1 , 2 ], [ winnr (), winnr ( '$' )]) 347call assert_equal ( 5 , line ( '.' )) 348call assert_equal ([ 1 , 2 ], [ tabpagenr (), tabpagenr ( '$' )]) 349close 350 351" Test for CTRL_W_F with 'usetab' 352set switchbuf = usetab 353exe "normal \<C-W>F" 354call assert_equal ([ 2 , 2 ], [ tabpagenr (), tabpagenr ( '$' )]) 355call assert_equal ([ 1 , 1 ], [ winnr (), winnr ( '$' )]) 356call assert_equal ( 5 , line ( '.' )) 357 358set switchbuf = 359set isfname & 360 %bw ! 361endfunc 362 363func Test_gf_with_suffixesadd () 364let cwd = getcwd () 365let dir = 'Xtestgf_sua_dir' 366call mkdir ( dir , 'R' ) 367call chdir ( dir ) 368 369call writefile ([], 'foo.c' , 'D' ) 370call writefile ([], 'bar.cpp' , 'D' ) 371call writefile ([], 'baz.cc' , 'D' ) 372call writefile ([], 'foo.o' , 'D' ) 373call writefile ([], 'bar.o' , 'D' ) 374call writefile ([], 'baz.o' , 'D' ) 375 376new 377setlocal path = ,,suffixesadd = .c,.cpp 378call setline ( 1 , [ './foo' , './bar' , './baz' ]) 379exe "normal! gg\<C-W>f" 380call assert_equal ( 'foo.c' , expand ( '%:t' )) 381close 382exe "normal! 2gg\<C-W>f" 383call assert_equal ( 'bar.cpp' , expand ( '%:t' )) 384close 385call assert_fails ( 'exe "normal! 3gg\<C-W>f"' , 'E447:' ) 386setlocal suffixesadd += .cc 387exe "normal! 3gg\<C-W>f" 388call assert_equal ( 'baz.cc' , expand ( '%:t' )) 389close 390 391 %bwipe ! 392call chdir ( cwd ) 393endfunc 394 395" vim: shiftwidth=2 sts=2 expandtab