test_source.vim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. " Tests for the :source command.
  2. source check.vim
  3. source view_util.vim
  4. func Test_source_autocmd()
  5. call writefile([
  6. \ 'let did_source = 1',
  7. \ ], 'Xsourced')
  8. au SourcePre *source* let did_source_pre = 1
  9. au SourcePost *source* let did_source_post = 1
  10. source Xsourced
  11. call assert_equal(g:did_source, 1)
  12. call assert_equal(g:did_source_pre, 1)
  13. call assert_equal(g:did_source_post, 1)
  14. call delete('Xsourced')
  15. au! SourcePre
  16. au! SourcePost
  17. unlet g:did_source
  18. unlet g:did_source_pre
  19. unlet g:did_source_post
  20. endfunc
  21. func Test_source_cmd()
  22. au SourceCmd *source* let did_source = expand('<afile>')
  23. au SourcePre *source* let did_source_pre = 2
  24. au SourcePost *source* let did_source_post = 2
  25. source Xsourced
  26. call assert_equal(g:did_source, 'Xsourced')
  27. call assert_false(exists('g:did_source_pre'))
  28. call assert_equal(g:did_source_post, 2)
  29. au! SourceCmd
  30. au! SourcePre
  31. au! SourcePost
  32. endfunc
  33. func Test_source_sandbox()
  34. new
  35. call writefile(["Ohello\<Esc>"], 'Xsourcehello')
  36. source! Xsourcehello | echo
  37. call assert_equal('hello', getline(1))
  38. call assert_fails('sandbox source! Xsourcehello', 'E48:')
  39. bwipe!
  40. call delete('Xsourcehello')
  41. endfunc
  42. " When deleting a file and immediately creating a new one the inode may be
  43. " recycled. Vim should not recognize it as the same script.
  44. func Test_different_script()
  45. call writefile(['let s:var = "asdf"'], 'XoneScript', 'D')
  46. source XoneScript
  47. call writefile(['let g:var = s:var'], 'XtwoScript', 'D')
  48. call assert_fails('source XtwoScript', 'E121:')
  49. endfunc
  50. " When sourcing a vim script, shebang should be ignored.
  51. func Test_source_ignore_shebang()
  52. call writefile(['#!./xyzabc', 'let g:val=369'], 'Xfile.vim')
  53. source Xfile.vim
  54. call assert_equal(g:val, 369)
  55. call delete('Xfile.vim')
  56. endfunc
  57. " Test for expanding <sfile> in a autocmd and for <slnum> and <sflnum>
  58. func Test_source_autocmd_sfile()
  59. let code =<< trim [CODE]
  60. let g:SfileName = ''
  61. augroup sfiletest
  62. au!
  63. autocmd User UserAutoCmd let g:Sfile = '<sfile>:t'
  64. augroup END
  65. doautocmd User UserAutoCmd
  66. let g:Slnum = expand('<slnum>')
  67. let g:Sflnum = expand('<sflnum>')
  68. augroup! sfiletest
  69. [CODE]
  70. call writefile(code, 'Xscript.vim')
  71. source Xscript.vim
  72. call assert_equal('Xscript.vim', g:Sfile)
  73. call assert_equal('7', g:Slnum)
  74. call assert_equal('8', g:Sflnum)
  75. call delete('Xscript.vim')
  76. endfunc
  77. func Test_source_error()
  78. call assert_fails('scriptencoding utf-8', 'E167:')
  79. call assert_fails('finish', 'E168:')
  80. " call assert_fails('scriptversion 2', 'E984:')
  81. endfunc
  82. " Test for sourcing a script recursively
  83. func Test_nested_script()
  84. CheckRunVimInTerminal
  85. call writefile([':source! Xscript.vim', ''], 'Xscript.vim')
  86. let buf = RunVimInTerminal('', {'rows': 6})
  87. call term_wait(buf)
  88. call term_sendkeys(buf, ":set noruler\n")
  89. call term_sendkeys(buf, ":source! Xscript.vim\n")
  90. call term_wait(buf)
  91. call WaitForAssert({-> assert_match('E22: Scripts nested too deep\s*', term_getline(buf, 6))})
  92. call delete('Xscript.vim')
  93. call StopVimInTerminal(buf)
  94. endfunc
  95. " vim: shiftwidth=2 sts=2 expandtab