blob: eaa8c0fc950ac545a1e9e03cad04a921312106b7 [file] [log] [blame]
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001" Test for the gf and gF (goto file) commands
Bram Moolenaar53f16732016-09-07 20:46:39 +02002
3" This is a test if a URL is recognized by "gf", with the cursor before and
4" after the "://". Also test ":\\".
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02005func Test_gf_url()
Bram Moolenaar53f16732016-09-07 20:46:39 +02006 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",
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +010011 \ "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",
Bram Moolenaarcbef8e12019-03-09 12:32:56 +010013 \ "sixth test for URL://machine.name:1234?q=vim and other text",
Bram Moolenaar53f16732016-09-07 20:46:39 +020014 \ ])
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 if has("ebcdic")
23 set isf=@,240-249,/,.,-,_,+,,,$,:,~,\
24 else
Bram Moolenaarcbef8e12019-03-09 12:32:56 +010025 set isf=@,48-57,/,.,-,_,+,,,$,~,\
Bram Moolenaar53f16732016-09-07 20:46:39 +020026 endif
27 call search("^third")
28 call search("name")
29 call assert_equal("URL:\\\\machine.name\\vimtest2c", expand("<cfile>"))
30 call search("^fourth")
31 call search("URL")
32 call assert_equal("URL:\\\\machine.name\\tmp\\vimtest2d", expand("<cfile>"))
33
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +010034 call search("^fifth")
35 call search("URL")
36 call assert_equal("URL://machine.name/tmp?q=vim&opt=yes", expand("<cfile>"))
37
Bram Moolenaarcbef8e12019-03-09 12:32:56 +010038 call search("^sixth")
39 call search("URL")
40 call assert_equal("URL://machine.name:1234?q=vim", expand("<cfile>"))
41
Bram Moolenaarbdd2c292020-06-22 21:34:30 +020042 %d
43 call setline(1, "demo://remote_file")
44 wincmd f
45 call assert_equal('demo://remote_file', @%)
46 call assert_equal(2, winnr('$'))
47 close!
48
Bram Moolenaar53f16732016-09-07 20:46:39 +020049 set isf&vim
50 enew!
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020051endfunc
52
53func Test_gF()
54 new
55 call setline(1, ['111', '222', '333', '444'])
56 w! Xfile
57 close
58 new
Bram Moolenaar712598f2017-05-24 10:42:37 +020059 set isfname-=:
60 call setline(1, ['one', 'Xfile:3', 'three'])
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020061 2
62 call assert_fails('normal gF', 'E37:')
63 call assert_equal(2, getcurpos()[1])
64 w! Xfile2
65 normal gF
66 call assert_equal('Xfile', bufname('%'))
67 call assert_equal(3, getcurpos()[1])
68
Bram Moolenaar64e74c92019-12-22 15:38:06 +010069 enew!
70 call setline(1, ['one', 'the Xfile line 2, and more', 'three'])
71 w! Xfile2
72 normal 2GfX
73 normal gF
74 call assert_equal('Xfile', bufname('%'))
75 call assert_equal(2, getcurpos()[1])
76
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020077 " jumping to the file/line with CTRL-W_F
78 %bw!
79 edit Xfile1
80 call setline(1, ['one', 'Xfile:4', 'three'])
81 exe "normal 2G\<C-W>F"
82 call assert_equal('Xfile', bufname('%'))
83 call assert_equal(4, getcurpos()[1])
84
Bram Moolenaar712598f2017-05-24 10:42:37 +020085 set isfname&
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020086 call delete('Xfile')
87 call delete('Xfile2')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020088 %bw!
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020089endfunc
Bram Moolenaarf0ab01f2019-05-06 22:00:00 +020090
91" Test for invoking 'gf' on a ${VAR} variable
92func Test_gf()
93 if has("ebcdic")
94 set isfname=@,240-249,/,.,-,_,+,,,$,:,~,{,}
95 else
96 set isfname=@,48-57,/,.,-,_,+,,,$,:,~,{,}
97 endif
98
99 call writefile(["Test for gf command"], "Xtest1")
100 if has("unix")
101 call writefile([" ${CDIR}/Xtest1"], "Xtestgf")
102 else
103 call writefile([" $TDIR/Xtest1"], "Xtestgf")
104 endif
105 new Xtestgf
106 if has("unix")
107 let $CDIR = "."
108 /CDIR
109 else
110 if has("amiga")
111 let $TDIR = "/testdir"
112 else
113 let $TDIR = "."
114 endif
115 /TDIR
116 endif
117
118 normal gf
119 call assert_equal('Xtest1', fnamemodify(bufname(''), ":t"))
120 close!
121
122 call delete('Xtest1')
123 call delete('Xtestgf')
124endfunc
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100125
126func Test_gf_visual()
127 call writefile([], "Xtest_gf_visual")
128 new
129 call setline(1, 'XXXtest_gf_visualXXX')
130 set hidden
131
132 " Visually select Xtest_gf_visual and use gf to go to that file
133 norm! ttvtXgf
134 call assert_equal('Xtest_gf_visual', bufname('%'))
135
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200136 " if multiple lines are selected, then gf should fail
137 call setline(1, ["one", "two"])
138 normal VGgf
139 call assert_equal('Xtest_gf_visual', @%)
140
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100141 bwipe!
142 call delete('Xtest_gf_visual')
143 set hidden&
144endfunc
145
146func Test_gf_error()
147 new
148 call assert_fails('normal gf', 'E446:')
149 call assert_fails('normal gF', 'E446:')
150 call setline(1, '/doesnotexist')
151 call assert_fails('normal gf', 'E447:')
152 call assert_fails('normal gF', 'E447:')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100153 call assert_fails('normal [f', 'E447:')
154
155 " gf is not allowed when text is locked
156 au InsertCharPre <buffer> normal! gF<CR>
Bram Moolenaarff06f282020-04-21 22:01:14 +0200157 let caught_e565 = 0
Bram Moolenaar1671f442020-03-10 07:48:13 +0100158 try
159 call feedkeys("ix\<esc>", 'xt')
Bram Moolenaarff06f282020-04-21 22:01:14 +0200160 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
161 let caught_e565 = 1
Bram Moolenaar1671f442020-03-10 07:48:13 +0100162 endtry
Bram Moolenaarff06f282020-04-21 22:01:14 +0200163 call assert_equal(1, caught_e565)
Bram Moolenaar1671f442020-03-10 07:48:13 +0100164 au! InsertCharPre
165
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100166 bwipe!
167endfunc
Bram Moolenaar1671f442020-03-10 07:48:13 +0100168
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200169" If a file is not found by 'gf', then 'includeexpr' should be used to locate
170" the file.
171func Test_gf_includeexpr()
172 new
173 let g:Inc_fname = ''
174 func IncFunc()
175 let g:Inc_fname = v:fname
176 return v:fname
177 endfunc
178 setlocal includeexpr=IncFunc()
179 call setline(1, 'somefile.java')
180 call assert_fails('normal gf', 'E447:')
181 call assert_equal('somefile.java', g:Inc_fname)
182 close!
183 delfunc IncFunc
184endfunc
185
Bram Moolenaar1671f442020-03-10 07:48:13 +0100186" vim: shiftwidth=2 sts=2 expandtab