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 | 712598f | 2017-05-24 10:42:37 +0200 | [diff] [blame] | 77 | set isfname& |
Bram Moolenaar | 2a79ed2 | 2017-05-24 09:51:39 +0200 | [diff] [blame] | 78 | call delete('Xfile') |
| 79 | call delete('Xfile2') |
| 80 | bwipe Xfile |
| 81 | bwipe Xfile2 |
| 82 | endfunc |
Bram Moolenaar | f0ab01f | 2019-05-06 22:00:00 +0200 | [diff] [blame] | 83 | |
| 84 | " Test for invoking 'gf' on a ${VAR} variable |
| 85 | func Test_gf() |
| 86 | if has("ebcdic") |
| 87 | set isfname=@,240-249,/,.,-,_,+,,,$,:,~,{,} |
| 88 | else |
| 89 | set isfname=@,48-57,/,.,-,_,+,,,$,:,~,{,} |
| 90 | endif |
| 91 | |
| 92 | call writefile(["Test for gf command"], "Xtest1") |
| 93 | if has("unix") |
| 94 | call writefile([" ${CDIR}/Xtest1"], "Xtestgf") |
| 95 | else |
| 96 | call writefile([" $TDIR/Xtest1"], "Xtestgf") |
| 97 | endif |
| 98 | new Xtestgf |
| 99 | if has("unix") |
| 100 | let $CDIR = "." |
| 101 | /CDIR |
| 102 | else |
| 103 | if has("amiga") |
| 104 | let $TDIR = "/testdir" |
| 105 | else |
| 106 | let $TDIR = "." |
| 107 | endif |
| 108 | /TDIR |
| 109 | endif |
| 110 | |
| 111 | normal gf |
| 112 | call assert_equal('Xtest1', fnamemodify(bufname(''), ":t")) |
| 113 | close! |
| 114 | |
| 115 | call delete('Xtest1') |
| 116 | call delete('Xtestgf') |
| 117 | endfunc |
Bram Moolenaar | 0208b6b | 2019-11-08 21:49:48 +0100 | [diff] [blame] | 118 | |
| 119 | func Test_gf_visual() |
| 120 | call writefile([], "Xtest_gf_visual") |
| 121 | new |
| 122 | call setline(1, 'XXXtest_gf_visualXXX') |
| 123 | set hidden |
| 124 | |
| 125 | " Visually select Xtest_gf_visual and use gf to go to that file |
| 126 | norm! ttvtXgf |
| 127 | call assert_equal('Xtest_gf_visual', bufname('%')) |
| 128 | |
Bram Moolenaar | bdd2c29 | 2020-06-22 21:34:30 +0200 | [diff] [blame] | 129 | " if multiple lines are selected, then gf should fail |
| 130 | call setline(1, ["one", "two"]) |
| 131 | normal VGgf |
| 132 | call assert_equal('Xtest_gf_visual', @%) |
| 133 | |
Bram Moolenaar | 0208b6b | 2019-11-08 21:49:48 +0100 | [diff] [blame] | 134 | bwipe! |
| 135 | call delete('Xtest_gf_visual') |
| 136 | set hidden& |
| 137 | endfunc |
| 138 | |
| 139 | func Test_gf_error() |
| 140 | new |
| 141 | call assert_fails('normal gf', 'E446:') |
| 142 | call assert_fails('normal gF', 'E446:') |
| 143 | call setline(1, '/doesnotexist') |
| 144 | call assert_fails('normal gf', 'E447:') |
| 145 | call assert_fails('normal gF', 'E447:') |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 146 | call assert_fails('normal [f', 'E447:') |
| 147 | |
| 148 | " gf is not allowed when text is locked |
| 149 | au InsertCharPre <buffer> normal! gF<CR> |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 150 | let caught_e565 = 0 |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 151 | try |
| 152 | call feedkeys("ix\<esc>", 'xt') |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 153 | catch /^Vim\%((\a\+)\)\=:E565/ " catch E565 |
| 154 | let caught_e565 = 1 |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 155 | endtry |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 156 | call assert_equal(1, caught_e565) |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 157 | au! InsertCharPre |
| 158 | |
Bram Moolenaar | 0208b6b | 2019-11-08 21:49:48 +0100 | [diff] [blame] | 159 | bwipe! |
| 160 | endfunc |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 161 | |
Bram Moolenaar | bdd2c29 | 2020-06-22 21:34:30 +0200 | [diff] [blame] | 162 | " If a file is not found by 'gf', then 'includeexpr' should be used to locate |
| 163 | " the file. |
| 164 | func Test_gf_includeexpr() |
| 165 | new |
| 166 | let g:Inc_fname = '' |
| 167 | func IncFunc() |
| 168 | let g:Inc_fname = v:fname |
| 169 | return v:fname |
| 170 | endfunc |
| 171 | setlocal includeexpr=IncFunc() |
| 172 | call setline(1, 'somefile.java') |
| 173 | call assert_fails('normal gf', 'E447:') |
| 174 | call assert_equal('somefile.java', g:Inc_fname) |
| 175 | close! |
| 176 | delfunc IncFunc |
| 177 | endfunc |
| 178 | |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 179 | " vim: shiftwidth=2 sts=2 expandtab |