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