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