test_source.vim (14967B)
1 " Tests for the :source command. 2 3 source check.vim 4 source view_util.vim 5 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 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! 50 call delete('Xsourcehello') 51 endfunc 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() 56 call writefile(['let s:var = "asdf"'], 'XoneScript', 'D') 57 source XoneScript 58 call writefile(['let g:var = s:var'], 'XtwoScript', 'D') 59 call assert_fails('source XtwoScript', 'E121:') 60 endfunc 61 62 " When sourcing a Vim script, shebang should be ignored. 63 func Test_source_ignore_shebang() 64 call writefile(['#!./xyzabc', 'let g:val=369'], 'Xsisfile.vim', 'D') 65 source Xsisfile.vim 66 call assert_equal(g:val, 369) 67 endfunc 68 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 90 func Test_source_error() 91 call assert_fails('scriptencoding utf-8', 'E167:') 92 call assert_fails('finish', 'E168:') 93 " call assert_fails('scriptversion 2', 'E984:') 94 call assert_fails('source!', 'E471:') 95 new 96 call setline(1, ['', '', '', '']) 97 call assert_fails('1,3source Xscript.vim', 'E481:') 98 call assert_fails('1,3source! Xscript.vim', 'E481:') 99 bw! 100 endfunc 101 102 " Test for sourcing a script recursively 103 func Test_nested_script() 104 CheckRunVimInTerminal 105 call writefile([':source! Xscript.vim', ''], 'Xscript.vim') 106 let buf = RunVimInTerminal('', {'rows': 6}) 107 call term_wait(buf) 108 call term_sendkeys(buf, ":set noruler\n") 109 call term_sendkeys(buf, ":source! Xscript.vim\n") 110 call term_wait(buf) 111 call WaitForAssert({-> assert_match('E22: Scripts nested too deep\s*', term_getline(buf, 6))}) 112 call delete('Xscript.vim') 113 call StopVimInTerminal(buf) 114 endfunc 115 116 " Test for sourcing a script from the current buffer 117 func Test_source_buffer() 118 new 119 " Source a simple script 120 let lines =<< trim END 121 let a = "Test" 122 let b = 20 123 124 let c = [1.1] 125 END 126 call setline(1, lines) 127 source 128 call assert_equal(['Test', 20, [1.1]], [g:a, g:b, g:c]) 129 130 " Source a range of lines in the current buffer 131 %d _ 132 let lines =<< trim END 133 let a = 10 134 let a += 20 135 let a += 30 136 let a += 40 137 END 138 call setline(1, lines) 139 .source 140 call assert_equal(10, g:a) 141 3source 142 call assert_equal(40, g:a) 143 2,3source 144 call assert_equal(90, g:a) 145 146 " Make sure the script line number is correct when sourcing a range of 147 " lines. 148 %d _ 149 let lines =<< trim END 150 Line 1 151 Line 2 152 func Xtestfunc() 153 return expand("<sflnum>") 154 endfunc 155 Line 3 156 Line 4 157 END 158 call setline(1, lines) 159 3,5source 160 call assert_equal('4', Xtestfunc()) 161 delfunc Xtestfunc 162 163 " Source a script with line continuation lines 164 %d _ 165 let lines =<< trim END 166 let m = [ 167 \ 1, 168 \ 2, 169 \ ] 170 call add(m, 3) 171 END 172 call setline(1, lines) 173 source 174 call assert_equal([1, 2, 3], g:m) 175 " Source a script with line continuation lines and a comment 176 %d _ 177 let lines =<< trim END 178 let m = [ 179 "\ first entry 180 \ 'a', 181 "\ second entry 182 \ 'b', 183 \ ] 184 " third entry 185 call add(m, 'c') 186 END 187 call setline(1, lines) 188 source 189 call assert_equal(['a', 'b', 'c'], g:m) 190 " Source an incomplete line continuation line 191 %d _ 192 let lines =<< trim END 193 let k = [ 194 \ 195 END 196 call setline(1, lines) 197 call assert_fails('source', 'E697:') 198 " Source a function with a for loop 199 %d _ 200 let lines =<< trim END 201 let m = [] 202 " test function 203 func! Xtest() 204 for i in range(5, 7) 205 call add(g:m, i) 206 endfor 207 endfunc 208 call Xtest() 209 END 210 call setline(1, lines) 211 source 212 call assert_equal([5, 6, 7], g:m) 213 " Source an empty buffer 214 %d _ 215 source 216 217 " test for script local functions and variables 218 let lines =<< trim END 219 let s:var1 = 10 220 func s:F1() 221 let s:var1 += 1 222 return s:var1 223 endfunc 224 func s:F2() 225 endfunc 226 let g:ScriptID = expand("<SID>") 227 END 228 call setline(1, lines) 229 source 230 call assert_true(g:ScriptID != '') 231 call assert_true(exists('*' .. g:ScriptID .. 'F1')) 232 call assert_true(exists('*' .. g:ScriptID .. 'F2')) 233 call assert_equal(11, call(g:ScriptID .. 'F1', [])) 234 235 " the same script ID should be used even if the buffer is sourced more than 236 " once 237 %d _ 238 let lines =<< trim END 239 let g:ScriptID = expand("<SID>") 240 let g:Count += 1 241 END 242 call setline(1, lines) 243 let g:Count = 0 244 source 245 call assert_true(g:ScriptID != '') 246 let scid = g:ScriptID 247 source 248 call assert_equal(scid, g:ScriptID) 249 call assert_equal(2, g:Count) 250 source 251 call assert_equal(scid, g:ScriptID) 252 call assert_equal(3, g:Count) 253 254 " test for the script line number 255 %d _ 256 let lines =<< trim END 257 " comment 258 let g:Slnum1 = expand("<slnum>") 259 let i = 1 + 260 \ 2 + 261 "\ comment 262 \ 3 263 let g:Slnum2 = expand("<slnum>") 264 END 265 call setline(1, lines) 266 source 267 call assert_equal('2', g:Slnum1) 268 call assert_equal('7', g:Slnum2) 269 270 " test for retaining the same script number across source calls 271 let lines =<< trim END 272 let g:ScriptID1 = expand("<SID>") 273 let g:Slnum1 = expand("<slnum>") 274 let l =<< trim END 275 let g:Slnum2 = expand("<slnum>") 276 let g:ScriptID2 = expand("<SID>") 277 END 278 new 279 call setline(1, l) 280 source 281 bw! 282 let g:ScriptID3 = expand("<SID>") 283 let g:Slnum3 = expand("<slnum>") 284 END 285 call writefile(lines, 'Xscript') 286 source Xscript 287 call assert_true(g:ScriptID1 != g:ScriptID2) 288 call assert_equal(g:ScriptID1, g:ScriptID3) 289 call assert_equal('2', g:Slnum1) 290 call assert_equal('1', g:Slnum2) 291 call assert_equal('12', g:Slnum3) 292 call delete('Xscript') 293 294 " test for sourcing a heredoc 295 %d _ 296 let lines =<< trim END 297 let a = 1 298 let heredoc =<< trim DATA 299 red 300 green 301 blue 302 DATA 303 let b = 2 304 END 305 call setline(1, lines) 306 source 307 call assert_equal(['red', ' green', 'blue'], g:heredoc) 308 309 " test for a while and for statement 310 %d _ 311 let lines =<< trim END 312 let a = 0 313 let b = 1 314 while b <= 10 315 let a += 10 316 let b += 1 317 endwhile 318 for i in range(5) 319 let a += 10 320 endfor 321 END 322 call setline(1, lines) 323 source 324 call assert_equal(150, g:a) 325 326 " test for sourcing the same buffer multiple times after changing a function 327 %d _ 328 let lines =<< trim END 329 func Xtestfunc() 330 return "one" 331 endfunc 332 END 333 call setline(1, lines) 334 source 335 call assert_equal("one", Xtestfunc()) 336 call setline(2, ' return "two"') 337 source 338 call assert_equal("two", Xtestfunc()) 339 call setline(2, ' return "three"') 340 source 341 call assert_equal("three", Xtestfunc()) 342 delfunc Xtestfunc 343 344 " test for using try/catch 345 %d _ 346 let lines =<< trim END 347 let Trace = '1' 348 try 349 let a1 = b1 350 catch 351 let Trace ..= '2' 352 finally 353 let Trace ..= '3' 354 endtry 355 END 356 call setline(1, lines) 357 source 358 call assert_equal("123", g:Trace) 359 360 " test with the finish command 361 %d _ 362 let lines =<< trim END 363 let g:Color = 'blue' 364 finish 365 let g:Color = 'green' 366 END 367 call setline(1, lines) 368 source 369 call assert_equal('blue', g:Color) 370 371 " Test for the SourcePre and SourcePost autocmds 372 augroup Xtest 373 au! 374 au SourcePre * let g:XsourcePre=4 375 \ | let g:XsourcePreFile = expand("<afile>") 376 au SourcePost * let g:XsourcePost=6 377 \ | let g:XsourcePostFile = expand("<afile>") 378 augroup END 379 %d _ 380 let lines =<< trim END 381 let a = 1 382 END 383 call setline(1, lines) 384 source 385 call assert_equal(4, g:XsourcePre) 386 call assert_equal(6, g:XsourcePost) 387 call assert_equal(':source buffer=' .. bufnr(), g:XsourcePreFile) 388 call assert_equal(':source buffer=' .. bufnr(), g:XsourcePostFile) 389 augroup Xtest 390 au! 391 augroup END 392 augroup! Xtest 393 394 %bw! 395 endfunc 396 397 " Test for sourcing a Vim9 script from the current buffer 398 func Test_source_buffer_vim9() 399 throw 'Skipped: Vim9 script is N/A' 400 new 401 402 " test for sourcing a Vim9 script 403 %d _ 404 let lines =<< trim END 405 vim9script 406 407 # check dict 408 var x: number = 10 409 def g:Xtestfunc(): number 410 return x 411 enddef 412 END 413 call setline(1, lines) 414 source 415 call assert_equal(10, Xtestfunc()) 416 417 " test for sourcing a Vim9 script with line continuation 418 %d _ 419 let lines =<< trim END 420 vim9script 421 422 g:Str1 = "hello " 423 .. "world" 424 .. ", how are you?" 425 g:Colors = [ 426 'red', 427 # comment 428 'blue' 429 ] 430 g:Dict = { 431 a: 22, 432 # comment 433 b: 33 434 } 435 436 # calling a function with line continuation 437 def Sum(...values: list<number>): number 438 var sum: number = 0 439 for v in values 440 sum += v 441 endfor 442 return sum 443 enddef 444 g:Total1 = Sum(10, 445 20, 446 30) 447 448 var i: number = 0 449 while i < 10 450 # while loop 451 i += 452 1 453 endwhile 454 g:Count1 = i 455 456 # for loop 457 g:Count2 = 0 458 for j in range(10, 20) 459 g:Count2 += 460 i 461 endfor 462 463 g:Total2 = 10 + 464 20 - 465 5 466 467 g:Result1 = g:Total2 > 1 468 ? 'red' 469 : 'blue' 470 471 g:Str2 = 'x' 472 ->repeat(10) 473 ->trim() 474 ->strpart(4) 475 476 g:Result2 = g:Dict 477 .a 478 479 augroup Test 480 au! 481 au BufNewFile Xfile g:readFile = 1 482 | g:readExtra = 2 483 augroup END 484 g:readFile = 0 485 g:readExtra = 0 486 new Xfile 487 bwipe! 488 augroup Test 489 au! 490 augroup END 491 END 492 call setline(1, lines) 493 source 494 call assert_equal("hello world, how are you?", g:Str1) 495 call assert_equal(['red', 'blue'], g:Colors) 496 call assert_equal(#{a: 22, b: 33}, g:Dict) 497 call assert_equal(60, g:Total1) 498 call assert_equal(10, g:Count1) 499 call assert_equal(110, g:Count2) 500 call assert_equal(25, g:Total2) 501 call assert_equal('red', g:Result1) 502 call assert_equal('xxxxxx', g:Str2) 503 call assert_equal(22, g:Result2) 504 call assert_equal(1, g:readFile) 505 call assert_equal(2, g:readExtra) 506 507 " test for sourcing the same buffer multiple times after changing a function 508 %d _ 509 let lines =<< trim END 510 vim9script 511 def g:Xtestfunc(): string 512 return "one" 513 enddef 514 END 515 call setline(1, lines) 516 source 517 call assert_equal("one", Xtestfunc()) 518 call setline(3, ' return "two"') 519 source 520 call assert_equal("two", Xtestfunc()) 521 call setline(3, ' return "three"') 522 source 523 call assert_equal("three", Xtestfunc()) 524 delfunc Xtestfunc 525 526 " Test for sourcing a range of lines. Make sure the script line number is 527 " correct. 528 %d _ 529 let lines =<< trim END 530 Line 1 531 Line 2 532 vim9script 533 def g:Xtestfunc(): string 534 return expand("<sflnum>") 535 enddef 536 Line 3 537 Line 4 538 END 539 call setline(1, lines) 540 3,6source 541 call assert_equal('5', Xtestfunc()) 542 delfunc Xtestfunc 543 544 " test for sourcing a heredoc 545 %d _ 546 let lines =<< trim END 547 vim9script 548 var a = 1 549 g:heredoc =<< trim DATA 550 red 551 green 552 blue 553 DATA 554 var b = 2 555 END 556 call setline(1, lines) 557 source 558 call assert_equal(['red', ' green', 'blue'], g:heredoc) 559 560 " test for using the :vim9cmd modifier 561 %d _ 562 let lines =<< trim END 563 first line 564 g:Math = { 565 pi: 3.12, 566 e: 2.71828 567 } 568 g:Editors = [ 569 'vim', 570 # comment 571 'nano' 572 ] 573 last line 574 END 575 call setline(1, lines) 576 vim9cmd :2,10source 577 call assert_equal(#{pi: 3.12, e: 2.71828}, g:Math) 578 call assert_equal(['vim', 'nano'], g:Editors) 579 580 " '<,'> range before the cmd modifier works 581 unlet g:Math 582 unlet g:Editors 583 exe "normal 6GV4j:vim9cmd source\<CR>" 584 call assert_equal(['vim', 'nano'], g:Editors) 585 unlet g:Editors 586 587 " test for using try/catch 588 %d _ 589 let lines =<< trim END 590 vim9script 591 g:Trace = '1' 592 try 593 a1 = b1 594 catch 595 g:Trace ..= '2' 596 finally 597 g:Trace ..= '3' 598 endtry 599 END 600 call setline(1, lines) 601 source 602 call assert_equal('123', g:Trace) 603 604 " test with the finish command 605 %d _ 606 let lines =<< trim END 607 vim9script 608 g:Color = 'red' 609 finish 610 g:Color = 'blue' 611 END 612 call setline(1, lines) 613 source 614 call assert_equal('red', g:Color) 615 616 " test for ++clear argument to clear all the functions/variables 617 %d _ 618 let lines =<< trim END 619 g:ScriptVarFound = exists("color") 620 g:MyFuncFound = exists('*Myfunc') 621 if g:MyFuncFound 622 finish 623 endif 624 var color = 'blue' 625 def Myfunc() 626 enddef 627 END 628 call setline(1, lines) 629 vim9cmd source 630 call assert_false(g:MyFuncFound) 631 call assert_false(g:ScriptVarFound) 632 vim9cmd source 633 call assert_true(g:MyFuncFound) 634 call assert_true(g:ScriptVarFound) 635 vim9cmd source ++clear 636 call assert_false(g:MyFuncFound) 637 call assert_false(g:ScriptVarFound) 638 vim9cmd source ++clear 639 call assert_false(g:MyFuncFound) 640 call assert_false(g:ScriptVarFound) 641 call assert_fails('vim9cmd source ++clearx', 'E475:') 642 call assert_fails('vim9cmd source ++abcde', 'E484:') 643 644 %bw! 645 endfunc 646 647 func Test_source_buffer_long_line() 648 " This was reading past the end of the line. 649 new 650 norm300gr0 651 so 652 bwipe! 653 654 let lines =<< trim END 655 new 656 norm 10a0000000000ΓΈ00000000000 657 norm i0000000000000000000 658 silent! so 659 END 660 call writefile(lines, 'Xtest.vim') 661 source Xtest.vim 662 bwipe! 663 call delete('Xtest.vim') 664 endfunc 665 666 667 " vim: shiftwidth=2 sts=2 expandtab