expand_spec.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. -- Test for expanding file names
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local eq = helpers.eq
  4. local call = helpers.call
  5. local nvim = helpers.meths
  6. local clear = helpers.clear
  7. local source = helpers.source
  8. local function expected_empty()
  9. eq({}, nvim.get_vvar('errors'))
  10. end
  11. describe('expand file name', function()
  12. after_each(function()
  13. helpers.rmdir('Xdir1')
  14. helpers.rmdir('Xdir2')
  15. helpers.rmdir('Xdir3')
  16. helpers.rmdir('Xdir4')
  17. end)
  18. before_each(function()
  19. clear()
  20. source([[
  21. func Test_with_directories()
  22. call mkdir('Xdir1')
  23. call mkdir('Xdir2')
  24. call mkdir('Xdir3')
  25. cd Xdir3
  26. call mkdir('Xdir4')
  27. cd ..
  28. split Xdir1/file
  29. call setline(1, ['a', 'b'])
  30. w
  31. w Xdir3/Xdir4/file
  32. close
  33. next Xdir?/*/file
  34. call assert_equal('Xdir3/Xdir4/file', expand('%'))
  35. if has('unix')
  36. next! Xdir?/*/nofile
  37. call assert_equal('Xdir?/*/nofile', expand('%'))
  38. endif
  39. " Edit another file, on MS-Windows the swap file would be in use and can't
  40. " be deleted
  41. edit foo
  42. call assert_equal(0, delete('Xdir1', 'rf'))
  43. call assert_equal(0, delete('Xdir2', 'rf'))
  44. call assert_equal(0, delete('Xdir3', 'rf'))
  45. endfunc
  46. func Test_with_tilde()
  47. let dir = getcwd()
  48. call mkdir('Xdir ~ dir')
  49. call assert_true(isdirectory('Xdir ~ dir'))
  50. cd Xdir\ ~\ dir
  51. call assert_true(getcwd() =~ 'Xdir \~ dir')
  52. exe 'cd ' . fnameescape(dir)
  53. call delete('Xdir ~ dir', 'd')
  54. call assert_false(isdirectory('Xdir ~ dir'))
  55. endfunc
  56. func Test_expand_tilde_filename()
  57. split ~
  58. call assert_equal('~', expand('%'))
  59. call assert_notequal(expand('%:p'), expand('~/'))
  60. call assert_match('\~', expand('%:p'))
  61. bwipe!
  62. endfunc
  63. func Test_expandcmd()
  64. let $FOO = 'Test'
  65. call assert_equal('e x/Test/y', expandcmd('e x/$FOO/y'))
  66. unlet $FOO
  67. new
  68. edit Xfile1
  69. call assert_equal('e Xfile1', expandcmd('e %'))
  70. edit Xfile2
  71. edit Xfile1
  72. call assert_equal('e Xfile2', expandcmd('e #'))
  73. edit Xfile2
  74. edit Xfile3
  75. edit Xfile4
  76. let bnum = bufnr('Xfile2')
  77. call assert_equal('e Xfile2', expandcmd('e #' . bnum))
  78. call setline('.', 'Vim!@#')
  79. call assert_equal('e Vim', expandcmd('e <cword>'))
  80. call assert_equal('e Vim!@#', expandcmd('e <cWORD>'))
  81. enew!
  82. edit Xfile.java
  83. call assert_equal('e Xfile.py', expandcmd('e %:r.py'))
  84. call assert_equal('make abc.java', expandcmd('make abc.%:e'))
  85. call assert_equal('make Xabc.java', expandcmd('make %:s?file?abc?'))
  86. edit a1a2a3.rb
  87. call assert_equal('make b1b2b3.rb a1a2a3 Xfile.o', expandcmd('make %:gs?a?b? %< #<.o'))
  88. call assert_fails('call expandcmd("make <afile>")', 'E495:')
  89. call assert_fails('call expandcmd("make <afile>")', 'E495:')
  90. enew
  91. call assert_fails('call expandcmd("make %")', 'E499:')
  92. close
  93. endfunc
  94. ]])
  95. end)
  96. it('works with directories', function()
  97. call('Test_with_directories')
  98. expected_empty()
  99. end)
  100. it('works with tilde', function()
  101. call('Test_with_tilde')
  102. expected_empty()
  103. end)
  104. it('does not expand tilde if it is a filename', function()
  105. call('Test_expand_tilde_filename')
  106. expected_empty()
  107. end)
  108. it('works with expandcmd()', function()
  109. call('Test_expandcmd')
  110. expected_empty()
  111. end)
  112. end)