blob: ba6fd5ad95d3f0b4828869d8d2220f6b2c1fa667 [file] [log] [blame]
Bram Moolenaar2b618522019-01-12 13:26:03 +01001" 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
Bram Moolenaar53575522019-05-22 22:38:25 +020039
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!
Bram Moolenaarddd33082019-06-03 23:07:25 +020047 call delete('Xsourcehello')
Bram Moolenaar53575522019-05-22 22:38:25 +020048endfunc
Bram Moolenaarca33eb22020-01-19 20:18:09 +010049
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()
Bram Moolenaarca33eb22020-01-19 20:18:09 +010053 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
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010060
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
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010069" 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
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010090" vim: shiftwidth=2 sts=2 expandtab