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 | |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 72 | " Test for expanding <sfile> in an autocmd and for <slnum> and <sflnum> |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 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:') |
Yegappan Lakshmanan | 36a5b68 | 2022-03-19 12:56:51 +0000 | [diff] [blame] | 97 | call assert_fails('source!', 'E471:') |
| 98 | new |
| 99 | call setline(1, ['', '', '', '']) |
| 100 | call assert_fails('1,3source Xscript.vim', 'E481:') |
| 101 | call assert_fails('1,3source! Xscript.vim', 'E481:') |
| 102 | bw! |
Bram Moolenaar | 476a613 | 2020-04-08 19:48:56 +0200 | [diff] [blame] | 103 | endfunc |
| 104 | |
Bram Moolenaar | 5c504f6 | 2021-04-01 13:39:51 +0200 | [diff] [blame] | 105 | " Test for sourcing a script recursively |
| 106 | func Test_nested_script() |
| 107 | CheckRunVimInTerminal |
| 108 | call writefile([':source! Xscript.vim', ''], 'Xscript.vim') |
| 109 | let buf = RunVimInTerminal('', {'rows': 6}) |
| 110 | call term_wait(buf) |
| 111 | call term_sendkeys(buf, ":set noruler\n") |
| 112 | call term_sendkeys(buf, ":source! Xscript.vim\n") |
| 113 | call term_wait(buf) |
| 114 | call WaitForAssert({-> assert_match('E22: Scripts nested too deep\s*', term_getline(buf, 6))}) |
| 115 | call delete('Xscript.vim') |
| 116 | call StopVimInTerminal(buf) |
| 117 | endfunc |
| 118 | |
Yegappan Lakshmanan | 36a5b68 | 2022-03-19 12:56:51 +0000 | [diff] [blame] | 119 | " Test for sourcing a script from the current buffer |
| 120 | func Test_source_buffer() |
| 121 | new |
| 122 | " Source a simple script |
| 123 | let lines =<< trim END |
| 124 | let a = "Test" |
| 125 | let b = 20 |
| 126 | |
| 127 | let c = [1.1] |
| 128 | END |
| 129 | call setline(1, lines) |
| 130 | source |
| 131 | call assert_equal(['Test', 20, [1.1]], [g:a, g:b, g:c]) |
| 132 | |
| 133 | " Source a range of lines in the current buffer |
| 134 | %d _ |
| 135 | let lines =<< trim END |
| 136 | let a = 10 |
| 137 | let a += 20 |
| 138 | let a += 30 |
| 139 | let a += 40 |
| 140 | END |
| 141 | call setline(1, lines) |
| 142 | .source |
| 143 | call assert_equal(10, g:a) |
| 144 | 3source |
| 145 | call assert_equal(40, g:a) |
| 146 | 2,3source |
| 147 | call assert_equal(90, g:a) |
| 148 | |
| 149 | " Source a script with line continuation lines |
| 150 | %d _ |
| 151 | let lines =<< trim END |
| 152 | let m = [ |
| 153 | \ 1, |
| 154 | \ 2, |
| 155 | \ ] |
| 156 | call add(m, 3) |
| 157 | END |
| 158 | call setline(1, lines) |
| 159 | source |
| 160 | call assert_equal([1, 2, 3], g:m) |
| 161 | " Source a script with line continuation lines and a comment |
| 162 | %d _ |
| 163 | let lines =<< trim END |
| 164 | let m = [ |
| 165 | "\ first entry |
| 166 | \ 'a', |
| 167 | "\ second entry |
| 168 | \ 'b', |
| 169 | \ ] |
| 170 | " third entry |
| 171 | call add(m, 'c') |
| 172 | END |
| 173 | call setline(1, lines) |
| 174 | source |
| 175 | call assert_equal(['a', 'b', 'c'], g:m) |
| 176 | " Source an incomplete line continuation line |
| 177 | %d _ |
| 178 | let lines =<< trim END |
| 179 | let k = [ |
| 180 | \ |
| 181 | END |
| 182 | call setline(1, lines) |
| 183 | call assert_fails('source', 'E697:') |
| 184 | " Source a function with a for loop |
| 185 | %d _ |
| 186 | let lines =<< trim END |
| 187 | let m = [] |
| 188 | " test function |
| 189 | func! Xtest() |
| 190 | for i in range(5, 7) |
| 191 | call add(g:m, i) |
| 192 | endfor |
| 193 | endfunc |
| 194 | call Xtest() |
| 195 | END |
| 196 | call setline(1, lines) |
| 197 | source |
| 198 | call assert_equal([5, 6, 7], g:m) |
| 199 | " Source an empty buffer |
| 200 | %d _ |
| 201 | source |
| 202 | |
| 203 | " test for script local functions and variables |
| 204 | let lines =<< trim END |
| 205 | let s:var1 = 10 |
| 206 | func s:F1() |
| 207 | let s:var1 += 1 |
| 208 | return s:var1 |
| 209 | endfunc |
| 210 | func s:F2() |
| 211 | endfunc |
| 212 | let g:ScriptID = expand("<SID>") |
| 213 | END |
| 214 | call setline(1, lines) |
| 215 | source |
| 216 | call assert_true(g:ScriptID != '') |
| 217 | call assert_true(exists('*' .. g:ScriptID .. 'F1')) |
| 218 | call assert_true(exists('*' .. g:ScriptID .. 'F2')) |
| 219 | call assert_equal(11, call(g:ScriptID .. 'F1', [])) |
| 220 | |
| 221 | " the same script ID should be used even if the buffer is sourced more than |
| 222 | " once |
| 223 | %d _ |
| 224 | let lines =<< trim END |
| 225 | let g:ScriptID = expand("<SID>") |
| 226 | let g:Count += 1 |
| 227 | END |
| 228 | call setline(1, lines) |
| 229 | let g:Count = 0 |
| 230 | source |
| 231 | call assert_true(g:ScriptID != '') |
| 232 | let scid = g:ScriptID |
| 233 | source |
| 234 | call assert_equal(scid, g:ScriptID) |
| 235 | call assert_equal(2, g:Count) |
| 236 | source |
| 237 | call assert_equal(scid, g:ScriptID) |
| 238 | call assert_equal(3, g:Count) |
| 239 | |
| 240 | " test for the script line number |
| 241 | %d _ |
| 242 | let lines =<< trim END |
| 243 | " comment |
| 244 | let g:Slnum1 = expand("<slnum>") |
| 245 | let i = 1 + |
| 246 | \ 2 + |
| 247 | "\ comment |
| 248 | \ 3 |
| 249 | let g:Slnum2 = expand("<slnum>") |
| 250 | END |
| 251 | call setline(1, lines) |
| 252 | source |
| 253 | call assert_equal('2', g:Slnum1) |
| 254 | call assert_equal('7', g:Slnum2) |
| 255 | |
| 256 | " test for retaining the same script number across source calls |
| 257 | let lines =<< trim END |
| 258 | let g:ScriptID1 = expand("<SID>") |
| 259 | let g:Slnum1 = expand("<slnum>") |
| 260 | let l =<< trim END |
| 261 | let g:Slnum2 = expand("<slnum>") |
| 262 | let g:ScriptID2 = expand("<SID>") |
| 263 | END |
| 264 | new |
| 265 | call setline(1, l) |
| 266 | source |
| 267 | bw! |
| 268 | let g:ScriptID3 = expand("<SID>") |
| 269 | let g:Slnum3 = expand("<slnum>") |
| 270 | END |
| 271 | call writefile(lines, 'Xscript') |
| 272 | source Xscript |
| 273 | call assert_true(g:ScriptID1 != g:ScriptID2) |
| 274 | call assert_equal(g:ScriptID1, g:ScriptID3) |
| 275 | call assert_equal('2', g:Slnum1) |
| 276 | call assert_equal('1', g:Slnum2) |
| 277 | call assert_equal('12', g:Slnum3) |
| 278 | call delete('Xscript') |
| 279 | |
| 280 | " test for sourcing a heredoc |
| 281 | %d _ |
| 282 | let lines =<< trim END |
| 283 | let a = 1 |
| 284 | let heredoc =<< trim DATA |
| 285 | red |
| 286 | green |
| 287 | blue |
| 288 | DATA |
| 289 | let b = 2 |
| 290 | END |
| 291 | call setline(1, lines) |
| 292 | source |
| 293 | call assert_equal(['red', ' green', 'blue'], g:heredoc) |
| 294 | |
| 295 | " test for a while and for statement |
| 296 | %d _ |
| 297 | let lines =<< trim END |
| 298 | let a = 0 |
| 299 | let b = 1 |
| 300 | while b <= 10 |
| 301 | let a += 10 |
| 302 | let b += 1 |
| 303 | endwhile |
| 304 | for i in range(5) |
| 305 | let a += 10 |
| 306 | endfor |
| 307 | END |
| 308 | call setline(1, lines) |
| 309 | source |
| 310 | call assert_equal(150, g:a) |
| 311 | |
| 312 | " test for sourcing the same buffer multiple times after changing a function |
| 313 | %d _ |
| 314 | let lines =<< trim END |
| 315 | func Xtestfunc() |
| 316 | return "one" |
| 317 | endfunc |
| 318 | END |
| 319 | call setline(1, lines) |
| 320 | source |
| 321 | call assert_equal("one", Xtestfunc()) |
| 322 | call setline(2, ' return "two"') |
| 323 | source |
| 324 | call assert_equal("two", Xtestfunc()) |
| 325 | call setline(2, ' return "three"') |
| 326 | source |
| 327 | call assert_equal("three", Xtestfunc()) |
| 328 | delfunc Xtestfunc |
| 329 | |
| 330 | " test for sourcing a Vim9 script |
| 331 | %d _ |
| 332 | let lines =<< trim END |
| 333 | vim9script |
| 334 | |
| 335 | # check dict |
| 336 | var x: number = 10 |
| 337 | def g:Xtestfunc(): number |
| 338 | return x |
| 339 | enddef |
| 340 | END |
| 341 | call setline(1, lines) |
| 342 | source |
| 343 | call assert_equal(10, Xtestfunc()) |
| 344 | |
| 345 | %bw! |
| 346 | endfunc |
| 347 | |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 348 | " vim: shiftwidth=2 sts=2 expandtab |