Bram Moolenaar | bdd2c29 | 2020-06-22 21:34:30 +0200 | [diff] [blame] | 1 | " Test for the gf and gF (goto file) commands |
Bram Moolenaar | 53f1673 | 2016-09-07 20:46:39 +0200 | [diff] [blame] | 2 | |
| 3 | " This is a test if a URL is recognized by "gf", with the cursor before and |
| 4 | " after the "://". Also test ":\\". |
Bram Moolenaar | 2a79ed2 | 2017-05-24 09:51:39 +0200 | [diff] [blame] | 5 | func Test_gf_url() |
Bram Moolenaar | 53f1673 | 2016-09-07 20:46:39 +0200 | [diff] [blame] | 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", |
Bram Moolenaar | 9e3dfc6 | 2017-12-25 14:29:18 +0100 | [diff] [blame] | 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", |
Bram Moolenaar | cbef8e1 | 2019-03-09 12:32:56 +0100 | [diff] [blame] | 13 | \ "sixth test for URL://machine.name:1234?q=vim and other text", |
Bram Moolenaar | 53f1673 | 2016-09-07 20:46:39 +0200 | [diff] [blame] | 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 | if has("ebcdic") |
| 23 | set isf=@,240-249,/,.,-,_,+,,,$,:,~,\ |
| 24 | else |
Bram Moolenaar | cbef8e1 | 2019-03-09 12:32:56 +0100 | [diff] [blame] | 25 | set isf=@,48-57,/,.,-,_,+,,,$,~,\ |
Bram Moolenaar | 53f1673 | 2016-09-07 20:46:39 +0200 | [diff] [blame] | 26 | 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 Moolenaar | 9e3dfc6 | 2017-12-25 14:29:18 +0100 | [diff] [blame] | 34 | call search("^fifth") |
| 35 | call search("URL") |
| 36 | call assert_equal("URL://machine.name/tmp?q=vim&opt=yes", expand("<cfile>")) |
| 37 | |
Bram Moolenaar | cbef8e1 | 2019-03-09 12:32:56 +0100 | [diff] [blame] | 38 | call search("^sixth") |
| 39 | call search("URL") |
| 40 | call assert_equal("URL://machine.name:1234?q=vim", expand("<cfile>")) |
| 41 | |
Bram Moolenaar | bdd2c29 | 2020-06-22 21:34:30 +0200 | [diff] [blame] | 42 | %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 Moolenaar | 53f1673 | 2016-09-07 20:46:39 +0200 | [diff] [blame] | 49 | set isf&vim |
| 50 | enew! |
Bram Moolenaar | 2a79ed2 | 2017-05-24 09:51:39 +0200 | [diff] [blame] | 51 | endfunc |
| 52 | |
| 53 | func Test_gF() |
| 54 | new |
| 55 | call setline(1, ['111', '222', '333', '444']) |
| 56 | w! Xfile |
| 57 | close |
| 58 | new |
Bram Moolenaar | 712598f | 2017-05-24 10:42:37 +0200 | [diff] [blame] | 59 | set isfname-=: |
| 60 | call setline(1, ['one', 'Xfile:3', 'three']) |
Bram Moolenaar | 2a79ed2 | 2017-05-24 09:51:39 +0200 | [diff] [blame] | 61 | 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 Moolenaar | 64e74c9 | 2019-12-22 15:38:06 +0100 | [diff] [blame] | 69 | 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 Moolenaar | 5d3c9f8 | 2020-06-26 20:41:39 +0200 | [diff] [blame] | 77 | " 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 Moolenaar | 712598f | 2017-05-24 10:42:37 +0200 | [diff] [blame] | 85 | set isfname& |
Bram Moolenaar | 2a79ed2 | 2017-05-24 09:51:39 +0200 | [diff] [blame] | 86 | call delete('Xfile') |
| 87 | call delete('Xfile2') |
Bram Moolenaar | 5d3c9f8 | 2020-06-26 20:41:39 +0200 | [diff] [blame] | 88 | %bw! |
Bram Moolenaar | 2a79ed2 | 2017-05-24 09:51:39 +0200 | [diff] [blame] | 89 | endfunc |
Bram Moolenaar | f0ab01f | 2019-05-06 22:00:00 +0200 | [diff] [blame] | 90 | |
| 91 | " Test for invoking 'gf' on a ${VAR} variable |
| 92 | func 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') |
| 124 | endfunc |
Bram Moolenaar | 0208b6b | 2019-11-08 21:49:48 +0100 | [diff] [blame] | 125 | |
| 126 | func Test_gf_visual() |
Bram Moolenaar | efd5d8a | 2020-09-14 19:11:45 +0200 | [diff] [blame] | 127 | call writefile(['one', 'two', 'three', 'four'], "Xtest_gf_visual") |
Bram Moolenaar | 0208b6b | 2019-11-08 21:49:48 +0100 | [diff] [blame] | 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 Moolenaar | bdd2c29 | 2020-06-22 21:34:30 +0200 | [diff] [blame] | 136 | " 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 Moolenaar | efd5d8a | 2020-09-14 19:11:45 +0200 | [diff] [blame] | 141 | " following line number is used for gF |
| 142 | bwipe! |
| 143 | new |
| 144 | call setline(1, 'XXXtest_gf_visual:3XXX') |
| 145 | norm! 0ttvt:gF |
| 146 | call assert_equal('Xtest_gf_visual', bufname('%')) |
| 147 | call assert_equal(3, getcurpos()[1]) |
| 148 | |
| 149 | " line number in visual area is used for file name |
| 150 | if has('unix') |
| 151 | bwipe! |
| 152 | call writefile([], "Xtest_gf_visual:3") |
| 153 | new |
| 154 | call setline(1, 'XXXtest_gf_visual:3XXX') |
| 155 | norm! 0ttvtXgF |
| 156 | call assert_equal('Xtest_gf_visual:3', bufname('%')) |
| 157 | call delete('Xtest_gf_visual:3') |
| 158 | endif |
| 159 | |
Bram Moolenaar | 0208b6b | 2019-11-08 21:49:48 +0100 | [diff] [blame] | 160 | bwipe! |
| 161 | call delete('Xtest_gf_visual') |
| 162 | set hidden& |
| 163 | endfunc |
| 164 | |
| 165 | func Test_gf_error() |
| 166 | new |
| 167 | call assert_fails('normal gf', 'E446:') |
| 168 | call assert_fails('normal gF', 'E446:') |
| 169 | call setline(1, '/doesnotexist') |
| 170 | call assert_fails('normal gf', 'E447:') |
| 171 | call assert_fails('normal gF', 'E447:') |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 172 | call assert_fails('normal [f', 'E447:') |
| 173 | |
| 174 | " gf is not allowed when text is locked |
| 175 | au InsertCharPre <buffer> normal! gF<CR> |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 176 | let caught_e565 = 0 |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 177 | try |
| 178 | call feedkeys("ix\<esc>", 'xt') |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 179 | catch /^Vim\%((\a\+)\)\=:E565/ " catch E565 |
| 180 | let caught_e565 = 1 |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 181 | endtry |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 182 | call assert_equal(1, caught_e565) |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 183 | au! InsertCharPre |
| 184 | |
Bram Moolenaar | 0208b6b | 2019-11-08 21:49:48 +0100 | [diff] [blame] | 185 | bwipe! |
Bram Moolenaar | d1ad99b | 2020-10-04 16:16:54 +0200 | [diff] [blame] | 186 | |
| 187 | " gf is not allowed when buffer is locked |
| 188 | new |
| 189 | augroup Test_gf |
| 190 | au! |
| 191 | au OptionSet diff norm! gf |
| 192 | augroup END |
| 193 | call setline(1, ['Xfile1', 'line2', 'line3', 'line4']) |
| 194 | call test_override('starting', 1) |
| 195 | call assert_fails('diffthis', 'E788:') |
| 196 | call test_override('starting', 0) |
| 197 | augroup Test_gf |
| 198 | au! |
| 199 | augroup END |
| 200 | bw! |
Bram Moolenaar | 0208b6b | 2019-11-08 21:49:48 +0100 | [diff] [blame] | 201 | endfunc |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 202 | |
Bram Moolenaar | bdd2c29 | 2020-06-22 21:34:30 +0200 | [diff] [blame] | 203 | " If a file is not found by 'gf', then 'includeexpr' should be used to locate |
| 204 | " the file. |
| 205 | func Test_gf_includeexpr() |
| 206 | new |
| 207 | let g:Inc_fname = '' |
| 208 | func IncFunc() |
| 209 | let g:Inc_fname = v:fname |
| 210 | return v:fname |
| 211 | endfunc |
| 212 | setlocal includeexpr=IncFunc() |
| 213 | call setline(1, 'somefile.java') |
| 214 | call assert_fails('normal gf', 'E447:') |
| 215 | call assert_equal('somefile.java', g:Inc_fname) |
| 216 | close! |
| 217 | delfunc IncFunc |
| 218 | endfunc |
| 219 | |
Yegappan Lakshmanan | 8bb65f2 | 2021-12-26 10:51:39 +0000 | [diff] [blame^] | 220 | " Test for using a script-local function for 'includeexpr' |
| 221 | func Test_includeexpr_scriptlocal_func() |
| 222 | func! s:IncludeFunc() |
| 223 | let g:IncludeFname = v:fname |
| 224 | return '' |
| 225 | endfunc |
| 226 | set includeexpr=s:IncludeFunc() |
| 227 | call assert_equal(expand('<SID>') .. 'IncludeFunc()', &includeexpr) |
| 228 | new | only |
| 229 | call setline(1, 'TestFile1') |
| 230 | let g:IncludeFname = '' |
| 231 | call assert_fails('normal! gf', 'E447:') |
| 232 | call assert_equal('TestFile1', g:IncludeFname) |
| 233 | bw! |
| 234 | set includeexpr=<SID>IncludeFunc() |
| 235 | call assert_equal(expand('<SID>') .. 'IncludeFunc()', &includeexpr) |
| 236 | new | only |
| 237 | call setline(1, 'TestFile2') |
| 238 | let g:IncludeFname = '' |
| 239 | call assert_fails('normal! gf', 'E447:') |
| 240 | call assert_equal('TestFile2', g:IncludeFname) |
| 241 | set includeexpr& |
| 242 | delfunc s:IncludeFunc |
| 243 | bw! |
| 244 | endfunc |
| 245 | |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 246 | " vim: shiftwidth=2 sts=2 expandtab |