Bram Moolenaar | 2b61852 | 2019-01-12 13:26:03 +0100 | [diff] [blame] | 1 | " Tests for the :source command. |
| 2 | |
| 3 | func 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 |
| 22 | endfunc |
| 23 | |
| 24 | func 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 |
| 38 | endfunc |
Bram Moolenaar | 5357552 | 2019-05-22 22:38:25 +0200 | [diff] [blame] | 39 | |
| 40 | func 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 Moolenaar | ddd3308 | 2019-06-03 23:07:25 +0200 | [diff] [blame] | 47 | call delete('Xsourcehello') |
Bram Moolenaar | 5357552 | 2019-05-22 22:38:25 +0200 | [diff] [blame] | 48 | endfunc |
Bram Moolenaar | ca33eb2 | 2020-01-19 20:18:09 +0100 | [diff] [blame] | 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. |
| 52 | func Test_different_script() |
Bram Moolenaar | ca33eb2 | 2020-01-19 20:18:09 +0100 | [diff] [blame] | 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') |
| 59 | endfunc |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 60 | |
| 61 | " When sourcing a vim script, shebang should be ignored. |
| 62 | func 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') |
| 67 | endfunc |
| 68 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 69 | " Test for expanding <sfile> in a autocmd and for <slnum> and <sflnum> |
| 70 | func 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') |
| 88 | endfunc |
| 89 | |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 90 | " vim: shiftwidth=2 sts=2 expandtab |