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