test_expand_func.vim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. " Tests for expand()
  2. source shared.vim
  3. let s:sfile = expand('<sfile>')
  4. let s:slnum = str2nr(expand('<slnum>'))
  5. let s:sflnum = str2nr(expand('<sflnum>'))
  6. func s:expand_sfile()
  7. return expand('<sfile>')
  8. endfunc
  9. func s:expand_slnum()
  10. return str2nr(expand('<slnum>'))
  11. endfunc
  12. func s:expand_sflnum()
  13. return str2nr(expand('<sflnum>'))
  14. endfunc
  15. " This test depends on the location in the test file, put it first.
  16. func Test_expand_sflnum()
  17. call assert_equal(7, s:sflnum)
  18. call assert_equal(24, str2nr(expand('<sflnum>')))
  19. " Line-continuation
  20. call assert_equal(
  21. \ 27,
  22. \ str2nr(expand('<sflnum>')))
  23. " Call in script-local function
  24. call assert_equal(18, s:expand_sflnum())
  25. " Call in command
  26. command Flnum echo expand('<sflnum>')
  27. call assert_equal(36, str2nr(trim(execute('Flnum'))))
  28. delcommand Flnum
  29. endfunc
  30. func Test_expand_sfile_and_stack()
  31. call assert_match('test_expand_func\.vim$', s:sfile)
  32. let expected = 'script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack'
  33. call assert_match(expected .. '$', expand('<sfile>'))
  34. call assert_match(expected .. '\[4\]$' , expand('<stack>'))
  35. " Call in script-local function
  36. call assert_match('script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack\[7\]\.\.<SNR>\d\+_expand_sfile$', s:expand_sfile())
  37. " Call in command
  38. command Sfile echo expand('<sfile>')
  39. call assert_match('script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack$', trim(execute('Sfile')))
  40. delcommand Sfile
  41. " Use <stack> from sourced script.
  42. let lines =<< trim END
  43. " comment here
  44. let g:stack_value = expand('<stack>')
  45. END
  46. call writefile(lines, 'Xstack')
  47. source Xstack
  48. call assert_match('\<Xstack\[2\]$', g:stack_value)
  49. unlet g:stack_value
  50. call delete('Xstack')
  51. if exists('+shellslash')
  52. call mkdir('Xshellslash')
  53. let lines =<< trim END
  54. let g:stack1 = expand('<stack>')
  55. set noshellslash
  56. let g:stack2 = expand('<stack>')
  57. set shellslash
  58. let g:stack3 = expand('<stack>')
  59. END
  60. call writefile(lines, 'Xshellslash/Xstack')
  61. " Test that changing 'shellslash' always affects the result of expand()
  62. " when sourcing a script multiple times.
  63. for i in range(2)
  64. source Xshellslash/Xstack
  65. call assert_match('\<Xshellslash/Xstack\[1\]$', g:stack1)
  66. call assert_match('\<Xshellslash\\Xstack\[3\]$', g:stack2)
  67. call assert_match('\<Xshellslash/Xstack\[5\]$', g:stack3)
  68. unlet g:stack1
  69. unlet g:stack2
  70. unlet g:stack3
  71. endfor
  72. call delete('Xshellslash', 'rf')
  73. endif
  74. endfunc
  75. func Test_expand_slnum()
  76. call assert_equal(6, s:slnum)
  77. call assert_equal(2, str2nr(expand('<slnum>')))
  78. " Line-continuation
  79. call assert_equal(
  80. \ 5,
  81. \ str2nr(expand('<slnum>')))
  82. " Call in script-local function
  83. call assert_equal(1, s:expand_slnum())
  84. " Call in command
  85. command Slnum echo expand('<slnum>')
  86. call assert_equal(14, str2nr(trim(execute('Slnum'))))
  87. delcommand Slnum
  88. endfunc
  89. func Test_expand()
  90. new
  91. call assert_equal("", expand('%:S'))
  92. call assert_equal('3', '<slnum>'->expand())
  93. call assert_equal(['4'], expand('<slnum>', v:false, v:true))
  94. " Don't add any line above this, otherwise <slnum> will change.
  95. call assert_equal("", expand('%'))
  96. set verbose=1
  97. call assert_equal("", expand('%'))
  98. set verbose=0
  99. call assert_equal("", expand('%:p'))
  100. quit
  101. endfunc
  102. func s:sid_test()
  103. return 'works'
  104. endfunc
  105. func Test_expand_SID()
  106. let sid = expand('<SID>')
  107. execute 'let g:sid_result = ' .. sid .. 'sid_test()'
  108. call assert_equal('works', g:sid_result)
  109. endfunc
  110. " Test for 'wildignore' with expand()
  111. func Test_expand_wildignore()
  112. set wildignore=*.vim
  113. call assert_equal('', expand('test_expand_func.vim'))
  114. call assert_equal('', expand('test_expand_func.vim', 0))
  115. call assert_equal([], expand('test_expand_func.vim', 0, 1))
  116. call assert_equal('test_expand_func.vim', expand('test_expand_func.vim', 1))
  117. call assert_equal(['test_expand_func.vim'],
  118. \ expand('test_expand_func.vim', 1, 1))
  119. call assert_fails("call expand('*', [])", 'E745:')
  120. set wildignore&
  121. endfunc
  122. " vim: shiftwidth=2 sts=2 expandtab