robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
241c161299
master
1" Test for expanding file names 2 3source shared.vim 4source check.vim 5 6func Test_with_directories () 7call mkdir ( 'Xdir1' ) 8call mkdir ( 'Xdir2' ) 9call mkdir ( 'Xdir3' ) 10cd Xdir3 11call mkdir ( 'Xdir4' ) 12cd .. 13 14split Xdir1/file 15call setline ( 1 , [ 'a' , 'b' ]) 16w 17w Xdir3/Xdir4/file 18close 19 20next Xdir?/*/file 21call assert_equal ( 'Xdir3/Xdir4/file' , expand ( '%' )) 22if has ( 'unix' ) 23next ! Xdir?/*/nofile 24call assert_equal ( 'Xdir?/*/nofile' , expand ( '%' )) 25endif 26" Edit another file, on MS-Windows the swap file would be in use and can't 27" be deleted. 28edit foo 29 30call assert_equal ( 0 , delete ( 'Xdir1' , 'rf' )) 31call assert_equal ( 0 , delete ( 'Xdir2' , 'rf' )) 32call assert_equal ( 0 , delete ( 'Xdir3' , 'rf' )) 33endfunc 34 35func Test_with_tilde () 36let dir = getcwd () 37call mkdir ( 'Xdir ~ dir' ) 38call assert_true ( isdirectory ( 'Xdir ~ dir' )) 39cd Xdir\ ~\ dir 40call assert_true ( getcwd () =~ 'Xdir \~ dir' ) 41call chdir ( dir ) 42call delete ( 'Xdir ~ dir' , 'd' ) 43call assert_false ( isdirectory ( 'Xdir ~ dir' )) 44endfunc 45 46func Test_expand_tilde_filename () 47split ~ 48call assert_equal ( '~' , expand ( '%' )) 49call assert_notequal ( expand ( '%:p' ), expand ( '~/' )) 50call assert_match ( '\~' , expand ( '%:p' )) 51bwipe ! 52endfunc 53 54func Test_expand_env_pathsep () 55let $ FOO = './foo' 56call assert_equal ( './foo/bar' , expand ( '$FOO/bar' )) 57let $ FOO = './foo/' 58call assert_equal ( './foo/bar' , expand ( '$FOO/bar' )) 59let $ FOO = 'C:' 60call assert_equal ( 'C:/bar' , expand ( '$FOO/bar' )) 61let $ FOO = 'C:/' 62call assert_equal ( 'C:/bar' , expand ( '$FOO/bar' )) 63 64unlet $ FOO 65endfunc 66 67func Test_expandcmd () 68let $ FOO = 'Test' 69call assert_equal ( 'e x/Test/y' , expandcmd ( 'e x/$FOO/y' )) 70unlet $ FOO 71 72new 73edit Xfile1 74call assert_equal ( 'e Xfile1' , expandcmd ( 'e %' )) 75edit Xfile2 76edit Xfile1 77call assert_equal ( 'e Xfile2' , 'e #' ->expandcmd ()) 78edit Xfile2 79edit Xfile3 80edit Xfile4 81let bnum = bufnr ( 'Xfile2' ) 82call assert_equal ( 'e Xfile2' , expandcmd ( 'e #' . bnum )) 83call setline ( '.' , 'Vim!@#' ) 84call assert_equal ( 'e Vim' , expandcmd ( 'e <cword>' )) 85call assert_equal ( 'e Vim!@#' , expandcmd ( 'e <cWORD>' )) 86enew ! 87edit Xfile.java 88call assert_equal ( 'e Xfile.py' , expandcmd ( 'e %:r.py' )) 89call assert_equal ( 'make abc.java' , expandcmd ( 'make abc.%:e' )) 90call assert_equal ( 'make Xabc.java' , expandcmd ( 'make %:s?file?abc?' )) 91edit a1a2a3.rb 92call assert_equal ( 'make b1b2b3.rb a1a2a3 Xfile.o' , expandcmd ( 'make %:gs?a?b? %< #<.o' )) 93 94call assert_equal ( 'make <afile>' , expandcmd ( "make <afile>" )) 95call assert_equal ( 'make <amatch>' , expandcmd ( "make <amatch>" )) 96call assert_equal ( 'make <abuf>' , expandcmd ( "make <abuf>" )) 97enew 98call assert_equal ( 'make %' , expandcmd ( "make %" )) 99let $ FOO = "blue\tsky" 100call setline ( 1 , "$FOO" ) 101call assert_equal ( "grep pat blue\tsky" , expandcmd ( 'grep pat <cfile>' )) 102 103" Test for expression expansion `= 104let $ FOO = "blue" 105call assert_equal ( "blue sky" , expandcmd ( "`=$FOO .. ' sky'`" )) 106let x = expandcmd ( "`=axbycz`" ) 107call assert_equal ( '`=axbycz`' , x ) 108call assert_fails ( 'let x = expandcmd("`=axbycz`", #{errmsg: 1})' , 'E121:' ) 109let x = expandcmd ( "`=axbycz`" , #{ abc : []}) 110call assert_equal ( '`=axbycz`' , x ) 111 112" Test for env variable with spaces 113let $ FOO = "foo bar baz" 114call assert_equal ( "e foo bar baz" , expandcmd ( "e $FOO" )) 115 116if has ( 'unix' ) && executable ( 'bash' ) 117" test for using the shell to expand a command argument. 118" only bash supports the {..} syntax 119set shell = bash 120let x = expandcmd ( '{1..4}' ) 121call assert_equal ( '{1..4}' , x ) 122call assert_fails ( "let x = expandcmd('{1..4}', #{errmsg: v:true})" , 'E77:' ) 123let x = expandcmd ( '{1..4}' , #{ error : v: true }) 124call assert_equal ( '{1..4}' , x ) 125set shell & 126endif 127 128unlet $ FOO 129close ! 130endfunc 131 132" Test for expanding <sfile>, <slnum> and <sflnum> outside of sourcing a script 133func Test_source_sfile () 134let lines =<< trim [SCRIPT] 135:call assert_equal('<sfile>', expandcmd("<sfile>")) 136:call assert_equal('<slnum>', expandcmd("<slnum>")) 137:call assert_equal('<sflnum>', expandcmd("<sflnum>")) 138:call assert_equal('edit <cfile>', expandcmd("edit <cfile>")) 139:call assert_equal('edit #', expandcmd("edit #")) 140:call assert_equal('edit #<2', expandcmd("edit #<2")) 141:call assert_equal('edit <cword>', expandcmd("edit <cword>")) 142:call assert_equal('edit <cexpr>', expandcmd("edit <cexpr>")) 143:call assert_fails('autocmd User MyCmd echo "<sfile>"', 'E498:') 144: 145:call assert_equal('', expand('<script>')) 146:verbose echo expand('<script>') 147:call add(v:errors, v:errmsg) 148:verbose echo expand('<sfile>') 149:call add(v:errors, v:errmsg) 150:call writefile(v:errors, 'Xresult') 151:qall! 152[SCRIPT] 153call writefile ( lines , 'Xscript' ) 154if RunVim ([], [], '--clean -s Xscript' ) 155call assert_equal ([ 156 \'E1274: No script file name to substitute for "<script>"' , 157 \'E498: No :source file name to substitute for "<sfile>"' ], 158 \readfile ( 'Xresult' )) 159endif 160call delete ( 'Xscript' ) 161call delete ( 'Xresult' ) 162endfunc 163 164" Test for expanding filenames multiple times in a command line 165func Test_expand_filename_multicmd () 166edit foo 167call setline ( 1 , 'foo!' ) 168new 169call setline ( 1 , 'foo!' ) 170new <cword> |new <cWORD> 171call assert_equal ( 4 , winnr ( '$' )) 172call assert_equal ( 'foo!' , bufname ( winbufnr ( 1 ))) 173call assert_equal ( 'foo' , bufname ( winbufnr ( 2 ))) 174call assert_fails ( 'e %:s/.*//' , 'E500:' ) 175 %bwipe ! 176endfunc 177 178func Test_expandcmd_shell_nonomatch () 179CheckNotMSWindows 180call assert_equal ( '$*' , expandcmd ( '$*' )) 181endfunc 182 183func Test_expand_script_source () 184let lines0 =<< trim [SCRIPT] 185call extend(g:script_level, [expand('<script>:t')]) 186so Xscript1 187func F0() 188call extend(g:func_level, [expand('<script>:t')]) 189endfunc 190 191au User * call extend(g:au_level, [expand('<script>:t')]) 192[SCRIPT] 193 194let lines1 =<< trim [SCRIPT] 195call extend(g:script_level, [expand('<script>:t')]) 196so Xscript2 197func F1() 198call extend(g:func_level, [expand('<script>:t')]) 199endfunc 200 201au User * call extend(g:au_level, [expand('<script>:t')]) 202[SCRIPT] 203 204let lines2 =<< trim [SCRIPT] 205call extend(g:script_level, [expand('<script>:t')]) 206func F2() 207call extend(g:func_level, [expand('<script>:t')]) 208endfunc 209 210au User * call extend(g:au_level, [expand('<script>:t')]) 211[SCRIPT] 212 213call writefile ( lines0 , 'Xscript0' ) 214call writefile ( lines1 , 'Xscript1' ) 215call writefile ( lines2 , 'Xscript2' ) 216 217" Check the expansion of <script> at different levels. 218let g: script_level = [] 219let g: func_level = [] 220let g: au_level = [] 221 222so Xscript0 223call F0 () 224call F1 () 225call F2 () 226doautocmd User 227 228call assert_equal ([ 'Xscript0' , 'Xscript1' , 'Xscript2' ], g: script_level ) 229call assert_equal ([ 'Xscript0' , 'Xscript1' , 'Xscript2' ], g: func_level ) 230call assert_equal ([ 'Xscript2' , 'Xscript1' , 'Xscript0' ], g: au_level ) 231 232unlet g: script_level g: func_level 233delfunc F0 234delfunc F1 235delfunc F2 236 237call delete ( 'Xscript0' ) 238call delete ( 'Xscript1' ) 239call delete ( 'Xscript2' ) 240endfunc 241 242" vim: shiftwidth=2 sts=2 expandtab