blob: 1b3b139810eca88e0b21620167742dbe79b0645a [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>"))
Bram Moolenaar424bcae2022-01-31 14:59:41 +000022 set isf=@,48-57,/,.,-,_,+,,,$,~,\
Bram Moolenaar53f16732016-09-07 20:46:39 +020023 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
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +010030 call search("^fifth")
31 call search("URL")
32 call assert_equal("URL://machine.name/tmp?q=vim&opt=yes", expand("<cfile>"))
33
Bram Moolenaarcbef8e12019-03-09 12:32:56 +010034 call search("^sixth")
35 call search("URL")
36 call assert_equal("URL://machine.name:1234?q=vim", expand("<cfile>"))
37
Bram Moolenaarbdd2c292020-06-22 21:34:30 +020038 %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
Bram Moolenaar53f16732016-09-07 20:46:39 +020045 set isf&vim
46 enew!
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020047endfunc
48
49func Test_gF()
50 new
51 call setline(1, ['111', '222', '333', '444'])
52 w! Xfile
53 close
54 new
Bram Moolenaar712598f2017-05-24 10:42:37 +020055 set isfname-=:
56 call setline(1, ['one', 'Xfile:3', 'three'])
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020057 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
Bram Moolenaar64e74c92019-12-22 15:38:06 +010065 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
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020073 " 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
Bram Moolenaar712598f2017-05-24 10:42:37 +020081 set isfname&
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020082 call delete('Xfile')
83 call delete('Xfile2')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020084 %bw!
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020085endfunc
Bram Moolenaarf0ab01f2019-05-06 22:00:00 +020086
87" Test for invoking 'gf' on a ${VAR} variable
88func Test_gf()
Bram Moolenaar424bcae2022-01-31 14:59:41 +000089 set isfname=@,48-57,/,.,-,_,+,,,$,:,~,{,}
Bram Moolenaarf0ab01f2019-05-06 22:00:00 +020090
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')
116endfunc
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100117
118func Test_gf_visual()
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +0200119 call writefile(['one', 'two', 'three', 'four'], "Xtest_gf_visual")
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100120 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
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200128 " 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
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +0200133 " 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
Bram Moolenaar395bd1f2022-05-14 21:29:44 +0100141 " 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 exe "set enc=" .. enc
146 new
147 call setline(1, 'X')
148 set nomodified
149 exe "normal \<C-V>$gf"
150 call assert_equal('X', bufname())
151 bwipe!
152 endfor
153 let &enc = save_enc
154 call delete('X')
155
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +0200156 " line number in visual area is used for file name
157 if has('unix')
158 bwipe!
159 call writefile([], "Xtest_gf_visual:3")
160 new
161 call setline(1, 'XXXtest_gf_visual:3XXX')
162 norm! 0ttvtXgF
163 call assert_equal('Xtest_gf_visual:3', bufname('%'))
164 call delete('Xtest_gf_visual:3')
165 endif
166
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100167 bwipe!
168 call delete('Xtest_gf_visual')
169 set hidden&
170endfunc
171
172func Test_gf_error()
173 new
174 call assert_fails('normal gf', 'E446:')
175 call assert_fails('normal gF', 'E446:')
176 call setline(1, '/doesnotexist')
177 call assert_fails('normal gf', 'E447:')
178 call assert_fails('normal gF', 'E447:')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100179 call assert_fails('normal [f', 'E447:')
180
181 " gf is not allowed when text is locked
182 au InsertCharPre <buffer> normal! gF<CR>
Bram Moolenaarff06f282020-04-21 22:01:14 +0200183 let caught_e565 = 0
Bram Moolenaar1671f442020-03-10 07:48:13 +0100184 try
185 call feedkeys("ix\<esc>", 'xt')
Bram Moolenaarff06f282020-04-21 22:01:14 +0200186 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
187 let caught_e565 = 1
Bram Moolenaar1671f442020-03-10 07:48:13 +0100188 endtry
Bram Moolenaarff06f282020-04-21 22:01:14 +0200189 call assert_equal(1, caught_e565)
Bram Moolenaar1671f442020-03-10 07:48:13 +0100190 au! InsertCharPre
191
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100192 bwipe!
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200193
194 " gf is not allowed when buffer is locked
195 new
196 augroup Test_gf
197 au!
198 au OptionSet diff norm! gf
199 augroup END
200 call setline(1, ['Xfile1', 'line2', 'line3', 'line4'])
201 call test_override('starting', 1)
202 call assert_fails('diffthis', 'E788:')
203 call test_override('starting', 0)
204 augroup Test_gf
205 au!
206 augroup END
207 bw!
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100208endfunc
Bram Moolenaar1671f442020-03-10 07:48:13 +0100209
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200210" If a file is not found by 'gf', then 'includeexpr' should be used to locate
211" the file.
212func Test_gf_includeexpr()
213 new
214 let g:Inc_fname = ''
215 func IncFunc()
216 let g:Inc_fname = v:fname
217 return v:fname
218 endfunc
219 setlocal includeexpr=IncFunc()
220 call setline(1, 'somefile.java')
221 call assert_fails('normal gf', 'E447:')
222 call assert_equal('somefile.java', g:Inc_fname)
223 close!
224 delfunc IncFunc
225endfunc
226
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000227" Test for using a script-local function for 'includeexpr'
228func Test_includeexpr_scriptlocal_func()
229 func! s:IncludeFunc()
230 let g:IncludeFname = v:fname
231 return ''
232 endfunc
233 set includeexpr=s:IncludeFunc()
234 call assert_equal(expand('<SID>') .. 'IncludeFunc()', &includeexpr)
235 new | only
236 call setline(1, 'TestFile1')
237 let g:IncludeFname = ''
238 call assert_fails('normal! gf', 'E447:')
239 call assert_equal('TestFile1', g:IncludeFname)
240 bw!
241 set includeexpr=<SID>IncludeFunc()
242 call assert_equal(expand('<SID>') .. 'IncludeFunc()', &includeexpr)
243 new | only
244 call setline(1, 'TestFile2')
245 let g:IncludeFname = ''
246 call assert_fails('normal! gf', 'E447:')
247 call assert_equal('TestFile2', g:IncludeFname)
248 set includeexpr&
249 delfunc s:IncludeFunc
250 bw!
251endfunc
252
Bram Moolenaar1671f442020-03-10 07:48:13 +0100253" vim: shiftwidth=2 sts=2 expandtab