summaryrefslogtreecommitdiff
path: root/src/testdir/test_gf.vim
blob: 3d429f3a98ed56a5b1bdae7f4e306c3635f4aff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
" Test for the gf and gF (goto file) commands

" This is a test if a URL is recognized by "gf", with the cursor before and
" after the "://".  Also test ":\\".
func Test_gf_url()
  enew!
  call append(0, [
      \ "first test for URL://machine.name/tmp/vimtest2a and other text",
      \ "second test for URL://machine.name/tmp/vimtest2b. And other text",
      \ "third test for URL:\\\\machine.name\\vimtest2c and other text",
      \ "fourth test for URL:\\\\machine.name\\tmp\\vimtest2d, and other text",
      \ "fifth test for URL://machine.name/tmp?q=vim&opt=yes and other text",
      \ "sixth test for URL://machine.name:1234?q=vim and other text",
      \ ])
  call cursor(1,1)
  call search("^first")
  call search("tmp")
  call assert_equal("URL://machine.name/tmp/vimtest2a", expand("<cfile>"))
  call search("^second")
  call search("URL")
  call assert_equal("URL://machine.name/tmp/vimtest2b", expand("<cfile>"))
  set isf=@,48-57,/,.,-,_,+,,,$,~,\
  call search("^third")
  call search("name")
  call assert_equal("URL:\\\\machine.name\\vimtest2c", expand("<cfile>"))
  call search("^fourth")
  call search("URL")
  call assert_equal("URL:\\\\machine.name\\tmp\\vimtest2d", expand("<cfile>"))

  call search("^fifth")
  call search("URL")
  call assert_equal("URL://machine.name/tmp?q=vim&opt=yes", expand("<cfile>"))

  call search("^sixth")
  call search("URL")
  call assert_equal("URL://machine.name:1234?q=vim", expand("<cfile>"))

  %d
  call setline(1, "demo://remote_file")
  wincmd f
  call assert_equal('demo://remote_file', @%)
  call assert_equal(2, winnr('$'))
  close!

  set isf&vim
  enew!
endfunc

func Test_gF()
  new
  call setline(1, ['111', '222', '333', '444'])
  w! Xgffile
  close
  new
  set isfname-=:
  call setline(1, ['one', 'Xgffile:3', 'three'])
  2
  call assert_fails('normal gF', 'E37:')
  call assert_equal(2, getcurpos()[1])
  w! Xgffile2
  normal gF
  call assert_equal('Xgffile', bufname('%'))
  call assert_equal(3, getcurpos()[1])

  enew!
  call setline(1, ['one', 'the Xgffile line 2, and more', 'three'])
  w! Xgffile2
  normal 2GfX
  normal gF
  call assert_equal('Xgffile', bufname('%'))
  call assert_equal(2, getcurpos()[1])

  " jumping to the file/line with CTRL-W_F
  %bw!
  edit Xfile1
  call setline(1, ['one', 'Xgffile:4', 'three'])
  exe "normal 2G\<C-W>F"
  call assert_equal('Xgffile', bufname('%'))
  call assert_equal(4, getcurpos()[1])

  set isfname&
  call delete('Xgffile')
  call delete('Xgffile2')
  %bw!
endfunc

" Test for invoking 'gf' on a ${VAR} variable
func Test_gf()
  set isfname=@,48-57,/,.,-,_,+,,,$,:,~,{,}

  call writefile(["Test for gf command"], "Xtest1", 'D')
  if has("unix")
    call writefile(["    ${CDIR}/Xtest1"], "Xtestgf", 'D')
  else
    call writefile(["    $TDIR/Xtest1"], "Xtestgf", 'D')
  endif
  new Xtestgf
  if has("unix")
    let $CDIR = "."
    /CDIR
  else
    if has("amiga")
      let $TDIR = "/testdir"
    else
      let $TDIR = "."
    endif
    /TDIR
  endif

  normal gf
  call assert_equal('Xtest1', fnamemodify(bufname(''), ":t"))

  close!
endfunc

