Bram Moolenaar | 2b61852 | 2019-01-12 13:26:03 +0100 | [diff] [blame] | 1 | " Tests for the :source command. |
| 2 | |
Bram Moolenaar | 5c504f6 | 2021-04-01 13:39:51 +0200 | [diff] [blame] | 3 | source check.vim |
| 4 | source view_util.vim |
| 5 | |
Bram Moolenaar | 2b61852 | 2019-01-12 13:26:03 +0100 | [diff] [blame] | 6 | func 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 |
| 25 | endfunc |
| 26 | |
| 27 | func 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 |
| 41 | endfunc |
Bram Moolenaar | 5357552 | 2019-05-22 22:38:25 +0200 | [diff] [blame] | 42 | |
| 43 | func 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! |
Bram Moolenaar | ddd3308 | 2019-06-03 23:07:25 +0200 | [diff] [blame] | 50 | call delete('Xsourcehello') |
Bram Moolenaar | 5357552 | 2019-05-22 22:38:25 +0200 | [diff] [blame] | 51 | endfunc |
Bram Moolenaar | ca33eb2 | 2020-01-19 20:18:09 +0100 | [diff] [blame] | 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. |
| 55 | func Test_different_script() |
Bram Moolenaar | ca33eb2 | 2020-01-19 20:18:09 +0100 | [diff] [blame] | 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') |
| 62 | endfunc |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 63 | |
| 64 | " When sourcing a vim script, shebang should be ignored. |
| 65 | func 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') |
| 70 | endfunc |
| 71 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 72 | " Test for expanding <sfile> in a autocmd and for <slnum> and <sflnum> |
| 73 | func 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') |
| 91 | endfunc |
| 92 | |
Bram Moolenaar | 476a613 | 2020-04-08 19:48:56 +0200 | [diff] [blame] | 93 | func Test_source_error() |
| 94 | call assert_fails('scriptencoding utf-8', 'E167:') |
| 95 | call assert_fails('finish', 'E168:') |
| 96 | call assert_fails('scriptversion 2', 'E984:') |
| 97 | endfunc |
| 98 | |
Bram Moolenaar | 5c504f6 | 2021-04-01 13:39:51 +0200 | [diff] [blame] | 99 | " Test for sourcing a script recursively |
| 100 | func 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) |
| 111 | endfunc |
| 112 | |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 113 | " vim: shiftwidth=2 sts=2 expandtab |