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