robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
e1f1386d5e
master
1" Tests for the :let command. 2 3source vim9.vim 4 5func Test_let () 6" Test to not autoload when assigning. It causes internal error. 7set runtimepath += ./sautest 8let Test104#numvar = function ( 'tr' ) 9call assert_equal ( "function('tr')" , string ( Test104#numvar )) 10 11let foo#tr = function ( 'tr' ) 12call assert_equal ( "function('tr')" , string ( foo#tr )) 13unlet foo#tr 14 15let a = 1 16let b = 2 17 18let out = execute ( 'let a b' ) 19let s = "\na #1\nb #2" 20call assert_equal ( s , out ) 21 22let out = execute ( 'let {0 == 1 ? "a" : "b"}' ) 23let s = "\nb #2" 24call assert_equal ( s , out ) 25 26let out = execute ( 'let {0 == 1 ? "a" : "b"} a' ) 27let s = "\nb #2\na #1" 28call assert_equal ( s , out ) 29 30let out = execute ( 'let a {0 == 1 ? "a" : "b"}' ) 31let s = "\na #1\nb #2" 32call assert_equal ( s , out ) 33 34" Test for displaying a string variable 35let s = 'vim' 36let out = execute ( 'let s' ) 37let s = "\ns vim" 38call assert_equal ( s , out ) 39 40" Test for displaying a list variable 41let l = [ 1 , 2 ] 42let out = execute ( 'let l' ) 43let s = "\nl [1, 2]" 44call assert_equal ( s , out ) 45 46" Test for displaying a dict variable 47let d = { 'k' : 'v' } 48let out = execute ( 'let d' ) 49let s = "\nd {'k': 'v'}" 50call assert_equal ( s , out ) 51 52" Test for displaying a function reference variable 53let F = function ( 'min' ) 54let out = execute ( 'let F' ) 55let s = "\nF *min()" 56call assert_equal ( s , out ) 57 58let x = 0 59if 0 |let x = 1 |endif 60call assert_equal ( 0 , x ) 61 62" Display a list item using an out of range index 63let l = [ 10 ] 64call assert_fails ( 'let l[1]' , 'E684:' ) 65 66" List special variable dictionaries 67let g: Test_Global_Var = 5 68call assert_match ( "\nTest_Global_Var #5" , execute ( 'let g:' )) 69unlet g: Test_Global_Var 70 71let b: Test_Buf_Var = 8 72call assert_match ( "\nb:Test_Buf_Var #8" , execute ( 'let b:' )) 73unlet b: Test_Buf_Var 74 75let w: Test_Win_Var = 'foo' 76call assert_equal ( "\nw:Test_Win_Var foo" , execute ( 'let w:' )) 77unlet w: Test_Win_Var 78 79let t: Test_Tab_Var = 'bar' 80call assert_equal ( "\nt:Test_Tab_Var bar" , execute ( 'let t:' )) 81unlet t: Test_Tab_Var 82 83let s: Test_Script_Var = [ 7 ] 84call assert_match ( "\ns:Test_Script_Var \\[7]" , execute ( 'let s:' )) 85unlet s: Test_Script_Var 86 87let l: Test_Local_Var = { 'k' : 5 } 88call assert_match ( "\nl:Test_Local_Var {'k': 5}" , execute ( 'let l:' )) 89call assert_match ( "v:errors []" , execute ( 'let v:' )) 90 91" Test for assigning multiple list items 92let l = [ 1 , 2 , 3 ] 93let [ l [ 0 ], l [ 1 ]] = [ 10 , 20 ] 94call assert_equal ([ 10 , 20 , 3 ], l ) 95 96" Test for using curly brace name in the LHS of an assignment 97let listvar = [ 1 , 2 ] 98let s = 'listvar' 99let { s } = [ 3 , 4 ] 100call assert_equal ([ 3 , 4 ], listvar ) 101 102" Test for using curly brace name as a list and as list index in the LHS of 103" an assignment 104let listvar = [ 1 , 2 ] 105let idx = 1 106let s = 'listvar' 107let { s }[ 0 ] = 10 108let s = 'idx' 109let listvar [ {s }] = 20 110call assert_equal ([ 10 , 20 ], listvar ) 111let s1 = 'listvar' 112let s2 = 'idx' 113let { s1 }[ {s2 }] = 30 114call assert_equal ([ 10 , 30 ], listvar ) 115 116" Test for errors in conditional expression 117call assert_fails ( 'let val = [] ? 1 : 2' , 'E745:' ) 118call assert_fails ( 'let val = 1 ? 5+ : 6' , 'E121:' ) 119call assert_fails ( 'let val = 1 ? 0 : 5+' , 'E15:' ) 120call assert_false ( exists ( 'val' )) 121 122" Test for errors in logical operators 123let @a= 'if [] || 0 | let val = 2 | endif' 124call assert_fails ( 'exe @a' , 'E745:' ) 125call assert_fails ( 'call feedkeys(":let val = 0 || []\<cr>", "xt")' , 'E745:' ) 126call assert_fails ( 'exe "let val = [] && 5"' , 'E745:' ) 127call assert_fails ( 'exe "let val = 6 && []"' , 'E745:' ) 128endfunc 129 130func s: set_arg1 ( a ) abort 131let a: a = 1 132endfunction 133 134func s: set_arg2 ( a ) abort 135let a: b = 1 136endfunction 137 138func s: set_arg3 ( a ) abort 139let b = a: 140let b [ 'a' ] = 1 141endfunction 142 143func s: set_arg4 ( a ) abort 144let b = a: 145let b [ 'a' ] = 1 146endfunction 147 148func s: set_arg5 ( a ) abort 149let b = a: 150let b [ 'a' ][ 0 ] = 1 151endfunction 152 153func s: set_arg6 ( a ) abort 154let a: a [ 0 ] = 1 155endfunction 156 157func s: set_arg7 ( a ) abort 158call extend ( a: , { 'a' : 1 }) 159endfunction 160 161func s: set_arg8 ( a ) abort 162call extend ( a: , { 'b' : 1 }) 163endfunction 164 165func s: set_arg9 ( a ) abort 166let a: [ 'b' ] = 1 167endfunction 168 169func s: set_arg10 ( a ) abort 170let b = a: 171call extend ( b , { 'a' : 1 }) 172endfunction 173 174func s: set_arg11 ( a ) abort 175let b = a: 176call extend ( b , { 'b' : 1 }) 177endfunction 178 179func s: set_arg12 ( a ) abort 180let b = a: 181let b [ 'b' ] = 1 182endfunction 183 184func Test_let_arg_fail () 185call assert_fails ( 'call s:set_arg1(1)' , 'E46:' ) 186call assert_fails ( 'call s:set_arg2(1)' , 'E461:' ) 187call assert_fails ( 'call s:set_arg3(1)' , 'E46:' ) 188call assert_fails ( 'call s:set_arg4(1)' , 'E46:' ) 189call assert_fails ( 'call s:set_arg5(1)' , 'E46:' ) 190call s: set_arg6 ([ 0 ]) 191call assert_fails ( 'call s:set_arg7(1)' , 'E742:' ) 192call assert_fails ( 'call s:set_arg8(1)' , 'E742:' ) 193call assert_fails ( 'call s:set_arg9(1)' , 'E461:' ) 194call assert_fails ( 'call s:set_arg10(1)' , 'E742:' ) 195call assert_fails ( 'call s:set_arg11(1)' , 'E742:' ) 196call assert_fails ( 'call s:set_arg12(1)' , 'E461:' ) 197endfunction 198 199func s: set_varg1 (...) abort 200let a: 000 = [] 201endfunction 202 203func s: set_varg2 (...) abort 204let a: 000 [ 0 ] = 1 205endfunction 206 207func s: set_varg3 (...) abort 208let a: 000 += [ 1 ] 209endfunction 210 211func s: set_varg4 (...) abort 212call add ( a: 000 , 1 ) 213endfunction 214 215func s: set_varg5 (...) abort 216let a: 000 [ 0 ][ 0 ] = 1 217endfunction 218 219func s: set_varg6 (...) abort 220let b = a: 000 221let b [ 0 ] = 1 222endfunction 223 224func s: set_varg7 (...) abort 225let b = a: 000 226let b += [ 1 ] 227endfunction 228 229func s: set_varg8 (...) abort 230let b = a: 000 231call add ( b , 1 ) 232endfunction 233 234func s: set_varg9 (...) abort 235let b = a: 000 236let b [ 0 ][ 0 ] = 1 237endfunction 238 239func Test_let_varg_fail () 240call assert_fails ( 'call s:set_varg1(1)' , 'E46:' ) 241call assert_fails ( 'call s:set_varg2(1)' , 'E742:' ) 242call assert_fails ( 'call s:set_varg3(1)' , 'E46:' ) 243call assert_fails ( 'call s:set_varg4(1)' , 'E742:' ) 244call s: set_varg5 ([ 0 ]) 245call assert_fails ( 'call s:set_varg6(1)' , 'E742:' ) 246call assert_fails ( 'call s:set_varg7(1)' , 'E742:' ) 247call assert_fails ( 'call s:set_varg8(1)' , 'E742:' ) 248call s: set_varg9 ([ 0 ]) 249endfunction 250 251func Test_let_utf8_environment () 252let $ a = 'ĀĒĪŌŪあいうえお' 253call assert_equal ( 'ĀĒĪŌŪあいうえお' , $ a ) 254endfunc 255 256func Test_let_no_type_checking () 257let v = 1 258let v = [ 1 , 2 , 3 ] 259let v = { 'a' : 1 , 'b' : 2 } 260let v = 3.4 261let v = 'hello' 262endfunc 263 264func Test_let_termcap () 265throw 'Skipped: Nvim does not support termcap options' 266" Terminal code 267let old_t_te = & t_te 268let & t_te = "\<Esc>[yes;" 269call assert_match ( 't_te.*^[[yes;' , execute ( "set termcap" )) 270let & t_te = old_t_te 271 272if exists ( "+t_k1" ) 273" Key code 274let old_t_k1 = & t_k1 275let & t_k1 = "that" 276call assert_match ( 't_k1.*that' , execute ( "set termcap" )) 277let & t_k1 = old_t_k1 278endif 279 280call assert_fails ( 'let x = &t_xx' , 'E113:' ) 281let & t_xx = "yes" 282call assert_equal ( "yes" , & t_xx ) 283let & t_xx = "" 284call assert_fails ( 'let x = &t_xx' , 'E113:' ) 285endfunc 286 287func Test_let_option_error () 288let _w = & tw 289let & tw = 80 290call assert_fails ( 'let &tw .= 1' , [ 'E734:' , 'E734:' ]) 291call assert_fails ( 'let &tw .= []' , [ 'E734:' , 'E734:' ]) 292call assert_fails ( 'let &tw = []' , [ 'E745:' , 'E745:' ]) 293call assert_fails ( 'let &tw += []' , [ 'E745:' , 'E745:' ]) 294call assert_equal ( 80 , & tw ) 295let & tw = _w 296 297let _w = & autoread 298let & autoread = 1 299call assert_fails ( 'let &autoread .= 1' , [ 'E734:' , 'E734:' ]) 300call assert_fails ( 'let &autoread .= []' , [ 'E734:' , 'E734:' ]) 301call assert_fails ( 'let &autoread = []' , [ 'E745:' , 'E745:' ]) 302call assert_fails ( 'let &autoread += []' , [ 'E745:' , 'E745:' ]) 303call assert_equal ( 1 , & autoread ) 304let & autoread = _w 305 306let _w = & fillchars 307let & fillchars = "vert:|" 308call assert_fails ( 'let &fillchars += "diff:-"' , [ 'E734:' , 'E734:' ]) 309call assert_fails ( 'let &fillchars += []' , [ 'E734:' , 'E734:' ]) 310call assert_fails ( 'let &fillchars = []' , [ 'E730:' , 'E730:' ]) 311call assert_fails ( 'let &fillchars .= []' , [ 'E730:' , 'E730:' ]) 312call assert_equal ( "vert:|" , & fillchars ) 313let & fillchars = _w 314 315call assert_fails ( 'let &nosuchoption = 1' , [ 'E355:' , 'E355:' ]) 316call assert_fails ( 'let &nosuchoption = ""' , [ 'E355:' , 'E355:' ]) 317call assert_fails ( 'let &nosuchoption = []' , [ 'E355:' , 'E355:' ]) 318call assert_fails ( 'let &t_xx = []' , [ 'E730:' , 'E730:' ]) 319endfunc 320 321" Errors with the :let statement 322func Test_let_errors () 323let s = 'abcd' 324call assert_fails ( 'let s[1] = 5' , 'E689:' ) 325 326let l = [ 1 , 2 , 3 ] 327call assert_fails ( 'let l[:] = 5' , 'E709:' ) 328 329call assert_fails ( 'let x:lnum=5' , [ 'E121:' , 'E488:' ]) 330call assert_fails ( 'let v:=5' , 'E461:' ) 331call assert_fails ( 'let [a]' , 'E474:' ) 332call assert_fails ( 'let [a, b] = [' , 'E697:' ) 333call assert_fails ( 'let [a, b] = [10, 20' , 'E696:' ) 334call assert_fails ( 'let [a, b] = 10' , 'E714:' ) 335call assert_fails ( 'let [a, , b] = [10, 20]' , 'E475:' ) 336call assert_fails ( 'let [a, b&] = [10, 20]' , 'E475:' ) 337call assert_fails ( 'let $ = 10' , 'E475:' ) 338call assert_fails ( 'let $FOO[1] = "abc"' , 'E18:' ) 339call assert_fails ( 'let &buftype[1] = "nofile"' , 'E18:' ) 340let s = "var" 341let var = 1 342call assert_fails ( 'let var += [1,2]' , 'E734:' ) 343call assert_fails ( 'let {s}.1 = 2' , 'E1203:' ) 344call assert_fails ( 'let a[1] = 5' , 'E121:' ) 345let l = [[ 1 , 2 ]] 346call assert_fails ( 'let l[:][0] = [5]' , 'E708:' ) 347let d = { 'k' : 4 } 348call assert_fails ( 'let d.# = 5' , 'E488:' ) 349call assert_fails ( 'let d.m += 5' , 'E716:' ) 350call assert_fails ( 'let m = d[{]' , 'E15:' ) 351let l = [ 1 , 2 ] 352call assert_fails ( 'let l[2] = 0' , 'E684:' ) 353call assert_fails ( 'let l[0:1] = [1, 2, 3]' , 'E710:' ) 354call assert_fails ( 'let l[-2:-3] = [3, 4]' , 'E684:' ) 355call assert_fails ( 'let l[0:4] = [5, 6]' , 'E711:' ) 356call assert_fails ( 'let l -= 2' , 'E734:' ) 357call assert_fails ( 'let l += 2' , 'E734:' ) 358call assert_fails ( 'let g:["a;b"] = 10' , 'E461:' ) 359call assert_fails ( 'let g:.min = function("max")' , 'E704:' ) 360call assert_fails ( 'let g:cos = "" | let g:.cos = {-> 42}' , 'E704:' ) 361if has ( 'channel' ) 362let ch = test_null_channel () 363call assert_fails ( 'let ch += 1' , 'E734:' ) 364endif 365call assert_fails ( 'let name = "a" .. "b",' , 'E488: Trailing characters: ,' ) 366 367" This test works only when the language is English 368if v: lang == "C" || v: lang =~ '^[Ee]n' 369call assert_fails ( 'let [a ; b;] = [10, 20]' , 370 \'Double ; in list of variables' ) 371endif 372endfunc 373 374func Test_let_heredoc_fails () 375call assert_fails ( 'let v =<< marker' , 'E991:' ) 376try 377exe "let v =<< TEXT | abc | TEXT" 378call assert_report ( 'No exception thrown' ) 379catch /E488:/ 380catch 381call assert_report ( 'Caught exception: ' .. v: exception ) 382endtry 383 384try 385let & commentstring =<< trim TEXT 386change 387insert 388append 389TEXT 390call assert_report ( 'No exception thrown' ) 391catch /E730:/ 392catch 393call assert_report ( 'Caught exception: ' .. v: exception ) 394endtry 395 396try 397let $ SOME_ENV_VAR =<< trim TEXT 398change 399insert 400append 401TEXT 402call assert_report ( 'No exception thrown' ) 403catch /E730:/ 404catch 405call assert_report ( 'Caught exception: ' .. v: exception ) 406endtry 407 408try 409let @r=<< trim TEXT 410change 411insert 412append 413TEXT 414call assert_report ( 'No exception thrown' ) 415catch /E730:/ 416catch 417call assert_report ( 'Caught exception: ' .. v: exception ) 418endtry 419 420try 421let @- =<< trimTEXT 422change 423insert 424append 425TEXT 426call assert_report ( 'No exception thrown' ) 427catch /E730:/ 428catch 429call assert_report ( 'Caught exception: ' .. v: exception ) 430endtry 431 432try 433let [] =<< trim TEXT 434TEXT 435call assert_report ( 'No exception thrown' ) 436catch /E475:/ 437catch 438call assert_report ( 'Caught exception: ' .. v: exception ) 439endtry 440 441try 442let [ a b c] =<< trimTEXT 443TEXT 444call assert_report ( 'No exception thrown' ) 445catch /E475:/ 446catch 447call assert_report ( 'Caught exception: ' .. v: exception ) 448endtry 449 450try 451let [ a ;b ;c ] =<< trim TEXT 452TEXT 453call assert_report ( 'No exception thrown' ) 454catch /E452:/ 455catch 456call assert_report ( 'Caught exception: ' .. v: exception ) 457endtry 458 459try 460let v =<< trim trimm 461trimm 462call assert_report ( 'No exception thrown' ) 463catch /E221:/ 464catch 465call assert_report ( 'Caught exception: ' .. v: exception ) 466endtry 467 468try 469let v =<< trim trim evall 470evall 471call assert_report ( 'No exception thrown' ) 472catch /E221:/ 473catch 474call assert_report ( 'Caught exception: ' .. v: exception ) 475endtry 476 477let text =<< trim END 478func WrongSyntax() 479let v =<< that there 480endfunc 481END 482call writefile ( text , 'XheredocFail' ) 483call assert_fails ( 'source XheredocFail' , 'E1145:' ) 484call delete ( 'XheredocFail' ) 485 486let text =<< trim CodeEnd 487func MissingEnd() 488let v =<< END 489endfunc 490CodeEnd 491call writefile ( text , 'XheredocWrong' ) 492call assert_fails ( 'source XheredocWrong' , 'E1145:' ) 493call delete ( 'XheredocWrong' ) 494 495let text =<< trim TEXTend 496let v =<< " comment 497TEXTend 498call writefile ( text , 'XheredocNoMarker' ) 499call assert_fails ( 'source XheredocNoMarker' , 'E172:' ) 500call delete ( 'XheredocNoMarker' ) 501 502let text =<< trim TEXTend 503let v =<< text 504TEXTend 505call writefile ( text , 'XheredocBadMarker' ) 506call assert_fails ( 'source XheredocBadMarker' , 'E221:' ) 507call delete ( 'XheredocBadMarker' ) 508 509call writefile ([ 'let v =<< TEXT' , 'abc' ], 'XheredocMissingMarker' ) 510call assert_fails ( 'source XheredocMissingMarker' , 'E990:' ) 511call delete ( 'XheredocMissingMarker' ) 512endfunc 513 514func Test_let_heredoc_trim_no_indent_marker () 515let text =<< trim END 516Text 517with 518indent 519END 520call assert_equal ([ 'Text' , 'with' , 'indent' ], text ) 521endfunc 522 523func Test_let_interpolated () 524call assert_equal ( '{text}' , $ '{{text}}') 525call assert_equal ( '{{text}}' , $ '{{{{text}}}}') 526let text = 'text' 527call assert_equal ( 'text{{' , $ '{text .. "{{"}') 528call assert_equal ( 'text{{' , $ "{text .. '{{'}") 529call assert_equal ( 'text{{' , $ '{text .. '{{'}') 530call assert_equal ( 'text{{' , $ "{text .. "{{"}") 531endfunc 532 533" Test for the setting a variable using the heredoc syntax. 534" Keep near the end, this messes up highlighting. 535func Test_let_heredoc () 536let var1 =<< END 537Some sample text 538Text with indent 539!@#$%^&*()-+_={}|[]\~`:";'<>?,./ 540END 541 542call assert_equal ([ "Some sample text" , "\tText with indent" , " !@#$%^&*()-+_={}|[]\\~`:\";'<>?,./" ], var1 ) 543 544let var2 =<< XXX 545Editor 546XXX 547call assert_equal ([ 'Editor' ], var2 ) 548 549let var3 =<< END 550END 551call assert_equal ([], var3 ) 552 553let var3 =<< END 554vim 555 556end 557END 558END 559END 560call assert_equal ([ 'vim' , '' , 'end' , ' END' , 'END ' ], var3 ) 561 562let var1 =<< trim END 563Line1 564Line2 565Line3 566END 567END 568call assert_equal ([ 'Line1' , ' Line2' , "\tLine3" , ' END' ], var1 ) 569 570let var1 =<< trim !!! 571Line1 572line2 573Line3 574!!! 575 !!! 576call assert_equal ([ 'Line1' , ' line2' , "\tLine3" , '!!!' ,], var1 ) 577 578let var1 =<< trim XX 579Line1 580XX 581call assert_equal ([ 'Line1' ], var1 ) 582 583let var1 =<< trim XX " comment 584Line1 585Line2 586Line3 587XX 588call assert_equal ([ 'Line1' , ' Line2' , 'Line3' ], var1 ) 589 590" ignore "endfunc" 591let var1 =<< END 592something 593endfunc 594END 595call assert_equal ([ 'something' , 'endfunc' ], var1 ) 596 597" ignore "endfunc" with trim 598let var1 =<< trim END 599something 600endfunc 601END 602call assert_equal ([ 'something' , 'endfunc' ], var1 ) 603 604" not concatenate lines 605let var1 =<< END 606some 607\thing 608\ else 609END 610call assert_equal ([ 'some' , ' \thing' , ' \ else' ], var1 ) 611 612" ignore "python << xx" 613let var1 =<< END 614something 615python << xx 616END 617call assert_equal ([ 'something' , 'python << xx' ], var1 ) 618 619" ignore "python << xx" with trim 620let var1 =<< trim END 621something 622python << xx 623END 624call assert_equal ([ 'something' , 'python << xx' ], var1 ) 625 626" ignore "append" 627let var1 =<< E 628something 629app 630E 631call assert_equal ([ 'something' , 'app' ], var1 ) 632 633" ignore "append" with trim 634let var1 =<< trim END 635something 636app 637END 638call assert_equal ([ 'something' , 'app' ], var1 ) 639 640let check = [] 641if 0 642let check =<< trim END 643from heredoc 644END 645endif 646call assert_equal ([], check ) 647 648" unpack assignment 649let [ a , b , c ] =<< END 650x 651\y 652z 653END 654call assert_equal ([ ' x' , ' \y' , ' z' ], [ a , b , c ]) 655 656" unpack assignment without whitespace 657let [ a , b , c ] =<< END 658change 659insert 660append 661END 662call assert_equal ([ 'change' , 'insert' , 'append' ], [ a , b , c ]) 663 664" unpack assignment with semicolon 665let [ a ;b ] =<< END 666change 667insert 668append 669END 670call assert_equal ([ 'change' , [ 'insert' , 'append' ]], [ a , b ]) 671 672" unpack assignment with registers 673let [ @/, @", @-] =<<END 674change 675insert 676append 677END 678call assert_equal ([ 'change' , 'insert' , 'append' ], [ @/, @", @-]) 679 680" curly braces name and list slice assignment 681let foo_3_bar = [ '' , '' , '' ] 682let foo_ { 1 + 2 } _bar [ : ] =<< END 683change 684insert 685append 686END 687call assert_equal ([ 'change' , 'insert' , 'append' ], foo_3_bar ) 688 689" dictionary key containing brackets and spaces 690let d = { 'abc] 123' : 'baz' } 691let d [ d [ 'abc] 123' ] .. '{' ] =<< END 692change 693insert 694append 695END 696call assert_equal ([ 'change' , 'insert' , 'append' ], d [ 'baz{' ]) 697endfunc 698 699" Test for evaluating Vim expressions in a heredoc using {expr} 700" Keep near the end, this messes up highlighting. 701func Test_let_heredoc_eval () 702let str = '' 703let code =<< trim eval END 704let a = {5 + 10} 705let b = {min([10, 6])} + {max([4, 6])} 706{str} 707let c = "abc{str}d" 708END 709call assert_equal ([ 'let a = 15' , 'let b = 6 + 6' , '' , 'let c = "abcd"' ], code ) 710 711let $ TESTVAR = "Hello" 712let code =<< eval trim END 713let s = "{$TESTVAR}" 714END 715call assert_equal ([ 'let s = "Hello"' ], code ) 716 717let code =<< eval END 718let s = "{$TESTVAR}" 719END 720call assert_equal ([ ' let s = "Hello"' ], code ) 721 722let a = 10 723let data =<< eval END 724{a} 725END 726call assert_equal ([ '10' ], data ) 727 728let x = 'X' 729let code =<< eval trim END 730let a = {{abc}} 731let b = {x} 732let c = {{ 733END 734call assert_equal ([ 'let a = {abc}' , 'let b = X' , 'let c = {' ], code ) 735 736" Evaluate a dictionary 737let d1 = #{ a : 10 , b : 'ss' , c : {}} 738let code =<< eval trim END 739let d2 = {d1} 740END 741call assert_equal ([ "let d2 = {'a': 10, 'b': 'ss', 'c': {}}" ], code ) 742 743" Empty dictionary 744let d1 = {} 745let code =<< eval trim END 746let d2 = {d1} 747END 748call assert_equal ([ "let d2 = {}" ], code ) 749 750" null dictionary 751let d1 = v: _null_dict 752let code =<< eval trim END 753let d2 = {d1} 754END 755call assert_equal ([ "let d2 = {}" ], code ) 756 757" Evaluate a List 758let l1 = [ 'a' , 'b' , 'c' ] 759let code =<< eval trim END 760let l2 = {l1} 761END 762call assert_equal ([ "let l2 = ['a', 'b', 'c']" ], code ) 763 764" Empty List 765let l1 = [] 766let code =<< eval trim END 767let l2 = {l1} 768END 769call assert_equal ([ "let l2 = []" ], code ) 770 771" Null List 772let l1 = v: _null_list 773let code =<< eval trim END 774let l2 = {l1} 775END 776call assert_equal ([ "let l2 = []" ], code ) 777 778let code = 'xxx' 779let code =<< eval trim END 780let n = {5 + 7816} 782END 783call assert_equal ( 'xxx' , code ) 784 785let code =<< eval trim END 786let n = {min([1, 2]} + {max([3, 4])} 787END 788call assert_equal ( 'xxx' , code ) 789 790let lines =<< trim LINES 791let text =<< eval trim END 792let b = { 793END 794LINES 795call CheckScriptFailure ( lines , 'E1279:' ) 796 797let lines =<< trim LINES 798let text =<< eval trim END 799let b = {abc 800END 801LINES 802call CheckScriptFailure ( lines , 'E1279:' ) 803 804let lines =<< trim LINES 805let text =<< eval trim END 806let b = {} 807END 808LINES 809call CheckScriptFailure ( lines , 'E15:' ) 810 811" Test for using heredoc in a single string using :execute or execute() 812for [ cmd , res ] in items ({ 813 \"let x =<< trim END\n one\n two\nEND" : [ 'one' , 'two' ], 814 \"let x =<< trim END\n one\n two\nEND" : [ 'one' , ' two' ], 815 \" let x =<< trim END\n one\n two\n END" : [ 'one' , 'two' ], 816 \" let x =<< trim END\n one\n two\n END" : [ 'one' , ' two' ], 817 \"let x =<< END\n one\n two\nEND" : [ ' one' , ' two' ], 818 \"let x =<< END\none\ntwo\nEND" : [ 'one' , 'two' ], 819 \"let x =<< END \" comment\none\ntwo\nEND" : [ 'one' , 'two' ], 820 \}) 821execute cmd 822call assert_equal ( res , x ) 823unlet x 824call assert_equal ( $ "\n{string(res)}", execute($"{cmd}\necho x")) 825unlet x 826endfor 827for [ cmd , err ] in items ({ 828 \"let x =<<\none\ntwo" : "E172:" , 829 \"let x =<< trim\n one\n two" : "E172:" , 830 \"let x =<< end\none\ntwo\nend" : "E221:" , 831 \"let x =<< END\none\ntwo" : "E990: Missing end marker 'END'" , 832 \"let x =<< END !\none\ntwo\nEND" : "E488: Trailing characters: !" , 833 \"let x =<< eval END\none\ntwo{y}\nEND" : "E121: Undefined variable: y" , 834 \}) 835call assert_fails ( 'execute cmd' , err ) 836call assert_fails ( 'call execute(cmd)' , err ) 837endfor 838 839" skipped heredoc 840if 0 841let msg =<< trim eval END 842n is: {n} 843END 844endif 845 846" Test for sourcing a script containing a heredoc with invalid expression. 847" Variable assignment should fail, if expression evaluation fails 848new 849let g: Xvar = 'test' 850let g: b = 10 851let lines =<< trim END 852let Xvar =<< eval CODE 853let a = 1 854let b = {5+} 855let c = 2 856CODE 857let g:Count += 1 858END 859call setline ( 1 , lines ) 860let g: Count = 0 861call assert_fails ( 'source' , 'E15:' ) 862call assert_equal ( 1 , g: Count ) 863call setline ( 3 , 'let b = {abc}' ) 864call assert_fails ( 'source' , 'E121:' ) 865call assert_equal ( 2 , g: Count ) 866call setline ( 3 , 'let b = {abc} + {min([9, 4])} + 2' ) 867call assert_fails ( 'source' , 'E121:' ) 868call assert_equal ( 3 , g: Count ) 869call assert_equal ( 'test' , g: Xvar ) 870call assert_equal ( 10 , g: b ) 871bw ! 872endfunc 873 874" vim: shiftwidth=2 sts=2 expandtab