test_expand.vim 6.8 KB

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