test_gf.vim 9.4 KB

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