func Test_gf_visual()
  call writefile(['one', 'two', 'three', 'four'], "Xtest_gf_visual", 'D')
  new
  call setline(1, 'XXXtest_gf_visualXXX')
  set hidden

  " Visually select Xtest_gf_visual and use gf to go to that file
  norm! ttvtXgf
  call assert_equal('Xtest_gf_visual', bufname('%'))

  " if multiple lines are selected, then gf should fail
  call setline(1, ["one", "two"])
  normal VGgf
  call assert_equal('Xtest_gf_visual', @%)

  " following line number is used for gF
  bwipe!
  new
  call setline(1, 'XXXtest_gf_visual:3XXX')
  norm! 0ttvt:gF
  call assert_equal('Xtest_gf_visual', bufname('%'))
  call assert_equal(3, getcurpos()[1])

  " do not include the NUL at the end
  call writefile(['x'], 'Xvisual', 'D')
  let save_enc = &enc
  for enc in ['latin1', 'utf-8']
    exe "set enc=" .. enc
    new
    call setline(1, 'Xvisual')
    set nomodified
    exe "normal \<C-V>$gf"
    call assert_equal('Xvisual', bufname())
    bwipe!
  endfor
  let &enc = save_enc

  " line number in visual area is used for file name
  if has('unix')
    bwipe!
    call writefile([], "Xtest_gf_visual:3", 'D')
    new
    call setline(1, 'XXXtest_gf_visual:3XXX')
    norm! 0ttvtXgF
    call assert_equal('Xtest_gf_visual:3', bufname('%'))
  endif

  bwipe!
  set hidden&
endfunc

func Test_gf_error()
  new
  call assert_fails('normal gf', 'E446:')
  call assert_fails('normal gF', 'E446:')
  call setline(1, '/doesnotexist')
  call assert_fails('normal gf', 'E447:')
  call assert_fails('normal gF', 'E447:')
  call assert_fails('normal [f', 'E447:')

  " gf is not allowed when text is locked
  au InsertCharPre <buffer> normal! gF<CR>
  let caught_e565 = 0
  try
    call feedkeys("ix\<esc>", 'xt')
  catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
    let caught_e565 = 1
  endtry
  call assert_equal(1, caught_e565)
  au! InsertCharPre

  bwipe!

  " gf is not allowed when buffer is locked
  new
  augroup Test_gf
    au!
    au OptionSet diff norm! gf
  augroup END
  call setline(1, ['Xfile1', 'line2', 'line3', 'line4'])
  call test_override('starting', 1)
  call assert_fails('diffthis', 'E788:')
  call test_override('starting', 0)
  augroup Test_gf
    au!
  augroup END
  bw!
endfunc

" If a file is not found by 'gf', then 'includeexpr' should be used to locate
" the file.
func Test_gf_includeexpr()
  new
  let g:Inc_fname = ''
  func IncFunc()
    let g:Inc_fname = v:fname
    return v:fname
  endfunc
  setlocal includeexpr=IncFunc()
  call setline(1, 'somefile.java')
  call assert_fails('normal gf', 'E447:')
  call assert_equal('somefile.java', g:Inc_fname)
  close!
  delfunc IncFunc
endfunc

" Test for using a script-local function for 'includeexpr'
func Test_includeexpr_scriptlocal_func()
  func! s:IncludeFunc()
    let g:IncludeFname = v:fname
    return ''
  endfunc
  set includeexpr=s:IncludeFunc()
  call assert_equal(expand('<SID>') .. 'IncludeFunc()', &includeexpr)
  new | only
  call setline(1, 'TestFile1')
  let g:IncludeFname = ''
  call assert_fails('normal! gf', 'E447:')
  call assert_equal('TestFile1', g:IncludeFname)
  bw!
  set includeexpr=<SID>IncludeFunc()
  call assert_equal(expand('<SID>') .. 'IncludeFunc()', &includeexpr)
  new | only
  call setline(1, 'TestFile2')
  let g:IncludeFname = ''
  call assert_fails('normal! gf', 'E447:')
  call assert_equal('TestFile2', g:IncludeFname)
  set includeexpr&
  delfunc s:IncludeFunc
  bw!
endfunc

" Check that expanding directories can handle more than 255 entries.
func Test_gf_subdirs_wildcard()
  let cwd = getcwd()
  let dir = 'Xtestgf_dir'
  call mkdir(dir, 'R')
  call chdir(dir)
  for i in range(300)
    call mkdir(i)
    call writefile([], i .. '/' .. i, 'S')
  endfor
  set path=./**

  new | only
  call setline(1, '99')
  w! Xtest1
  normal gf
  call assert_equal('99', fnamemodify(bufname(''), ":t"))

  call chdir(cwd)
  set path&
endfunc

" vim: shiftwidth=2 sts=2 expandtab