robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
edcaf1887a
master
1" Test various aspects of the Vim script language. 2" Most of this was formerly in test49.vim (developed by Servatius Brandt 3" <Servatius.Brandt@fujitsu-siemens.com>) 4 5source check.vim 6source shared.vim 7source script_util.vim 8 9"------------------------------------------------------------------------------- 10" Test environment {{{1 11"------------------------------------------------------------------------------- 12 13" Append a message to the "messages" file 14func Xout ( text ) 15split messages 16$ put =a:text 17wq 18endfunc 19 20com ! -nargs = 1 Xout call Xout(<args>) 21 22" Create a new instance of Vim and run the commands in 'test' and then 'verify' 23" The commands in 'test' are expected to store the test results in the Xtest.out 24" file. If the test passes successfully, then Xtest.out should be empty. 25func RunInNewVim ( test , verify ) 26let init =<< trim END 27set cpo-=C " support line-continuation in sourced script 28source script_util.vim 29XpathINIT 30XloopINIT 31END 32let cleanup =<< trim END 33call writefile(v:errors, 'Xtest.out') 34qall 35END 36call writefile ( init , 'Xtest.vim' ) 37call writefile ( a: test , 'Xtest.vim' , 'a' ) 38call writefile ( a: verify , 'Xverify.vim' ) 39call writefile ( cleanup , 'Xverify.vim' , 'a' ) 40call RunVim ([], [], "-S Xtest.vim -S Xverify.vim" ) 41call assert_equal ([], readfile ( 'Xtest.out' )) 42call delete ( 'Xtest.out' ) 43call delete ( 'Xtest.vim' ) 44call delete ( 'Xverify.vim' ) 45endfunc 46 47"------------------------------------------------------------------------------- 48" Test 1: :endwhile in function {{{1 49" 50" Detect if a broken loop is (incorrectly) reactivated by the 51" :endwhile. Use a :return to prevent an endless loop, and make 52" this test first to get a meaningful result on an error before other 53" tests will hang. 54"------------------------------------------------------------------------------- 55 56func T1_F () 57Xpath 'a' 58let first = 1 59while 1 60Xpath 'b' 61if first 62Xpath 'c' 63let first = 0 64break 65else 66Xpath 'd' 67return 68endif 69endwhile 70endfunc 71 72func T1_G () 73Xpath 'h' 74let first = 1 75while 1 76Xpath 'i' 77if first 78Xpath 'j' 79let first = 0 80break 81else 82Xpath 'k' 83return 84endif 85if 1 " unmatched :if 86endwhile 87endfunc 88 89func Test_endwhile_function () 90XpathINIT 91call T1_F () 92Xpath 'F' 93 94try 95call T1_G () 96catch 97" Catch missing :endif 98call assert_true ( v: exception =~ 'E171' ) 99Xpath 'x' 100endtry 101Xpath 'G' 102 103call assert_equal ( 'abcFhijxG' , g: Xpath ) 104endfunc 105 106"------------------------------------------------------------------------------- 107" Test 2: :endwhile in script {{{1 108" 109" Detect if a broken loop is (incorrectly) reactivated by the 110" :endwhile. Use a :finish to prevent an endless loop, and place 111" this test before others that might hang to get a meaningful result 112" on an error. 113" 114" This test executes the bodies of the functions T1_F and T1_G from 115" the previous test as script files (:return replaced by :finish). 116"------------------------------------------------------------------------------- 117 118func Test_endwhile_script () 119XpathINIT 120ExecAsScript T1_F 121Xpath 'F' 122call DeleteTheScript () 123 124try 125ExecAsScript T1_G 126catch 127" Catch missing :endif 128call assert_true ( v: exception =~ 'E171' ) 129Xpath 'x' 130endtry 131Xpath 'G' 132call DeleteTheScript () 133 134call assert_equal ( 'abcFhijxG' , g: Xpath ) 135endfunc 136 137"------------------------------------------------------------------------------- 138" Test 3: :if, :elseif, :while, :continue, :break {{{1 139"------------------------------------------------------------------------------- 140 141func Test_if_while () 142XpathINIT 143if 1 144Xpath 'a' 145let loops = 3 146while loops > -1 " main loop: loops == 3, 2, 1 (which breaks) 147if loops <= 0 148let break_err = 1 149let loops = -1 150else 151Xpath 'b' . loops 152endif 153if ( loops == 2 ) 154while loops == 2 " dummy loop 155Xpath 'c' . loops 156let loops = loops - 1 157continue " stop dummy loop 158Xpath 'd' . loops 159endwhile 160continue " continue main loop 161Xpath 'e' . loops 162elseif ( loops == 1 ) 163let p = 1 164while p " dummy loop 165Xpath 'f' . loops 166let p = 0 167break " break dummy loop 168Xpath 'g' . loops 169endwhile 170Xpath 'h' . loops 171unlet p 172break " break main loop 173Xpath 'i' . loops 174endif 175if ( loops > 0 ) 176Xpath 'j' . loops 177endif 178while loops == 3 " dummy loop 179let loops = loops - 1 180endwhile " end dummy loop 181endwhile " end main loop 182Xpath 'k' 183else 184Xpath 'l' 185endif 186Xpath 'm' 187if exists ( "break_err" ) 188Xpath 'm' 189unlet break_err 190endif 191 192unlet loops 193 194call assert_equal ( 'ab3j3b2c2b1f1h1km' , g: Xpath ) 195endfunc 196 197" Check double quote after skipped "elseif" does not give error E15 198func Test_skipped_elseif () 199if "foo" ==? "foo" 200let result = "first" 201elseif "foo" ==? "foo" 202let result = "second" 203endif 204call assert_equal ( 'first' , result ) 205endfunc 206 207"------------------------------------------------------------------------------- 208" Test 4: :return {{{1 209"------------------------------------------------------------------------------- 210 211func T4_F () 212if 1 213Xpath 'a' 214let loops = 3 215while loops > 0 " 3: 2: 1: 216Xpath 'b' . loops 217if ( loops == 2 ) 218Xpath 'c' . loops 219return 220Xpath 'd' . loops 221endif 222Xpath 'e' . loops 223let loops = loops - 1 224endwhile 225Xpath 'f' 226else 227Xpath 'g' 228endif 229endfunc 230 231func Test_return () 232XpathINIT 233call T4_F () 234Xpath '4' 235 236call assert_equal ( 'ab3e3b2c24' , g: Xpath ) 237endfunc 238 239 240"------------------------------------------------------------------------------- 241" Test 5: :finish {{{1 242" 243" This test executes the body of the function T4_F from the previous 244" test as a script file (:return replaced by :finish). 245"------------------------------------------------------------------------------- 246 247func Test_finish () 248XpathINIT 249ExecAsScript T4_F 250Xpath '5' 251call DeleteTheScript () 252 253call assert_equal ( 'ab3e3b2c25' , g: Xpath ) 254endfunc 255 256 257 258"------------------------------------------------------------------------------- 259" Test 6: Defining functions in :while loops {{{1 260" 261" Functions can be defined inside other functions. An inner function 262" gets defined when the outer function is executed. Functions may 263" also be defined inside while loops. Expressions in braces for 264" defining the function name are allowed. 265" 266" The functions are defined when sourcing the script, only the 267" resulting path is checked in the test function. 268"------------------------------------------------------------------------------- 269 270XpathINIT 271 272" The command CALL collects the argument of all its invocations in "calls" 273" when used from a function (that is, when the global variable "calls" needs 274" the "g:" prefix). This is to check that the function code is skipped when 275" the function is defined. For inner functions, do so only if the outer 276" function is not being executed. 277" 278let calls = "" 279com ! -nargs = 1 CALL 280 \if !exists("calls") && !exists("outer") | 281 \ letg: calls =g: calls . <args> | 282 \ endif 283 284let i = 0 285while i < 3 286let i = i + 1 287if i == 1 288Xpath 'a' 289function ! F1 ( arg ) 290CALL a:arg 291let outer = 1 292 293let j = 0 294while j < 1 295Xpath 'b' 296let j = j + 1 297function ! G1 ( arg ) 298CALL a:arg 299endfunction 300Xpath 'c' 301endwhile 302endfunction 303Xpath 'd' 304 305continue 306endif 307 308Xpath 'e' . i 309function ! F { i }( i , arg ) 310CALL a:arg 311let outer = 1 312 313if a: i == 3 314Xpath 'f' 315endif 316let k = 0 317while k < 3 318Xpath 'g' . k 319let k = k + 1 320function ! G { a: i }{ k }( arg ) 321CALL a:arg 322endfunction 323Xpath 'h' . k 324endwhile 325endfunction 326Xpath 'i' 327 328endwhile 329 330if exists ( "*G1" ) 331Xpath 'j' 332endif 333if exists ( "*F1" ) 334call F1 ( "F1" ) 335if exists ( "*G1" ) 336call G1 ( "G1" ) 337endif 338endif 339 340if exists ( "G21" ) || exists ( "G22" ) || exists ( "G23" ) 341Xpath 'k' 342endif 343if exists ( "*F2" ) 344call F2 ( 2 , "F2" ) 345if exists ( "*G21" ) 346call G21 ( "G21" ) 347endif 348if exists ( "*G22" ) 349call G22 ( "G22" ) 350endif 351if exists ( "*G23" ) 352call G23 ( "G23" ) 353endif 354endif 355 356if exists ( "G31" ) || exists ( "G32" ) || exists ( "G33" ) 357Xpath 'l' 358endif 359if exists ( "*F3" ) 360call F3 ( 3 , "F3" ) 361if exists ( "*G31" ) 362call G31 ( "G31" ) 363endif 364if exists ( "*G32" ) 365call G32 ( "G32" ) 366endif 367if exists ( "*G33" ) 368call G33 ( "G33" ) 369endif 370endif 371 372Xpath 'm' 373 374let g: test6_result = g: Xpath 375let g: test6_calls = calls 376 377unlet calls 378delfunction F1 379delfunction G1 380delfunction F2 381delfunction G21 382delfunction G22 383delfunction G23 384delfunction G31 385delfunction G32 386delfunction G33 387 388func Test_defining_functions () 389call assert_equal ( 'ade2ie3ibcg0h1g1h2g2h3fg0h1g1h2g2h3m' , g: test6_result ) 390call assert_equal ( 'F1G1F2G21G22G23F3G31G32G33' , g: test6_calls ) 391endfunc 392 393"------------------------------------------------------------------------------- 394" Test 7: Continuing on errors outside functions {{{1 395" 396" On an error outside a function, the script processing continues 397" at the line following the outermost :endif or :endwhile. When not 398" inside an :if or :while, the script processing continues at the next 399" line. 400"------------------------------------------------------------------------------- 401 402XpathINIT 403 404if 1 405Xpath 'a' 406while 1 407Xpath 'b' 408asdf 409Xpath 'c' 410break 411endwhile |Xpath 'd' 412Xpath 'e' 413endif |Xpath 'f' 414Xpath 'g' 415 416while 1 417Xpath 'h' 418if 1 419Xpath 'i' 420asdf 421Xpath 'j' 422endif |Xpath 'k' 423Xpath 'l' 424break 425endwhile |Xpath 'm' 426Xpath 'n' 427 428asdf 429Xpath 'o' 430 431asdf |Xpath 'p' 432Xpath 'q' 433 434let g: test7_result = g: Xpath 435 436func Test_error_in_script () 437call assert_equal ( 'abghinoq' , g: test7_result ) 438endfunc 439 440"------------------------------------------------------------------------------- 441" Test 8: Aborting and continuing on errors inside functions {{{1 442" 443" On an error inside a function without the "abort" attribute, the 444" script processing continues at the next line (unless the error was 445" in a :return command). On an error inside a function with the 446" "abort" attribute, the function is aborted and the script processing 447" continues after the function call; the value -1 is returned then. 448"------------------------------------------------------------------------------- 449 450XpathINIT 451 452func T8_F () 453if 1 454Xpath 'a' 455while 1 456Xpath 'b' 457asdf 458Xpath 'c' 459asdf |Xpath 'd' 460Xpath 'e' 461break 462endwhile 463Xpath 'f' 464endif |Xpath 'g' 465Xpath 'h' 466 467while 1 468Xpath 'i' 469if 1 470Xpath 'j' 471asdf 472Xpath 'k' 473asdf |Xpath 'l' 474Xpath 'm' 475endif 476Xpath 'n' 477break 478endwhile |Xpath 'o' 479Xpath 'p' 480 481return novar " returns (default return value 0) 482Xpath 'q' 483return 1 " not reached 484endfunc 485 486func T8_G () abort 487if 1 488Xpath 'r' 489while 1 490Xpath 's' 491asdf " returns -1 492Xpath 't' 493break 494endwhile 495Xpath 'v' 496endif | Xpath 'w' 497Xpath 'x' 498 499return -4 " not reached 500endfunc 501 502func T8_H () abort 503while 1 504Xpath 'A' 505if 1 506Xpath 'B' 507asdf " returns -1 508Xpath 'C' 509endif 510Xpath 'D' 511break 512endwhile | Xpath 'E' 513Xpath 'F' 514 515return -4 " not reached 516endfunc 517 518" Aborted functions (T8_G and T8_H) return -1. 519let g: test8_sum = ( T8_F () + 1 ) - 4 * T8_G () - 8 * T8_H () 520Xpath 'X' 521let g: test8_result = g: Xpath 522 523func Test_error_in_function () 524call assert_equal ( 13 , g: test8_sum ) 525call assert_equal ( 'abcefghijkmnoprsABX' , g: test8_result ) 526 527delfunction T8_F 528delfunction T8_G 529delfunction T8_H 530endfunc 531 532 533"------------------------------------------------------------------------------- 534" Test 9: Continuing after aborted functions {{{1 535" 536" When a function with the "abort" attribute is aborted due to an 537" error, the next function back in the call hierarchy without an 538" "abort" attribute continues; the value -1 is returned then. 539"------------------------------------------------------------------------------- 540 541XpathINIT 542 543func F () abort 544Xpath 'a' 545let result = G () " not aborted 546Xpath 'b' 547if result != 2 548Xpath 'c' 549endif 550return 1 551endfunc 552 553func G () " no abort attribute 554Xpath 'd' 555if H () != -1 " aborted 556Xpath 'e' 557endif 558Xpath 'f' 559return 2 560endfunc 561 562func H () abort 563Xpath 'g' 564call I () " aborted 565Xpath 'h' 566return 4 567endfunc 568 569func I () abort 570Xpath 'i' 571asdf " error 572Xpath 'j' 573return 8 574endfunc 575 576if F () != 1 577Xpath 'k' 578endif 579 580let g: test9_result = g: Xpath 581 582delfunction F 583delfunction G 584delfunction H 585delfunction I 586 587func Test_func_abort () 588call assert_equal ( 'adgifb' , g: test9_result ) 589endfunc 590 591 592"------------------------------------------------------------------------------- 593" Test 10: :if, :elseif, :while argument parsing {{{1 594" 595" A '"' or '|' in an argument expression must not be mixed up with 596" a comment or a next command after a bar. Parsing errors should 597" be recognized. 598"------------------------------------------------------------------------------- 599 600XpathINIT 601 602func MSG ( enr , emsg ) 603let english = v: lang == "C" || v: lang =~ '^[Ee]n' 604if a: enr == "" 605Xout "TODO: Add message number for:" a:emsg 606let v: errmsg = ":" . v: errmsg 607endif 608let match = 1 609if v: errmsg !~ '^' . a: enr . ':' || ( english && v: errmsg !~ a: emsg ) 610let match = 0 611if v: errmsg == "" 612Xout "Message missing." 613else 614let v: errmsg = v: errmsg -> escape ( '"' ) 615Xout "Unexpected message:" v:errmsg 616endif 617endif 618return match 619endfunc 620 621if 1 || strlen ( "\"" ) | Xpath 'a' 622Xpath 'b' 623endif 624Xpath 'c' 625 626if 0 627elseif 1 || strlen ( "\"" ) | Xpath 'd' 628Xpath 'e' 629endif 630Xpath 'f' 631 632while 1 || strlen ( "\"" ) | Xpath 'g' 633Xpath 'h' 634break 635endwhile 636Xpath 'i' 637 638let v: errmsg = "" 639if 1 || | strlen("\"") | Xpath 'j' 640Xpath 'k' 641endif 642Xpath 'l' 643if ! MSG ( 'E15' , "Invalid expression" ) 644Xpath 'm' 645endif 646 647let v: errmsg = "" 648if 0 649elseif 1 || | strlen("\"") | Xpath 'n' 650Xpath 'o' 651endif 652Xpath 'p' 653if ! MSG ( 'E15' , "Invalid expression" ) 654Xpath 'q' 655endif 656 657let v: errmsg = "" 658while 1 || | strlen("\"") | Xpath 'r' 659Xpath 's' 660break 661endwhile 662Xpath 't' 663if ! MSG ( 'E15' , "Invalid expression" ) 664Xpath 'u' 665endif 666 667let g: test10_result = g: Xpath 668delfunction MSG 669 670func Test_expr_parsing () 671call assert_equal ( 'abcdefghilpt' , g: test10_result ) 672endfunc 673 674 675"------------------------------------------------------------------------------- 676" Test 11: :if, :elseif, :while argument evaluation after abort {{{1 677" 678" When code is skipped over due to an error, the boolean argument to 679" an :if, :elseif, or :while must not be evaluated. 680"------------------------------------------------------------------------------- 681 682XpathINIT 683 684let calls = 0 685 686func P ( num ) 687let g: calls = g: calls + a: num " side effect on call 688return 0 689endfunc 690 691if 1 692Xpath 'a' 693asdf " error 694Xpath 'b' 695if P ( 1 ) " should not be called 696Xpath 'c' 697elseif ! P ( 2 ) " should not be called 698Xpath 'd' 699else 700Xpath 'e' 701endif 702Xpath 'f' 703while P ( 4 ) " should not be called 704Xpath 'g' 705endwhile 706Xpath 'h' 707endif 708Xpath 'x' 709 710let g: test11_calls = calls 711let g: test11_result = g: Xpath 712 713unlet calls 714delfunction P 715 716func Test_arg_abort () 717call assert_equal ( 0 , g: test11_calls ) 718call assert_equal ( 'ax' , g: test11_result ) 719endfunc 720 721 722"------------------------------------------------------------------------------- 723" Test 12: Expressions in braces in skipped code {{{1 724" 725" In code skipped over due to an error or inactive conditional, 726" an expression in braces as part of a variable or function name 727" should not be evaluated. 728"------------------------------------------------------------------------------- 729 730XpathINIT 731 732func NULL () 733Xpath 'a' 734return 0 735endfunc 736 737func ZERO () 738Xpath 'b' 739return 0 740endfunc 741 742func ! F0 () 743Xpath 'c' 744endfunc 745 746func ! F1 ( arg ) 747Xpath 'e' 748endfunc 749 750let V0 = 1 751 752Xpath 'f' 753echo 0 ? F { NULL () + V { ZERO ()}}() : 1 754 755Xpath 'g' 756if 0 757Xpath 'h' 758call F { NULL () + V { ZERO ()}}() 759endif 760 761Xpath 'i' 762if 1 763asdf " error 764Xpath 'j' 765call F1 ( F { NULL () + V { ZERO ()}}()) 766endif 767 768Xpath 'k' 769if 1 770asdf " error 771Xpath 'l' 772call F { NULL () + V { ZERO ()}}() 773endif 774 775let g: test12_result = g: Xpath 776 777func Test_braces_skipped () 778call assert_equal ( 'fgik' , g: test12_result ) 779endfunc 780 781 782"------------------------------------------------------------------------------- 783" Test 13: Failure in argument evaluation for :while {{{1 784" 785" A failure in the expression evaluation for the condition of a :while 786" causes the whole :while loop until the matching :endwhile being 787" ignored. Continuation is at the next following line. 788"------------------------------------------------------------------------------- 789 790XpathINIT 791 792Xpath 'a' 793while asdf 794Xpath 'b' 795while 1 796Xpath 'c' 797break 798endwhile 799Xpath 'd' 800break 801endwhile 802Xpath 'e' 803 804while asdf | Xpath 'f' | endwhile | Xpath 'g' 805Xpath 'h' 806let g: test13_result = g: Xpath 807 808func Test_while_fail () 809call assert_equal ( 'aeh' , g: test13_result ) 810endfunc 811 812 813"------------------------------------------------------------------------------- 814" Test 14: Failure in argument evaluation for :if {{{1 815" 816" A failure in the expression evaluation for the condition of an :if 817" does not cause the corresponding :else or :endif being matched to 818" a previous :if/:elseif. Neither of both branches of the failed :if 819" are executed. 820"------------------------------------------------------------------------------- 821 822XpathINIT 823 824function ! F () 825Xpath 'a' 826let x = 0 827if x " false 828Xpath 'b' 829elseif ! x " always true 830Xpath 'c' 831let x = 1 832if g: boolvar " possibly undefined 833Xpath 'd' 834else 835Xpath 'e' 836endif 837Xpath 'f' 838elseif x " never executed 839Xpath 'g' 840endif 841Xpath 'h' 842endfunction 843 844let boolvar = 1 845call F () 846Xpath '-' 847 848unlet boolvar 849call F () 850let g: test14_result = g: Xpath 851 852delfunction F 853 854func Test_if_fail () 855call assert_equal ( 'acdfh-acfh' , g: test14_result ) 856endfunc 857 858 859"------------------------------------------------------------------------------- 860" Test 15: Failure in argument evaluation for :if (bar) {{{1 861" 862" Like previous test, except that the failing :if ... | ... | :endif 863" is in a single line. 864"------------------------------------------------------------------------------- 865 866XpathINIT 867 868function ! F () 869Xpath 'a' 870let x = 0 871if x " false 872Xpath 'b' 873elseif ! x " always true 874Xpath 'c' 875let x = 1 876if g: boolvar | Xpath 'd' | else | Xpath 'e' | endif 877Xpath 'f' 878elseif x " never executed 879Xpath 'g' 880endif 881Xpath 'h' 882endfunction 883 884let boolvar = 1 885call F () 886Xpath '-' 887 888unlet boolvar 889call F () 890let g: test15_result = g: Xpath 891 892delfunction F 893 894func Test_if_bar_fail () 895call assert_equal ( 'acdfh-acfh' , g: test15_result ) 896endfunc 897 898"------------------------------------------------------------------------------- 899" Test 16: Double :else or :elseif after :else {{{1 900" 901" Multiple :elses or an :elseif after an :else are forbidden. 902"------------------------------------------------------------------------------- 903 904func T16_F () abort 905if 0 906Xpath 'a' 907else 908Xpath 'b' 909else " aborts function 910Xpath 'c' 911endif 912Xpath 'd' 913endfunc 914 915func T16_G () abort 916if 0 917Xpath 'a' 918else 919Xpath 'b' 920elseif 1 " aborts function 921Xpath 'c' 922else 923Xpath 'd' 924endif 925Xpath 'e' 926endfunc 927 928func T16_H () abort 929if 0 930Xpath 'a' 931elseif 0 932Xpath 'b' 933else 934Xpath 'c' 935else " aborts function 936Xpath 'd' 937endif 938Xpath 'e' 939endfunc 940 941func T16_I () abort 942if 0 943Xpath 'a' 944elseif 0 945Xpath 'b' 946else 947Xpath 'c' 948elseif 1 " aborts function 949Xpath 'd' 950else 951Xpath 'e' 952endif 953Xpath 'f' 954endfunc 955 956func Test_Multi_Else () 957XpathINIT 958try 959call T16_F () 960catch /E583:/ 961Xpath 'e' 962endtry 963call assert_equal ( 'be' , g: Xpath ) 964 965XpathINIT 966try 967call T16_G () 968catch /E584:/ 969Xpath 'f' 970endtry 971call assert_equal ( 'bf' , g: Xpath ) 972 973XpathINIT 974try 975call T16_H () 976catch /E583:/ 977Xpath 'f' 978endtry 979call assert_equal ( 'cf' , g: Xpath ) 980 981XpathINIT 982try 983call T16_I () 984catch /E584:/ 985Xpath 'g' 986endtry 987call assert_equal ( 'cg' , g: Xpath ) 988endfunc 989 990"------------------------------------------------------------------------------- 991" Test 17: Nesting of unmatched :if or :endif inside a :while {{{1 992" 993" The :while/:endwhile takes precedence in nesting over an unclosed 994" :if or an unopened :endif. 995"------------------------------------------------------------------------------- 996 997" While loops inside a function are continued on error. 998func T17_F () 999let loops = 3 1000while loops > 0 1001let loops -= 1 1002Xpath 'a' . loops 1003if ( loops == 1 ) 1004Xpath 'b' . loops 1005continue 1006elseif ( loops == 0 ) 1007Xpath 'c' . loops 1008break 1009elseif 1 1010Xpath 'd' . loops 1011" endif missing! 1012endwhile " :endwhile after :if 1 1013Xpath 'e' 1014endfunc 1015 1016func T17_G () 1017let loops = 2 1018while loops > 0 1019let loops -= 1 1020Xpath 'a' . loops 1021if 0 1022Xpath 'b' . loops 1023" endif missing 1024endwhile " :endwhile after :if 0 1025endfunc 1026 1027func T17_H () 1028let loops = 2 1029while loops > 0 1030let loops -= 1 1031Xpath 'a' . loops 1032" if missing! 1033endif " :endif without :if in while 1034Xpath 'b' . loops 1035endwhile 1036endfunc 1037 1038" Error continuation outside a function is at the outermost :endwhile or :endif. 1039XpathINIT 1040let v: errmsg = '' 1041let loops = 2 1042while loops > 0 1043let loops -= 1 1044Xpath 'a' . loops 1045if 0 1046Xpath 'b' . loops 1047" endif missing! Following :endwhile fails. 1048endwhile | Xpath 'c' 1049Xpath 'd' 1050call assert_match ( 'E171:' , v: errmsg ) 1051call assert_equal ( 'a1d' , g: Xpath ) 1052 1053func Test_unmatched_if_in_while () 1054XpathINIT 1055call assert_fails ( 'call T17_F()' , 'E171:' ) 1056call assert_equal ( 'a2d2a1b1a0c0e' , g: Xpath ) 1057 1058XpathINIT 1059call assert_fails ( 'call T17_G()' , 'E171:' ) 1060call assert_equal ( 'a1a0' , g: Xpath ) 1061 1062XpathINIT 1063call assert_fails ( 'call T17_H()' , 'E580:' ) 1064call assert_equal ( 'a1b1a0b0' , g: Xpath ) 1065endfunc 1066 1067"------------------------------------------------------------------------------- 1068" Test 18: Interrupt (Ctrl-C pressed) {{{1 1069" 1070" On an interrupt, the script processing is terminated immediately. 1071"------------------------------------------------------------------------------- 1072 1073func Test_interrupt_while_if () 1074let test =<< trim [CODE] 1075try 1076if 1 1077Xpath 'a' 1078while 1 1079Xpath 'b' 1080if 1 1081Xpath 'c' 1082call interrupt() 1083call assert_report('should not get here') 1084break 1085finish 1086endif | call assert_report('should not get here') 1087call assert_report('should not get here') 1088endwhile | call assert_report('should not get here') 1089call assert_report('should not get here') 1090endif | call assert_report('should not get here') 1091call assert_report('should not get here') 1092catch /^Vim:Interrupt$/ 1093Xpath 'd' 1094endtry | Xpath 'e' 1095Xpath 'f' 1096[CODE] 1097let verify =<< trim [CODE] 1098call assert_equal('abcdef', g:Xpath) 1099[CODE] 1100call RunInNewVim ( test , verify ) 1101endfunc 1102 1103func Test_interrupt_try () 1104let test =<< trim [CODE] 1105try 1106try 1107Xpath 'a' 1108call interrupt() 1109call assert_report('should not get here') 1110endtry | call assert_report('should not get here') 1111call assert_report('should not get here') 1112catch /^Vim:Interrupt$/ 1113Xpath 'b' 1114endtry | Xpath 'c' 1115Xpath 'd' 1116[CODE] 1117let verify =<< trim [CODE] 1118call assert_equal('abcd', g:Xpath) 1119[CODE] 1120call RunInNewVim ( test , verify ) 1121endfunc 1122 1123func Test_interrupt_func_while_if () 1124let test =<< trim [CODE] 1125func F() 1126if 1 1127Xpath 'a' 1128while 1 1129Xpath 'b' 1130if 1 1131Xpath 'c' 1132call interrupt() 1133call assert_report('should not get here') 1134break 1135return 1136endif | call assert_report('should not get here') 1137call assert_report('should not get here') 1138endwhile | call assert_report('should not get here') 1139call assert_report('should not get here') 1140endif | call assert_report('should not get here') 1141call assert_report('should not get here') 1142endfunc 1143 1144Xpath 'd' 1145try 1146call F() | call assert_report('should not get here') 1147catch /^Vim:Interrupt$/ 1148Xpath 'e' 1149endtry | Xpath 'f' 1150Xpath 'g' 1151[CODE] 1152let verify =<< trim [CODE] 1153call assert_equal('dabcefg', g:Xpath) 1154[CODE] 1155call RunInNewVim ( test , verify ) 1156endfunc 1157 1158func Test_interrupt_func_try () 1159let test =<< trim [CODE] 1160func G() 1161try 1162Xpath 'a' 1163call interrupt() 1164call assert_report('should not get here') 1165endtry | call assert_report('should not get here') 1166call assert_report('should not get here') 1167endfunc 1168 1169Xpath 'b' 1170try 1171call G() | call assert_report('should not get here') 1172catch /^Vim:Interrupt$/ 1173Xpath 'c' 1174endtry | Xpath 'd' 1175Xpath 'e' 1176[CODE] 1177let verify =<< trim [CODE] 1178call assert_equal('bacde', g:Xpath) 1179[CODE] 1180call RunInNewVim ( test , verify ) 1181endfunc 1182 1183"------------------------------------------------------------------------------- 1184" Test 19: Aborting on errors inside :try/:endtry {{{1 1185" 1186" An error in a command dynamically enclosed in a :try/:endtry region 1187" aborts script processing immediately. It does not matter whether 1188" the failing command is outside or inside a function and whether a 1189" function has an "abort" attribute. 1190"------------------------------------------------------------------------------- 1191 1192func Test_try_error_abort_1 () 1193let test =<< trim [CODE] 1194func F() abort 1195Xpath 'a' 1196asdf 1197call assert_report('should not get here') 1198endfunc 1199 1200try 1201Xpath 'b' 1202call F() 1203call assert_report('should not get here') 1204endtry | call assert_report('should not get here') 1205call assert_report('should not get here') 1206[CODE] 1207let verify =<< trim [CODE] 1208call assert_equal('ba', g:Xpath) 1209[CODE] 1210call RunInNewVim ( test , verify ) 1211endfunc 1212 1213func Test_try_error_abort_2 () 1214let test =<< trim [CODE] 1215func G() 1216Xpath 'a' 1217asdf 1218call assert_report('should not get here') 1219endfunc 1220 1221try 1222Xpath 'b' 1223call G() 1224call assert_report('should not get here') 1225endtry | call assert_report('should not get here') 1226call assert_report('should not get here') 1227[CODE] 1228let verify =<< trim [CODE] 1229call assert_equal('ba', g:Xpath) 1230[CODE] 1231call RunInNewVim ( test , verify ) 1232endfunc 1233 1234func Test_try_error_abort_3 () 1235let test =<< trim [CODE] 1236try 1237Xpath 'a' 1238asdf 1239call assert_report('should not get here') 1240endtry | call assert_report('should not get here') 1241call assert_report('should not get here') 1242[CODE] 1243let verify =<< trim [CODE] 1244call assert_equal('a', g:Xpath) 1245[CODE] 1246call RunInNewVim ( test , verify ) 1247endfunc 1248 1249func Test_try_error_abort_4 () 1250let test =<< trim [CODE] 1251if 1 1252try 1253Xpath 'a' 1254asdf 1255call assert_report('should not get here') 1256endtry | call assert_report('should not get here') 1257endif | call assert_report('should not get here') 1258call assert_report('should not get here') 1259[CODE] 1260let verify =<< trim [CODE] 1261call assert_equal('a', g:Xpath) 1262[CODE] 1263call RunInNewVim ( test , verify ) 1264endfunc 1265 1266func Test_try_error_abort_5 () 1267let test =<< trim [CODE] 1268let p = 1 1269while p 1270let p = 0 1271try 1272Xpath 'a' 1273asdf 1274call assert_report('should not get here') 1275endtry | call assert_report('should not get here') 1276endwhile | call assert_report('should not get here') 1277call assert_report('should not get here') 1278[CODE] 1279let verify =<< trim [CODE] 1280call assert_equal('a', g:Xpath) 1281[CODE] 1282call RunInNewVim ( test , verify ) 1283endfunc 1284 1285func Test_try_error_abort_6 () 1286let test =<< trim [CODE] 1287let p = 1 1288Xpath 'a' 1289while p 1290Xpath 'b' 1291let p = 0 1292try 1293Xpath 'c' 1294endwhile | call assert_report('should not get here') 1295call assert_report('should not get here') 1296[CODE] 1297let verify =<< trim [CODE] 1298call assert_equal('abc', g:Xpath) 1299[CODE] 1300call RunInNewVim ( test , verify ) 1301endfunc 1302 1303"------------------------------------------------------------------------------- 1304" Test 20: Aborting on errors after :try/:endtry {{{1 1305" 1306" When an error occurs after the last active :try/:endtry region has 1307" been left, termination behavior is as if no :try/:endtry has been 1308" seen. 1309"------------------------------------------------------------------------------- 1310 1311func Test_error_after_try_1 () 1312let test =<< trim [CODE] 1313let p = 1 1314while p 1315let p = 0 1316Xpath 'a' 1317try 1318Xpath 'b' 1319endtry 1320asdf 1321call assert_report('should not get here') 1322endwhile | call assert_report('should not get here') 1323Xpath 'c' 1324[CODE] 1325let verify =<< trim [CODE] 1326call assert_equal('abc', g:Xpath) 1327[CODE] 1328call RunInNewVim ( test , verify ) 1329endfunc 1330 1331func Test_error_after_try_2 () 1332let test =<< trim [CODE] 1333while 1 1334try 1335Xpath 'a' 1336break 1337call assert_report('should not get here') 1338endtry 1339endwhile 1340Xpath 'b' 1341asdf 1342Xpath 'c' 1343[CODE] 1344let verify =<< trim [CODE] 1345call assert_equal('abc', g:Xpath) 1346[CODE] 1347call RunInNewVim ( test , verify ) 1348endfunc 1349 1350func Test_error_after_try_3 () 1351let test =<< trim [CODE] 1352while 1 1353try 1354Xpath 'a' 1355break 1356call assert_report('should not get here') 1357finally 1358Xpath 'b' 1359endtry 1360endwhile 1361Xpath 'c' 1362asdf 1363Xpath 'd' 1364[CODE] 1365let verify =<< trim [CODE] 1366call assert_equal('abcd', g:Xpath) 1367[CODE] 1368call RunInNewVim ( test , verify ) 1369endfunc 1370 1371func Test_error_after_try_4 () 1372let test =<< trim [CODE] 1373while 1 1374try 1375Xpath 'a' 1376finally 1377Xpath 'b' 1378break 1379call assert_report('should not get here') 1380endtry 1381endwhile 1382Xpath 'c' 1383asdf 1384Xpath 'd' 1385[CODE] 1386let verify =<< trim [CODE] 1387call assert_equal('abcd', g:Xpath) 1388[CODE] 1389call RunInNewVim ( test , verify ) 1390endfunc 1391 1392func Test_error_after_try_5 () 1393let test =<< trim [CODE] 1394let p = 1 1395while p 1396let p = 0 1397try 1398Xpath 'a' 1399continue 1400call assert_report('should not get here') 1401endtry 1402endwhile 1403Xpath 'b' 1404asdf 1405Xpath 'c' 1406[CODE] 1407let verify =<< trim [CODE] 1408call assert_equal('abc', g:Xpath) 1409[CODE] 1410call RunInNewVim ( test , verify ) 1411endfunc 1412 1413func Test_error_after_try_6 () 1414let test =<< trim [CODE] 1415let p = 1 1416while p 1417let p = 0 1418try 1419Xpath 'a' 1420continue 1421call assert_report('should not get here') 1422finally 1423Xpath 'b' 1424endtry 1425endwhile 1426Xpath 'c' 1427asdf 1428Xpath 'd' 1429[CODE] 1430let verify =<< trim [CODE] 1431call assert_equal('abcd', g:Xpath) 1432[CODE] 1433call RunInNewVim ( test , verify ) 1434endfunc 1435 1436func Test_error_after_try_7 () 1437let test =<< trim [CODE] 1438let p = 1 1439while p 1440let p = 0 1441try 1442Xpath 'a' 1443finally 1444Xpath 'b' 1445continue 1446call assert_report('should not get here') 1447endtry 1448endwhile 1449Xpath 'c' 1450asdf 1451Xpath 'd' 1452[CODE] 1453let verify =<< trim [CODE] 1454call assert_equal('abcd', g:Xpath) 1455[CODE] 1456call RunInNewVim ( test , verify ) 1457endfunc 1458 1459"------------------------------------------------------------------------------- 1460" Test 21: :finally for :try after :continue/:break/:return/:finish {{{1 1461" 1462" If a :try conditional stays inactive due to a preceding :continue, 1463" :break, :return, or :finish, its :finally clause should not be 1464" executed. 1465"------------------------------------------------------------------------------- 1466 1467func Test_finally_after_loop_ctrl_statement () 1468let test =<< trim [CODE] 1469func F() 1470let loops = 2 1471while loops > 0 1472XloopNEXT 1473let loops = loops - 1 1474try 1475if loops == 1 1476Xloop 'a' 1477continue 1478call assert_report('should not get here') 1479elseif loops == 0 1480Xloop 'b' 1481break 1482call assert_report('should not get here') 1483endif 1484 1485try " inactive 1486call assert_report('should not get here') 1487finally 1488call assert_report('should not get here') 1489endtry 1490finally 1491Xloop 'c' 1492endtry 1493call assert_report('should not get here') 1494endwhile 1495 1496try 1497Xpath 'd' 1498return 1499call assert_report('should not get here') 1500try " inactive 1501call assert_report('should not get here') 1502finally 1503call assert_report('should not get here') 1504endtry 1505finally 1506Xpath 'e' 1507endtry 1508call assert_report('should not get here') 1509endfunc 1510 1511try 1512Xpath 'f' 1513call F() 1514Xpath 'g' 1515finish 1516call assert_report('should not get here') 1517try " inactive 1518call assert_report('should not get here') 1519finally 1520call assert_report('should not get here') 1521endtry 1522finally 1523Xpath 'h' 1524endtry 1525call assert_report('should not get here') 1526[CODE] 1527let verify =<< trim [CODE] 1528call assert_equal('fa2c2b3c3degh', g:Xpath) 1529[CODE] 1530call RunInNewVim ( test , verify ) 1531endfunc 1532 1533"------------------------------------------------------------------------------- 1534" Test 22: :finally for a :try after an error/interrupt/:throw {{{1 1535" 1536" If a :try conditional stays inactive due to a preceding error or 1537" interrupt or :throw, its :finally clause should not be executed. 1538"------------------------------------------------------------------------------- 1539 1540func Test_finally_after_error_in_func () 1541let test =<< trim [CODE] 1542func Error() 1543try 1544Xpath 'b' 1545asdf " aborting error, triggering error exception 1546call assert_report('should not get here') 1547endtry 1548call assert_report('should not get here') 1549endfunc 1550 1551Xpath 'a' 1552call Error() 1553call assert_report('should not get here') 1554 1555if 1 " not active due to error 1556try " not active since :if inactive 1557call assert_report('should not get here') 1558finally 1559call assert_report('should not get here') 1560endtry 1561endif 1562 1563try " not active due to error 1564call assert_report('should not get here') 1565finally 1566call assert_report('should not get here') 1567endtry 1568[CODE] 1569let verify =<< trim [CODE] 1570call assert_equal('ab', g:Xpath) 1571[CODE] 1572call RunInNewVim ( test , verify ) 1573endfunc 1574 1575func Test_finally_after_interrupt () 1576let test =<< trim [CODE] 1577func Interrupt() 1578try 1579Xpath 'a' 1580call interrupt() " triggering interrupt exception 1581call assert_report('should not get here') 1582endtry 1583endfunc 1584 1585Xpath 'b' 1586try 1587call Interrupt() 1588catch /^Vim:Interrupt$/ 1589Xpath 'c' 1590finish 1591endtry 1592call assert_report('should not get here') 1593 1594if 1 " not active due to interrupt 1595try " not active since :if inactive 1596call assert_report('should not get here') 1597finally 1598call assert_report('should not get here') 1599endtry 1600endif 1601 1602try " not active due to interrupt 1603call assert_report('should not get here') 1604finally 1605call assert_report('should not get here') 1606endtry 1607[CODE] 1608let verify =<< trim [CODE] 1609call assert_equal('bac', g:Xpath) 1610[CODE] 1611call RunInNewVim ( test , verify ) 1612endfunc 1613 1614func Test_finally_after_throw () 1615let test =<< trim [CODE] 1616func Throw() 1617Xpath 'a' 1618throw 'xyz' 1619endfunc 1620 1621Xpath 'b' 1622call Throw() 1623call assert_report('should not get here') 1624 1625if 1 " not active due to :throw 1626try " not active since :if inactive 1627call assert_report('should not get here') 1628finally 1629call assert_report('should not get here') 1630endtry 1631endif 1632 1633try " not active due to :throw 1634call assert_report('should not get here') 1635finally 1636call assert_report('should not get here') 1637endtry 1638[CODE] 1639let verify =<< trim [CODE] 1640call assert_equal('ba', g:Xpath) 1641[CODE] 1642call RunInNewVim ( test , verify ) 1643endfunc 1644 1645"------------------------------------------------------------------------------- 1646" Test 23: :catch clauses for a :try after a :throw {{{1 1647" 1648" If a :try conditional stays inactive due to a preceding :throw, 1649" none of its :catch clauses should be executed. 1650"------------------------------------------------------------------------------- 1651 1652func Test_catch_after_throw () 1653let test =<< trim [CODE] 1654try 1655Xpath 'a' 1656throw "xyz" 1657call assert_report('should not get here') 1658 1659if 1 " not active due to :throw 1660try " not active since :if inactive 1661call assert_report('should not get here') 1662catch /xyz/ 1663call assert_report('should not get here') 1664endtry 1665endif 1666catch /xyz/ 1667Xpath 'b' 1668endtry 1669 1670Xpath 'c' 1671throw "abc" 1672call assert_report('should not get here') 1673 1674try " not active due to :throw 1675call assert_report('should not get here') 1676catch /abc/ 1677call assert_report('should not get here') 1678endtry 1679[CODE] 1680let verify =<< trim [CODE] 1681call assert_equal('abc', g:Xpath) 1682[CODE] 1683call RunInNewVim ( test , verify ) 1684endfunc 1685 1686"------------------------------------------------------------------------------- 1687" Test 24: :endtry for a :try after a :throw {{{1 1688" 1689" If a :try conditional stays inactive due to a preceding :throw, 1690" its :endtry should not rethrow the exception to the next surrounding 1691" active :try conditional. 1692"------------------------------------------------------------------------------- 1693 1694func Test_endtry_after_throw () 1695let test =<< trim [CODE] 1696try " try 1 1697try " try 2 1698Xpath 'a' 1699throw "xyz" " makes try 2 inactive 1700call assert_report('should not get here') 1701 1702try " try 3 1703call assert_report('should not get here') 1704endtry " no rethrow to try 1 1705catch /xyz/ " should catch although try 2 inactive 1706Xpath 'b' 1707endtry 1708catch /xyz/ " try 1 active, but exception already caught 1709call assert_report('should not get here') 1710endtry 1711Xpath 'c' 1712[CODE] 1713let verify =<< trim [CODE] 1714call assert_equal('abc', g:Xpath) 1715[CODE] 1716call RunInNewVim ( test , verify ) 1717endfunc 1718 1719"------------------------------------------------------------------------------- 1720" Test 27: Executing :finally clauses after :return {{{1 1721" 1722" For a :return command dynamically enclosed in a :try/:endtry region, 1723" :finally clauses are executed and the called function is ended. 1724"------------------------------------------------------------------------------- 1725 1726func T27_F () 1727try 1728Xpath 'a' 1729try 1730Xpath 'b' 1731return 1732call assert_report ( 'should not get here' ) 1733finally 1734Xpath 'c' 1735endtry 1736Xpath 'd' 1737finally 1738Xpath 'e' 1739endtry 1740call assert_report ( 'should not get here' ) 1741endfunc 1742 1743func T27_G () 1744try 1745Xpath 'f' 1746return 1747call assert_report ( 'should not get here' ) 1748finally 1749Xpath 'g' 1750call T27_F () 1751Xpath 'h' 1752endtry 1753call assert_report ( 'should not get here' ) 1754endfunc 1755 1756func T27_H () 1757try 1758Xpath 'i' 1759call T27_G () 1760Xpath 'j' 1761finally 1762Xpath 'k' 1763return 1764call assert_report ( 'should not get here' ) 1765endtry 1766call assert_report ( 'should not get here' ) 1767endfunction 1768 1769func Test_finally_after_return () 1770XpathINIT 1771try 1772Xpath 'l' 1773call T27_H () 1774Xpath 'm' 1775finally 1776Xpath 'n' 1777endtry 1778call assert_equal ( 'lifgabcehjkmn' , g: Xpath ) 1779endfunc 1780 1781"------------------------------------------------------------------------------- 1782" Test 28: Executing :finally clauses after :finish {{{1 1783" 1784" For a :finish command dynamically enclosed in a :try/:endtry region, 1785" :finally clauses are executed and the sourced file is finished. 1786" 1787" This test executes the bodies of the functions F, G, and H from the 1788" previous test as script files (:return replaced by :finish). 1789"------------------------------------------------------------------------------- 1790 1791func Test_finally_after_finish () 1792XpathINIT 1793 1794let scriptF = MakeScript ( "T27_F" ) 1795let scriptG = MakeScript ( "T27_G" , scriptF ) 1796let scriptH = MakeScript ( "T27_H" , scriptG ) 1797 1798try 1799Xpath 'A' 1800exec "source" scriptH 1801Xpath 'B' 1802finally 1803Xpath 'C' 1804endtry 1805Xpath 'D' 1806call assert_equal ( 'AifgabcehjkBCD' , g: Xpath ) 1807call delete ( scriptF ) 1808call delete ( scriptG ) 1809call delete ( scriptH ) 1810endfunc 1811 1812"------------------------------------------------------------------------------- 1813" Test 29: Executing :finally clauses on errors {{{1 1814" 1815" After an error in a command dynamically enclosed in a :try/:endtry 1816" region, :finally clauses are executed and the script processing is 1817" terminated. 1818"------------------------------------------------------------------------------- 1819 1820func Test_finally_after_error_1 () 1821let test =<< trim [CODE] 1822func F() 1823while 1 1824try 1825Xpath 'a' 1826while 1 1827try 1828Xpath 'b' 1829asdf " error 1830call assert_report('should not get here') 1831finally 1832Xpath 'c' 1833endtry | call assert_report('should not get here') 1834call assert_report('should not get here') 1835break 1836endwhile 1837call assert_report('should not get here') 1838finally 1839Xpath 'd' 1840endtry | call assert_report('should not get here') 1841call assert_report('should not get here') 1842break 1843endwhile 1844call assert_report('should not get here') 1845endfunc 1846 1847while 1 1848try 1849Xpath 'e' 1850while 1 1851call F() 1852call assert_report('should not get here') 1853break 1854endwhile | call assert_report('should not get here') 1855call assert_report('should not get here') 1856finally 1857Xpath 'f' 1858endtry | call assert_report('should not get here') 1859endwhile | call assert_report('should not get here') 1860call assert_report('should not get here') 1861[CODE] 1862let verify =<< trim [CODE] 1863call assert_equal('eabcdf', g:Xpath) 1864[CODE] 1865call RunInNewVim ( test , verify ) 1866endfunc 1867 1868func Test_finally_after_error_2 () 1869let test =<< trim [CODE] 1870func G() abort 1871if 1 1872try 1873Xpath 'a' 1874asdf " error 1875call assert_report('should not get here') 1876finally 1877Xpath 'b' 1878endtry | Xpath 'c' 1879endif | Xpath 'd' 1880call assert_report('should not get here') 1881endfunc 1882 1883if 1 1884try 1885Xpath 'e' 1886call G() 1887call assert_report('should not get here') 1888finally 1889Xpath 'f' 1890endtry | call assert_report('should not get here') 1891endif | call assert_report('should not get here') 1892call assert_report('should not get here') 1893[CODE] 1894let verify =<< trim [CODE] 1895call assert_equal('eabf', g:Xpath) 1896[CODE] 1897call RunInNewVim ( test , verify ) 1898endfunc 1899 1900"------------------------------------------------------------------------------- 1901" Test 30: Executing :finally clauses on interrupt {{{1 1902" 1903" After an interrupt in a command dynamically enclosed in 1904" a :try/:endtry region, :finally clauses are executed and the 1905" script processing is terminated. 1906"------------------------------------------------------------------------------- 1907 1908func Test_finally_on_interrupt () 1909let test =<< trim [CODE] 1910func F() 1911try 1912Xloop 'a' 1913call interrupt() 1914call assert_report('should not get here') 1915finally 1916Xloop 'b' 1917endtry 1918call assert_report('should not get here') 1919endfunc 1920 1921try 1922try 1923Xpath 'c' 1924try 1925Xpath 'd' 1926call interrupt() 1927call assert_report('should not get here') 1928finally 1929Xpath 'e' 1930try 1931Xpath 'f' 1932try 1933Xpath 'g' 1934finally 1935Xpath 'h' 1936try 1937Xpath 'i' 1938call interrupt() 1939call assert_report('should not get here') 1940endtry 1941call assert_report('should not get here') 1942endtry 1943call assert_report('should not get here') 1944endtry 1945call assert_report('should not get here') 1946endtry 1947call assert_report('should not get here') 1948finally 1949Xpath 'j' 1950try 1951Xpath 'k' 1952call F() 1953call assert_report('should not get here') 1954finally 1955Xpath 'l' 1956try 1957Xpath 'm' 1958XloopNEXT 1959ExecAsScript F 1960call assert_report('should not get here') 1961finally 1962Xpath 'n' 1963endtry 1964call assert_report('should not get here') 1965endtry 1966call assert_report('should not get here') 1967endtry 1968call assert_report('should not get here') 1969catch /^Vim:Interrupt$/ 1970Xpath 'o' 1971endtry 1972[CODE] 1973let verify =<< trim [CODE] 1974call assert_equal('cdefghijka1b1lma2b2no', g:Xpath) 1975[CODE] 1976call RunInNewVim ( test , verify ) 1977endfunc 1978 1979"------------------------------------------------------------------------------- 1980" Test 31: Executing :finally clauses after :throw {{{1 1981" 1982" After a :throw dynamically enclosed in a :try/:endtry region, 1983" :finally clauses are executed and the script processing is 1984" terminated. 1985"------------------------------------------------------------------------------- 1986 1987func Test_finally_after_throw_2 () 1988let test =<< trim [CODE] 1989func F() 1990try 1991Xloop 'a' 1992throw "exception" 1993call assert_report('should not get here') 1994finally 1995Xloop 'b' 1996endtry 1997call assert_report('should not get here') 1998endfunc 1999 2000try 2001Xpath 'c' 2002try 2003Xpath 'd' 2004throw "exception" 2005call assert_report('should not get here') 2006finally 2007Xpath 'e' 2008try 2009Xpath 'f' 2010try 2011Xpath 'g' 2012finally 2013Xpath 'h' 2014try 2015Xpath 'i' 2016throw "exception" 2017call assert_report('should not get here') 2018endtry 2019call assert_report('should not get here') 2020endtry 2021call assert_report('should not get here') 2022endtry 2023call assert_report('should not get here') 2024endtry 2025call assert_report('should not get here') 2026finally 2027Xpath 'j' 2028try 2029Xpath 'k' 2030call F() 2031call assert_report('should not get here') 2032finally 2033Xpath 'l' 2034try 2035Xpath 'm' 2036XloopNEXT 2037ExecAsScript F 2038call assert_report('should not get here') 2039finally 2040Xpath 'n' 2041endtry 2042call assert_report('should not get here') 2043endtry 2044call assert_report('should not get here') 2045endtry 2046call assert_report('should not get here') 2047[CODE] 2048let verify =<< trim [CODE] 2049call assert_equal('cdefghijka1b1lma2b2n', g:Xpath) 2050[CODE] 2051call RunInNewVim ( test , verify ) 2052endfunc 2053 2054"------------------------------------------------------------------------------- 2055" Test 34: :finally reason discarded by :continue {{{1 2056" 2057" When a :finally clause is executed due to a :continue, :break, 2058" :return, :finish, error, interrupt or :throw, the jump reason is 2059" discarded by a :continue in the finally clause. 2060"------------------------------------------------------------------------------- 2061 2062func Test_finally_after_continue () 2063let test =<< trim [CODE] 2064func C(jump) 2065XloopNEXT 2066let loop = 0 2067while loop < 2 2068let loop = loop + 1 2069if loop == 1 2070try 2071if a:jump == "continue" 2072continue 2073elseif a:jump == "break" 2074break 2075elseif a:jump == "return" || a:jump == "finish" 2076return 2077elseif a:jump == "error" 2078asdf 2079elseif a:jump == "interrupt" 2080call interrupt() 2081let dummy = 0 2082elseif a:jump == "throw" 2083throw "abc" 2084endif 2085finally 2086continue " discards jump that caused the :finally 2087call assert_report('should not get here') 2088endtry 2089call assert_report('should not get here') 2090elseif loop == 2 2091Xloop 'a' 2092endif 2093endwhile 2094endfunc 2095 2096call C("continue") 2097Xpath 'b' 2098call C("break") 2099Xpath 'c' 2100call C("return") 2101Xpath 'd' 2102let g:jump = "finish" 2103ExecAsScript C 2104unlet g:jump 2105Xpath 'e' 2106try 2107call C("error") 2108Xpath 'f' 2109finally 2110Xpath 'g' 2111try 2112call C("interrupt") 2113Xpath 'h' 2114finally 2115Xpath 'i' 2116call C("throw") 2117Xpath 'j' 2118endtry 2119endtry 2120Xpath 'k' 2121[CODE] 2122let verify =<< trim [CODE] 2123call assert_equal('a2ba3ca4da5ea6fga7hia8jk', g:Xpath) 2124[CODE] 2125call RunInNewVim ( test , verify ) 2126endfunc 2127 2128"------------------------------------------------------------------------------- 2129" Test 35: :finally reason discarded by :break {{{1 2130" 2131" When a :finally clause is executed due to a :continue, :break, 2132" :return, :finish, error, interrupt or :throw, the jump reason is 2133" discarded by a :break in the finally clause. 2134"------------------------------------------------------------------------------- 2135 2136func Test_finally_discard_by_break () 2137let test =<< trim [CODE] 2138func B(jump) 2139XloopNEXT 2140let loop = 0 2141while loop < 2 2142let loop = loop + 1 2143if loop == 1 2144try 2145if a:jump == "continue" 2146continue 2147elseif a:jump == "break" 2148break 2149elseif a:jump == "return" || a:jump == "finish" 2150return 2151elseif a:jump == "error" 2152asdf 2153elseif a:jump == "interrupt" 2154call interrupt() 2155let dummy = 0 2156elseif a:jump == "throw" 2157throw "abc" 2158endif 2159finally 2160break " discards jump that caused the :finally 2161call assert_report('should not get here') 2162endtry 2163elseif loop == 2 2164call assert_report('should not get here') 2165endif 2166endwhile 2167Xloop 'a' 2168endfunc 2169 2170call B("continue") 2171Xpath 'b' 2172call B("break") 2173Xpath 'c' 2174call B("return") 2175Xpath 'd' 2176let g:jump = "finish" 2177ExecAsScript B 2178unlet g:jump 2179Xpath 'e' 2180try 2181call B("error") 2182Xpath 'f' 2183finally 2184Xpath 'g' 2185try 2186call B("interrupt") 2187Xpath 'h' 2188finally 2189Xpath 'i' 2190call B("throw") 2191Xpath 'j' 2192endtry 2193endtry 2194Xpath 'k' 2195[CODE] 2196let verify =<< trim [CODE] 2197call assert_equal('a2ba3ca4da5ea6fga7hia8jk', g:Xpath) 2198[CODE] 2199call RunInNewVim ( test , verify ) 2200endfunc 2201 2202"------------------------------------------------------------------------------- 2203" Test 36: :finally reason discarded by :return {{{1 2204" 2205" When a :finally clause is executed due to a :continue, :break, 2206" :return, :finish, error, interrupt or :throw, the jump reason is 2207" discarded by a :return in the finally clause. 2208"------------------------------------------------------------------------------- 2209 2210func Test_finally_discard_by_return () 2211let test =<< trim [CODE] 2212func R(jump, retval) abort 2213let loop = 0 2214while loop < 2 2215let loop = loop + 1 2216if loop == 1 2217try 2218if a:jump == "continue" 2219continue 2220elseif a:jump == "break" 2221break 2222elseif a:jump == "return" 2223return 2224elseif a:jump == "error" 2225asdf 2226elseif a:jump == "interrupt" 2227call interrupt() 2228let dummy = 0 2229elseif a:jump == "throw" 2230throw "abc" 2231endif 2232finally 2233return a:retval " discards jump that caused the :finally 2234call assert_report('should not get here') 2235endtry 2236elseif loop == 2 2237call assert_report('should not get here') 2238endif 2239endwhile 2240call assert_report('should not get here') 2241endfunc 2242 2243let sum = -R("continue", -8) 2244Xpath 'a' 2245let sum = sum - R("break", -16) 2246Xpath 'b' 2247let sum = sum - R("return", -32) 2248Xpath 'c' 2249try 2250let sum = sum - R("error", -64) 2251Xpath 'd' 2252finally 2253Xpath 'e' 2254try 2255let sum = sum - R("interrupt", -128) 2256Xpath 'f' 2257finally 2258Xpath 'g' 2259let sum = sum - R("throw", -256) 2260Xpath 'h' 2261endtry 2262endtry 2263Xpath 'i' 2264 2265let expected = 8 + 16 + 32 + 64 + 128 + 256 2266call assert_equal(sum, expected) 2267[CODE] 2268let verify =<< trim [CODE] 2269call assert_equal('abcdefghi', g:Xpath) 2270[CODE] 2271call RunInNewVim ( test , verify ) 2272endfunc 2273 2274"------------------------------------------------------------------------------- 2275" Test 37: :finally reason discarded by :finish {{{1 2276" 2277" When a :finally clause is executed due to a :continue, :break, 2278" :return, :finish, error, interrupt or :throw, the jump reason is 2279" discarded by a :finish in the finally clause. 2280"------------------------------------------------------------------------------- 2281 2282func Test_finally_discard_by_finish () 2283let test =<< trim [CODE] 2284func F(jump) " not executed as function, transformed to a script 2285let loop = 0 2286while loop < 2 2287let loop = loop + 1 2288if loop == 1 2289try 2290if a:jump == "continue" 2291continue 2292elseif a:jump == "break" 2293break 2294elseif a:jump == "finish" 2295finish 2296elseif a:jump == "error" 2297asdf 2298elseif a:jump == "interrupt" 2299call interrupt() 2300let dummy = 0 2301elseif a:jump == "throw" 2302throw "abc" 2303endif 2304finally 2305finish " discards jump that caused the :finally 2306call assert_report('should not get here') 2307endtry 2308elseif loop == 2 2309call assert_report('should not get here') 2310endif 2311endwhile 2312call assert_report('should not get here') 2313endfunc 2314 2315let scriptF = MakeScript("F") 2316delfunction F 2317 2318let g:jump = "continue" 2319exec "source" scriptF 2320Xpath 'a' 2321let g:jump = "break" 2322exec "source" scriptF 2323Xpath 'b' 2324let g:jump = "finish" 2325exec "source" scriptF 2326Xpath 'c' 2327try 2328let g:jump = "error" 2329exec "source" scriptF 2330Xpath 'd' 2331finally 2332Xpath 'e' 2333try 2334let g:jump = "interrupt" 2335exec "source" scriptF 2336Xpath 'f' 2337finally 2338Xpath 'g' 2339try 2340let g:jump = "throw" 2341exec "source" scriptF 2342Xpath 'h' 2343finally 2344Xpath 'i' 2345endtry 2346endtry 2347endtry 2348unlet g:jump 2349call delete(scriptF) 2350[CODE] 2351let verify =<< trim [CODE] 2352call assert_equal('abcdefghi', g:Xpath) 2353[CODE] 2354call RunInNewVim ( test , verify ) 2355endfunc 2356 2357"------------------------------------------------------------------------------- 2358" Test 38: :finally reason discarded by an error {{{1 2359" 2360" When a :finally clause is executed due to a :continue, :break, 2361" :return, :finish, error, interrupt or :throw, the jump reason is 2362" discarded by an error in the finally clause. 2363"------------------------------------------------------------------------------- 2364 2365func Test_finally_discard_by_error () 2366let test =<< trim [CODE] 2367func E(jump) 2368let loop = 0 2369while loop < 2 2370let loop = loop + 1 2371if loop == 1 2372try 2373if a:jump == "continue" 2374continue 2375elseif a:jump == "break" 2376break 2377elseif a:jump == "return" || a:jump == "finish" 2378return 2379elseif a:jump == "error" 2380asdf 2381elseif a:jump == "interrupt" 2382call interrupt() 2383let dummy = 0 2384elseif a:jump == "throw" 2385throw "abc" 2386endif 2387finally 2388asdf " error; discards jump that caused the :finally 2389endtry 2390elseif loop == 2 2391call assert_report('should not get here') 2392endif 2393endwhile 2394call assert_report('should not get here') 2395endfunc 2396 2397try 2398Xpath 'a' 2399call E("continue") 2400call assert_report('should not get here') 2401finally 2402try 2403Xpath 'b' 2404call E("break") 2405call assert_report('should not get here') 2406finally 2407try 2408Xpath 'c' 2409call E("return") 2410call assert_report('should not get here') 2411finally 2412try 2413Xpath 'd' 2414let g:jump = "finish" 2415ExecAsScript E 2416call assert_report('should not get here') 2417finally 2418unlet g:jump 2419try 2420Xpath 'e' 2421call E("error") 2422call assert_report('should not get here') 2423finally 2424try 2425Xpath 'f' 2426call E("interrupt") 2427call assert_report('should not get here') 2428finally 2429try 2430Xpath 'g' 2431call E("throw") 2432call assert_report('should not get here') 2433finally 2434Xpath 'h' 2435delfunction E 2436endtry 2437endtry 2438endtry 2439endtry 2440endtry 2441endtry 2442endtry 2443call assert_report('should not get here') 2444[CODE] 2445let verify =<< trim [CODE] 2446call assert_equal('abcdefgh', g:Xpath) 2447[CODE] 2448call RunInNewVim ( test , verify ) 2449endfunc 2450 2451"------------------------------------------------------------------------------- 2452" Test 39: :finally reason discarded by an interrupt {{{1 2453" 2454" When a :finally clause is executed due to a :continue, :break, 2455" :return, :finish, error, interrupt or :throw, the jump reason is 2456" discarded by an interrupt in the finally clause. 2457"------------------------------------------------------------------------------- 2458 2459func Test_finally_discarded_by_interrupt () 2460let test =<< trim [CODE] 2461func I(jump) 2462let loop = 0 2463while loop < 2 2464let loop = loop + 1 2465if loop == 1 2466try 2467if a:jump == "continue" 2468continue 2469elseif a:jump == "break" 2470break 2471elseif a:jump == "return" || a:jump == "finish" 2472return 2473elseif a:jump == "error" 2474asdf 2475elseif a:jump == "interrupt" 2476call interrupt() 2477let dummy = 0 2478elseif a:jump == "throw" 2479throw "abc" 2480endif 2481finally 2482call interrupt() 2483let dummy = 0 2484endtry 2485elseif loop == 2 2486call assert_report('should not get here') 2487endif 2488endwhile 2489call assert_report('should not get here') 2490endfunc 2491 2492try 2493try 2494Xpath 'a' 2495call I("continue") 2496call assert_report('should not get here') 2497finally 2498try 2499Xpath 'b' 2500call I("break") 2501call assert_report('should not get here') 2502finally 2503try 2504Xpath 'c' 2505call I("return") 2506call assert_report('should not get here') 2507finally 2508try 2509Xpath 'd' 2510let g:jump = "finish" 2511ExecAsScript I 2512call assert_report('should not get here') 2513finally 2514unlet g:jump 2515try 2516Xpath 'e' 2517call I("error") 2518call assert_report('should not get here') 2519finally 2520try 2521Xpath 'f' 2522call I("interrupt") 2523call assert_report('should not get here') 2524finally 2525try 2526Xpath 'g' 2527call I("throw") 2528call assert_report('should not get here') 2529finally 2530Xpath 'h' 2531delfunction I 2532endtry 2533endtry 2534endtry 2535endtry 2536endtry 2537endtry 2538endtry 2539call assert_report('should not get here') 2540catch /^Vim:Interrupt$/ 2541Xpath 'A' 2542endtry 2543[CODE] 2544let verify =<< trim [CODE] 2545call assert_equal('abcdefghA', g:Xpath) 2546[CODE] 2547call RunInNewVim ( test , verify ) 2548endfunc 2549 2550"------------------------------------------------------------------------------- 2551" Test 40: :finally reason discarded by :throw {{{1 2552" 2553" When a :finally clause is executed due to a :continue, :break, 2554" :return, :finish, error, interrupt or :throw, the jump reason is 2555" discarded by a :throw in the finally clause. 2556"------------------------------------------------------------------------------- 2557 2558func Test_finally_discard_by_throw () 2559let test =<< trim [CODE] 2560func T(jump) 2561let loop = 0 2562while loop < 2 2563let loop = loop + 1 2564if loop == 1 2565try 2566if a:jump == "continue" 2567continue 2568elseif a:jump == "break" 2569break 2570elseif a:jump == "return" || a:jump == "finish" 2571return 2572elseif a:jump == "error" 2573asdf 2574elseif a:jump == "interrupt" 2575call interrupt() 2576let dummy = 0 2577elseif a:jump == "throw" 2578throw "abc" 2579endif 2580finally 2581throw "xyz" " discards jump that caused the :finally 2582endtry 2583elseif loop == 2 2584call assert_report('should not get here') 2585endif 2586endwhile 2587call assert_report('should not get here') 2588endfunc 2589 2590try 2591Xpath 'a' 2592call T("continue") 2593call assert_report('should not get here') 2594finally 2595try 2596Xpath 'b' 2597call T("break") 2598call assert_report('should not get here') 2599finally 2600try 2601Xpath 'c' 2602call T("return") 2603call assert_report('should not get here') 2604finally 2605try 2606Xpath 'd' 2607let g:jump = "finish" 2608ExecAsScript T 2609call assert_report('should not get here') 2610finally 2611unlet g:jump 2612try 2613Xpath 'e' 2614call T("error") 2615call assert_report('should not get here') 2616finally 2617try 2618Xpath 'f' 2619call T("interrupt") 2620call assert_report('should not get here') 2621finally 2622try 2623Xpath 'g' 2624call T("throw") 2625call assert_report('should not get here') 2626finally 2627Xpath 'h' 2628delfunction T 2629endtry 2630endtry 2631endtry 2632endtry 2633endtry 2634endtry 2635endtry 2636call assert_report('should not get here') 2637[CODE] 2638let verify =<< trim [CODE] 2639call assert_equal('abcdefgh', g:Xpath) 2640[CODE] 2641call RunInNewVim ( test , verify ) 2642endfunc 2643 2644"------------------------------------------------------------------------------- 2645" Test 49: Throwing exceptions across functions {{{1 2646" 2647" When an exception is thrown but not caught inside a function, the 2648" caller is checked for a matching :catch clause. 2649"------------------------------------------------------------------------------- 2650 2651func T49_C () 2652try 2653Xpath 'a' 2654throw "arrgh" 2655call assert_report ( 'should not get here' ) 2656catch /arrgh/ 2657Xpath 'b' 2658endtry 2659Xpath 'c' 2660endfunc 2661 2662func T49_T1 () 2663XloopNEXT 2664try 2665Xloop 'd' 2666throw "arrgh" 2667call assert_report ( 'should not get here' ) 2668finally 2669Xloop 'e' 2670endtry 2671Xloop 'f' 2672endfunc 2673 2674func T49_T2 () 2675try 2676Xpath 'g' 2677call T49_T1 () 2678call assert_report ( 'should not get here' ) 2679finally 2680Xpath 'h' 2681endtry 2682call assert_report ( 'should not get here' ) 2683endfunc 2684 2685func Test_throw_exception_across_funcs () 2686XpathINIT 2687XloopINIT 2688try 2689Xpath 'i' 2690call T49_C () " throw and catch 2691Xpath 'j' 2692catch /.*/ 2693call assert_report ( 'should not get here' ) 2694endtry 2695 2696try 2697Xpath 'k' 2698call T49_T1 () " throw, one level 2699call assert_report ( 'should not get here' ) 2700catch /arrgh/ 2701Xpath 'l' 2702catch /.*/ 2703call assert_report ( 'should not get here' ) 2704endtry 2705 2706try 2707Xpath 'm' 2708call T49_T2 () " throw, two levels 2709call assert_report ( 'should not get here' ) 2710catch /arrgh/ 2711Xpath 'n' 2712catch /.*/ 2713call assert_report ( 'should not get here' ) 2714endtry 2715Xpath 'o' 2716 2717call assert_equal ( 'iabcjkd2e2lmgd3e3hno' , g: Xpath ) 2718endfunc 2719 2720"------------------------------------------------------------------------------- 2721" Test 50: Throwing exceptions across script files {{{1 2722" 2723" When an exception is thrown but not caught inside a script file, 2724" the sourcing script or function is checked for a matching :catch 2725" clause. 2726" 2727" This test executes the bodies of the functions C, T1, and T2 from 2728" the previous test as script files (:return replaced by :finish). 2729"------------------------------------------------------------------------------- 2730 2731func T50_F () 2732try 2733Xpath 'A' 2734exec "source" g: scriptC 2735Xpath 'B' 2736catch /.*/ 2737call assert_report ( 'should not get here' ) 2738endtry 2739 2740try 2741Xpath 'C' 2742exec "source" g: scriptT1 2743call assert_report ( 'should not get here' ) 2744catch /arrgh/ 2745Xpath 'D' 2746catch /.*/ 2747call assert_report ( 'should not get here' ) 2748endtry 2749endfunc 2750 2751func Test_throw_across_script () 2752XpathINIT 2753XloopINIT 2754let g: scriptC = MakeScript ( "T49_C" ) 2755let g: scriptT1 = MakeScript ( "T49_T1" ) 2756let scriptT2 = MakeScript ( "T49_T2" , g: scriptT1 ) 2757 2758try 2759Xpath 'E' 2760call T50_F () 2761Xpath 'F' 2762exec "source" scriptT2 2763call assert_report ( 'should not get here' ) 2764catch /arrgh/ 2765Xpath 'G' 2766catch /.*/ 2767call assert_report ( 'should not get here' ) 2768endtry 2769Xpath 'H' 2770call assert_equal ( 'EAabcBCd2e2DFgd3e3hGH' , g: Xpath ) 2771 2772call delete ( g: scriptC ) 2773call delete ( g: scriptT1 ) 2774call delete ( scriptT2 ) 2775unlet g: scriptC g: scriptT1 scriptT2 2776endfunc 2777 2778"------------------------------------------------------------------------------- 2779" Test 52: Uncaught exceptions {{{1 2780" 2781" When an exception is thrown but not caught, an error message is 2782" displayed when the script is terminated. In case of an interrupt 2783" or error exception, the normal interrupt or error message(s) are 2784" displayed. 2785"------------------------------------------------------------------------------- 2786 2787func Test_uncaught_exception_1 () 2788CheckEnglish 2789 2790let test =<< trim [CODE] 2791Xpath 'a' 2792throw "arrgh" 2793call assert_report('should not get here')` 2794[CODE] 2795let verify =<< trim [CODE] 2796call assert_equal('E605: Exception not caught: arrgh', v:errmsg) 2797call assert_equal('a', g:Xpath) 2798[CODE] 2799call RunInNewVim ( test , verify ) 2800endfunc 2801 2802func Test_uncaught_exception_2 () 2803CheckEnglish 2804 2805let test =<< trim [CODE] 2806try 2807Xpath 'a' 2808throw "oops" 2809call assert_report('should not get here')` 2810catch /arrgh/ 2811call assert_report('should not get here')` 2812endtry 2813call assert_report('should not get here')` 2814[CODE] 2815let verify =<< trim [CODE] 2816call assert_equal('E605: Exception not caught: oops', v:errmsg) 2817call assert_equal('a', g:Xpath) 2818[CODE] 2819call RunInNewVim ( test , verify ) 2820endfunc 2821 2822func Test_uncaught_exception_3 () 2823CheckEnglish 2824 2825let test =<< trim [CODE] 2826func T() 2827Xpath 'c' 2828throw "brrr" 2829call assert_report('should not get here')` 2830endfunc 2831 2832try 2833Xpath 'a' 2834throw "arrgh" 2835call assert_report('should not get here')` 2836catch /.*/ 2837Xpath 'b' 2838call T() 2839call assert_report('should not get here')` 2840endtry 2841call assert_report('should not get here')` 2842[CODE] 2843let verify =<< trim [CODE] 2844call assert_equal('E605: Exception not caught: brrr', v:errmsg) 2845call assert_equal('abc', g:Xpath) 2846[CODE] 2847call RunInNewVim ( test , verify ) 2848endfunc 2849 2850func Test_uncaught_exception_4 () 2851CheckEnglish 2852 2853let test =<< trim [CODE] 2854try 2855Xpath 'a' 2856throw "arrgh" 2857call assert_report('should not get here')` 2858finally 2859Xpath 'b' 2860throw "brrr" 2861call assert_report('should not get here')` 2862endtry 2863call assert_report('should not get here')` 2864[CODE] 2865let verify =<< trim [CODE] 2866call assert_equal('E605: Exception not caught: brrr', v:errmsg) 2867call assert_equal('ab', g:Xpath) 2868[CODE] 2869call RunInNewVim ( test , verify ) 2870endfunc 2871 2872func Test_uncaught_exception_5 () 2873CheckEnglish 2874 2875" Need to catch and handle interrupt, otherwise the test will wait for the 2876" user to press <Enter> to continue 2877let test =<< trim [CODE] 2878try 2879try 2880Xpath 'a' 2881call interrupt() 2882call assert_report('should not get here') 2883endtry 2884call assert_report('should not get here') 2885catch /^Vim:Interrupt$/ 2886Xpath 'b' 2887endtry 2888[CODE] 2889let verify =<< trim [CODE] 2890call assert_equal('ab', g:Xpath) 2891[CODE] 2892call RunInNewVim ( test , verify ) 2893endfunc 2894 2895func Test_uncaught_exception_6 () 2896CheckEnglish 2897 2898let test =<< trim [CODE] 2899try 2900Xpath 'a' 2901let x = novar " error E121; exception: E121 2902catch /E15:/ " should not catch 2903call assert_report('should not get here') 2904endtry 2905call assert_report('should not get here') 2906[CODE] 2907let verify =<< trim [CODE] 2908call assert_equal('a', g:Xpath) 2909call assert_equal('E121: Undefined variable: novar', v:errmsg) 2910[CODE] 2911call RunInNewVim ( test , verify ) 2912endfunc 2913 2914func Test_uncaught_exception_7 () 2915CheckEnglish 2916 2917let test =<< trim [CODE] 2918try 2919Xpath 'a' 2920" error E108/E488; exception: E488 2921unlet novar # 2922catch /E108:/ " should not catch 2923call assert_report('should not get here') 2924endtry 2925call assert_report('should not get here') 2926[CODE] 2927let verify =<< trim [CODE] 2928call assert_equal('a', g:Xpath) 2929call assert_equal('E488: Trailing characters: #', v:errmsg) 2930[CODE] 2931call RunInNewVim ( test , verify ) 2932endfunc 2933 2934"------------------------------------------------------------------------------- 2935" Test 53: Nesting errors: :endif/:else/:elseif {{{1 2936" 2937" For nesting errors of :if conditionals the correct error messages 2938" should be given. 2939"------------------------------------------------------------------------------- 2940 2941func Test_nested_if_else_errors () 2942CheckEnglish 2943 2944" :endif without :if 2945let code =<< trim END 2946endif 2947END 2948call writefile ( code , 'Xtest' ) 2949call AssertException ([ 'source Xtest' ], 'Vim(endif):E580: :endif without :if' ) 2950 2951" :endif without :if 2952let code =<< trim END 2953while 1 2954endif 2955endwhile 2956END 2957call writefile ( code , 'Xtest' ) 2958call AssertException ([ 'source Xtest' ], 'Vim(endif):E580: :endif without :if' ) 2959 2960" :endif without :if 2961let code =<< trim END 2962try 2963finally 2964endif 2965endtry 2966END 2967call writefile ( code , 'Xtest' ) 2968call AssertException ([ 'source Xtest' ], 'Vim(endif):E580: :endif without :if' ) 2969 2970" :endif without :if 2971let code =<< trim END 2972try 2973endif 2974endtry 2975END 2976call writefile ( code , 'Xtest' ) 2977call AssertException ([ 'source Xtest' ], 'Vim(endif):E580: :endif without :if' ) 2978 2979" :endif without :if 2980let code =<< trim END 2981try 2982throw "a" 2983catch /a/ 2984endif 2985endtry 2986END 2987call writefile ( code , 'Xtest' ) 2988call AssertException ([ 'source Xtest' ], 'Vim(endif):E580: :endif without :if' ) 2989 2990" :else without :if 2991let code =<< trim END 2992else 2993END 2994call writefile ( code , 'Xtest' ) 2995call AssertException ([ 'source Xtest' ], 'Vim(else):E581: :else without :if' ) 2996 2997" :else without :if 2998let code =<< trim END 2999while 1 3000else 3001endwhile 3002END 3003call writefile ( code , 'Xtest' ) 3004call AssertException ([ 'source Xtest' ], 'Vim(else):E581: :else without :if' ) 3005 3006" :else without :if 3007let code =<< trim END 3008try 3009finally 3010else 3011endtry 3012END 3013call writefile ( code , 'Xtest' ) 3014call AssertException ([ 'source Xtest' ], 'Vim(else):E581: :else without :if' ) 3015 3016" :else without :if 3017let code =<< trim END 3018try 3019else 3020endtry 3021END 3022call writefile ( code , 'Xtest' ) 3023call AssertException ([ 'source Xtest' ], 'Vim(else):E581: :else without :if' ) 3024 3025" :else without :if 3026let code =<< trim END 3027try 3028throw "a" 3029catch /a/ 3030else 3031endtry 3032END 3033call writefile ( code , 'Xtest' ) 3034call AssertException ([ 'source Xtest' ], 'Vim(else):E581: :else without :if' ) 3035 3036" :elseif without :if 3037let code =<< trim END 3038elseif 1 3039END 3040call writefile ( code , 'Xtest' ) 3041call AssertException ([ 'source Xtest' ], 'Vim(elseif):E582: :elseif without :if' ) 3042 3043" :elseif without :if 3044let code =<< trim END 3045while 1 3046elseif 1 3047endwhile 3048END 3049call writefile ( code , 'Xtest' ) 3050call AssertException ([ 'source Xtest' ], 'Vim(elseif):E582: :elseif without :if' ) 3051 3052" :elseif without :if 3053let code =<< trim END 3054try 3055finally 3056elseif 1 3057endtry 3058END 3059call writefile ( code , 'Xtest' ) 3060call AssertException ([ 'source Xtest' ], 'Vim(elseif):E582: :elseif without :if' ) 3061 3062" :elseif without :if 3063let code =<< trim END 3064try 3065elseif 1 3066endtry 3067END 3068call writefile ( code , 'Xtest' ) 3069call AssertException ([ 'source Xtest' ], 'Vim(elseif):E582: :elseif without :if' ) 3070 3071" :elseif without :if 3072let code =<< trim END 3073try 3074throw "a" 3075catch /a/ 3076elseif 1 3077endtry 3078END 3079call writefile ( code , 'Xtest' ) 3080call AssertException ([ 'source Xtest' ], 'Vim(elseif):E582: :elseif without :if' ) 3081 3082" multiple :else 3083let code =<< trim END 3084if 1 3085else 3086else 3087endif 3088END 3089call writefile ( code , 'Xtest' ) 3090call AssertException ([ 'source Xtest' ], 'Vim(else):E583: Multiple :else' ) 3091 3092" :elseif after :else 3093let code =<< trim END 3094if 1 3095else 3096elseif 1 3097endif 3098END 3099call writefile ( code , 'Xtest' ) 3100call AssertException ([ 'source Xtest' ], 'Vim(elseif):E584: :elseif after :else' ) 3101 3102call delete ( 'Xtest' ) 3103endfunc 3104 3105"------------------------------------------------------------------------------- 3106" Test 54: Nesting errors: :while/:endwhile {{{1 3107" 3108" For nesting errors of :while conditionals the correct error messages 3109" should be given. 3110" 3111" This test reuses the function MESSAGES() from the previous test. 3112" This function checks the messages in g:msgfile. 3113"------------------------------------------------------------------------------- 3114 3115func Test_nested_while_error () 3116CheckEnglish 3117 3118" :endwhile without :while 3119let code =<< trim END 3120endwhile 3121END 3122call writefile ( code , 'Xtest' ) 3123call AssertException ([ 'source Xtest' ], 'Vim(endwhile):E588: :endwhile without :while' ) 3124 3125" :endwhile without :while 3126let code =<< trim END 3127if 1 3128endwhile 3129endif 3130END 3131call writefile ( code , 'Xtest' ) 3132call AssertException ([ 'source Xtest' ], 'Vim(endwhile):E588: :endwhile without :while' ) 3133 3134" Missing :endif 3135let code =<< trim END 3136while 1 3137if 1 3138endwhile 3139END 3140call writefile ( code , 'Xtest' ) 3141call AssertException ([ 'source Xtest' ], 'Vim(endwhile):E171: Missing :endif' ) 3142 3143" :endwhile without :while 3144let code =<< trim END 3145try 3146finally 3147endwhile 3148endtry 3149END 3150call writefile ( code , 'Xtest' ) 3151call AssertException ([ 'source Xtest' ], 'Vim(endwhile):E588: :endwhile without :while' ) 3152 3153" Missing :endtry 3154let code =<< trim END 3155while 1 3156try 3157finally 3158endwhile 3159END 3160call writefile ( code , 'Xtest' ) 3161call AssertException ([ 'source Xtest' ], 'Vim(endwhile):E600: Missing :endtry' ) 3162 3163" Missing :endtry 3164let code =<< trim END 3165while 1 3166if 1 3167try 3168finally 3169endwhile 3170END 3171call writefile ( code , 'Xtest' ) 3172call AssertException ([ 'source Xtest' ], 'Vim(endwhile):E600: Missing :endtry' ) 3173 3174" Missing :endif 3175let code =<< trim END 3176while 1 3177try 3178finally 3179if 1 3180endwhile 3181END 3182call writefile ( code , 'Xtest' ) 3183call AssertException ([ 'source Xtest' ], 'Vim(endwhile):E171: Missing :endif' ) 3184 3185" :endwhile without :while 3186let code =<< trim END 3187try 3188endwhile 3189endtry 3190END 3191call writefile ( code , 'Xtest' ) 3192call AssertException ([ 'source Xtest' ], 'Vim(endwhile):E588: :endwhile without :while' ) 3193 3194" :endwhile without :while 3195let code =<< trim END 3196while 1 3197try 3198endwhile 3199endtry 3200endwhile 3201END 3202call writefile ( code , 'Xtest' ) 3203call AssertException ([ 'source Xtest' ], 'Vim(endwhile):E588: :endwhile without :while' ) 3204 3205" :endwhile without :while 3206let code =<< trim END 3207try 3208throw "a" 3209catch /a/ 3210endwhile 3211endtry 3212END 3213call writefile ( code , 'Xtest' ) 3214call AssertException ([ 'source Xtest' ], 'Vim(endwhile):E588: :endwhile without :while' ) 3215 3216" :endwhile without :while 3217let code =<< trim END 3218while 1 3219try 3220throw "a" 3221catch /a/ 3222endwhile 3223endtry 3224endwhile 3225END 3226call writefile ( code , 'Xtest' ) 3227call AssertException ([ 'source Xtest' ], 'Vim(endwhile):E588: :endwhile without :while' ) 3228 3229call delete ( 'Xtest' ) 3230endfunc 3231 3232"------------------------------------------------------------------------------- 3233" Test 55: Nesting errors: :continue/:break {{{1 3234" 3235" For nesting errors of :continue and :break commands the correct 3236" error messages should be given. 3237" 3238" This test reuses the function MESSAGES() from the previous test. 3239" This function checks the messages in g:msgfile. 3240"------------------------------------------------------------------------------- 3241 3242func Test_nested_cont_break_error () 3243CheckEnglish 3244 3245" :continue without :while 3246let code =<< trim END 3247continue 3248END 3249call writefile ( code , 'Xtest' ) 3250call AssertException ([ 'source Xtest' ], 'Vim(continue):E586: :continue without :while or :for' ) 3251 3252" :continue without :while 3253let code =<< trim END 3254if 1 3255continue 3256endif 3257END 3258call writefile ( code , 'Xtest' ) 3259call AssertException ([ 'source Xtest' ], 'Vim(continue):E586: :continue without :while or :for' ) 3260 3261" :continue without :while 3262let code =<< trim END 3263try 3264finally 3265continue 3266endtry 3267END 3268call writefile ( code , 'Xtest' ) 3269call AssertException ([ 'source Xtest' ], 'Vim(continue):E586: :continue without :while or :for' ) 3270 3271" :continue without :while 3272let code =<< trim END 3273try 3274continue 3275endtry 3276END 3277call writefile ( code , 'Xtest' ) 3278call AssertException ([ 'source Xtest' ], 'Vim(continue):E586: :continue without :while or :for' ) 3279 3280" :continue without :while 3281let code =<< trim END 3282try 3283throw "a" 3284catch /a/ 3285continue 3286endtry 3287END 3288call writefile ( code , 'Xtest' ) 3289call AssertException ([ 'source Xtest' ], 'Vim(continue):E586: :continue without :while or :for' ) 3290 3291" :break without :while 3292let code =<< trim END 3293break 3294END 3295call writefile ( code , 'Xtest' ) 3296call AssertException ([ 'source Xtest' ], 'Vim(break):E587: :break without :while or :for' ) 3297 3298" :break without :while 3299let code =<< trim END 3300if 1 3301break 3302endif 3303END 3304call writefile ( code , 'Xtest' ) 3305call AssertException ([ 'source Xtest' ], 'Vim(break):E587: :break without :while or :for' ) 3306 3307" :break without :while 3308let code =<< trim END 3309try 3310finally 3311break 3312endtry 3313END 3314call writefile ( code , 'Xtest' ) 3315call AssertException ([ 'source Xtest' ], 'Vim(break):E587: :break without :while or :for' ) 3316 3317" :break without :while 3318let code =<< trim END 3319try 3320break 3321endtry 3322END 3323call writefile ( code , 'Xtest' ) 3324call AssertException ([ 'source Xtest' ], 'Vim(break):E587: :break without :while or :for' ) 3325 3326" :break without :while 3327let code =<< trim END 3328try 3329throw "a" 3330catch /a/ 3331break 3332endtry 3333END 3334call writefile ( code , 'Xtest' ) 3335call AssertException ([ 'source Xtest' ], 'Vim(break):E587: :break without :while or :for' ) 3336 3337call delete ( 'Xtest' ) 3338endfunc 3339 3340"------------------------------------------------------------------------------- 3341" Test 56: Nesting errors: :endtry {{{1 3342" 3343" For nesting errors of :try conditionals the correct error messages 3344" should be given. 3345" 3346" This test reuses the function MESSAGES() from the previous test. 3347" This function check the messages in g:msgfile. 3348"------------------------------------------------------------------------------- 3349 3350func Test_nested_endtry_error () 3351CheckEnglish 3352 3353" :endtry without :try 3354let code =<< trim END 3355endtry 3356END 3357call writefile ( code , 'Xtest' ) 3358call AssertException ([ 'source Xtest' ], 'Vim(endtry):E602: :endtry without :try' ) 3359 3360" :endtry without :try 3361let code =<< trim END 3362if 1 3363endtry 3364endif 3365END 3366call writefile ( code , 'Xtest' ) 3367call AssertException ([ 'source Xtest' ], 'Vim(endtry):E602: :endtry without :try' ) 3368 3369" :endtry without :try 3370let code =<< trim END 3371while 1 3372endtry 3373endwhile 3374END 3375call writefile ( code , 'Xtest' ) 3376call AssertException ([ 'source Xtest' ], 'Vim(endtry):E602: :endtry without :try' ) 3377 3378" Missing :endif 3379let code =<< trim END 3380try 3381if 1 3382endtry 3383END 3384call writefile ( code , 'Xtest' ) 3385call AssertException ([ 'source Xtest' ], 'Vim(endtry):E171: Missing :endif' ) 3386 3387" Missing :endwhile 3388let code =<< trim END 3389try 3390while 1 3391endtry 3392END 3393call writefile ( code , 'Xtest' ) 3394call AssertException ([ 'source Xtest' ], 'Vim(endtry):E170: Missing :endwhile' ) 3395 3396" Missing :endif 3397let code =<< trim END 3398try 3399finally 3400if 1 3401endtry 3402END 3403call writefile ( code , 'Xtest' ) 3404call AssertException ([ 'source Xtest' ], 'Vim(endtry):E171: Missing :endif' ) 3405 3406" Missing :endwhile 3407let code =<< trim END 3408try 3409finally 3410while 1 3411endtry 3412END 3413call writefile ( code , 'Xtest' ) 3414call AssertException ([ 'source Xtest' ], 'Vim(endtry):E170: Missing :endwhile' ) 3415 3416" Missing :endif 3417let code =<< trim END 3418try 3419throw "a" 3420catch /a/ 3421if 1 3422endtry 3423END 3424call writefile ( code , 'Xtest' ) 3425call AssertException ([ 'source Xtest' ], 'Vim(endtry):E171: Missing :endif' ) 3426 3427" Missing :endwhile 3428let code =<< trim END 3429try 3430throw "a" 3431catch /a/ 3432while 1 3433endtry 3434END 3435call writefile ( code , 'Xtest' ) 3436call AssertException ([ 'source Xtest' ], 'Vim(endtry):E170: Missing :endwhile' ) 3437 3438call delete ( 'Xtest' ) 3439endfunc 3440 3441"------------------------------------------------------------------------------- 3442" Test 57: v:exception and v:throwpoint for user exceptions {{{1 3443" 3444" v:exception evaluates to the value of the exception that was caught 3445" most recently and is not finished. (A caught exception is finished 3446" when the next ":catch", ":finally", or ":endtry" is reached.) 3447" v:throwpoint evaluates to the script/function name and line number 3448" where that exception has been thrown. 3449"------------------------------------------------------------------------------- 3450 3451func Test_user_exception_info () 3452CheckEnglish 3453 3454XpathINIT 3455XloopINIT 3456 3457func FuncException () 3458let g: exception = v: exception 3459endfunc 3460 3461func FuncThrowpoint () 3462let g: throwpoint = v: throwpoint 3463endfunc 3464 3465let scriptException = MakeScript ( "FuncException" ) 3466let scriptThrowPoint = MakeScript ( "FuncThrowpoint" ) 3467 3468command ! CmdException let g:exception = v:exception 3469command ! CmdThrowpoint let g:throwpoint = v:throwpoint 3470 3471func T ( arg , line ) 3472if a: line == 2 3473throw a: arg " in line 2 3474elseif a: line == 4 3475throw a: arg " in line 4 3476elseif a: line == 6 3477throw a: arg " in line 6 3478elseif a: line == 8 3479throw a: arg " in line 8 3480endif 3481endfunc 3482 3483func G ( arg , line ) 3484call T ( a: arg , a: line ) 3485endfunc 3486 3487func F ( arg , line ) 3488call G ( a: arg , a: line ) 3489endfunc 3490 3491let scriptT = MakeScript ( "T" ) 3492let scriptG = MakeScript ( "G" , scriptT ) 3493let scriptF = MakeScript ( "F" , scriptG ) 3494 3495try 3496Xpath 'a' 3497call F ( "oops" , 2 ) 3498catch /.*/ 3499Xpath 'b' 3500let exception = v: exception 3501let throwpoint = v: throwpoint 3502call assert_equal ( "oops" , v: exception ) 3503call assert_match ( '\<F\[1]\.\.G\[1]\.\.T\>' , v: throwpoint ) 3504call assert_match ( '\<2\>' , v: throwpoint ) 3505 3506exec "let exception = v:exception" 3507exec "let throwpoint = v:throwpoint" 3508call assert_equal ( "oops" , v: exception ) 3509call assert_match ( '\<F\[1]\.\.G\[1]\.\.T\>' , v: throwpoint ) 3510call assert_match ( '\<2\>' , v: throwpoint ) 3511 3512CmdException 3513CmdThrowpoint 3514call assert_equal ( "oops" , v: exception ) 3515call assert_match ( '\<F\[1]\.\.G\[1]\.\.T\>' , v: throwpoint ) 3516call assert_match ( '\<2\>' , v: throwpoint ) 3517 3518call FuncException () 3519call FuncThrowpoint () 3520call assert_equal ( "oops" , v: exception ) 3521call assert_match ( '\<F\[1]\.\.G\[1]\.\.T\>' , v: throwpoint ) 3522call assert_match ( '\<2\>' , v: throwpoint ) 3523 3524exec "source" scriptException 3525exec "source" scriptThrowPoint 3526call assert_equal ( "oops" , v: exception ) 3527call assert_match ( '\<F\[1]\.\.G\[1]\.\.T\>' , v: throwpoint ) 3528call assert_match ( '\<2\>' , v: throwpoint ) 3529 3530try 3531Xpath 'c' 3532call G ( "arrgh" , 4 ) 3533catch /.*/ 3534Xpath 'd' 3535let exception = v: exception 3536let throwpoint = v: throwpoint 3537call assert_equal ( "arrgh" , v: exception ) 3538call assert_match ( '\<G\[1]\.\.T\>' , v: throwpoint ) 3539call assert_match ( '\<4\>' , v: throwpoint ) 3540 3541try 3542Xpath 'e' 3543let g: arg = "autsch" 3544let g: line = 6 3545exec "source" scriptF 3546catch /.*/ 3547Xpath 'f' 3548let exception = v: exception 3549let throwpoint = v: throwpoint 3550call assert_equal ( "autsch" , v: exception ) 3551call assert_match ( fnamemodify ( scriptT , ':t' ), v: throwpoint ) 3552call assert_match ( '\<6\>' , v: throwpoint ) 3553finally 3554Xpath 'g' 3555let exception = v: exception 3556let throwpoint = v: throwpoint 3557call assert_equal ( "arrgh" , v: exception ) 3558call assert_match ( '\<G\[1]\.\.T\>' , v: throwpoint ) 3559call assert_match ( '\<4\>' , v: throwpoint ) 3560try 3561Xpath 'h' 3562let g: arg = "brrrr" 3563let g: line = 8 3564exec "source" scriptG 3565catch /.*/ 3566Xpath 'i' 3567let exception = v: exception 3568let throwpoint = v: throwpoint 3569" Resolve scriptT for matching it against v:throwpoint. 3570call assert_equal ( "brrrr" , v: exception ) 3571call assert_match ( fnamemodify ( scriptT , ':t' ), v: throwpoint ) 3572call assert_match ( '\<8\>' , v: throwpoint ) 3573finally 3574Xpath 'j' 3575let exception = v: exception 3576let throwpoint = v: throwpoint 3577call assert_equal ( "arrgh" , v: exception ) 3578call assert_match ( '\<G\[1]\.\.T\>' , v: throwpoint ) 3579call assert_match ( '\<4\>' , v: throwpoint ) 3580endtry 3581Xpath 'k' 3582let exception = v: exception 3583let throwpoint = v: throwpoint 3584call assert_equal ( "arrgh" , v: exception ) 3585call assert_match ( '\<G\[1]\.\.T\>' , v: throwpoint ) 3586call assert_match ( '\<4\>' , v: throwpoint ) 3587endtry 3588Xpath 'l' 3589let exception = v: exception 3590let throwpoint = v: throwpoint 3591call assert_equal ( "arrgh" , v: exception ) 3592call assert_match ( '\<G\[1]\.\.T\>' , v: throwpoint ) 3593call assert_match ( '\<4\>' , v: throwpoint ) 3594finally 3595Xpath 'm' 3596let exception = v: exception 3597let throwpoint = v: throwpoint 3598call assert_equal ( "oops" , v: exception ) 3599call assert_match ( '\<F\[1]\.\.G\[1]\.\.T\>' , v: throwpoint ) 3600call assert_match ( '\<2\>' , v: throwpoint ) 3601endtry 3602Xpath 'n' 3603let exception = v: exception 3604let throwpoint = v: throwpoint 3605call assert_equal ( "oops" , v: exception ) 3606call assert_match ( '\<F\[1]\.\.G\[1]\.\.T\>' , v: throwpoint ) 3607call assert_match ( '\<2\>' , v: throwpoint ) 3608finally 3609Xpath 'o' 3610let exception = v: exception 3611let throwpoint = v: throwpoint 3612call assert_equal ( "" , v: exception ) 3613call assert_match ( '^$' , v: throwpoint ) 3614call assert_match ( '^$' , v: throwpoint ) 3615endtry 3616 3617call assert_equal ( 'abcdefghijklmno' , g: Xpath ) 3618 3619unlet exception throwpoint 3620delfunction FuncException 3621delfunction FuncThrowpoint 3622call delete ( scriptException ) 3623call delete ( scriptThrowPoint ) 3624unlet scriptException scriptThrowPoint 3625delcommand CmdException 3626delcommand CmdThrowpoint 3627delfunction T 3628delfunction G 3629delfunction F 3630call delete ( scriptT ) 3631call delete ( scriptG ) 3632call delete ( scriptF ) 3633unlet scriptT scriptG scriptF 3634endfunc 3635 3636"------------------------------------------------------------------------------- 3637" 3638" Test 58: v:exception and v:throwpoint for error/interrupt exceptions {{{1 3639" 3640" v:exception and v:throwpoint work also for error and interrupt 3641" exceptions. 3642"------------------------------------------------------------------------------- 3643 3644func Test_exception_info_for_error () 3645CheckEnglish 3646 3647let test =<< trim [CODE] 3648func T(line) 3649if a:line == 2 3650delfunction T " error (function in use) in line 2 3651elseif a:line == 4 3652call interrupt() 3653endif 3654endfunc 3655 3656while 1 3657try 3658Xpath 'a' 3659call T(2) 3660call assert_report('should not get here') 3661catch /.*/ 3662Xpath 'b' 3663if v:exception !~ 'Vim(delfunction):' 3664call assert_report('should not get here') 3665endif 3666if v:throwpoint !~ '\<T\>' 3667call assert_report('should not get here') 3668endif 3669if v:throwpoint !~ '\<2\>' 3670call assert_report('should not get here') 3671endif 3672finally 3673Xpath 'c' 3674if v:exception != "" 3675call assert_report('should not get here') 3676endif 3677if v:throwpoint != "" 3678call assert_report('should not get here') 3679endif 3680break 3681endtry 3682endwhile 3683 3684Xpath 'd' 3685if v:exception != "" 3686call assert_report('should not get here') 3687endif 3688if v:throwpoint != "" 3689call assert_report('should not get here') 3690endif 3691 3692while 1 3693try 3694Xpath 'e' 3695call T(4) 3696call assert_report('should not get here') 3697catch /.*/ 3698Xpath 'f' 3699if v:exception != 'Vim:Interrupt' 3700call assert_report('should not get here') 3701endif 3702if v:throwpoint !~ 'function T' 3703call assert_report('should not get here') 3704endif 3705if v:throwpoint !~ '\<4\>' 3706call assert_report('should not get here') 3707endif 3708finally 3709Xpath 'g' 3710if v:exception != "" 3711call assert_report('should not get here') 3712endif 3713if v:throwpoint != "" 3714call assert_report('should not get here') 3715endif 3716break 3717endtry 3718endwhile 3719 3720Xpath 'h' 3721if v:exception != "" 3722call assert_report('should not get here') 3723endif 3724if v:throwpoint != "" 3725call assert_report('should not get here') 3726endif 3727[CODE] 3728let verify =<< trim [CODE] 3729call assert_equal('abcdefgh', g:Xpath) 3730[CODE] 3731call RunInNewVim ( test , verify ) 3732endfunc 3733 3734"------------------------------------------------------------------------------- 3735" 3736" Test 59: v:exception and v:throwpoint when discarding exceptions {{{1 3737" 3738" When a :catch clause is left by a ":break" etc or an error or 3739" interrupt exception, v:exception and v:throwpoint are reset. They 3740" are not affected by an exception that is discarded before being 3741" caught. 3742"------------------------------------------------------------------------------- 3743func Test_exception_info_on_discard () 3744CheckEnglish 3745 3746let test =<< trim [CODE] 3747let sfile = expand("<sfile>") 3748 3749while 1 3750try 3751throw "x1" 3752catch /.*/ 3753break 3754endtry 3755endwhile 3756call assert_equal('', v:exception) 3757call assert_equal('', v:throwpoint) 3758 3759while 1 3760try 3761throw "x2" 3762catch /.*/ 3763break 3764finally 3765call assert_equal('', v:exception) 3766call assert_equal('', v:throwpoint) 3767endtry 3768break 3769endwhile 3770call assert_equal('', v:exception) 3771call assert_equal('', v:throwpoint) 3772 3773while 1 3774try 3775let errcaught = 0 3776try 3777try 3778throw "x3" 3779catch /.*/ 3780let lnum = expand("<sflnum>") 3781asdf 3782endtry 3783catch /.*/ 3784let errcaught = 1 3785call assert_match('Vim:E492: Not an editor command:', v:exception) 3786call assert_match('line ' .. (lnum + 1), v:throwpoint) 3787endtry 3788finally 3789call assert_equal(1, errcaught) 3790break 3791endtry 3792endwhile 3793call assert_equal('', v:exception) 3794call assert_equal('', v:throwpoint) 3795 3796Xpath 'a' 3797 3798while 1 3799try 3800let intcaught = 0 3801try 3802try 3803throw "x4" 3804catch /.*/ 3805let lnum = expand("<sflnum>") 3806call interrupt() 3807endtry 3808catch /.*/ 3809let intcaught = 1 3810call assert_match('Vim:Interrupt', v:exception) 3811call assert_match('line ' .. (lnum + 1), v:throwpoint) 3812endtry 3813finally 3814call assert_equal(1, intcaught) 3815break 3816endtry 3817endwhile 3818call assert_equal('', v:exception) 3819call assert_equal('', v:throwpoint) 3820 3821Xpath 'b' 3822 3823while 1 3824try 3825let errcaught = 0 3826try 3827try 3828if 1 3829let lnum = expand("<sflnum>") 3830throw "x5" 3831" missing endif 3832catch /.*/ 3833call assert_report('should not get here') 3834endtry 3835catch /.*/ 3836let errcaught = 1 3837call assert_match('Vim(catch):E171: Missing :endif:', v:exception) 3838call assert_match('line ' .. (lnum + 3), v:throwpoint) 3839endtry 3840finally 3841call assert_equal(1, errcaught) 3842break 3843endtry 3844endwhile 3845call assert_equal('', v:exception) 3846call assert_equal('', v:throwpoint) 3847 3848Xpath 'c' 3849 3850try 3851while 1 3852try 3853throw "x6" 3854finally 3855break 3856endtry 3857break 3858endwhile 3859catch /.*/ 3860call assert_report('should not get here') 3861endtry 3862call assert_equal('', v:exception) 3863call assert_equal('', v:throwpoint) 3864 3865try 3866while 1 3867try 3868throw "x7" 3869finally 3870break 3871endtry 3872break 3873endwhile 3874catch /.*/ 3875call assert_report('should not get here') 3876finally 3877call assert_equal('', v:exception) 3878call assert_equal('', v:throwpoint) 3879endtry 3880call assert_equal('', v:exception) 3881call assert_equal('', v:throwpoint) 3882 3883while 1 3884try 3885let errcaught = 0 3886try 3887try 3888throw "x8" 3889finally 3890let lnum = expand("<sflnum>") 3891asdf 3892endtry 3893catch /.*/ 3894let errcaught = 1 3895call assert_match('Vim:E492: Not an editor command:', v:exception) 3896call assert_match('line ' .. (lnum + 1), v:throwpoint) 3897endtry 3898finally 3899call assert_equal(1, errcaught) 3900break 3901endtry 3902endwhile 3903call assert_equal('', v:exception) 3904call assert_equal('', v:throwpoint) 3905 3906Xpath 'd' 3907 3908while 1 3909try 3910let intcaught = 0 3911try 3912try 3913throw "x9" 3914finally 3915let lnum = expand("<sflnum>") 3916call interrupt() 3917endtry 3918catch /.*/ 3919let intcaught = 1 3920call assert_match('Vim:Interrupt', v:exception) 3921call assert_match('line ' .. (lnum + 1), v:throwpoint) 3922endtry 3923finally 3924call assert_equal(1, intcaught) 3925break 3926endtry 3927endwhile 3928call assert_equal('', v:exception) 3929call assert_equal('', v:throwpoint) 3930 3931Xpath 'e' 3932 3933while 1 3934try 3935let errcaught = 0 3936try 3937try 3938if 1 3939let lnum = expand("<sflnum>") 3940throw "x10" 3941" missing endif 3942finally 3943call assert_equal('', v:exception) 3944call assert_equal('', v:throwpoint) 3945endtry 3946catch /.*/ 3947let errcaught = 1 3948call assert_match('Vim(finally):E171: Missing :endif:', v:exception) 3949call assert_match('line ' .. (lnum + 3), v:throwpoint) 3950endtry 3951finally 3952call assert_equal(1, errcaught) 3953break 3954endtry 3955endwhile 3956call assert_equal('', v:exception) 3957call assert_equal('', v:throwpoint) 3958 3959Xpath 'f' 3960 3961while 1 3962try 3963let errcaught = 0 3964try 3965try 3966if 1 3967let lnum = expand("<sflnum>") 3968throw "x11" 3969" missing endif 3970endtry 3971catch /.*/ 3972let errcaught = 1 3973call assert_match('Vim(endtry):E171: Missing :endif:', v:exception) 3974call assert_match('line ' .. (lnum + 3), v:throwpoint) 3975endtry 3976finally 3977call assert_equal(1, errcaught) 3978break 3979endtry 3980endwhile 3981call assert_equal('', v:exception) 3982call assert_equal('', v:throwpoint) 3983 3984Xpath 'g' 3985[CODE] 3986let verify =<< trim [CODE] 3987call assert_equal('abcdefg', g:Xpath) 3988[CODE] 3989call RunInNewVim ( test , verify ) 3990endfunc 3991 3992"------------------------------------------------------------------------------- 3993" 3994" Test 60: (Re)throwing v:exception; :echoerr. {{{1 3995" 3996" A user exception can be rethrown after catching by throwing 3997" v:exception. An error or interrupt exception cannot be rethrown 3998" because Vim exceptions cannot be faked. A Vim exception using the 3999" value of v:exception can, however, be triggered by the :echoerr 4000" command. 4001"------------------------------------------------------------------------------- 4002 4003func Test_rethrow_exception_1 () 4004XpathINIT 4005try 4006try 4007Xpath 'a' 4008throw "oops" 4009catch /oops/ 4010Xpath 'b' 4011throw v: exception " rethrow user exception 4012catch /.*/ 4013call assert_report ( 'should not get here' ) 4014endtry 4015catch /^oops$/ " catches rethrown user exception 4016Xpath 'c' 4017catch /.*/ 4018call assert_report ( 'should not get here' ) 4019endtry 4020call assert_equal ( 'abc' , g: Xpath ) 4021endfunc 4022 4023func Test_rethrow_exception_2 () 4024XpathINIT 4025try 4026let caught = 0 4027try 4028Xpath 'a' 4029write /n/o/n/w/r/i/t/a/b/l/e/_/f/i/l/e 4030call assert_report ( 'should not get here' ) 4031catch /^Vim(write):/ 4032let caught = 1 4033throw v: exception " throw error: cannot fake Vim exception 4034catch /.*/ 4035call assert_report ( 'should not get here' ) 4036finally 4037Xpath 'b' 4038call assert_equal ( 1 , caught ) 4039endtry 4040catch /^Vim(throw):/ " catches throw error 4041let caught = caught + 1 4042catch /.*/ 4043call assert_report ( 'should not get here' ) 4044finally 4045Xpath 'c' 4046call assert_equal ( 2 , caught ) 4047endtry 4048call assert_equal ( 'abc' , g: Xpath ) 4049endfunc 4050 4051func Test_rethrow_exception_3 () 4052XpathINIT 4053try 4054let caught = 0 4055try 4056Xpath 'a' 4057asdf 4058catch /^Vim/ " catch error exception 4059let caught = 1 4060" Trigger Vim error exception with value specified after :echoerr 4061let value = substitute ( v: exception , '^Vim\((.*)\)\=:' , '' , "" ) 4062echoerr value 4063catch /.*/ 4064call assert_report ( 'should not get here' ) 4065finally 4066Xpath 'b' 4067call assert_equal ( 1 , caught ) 4068endtry 4069catch /^Vim(echoerr):/ 4070let caught = caught + 1 4071call assert_match ( value , v: exception ) 4072catch /.*/ 4073call assert_report ( 'should not get here' ) 4074finally 4075Xpath 'c' 4076call assert_equal ( 2 , caught ) 4077endtry 4078call assert_equal ( 'abc' , g: Xpath ) 4079endfunc 4080 4081func Test_rethrow_exception_3 () 4082XpathINIT 4083try 4084let errcaught = 0 4085try 4086Xpath 'a' 4087let intcaught = 0 4088call interrupt () 4089catch /^Vim:/ " catch interrupt exception 4090let intcaught = 1 4091" Trigger Vim error exception with value specified after :echoerr 4092echoerr substitute ( v: exception , '^Vim\((.*)\)\=:' , '' , "" ) 4093catch /.*/ 4094call assert_report ( 'should not get here' ) 4095finally 4096Xpath 'b' 4097call assert_equal ( 1 , intcaught ) 4098endtry 4099catch /^Vim(echoerr):/ 4100let errcaught = 1 4101call assert_match ( 'Interrupt' , v: exception ) 4102finally 4103Xpath 'c' 4104call assert_equal ( 1 , errcaught ) 4105endtry 4106call assert_equal ( 'abc' , g: Xpath ) 4107endfunc 4108 4109"------------------------------------------------------------------------------- 4110" Test 61: Catching interrupt exceptions {{{1 4111" 4112" When an interrupt occurs inside a :try/:endtry region, an 4113" interrupt exception is thrown and can be caught. Its value is 4114" "Vim:Interrupt". If the interrupt occurs after an error or a :throw 4115" but before a matching :catch is reached, all following :catches of 4116" that try block are ignored, but the interrupt exception can be 4117" caught by the next surrounding try conditional. An interrupt is 4118" ignored when there is a previous interrupt that has not been caught 4119" or causes a :finally clause to be executed. 4120"------------------------------------------------------------------------------- 4121 4122func Test_catch_intr_exception () 4123let test =<< trim [CODE] 4124while 1 4125try 4126try 4127Xpath 'a' 4128call interrupt() 4129call assert_report('should not get here') 4130catch /^Vim:Interrupt$/ 4131Xpath 'b' 4132finally 4133Xpath 'c' 4134endtry 4135catch /.*/ 4136call assert_report('should not get here') 4137finally 4138Xpath 'd' 4139break 4140endtry 4141endwhile 4142 4143while 1 4144try 4145try 4146try 4147Xpath 'e' 4148asdf 4149call assert_report('should not get here') 4150catch /do_not_catch/ 4151call assert_report('should not get here') 4152catch /.*/ 4153Xpath 'f' 4154call interrupt() 4155call assert_report('should not get here') 4156catch /.*/ 4157call assert_report('should not get here') 4158finally 4159Xpath 'g' 4160call interrupt() 4161call assert_report('should not get here') 4162endtry 4163catch /^Vim:Interrupt$/ 4164Xpath 'h' 4165finally 4166Xpath 'i' 4167endtry 4168catch /.*/ 4169call assert_report('should not get here') 4170finally 4171Xpath 'j' 4172break 4173endtry 4174endwhile 4175 4176while 1 4177try 4178try 4179try 4180Xpath 'k' 4181throw "x" 4182call assert_report('should not get here') 4183catch /do_not_catch/ 4184call assert_report('should not get here') 4185catch /x/ 4186Xpath 'l' 4187call interrupt() 4188call assert_report('should not get here') 4189catch /.*/ 4190call assert_report('should not get here') 4191endtry 4192catch /^Vim:Interrupt$/ 4193Xpath 'm' 4194finally 4195Xpath 'n' 4196endtry 4197catch /.*/ 4198call assert_report('should not get here') 4199finally 4200Xpath 'o' 4201break 4202endtry 4203endwhile 4204 4205while 1 4206try 4207try 4208Xpath 'p' 4209call interrupt() 4210call assert_report('should not get here') 4211catch /do_not_catch/ 4212call interrupt() 4213call assert_report('should not get here') 4214catch /^Vim:Interrupt$/ 4215Xpath 'q' 4216finally 4217Xpath 'r' 4218endtry 4219catch /.*/ 4220call assert_report('should not get here') 4221finally 4222Xpath 's' 4223break 4224endtry 4225endwhile 4226 4227Xpath 't' 4228[CODE] 4229let verify =<< trim [CODE] 4230call assert_equal('abcdefghijklmnopqrst', g:Xpath) 4231[CODE] 4232call RunInNewVim ( test , verify ) 4233endfunc 4234 4235"------------------------------------------------------------------------------- 4236" Test 62: Catching error exceptions {{{1 4237" 4238" An error inside a :try/:endtry region is converted to an exception 4239" and can be caught. The error exception has a "Vim(cmdname):" prefix 4240" where cmdname is the name of the failing command, or a "Vim:" prefix 4241" if no command name is known. The "Vim" prefixes cannot be faked. 4242"------------------------------------------------------------------------------- 4243 4244func Test_catch_err_exception_1 () 4245XpathINIT 4246while 1 4247try 4248try 4249let caught = 0 4250unlet novar 4251catch /^Vim(unlet):/ 4252Xpath 'a' 4253let caught = 1 4254let v: errmsg = substitute ( v: exception , '^Vim(unlet):' , '' , "" ) 4255finally 4256Xpath 'b' 4257call assert_equal ( 1 , caught ) 4258call assert_match ( 'E108: No such variable: "novar"' , v: errmsg ) 4259endtry 4260catch /.*/ 4261call assert_report ( 'should not get here' ) 4262finally 4263Xpath 'c' 4264break 4265endtry 4266call assert_report ( 'should not get here' ) 4267endwhile 4268call assert_equal ( 'abc' , g: Xpath ) 4269endfunc 4270 4271func Test_catch_err_exception_2 () 4272XpathINIT 4273while 1 4274try 4275try 4276let caught = 0 4277throw novar " error in :throw 4278catch /^Vim(throw):/ 4279Xpath 'a' 4280let caught = 1 4281let v: errmsg = substitute ( v: exception , '^Vim(throw):' , '' , "" ) 4282finally 4283Xpath 'b' 4284call assert_equal ( 1 , caught ) 4285call assert_match ( 'E121: Undefined variable: novar' , v: errmsg ) 4286endtry 4287catch /.*/ 4288call assert_report ( 'should not get here' ) 4289finally 4290Xpath 'c' 4291break 4292endtry 4293call assert_report ( 'should not get here' ) 4294endwhile 4295call assert_equal ( 'abc' , g: Xpath ) 4296endfunc 4297 4298func Test_catch_err_exception_3 () 4299XpathINIT 4300while 1 4301try 4302try 4303let caught = 0 4304throw "Vim:faked" " error: cannot fake Vim exception 4305catch /^Vim(throw):/ 4306Xpath 'a' 4307let caught = 1 4308let v: errmsg = substitute ( v: exception , '^Vim(throw):' , '' , "" ) 4309finally 4310Xpath 'b' 4311call assert_equal ( 1 , caught ) 4312call assert_match ( "E608: Cannot :throw exceptions with 'Vim' prefix" , 4313\ v: errmsg ) 4314endtry 4315catch /.*/ 4316call assert_report ( 'should not get here' ) 4317finally 4318Xpath 'c' 4319break 4320endtry 4321call assert_report ( 'should not get here' ) 4322endwhile 4323call assert_equal ( 'abc' , g: Xpath ) 4324endfunc 4325 4326func Test_catch_err_exception_4 () 4327XpathINIT 4328func F () 4329while 1 4330" Missing :endwhile 4331endfunc 4332 4333while 1 4334try 4335try 4336let caught = 0 4337call F () 4338catch /^Vim(endfunction):/ 4339Xpath 'a' 4340let caught = 1 4341let v: errmsg = substitute ( v: exception , '^Vim(endfunction):' , '' , "" ) 4342finally 4343Xpath 'b' 4344call assert_equal ( 1 , caught ) 4345call assert_match ( "E170: Missing :endwhile" , v: errmsg ) 4346endtry 4347catch /.*/ 4348call assert_report ( 'should not get here' ) 4349finally 4350Xpath 'c' 4351break 4352endtry 4353call assert_report ( 'should not get here' ) 4354endwhile 4355call assert_equal ( 'abc' , g: Xpath ) 4356delfunc F 4357endfunc 4358 4359func Test_catch_err_exception_5 () 4360XpathINIT 4361func F () 4362while 1 4363" Missing :endwhile 4364endfunc 4365 4366while 1 4367try 4368try 4369let caught = 0 4370ExecAsScript F 4371catch /^Vim:/ 4372Xpath 'a' 4373let caught = 1 4374let v: errmsg = substitute ( v: exception , '^Vim:' , '' , "" ) 4375finally 4376Xpath 'b' 4377call assert_equal ( 1 , caught ) 4378call assert_match ( "E170: Missing :endwhile" , v: errmsg ) 4379endtry 4380catch /.*/ 4381call assert_report ( 'should not get here' ) 4382finally 4383Xpath 'c' 4384break 4385endtry 4386call assert_report ( 'should not get here' ) 4387endwhile 4388call assert_equal ( 'abc' , g: Xpath ) 4389delfunc F 4390endfunc 4391 4392func Test_catch_err_exception_6 () 4393XpathINIT 4394func G () 4395call G () 4396endfunc 4397 4398while 1 4399try 4400let mfd_save = & mfd 4401set mfd = 3 4402try 4403let caught = 0 4404call G () 4405catch /^Vim(call):/ 4406Xpath 'a' 4407let caught = 1 4408let v: errmsg = substitute ( v: exception , '^Vim(call):' , '' , "" ) 4409finally 4410Xpath 'b' 4411call assert_equal ( 1 , caught ) 4412call assert_match ( "E132: Function call depth is higher than 'maxfuncdepth'" , v: errmsg ) 4413endtry 4414catch /.*/ 4415call assert_report ( 'should not get here' ) 4416finally 4417Xpath 'c' 4418let & mfd = mfd_save 4419break 4420endtry 4421call assert_report ( 'should not get here' ) 4422endwhile 4423call assert_equal ( 'abc' , g: Xpath ) 4424delfunc G 4425endfunc 4426 4427func Test_catch_err_exception_7 () 4428XpathINIT 4429func H () 4430return H () 4431endfunc 4432 4433while 1 4434try 4435let mfd_save = & mfd 4436set mfd = 3 4437try 4438let caught = 0 4439call H () 4440catch /^Vim(return):/ 4441Xpath 'a' 4442let caught = 1 4443let v: errmsg = substitute ( v: exception , '^Vim(return):' , '' , "" ) 4444finally 4445Xpath 'b' 4446call assert_equal ( 1 , caught ) 4447call assert_match ( "E132: Function call depth is higher than 'maxfuncdepth'" , v: errmsg ) 4448endtry 4449catch /.*/ 4450call assert_report ( 'should not get here' ) 4451finally 4452Xpath 'c' 4453let & mfd = mfd_save 4454break " discard error for $VIMNOERRTHROW 4455endtry 4456call assert_report ( 'should not get here' ) 4457endwhile 4458 4459call assert_equal ( 'abc' , g: Xpath ) 4460delfunc H 4461endfunc 4462 4463"------------------------------------------------------------------------------- 4464" Test 63: Suppressing error exceptions by :silent!. {{{1 4465" 4466" A :silent! command inside a :try/:endtry region suppresses the 4467" conversion of errors to an exception and the immediate abortion on 4468" error. When the commands executed by the :silent! themselves open 4469" a new :try/:endtry region, conversion of errors to exception and 4470" immediate abortion is switched on again - until the next :silent! 4471" etc. The :silent! has the effect of setting v:errmsg to the error 4472" message text (without displaying it) and continuing with the next 4473" script line. 4474" 4475" When a command triggering autocommands is executed by :silent! 4476" inside a :try/:endtry, the autocommand execution is not suppressed 4477" on error. 4478" 4479" This test reuses the function MSG() from the previous test. 4480"------------------------------------------------------------------------------- 4481 4482func Test_silent_exception () 4483XpathINIT 4484XloopINIT 4485let g: taken = "" 4486 4487func S ( n ) abort 4488XloopNEXT 4489let g: taken = g: taken . "E" . a: n 4490let v: errmsg = "" 4491exec "asdf" . a: n 4492 4493" Check that ":silent!" continues: 4494Xloop 'a' 4495 4496" Check that ":silent!" sets "v:errmsg": 4497call assert_match ( "E492: Not an editor command" , v: errmsg ) 4498endfunc 4499 4500func Foo () 4501while 1 4502try 4503try 4504let caught = 0 4505" This is not silent: 4506call S ( 3 ) 4507catch /^Vim:/ 4508Xpath 'b' 4509let caught = 1 4510let errmsg3 = substitute ( v: exception , '^Vim:' , '' , "" ) 4511silent ! call S ( 4 ) 4512finally 4513call assert_equal ( 1 , caught ) 4514Xpath 'c' 4515call assert_match ( "E492: Not an editor command" , errmsg3 ) 4516silent ! call S ( 5 ) 4517" Break out of try conditionals that cover ":silent!". This also 4518" discards the aborting error when $VIMNOERRTHROW is non-zero. 4519break 4520endtry 4521catch /.*/ 4522call assert_report ( 'should not get here' ) 4523endtry 4524endwhile 4525" This is a double ":silent!" (see caller). 4526silent ! call S ( 6 ) 4527endfunc 4528 4529func Bar () 4530try 4531silent ! call S ( 2 ) 4532silent ! execute "call Foo() | call S(7)" 4533silent ! call S ( 8 ) 4534endtry " normal end of try cond that covers ":silent!" 4535" This has a ":silent!" from the caller: 4536call S ( 9 ) 4537endfunc 4538 4539silent ! call S ( 1 ) 4540silent ! call Bar () 4541silent ! call S ( 10 ) 4542 4543call assert_equal ( "E1E2E3E4E5E6E7E8E9E10" , g: taken ) 4544 4545augroup TMP 4546au ! 4547autocmd BufWritePost * Xpath 'd' 4548augroup END 4549 4550Xpath 'e' 4551silent ! write /i/m/p/o/s/s/i/b/l/e 4552Xpath 'f' 4553 4554call assert_equal ( 'a2a3ba5ca6a7a8a9a10a11edf' , g: Xpath ) 4555 4556augroup TMP 4557au ! 4558augroup END 4559augroup ! TMP 4560delfunction S 4561delfunction Foo 4562delfunction Bar 4563endfunc 4564 4565"------------------------------------------------------------------------------- 4566" Test 64: Error exceptions after error, interrupt or :throw {{{1 4567" 4568" When an error occurs after an interrupt or a :throw but before 4569" a matching :catch is reached, all following :catches of that try 4570" block are ignored, but the error exception can be caught by the next 4571" surrounding try conditional. Any previous error exception is 4572" discarded. An error is ignored when there is a previous error that 4573" has not been caught. 4574"------------------------------------------------------------------------------- 4575 4576func Test_exception_after_error_1 () 4577XpathINIT 4578while 1 4579try 4580try 4581Xpath 'a' 4582let caught = 0 4583while 1 4584if 1 4585" Missing :endif 4586endwhile " throw error exception 4587catch /^Vim(/ 4588Xpath 'b' 4589let caught = 1 4590finally 4591Xpath 'c' 4592call assert_equal ( 1 , caught ) 4593endtry 4594catch /.*/ 4595call assert_report ( 'should not get here' ) 4596finally 4597Xpath 'd' 4598break 4599endtry 4600call assert_report ( 'should not get here' ) 4601endwhile 4602call assert_equal ( 'abcd' , g: Xpath ) 4603endfunc 4604 4605func Test_exception_after_error_2 () 4606XpathINIT 4607while 1 4608try 4609try 4610Xpath 'a' 4611let caught = 0 4612try 4613if 1 4614" Missing :endif 4615catch /.*/ " throw error exception 4616call assert_report ( 'should not get here' ) 4617catch /.*/ 4618call assert_report ( 'should not get here' ) 4619endtry 4620catch /^Vim(/ 4621Xpath 'b' 4622let caught = 1 4623finally 4624Xpath 'c' 4625call assert_equal ( 1 , caught ) 4626endtry 4627catch /.*/ 4628call assert_report ( 'should not get here' ) 4629finally 4630Xpath 'd' 4631break 4632endtry 4633call assert_report ( 'should not get here' ) 4634endwhile 4635call assert_equal ( 'abcd' , g: Xpath ) 4636endfunc 4637 4638func Test_exception_after_error_3 () 4639XpathINIT 4640while 1 4641try 4642try 4643let caught = 0 4644try 4645Xpath 'a' 4646call interrupt () 4647catch /do_not_catch/ 4648call assert_report ( 'should not get here' ) 4649if 1 4650" Missing :endif 4651catch /.*/ " throw error exception 4652call assert_report ( 'should not get here' ) 4653catch /.*/ 4654call assert_report ( 'should not get here' ) 4655endtry 4656catch /^Vim(/ 4657Xpath 'b' 4658let caught = 1 4659finally 4660Xpath 'c' 4661call assert_equal ( 1 , caught ) 4662endtry 4663catch /.*/ 4664call assert_report ( 'should not get here' ) 4665finally 4666Xpath 'd' 4667break 4668endtry 4669call assert_report ( 'should not get here' ) 4670endwhile 4671call assert_equal ( 'abcd' , g: Xpath ) 4672endfunc 4673 4674func Test_exception_after_error_4 () 4675XpathINIT 4676while 1 4677try 4678try 4679let caught = 0 4680try 4681Xpath 'a' 4682throw "x" 4683catch /do_not_catch/ 4684call assert_report ( 'should not get here' ) 4685if 1 4686" Missing :endif 4687catch /x/ " throw error exception 4688call assert_report ( 'should not get here' ) 4689catch /.*/ 4690call assert_report ( 'should not get here' ) 4691endtry 4692catch /^Vim(/ 4693Xpath 'b' 4694let caught = 1 4695finally 4696Xpath 'c' 4697call assert_equal ( 1 , caught ) 4698endtry 4699catch /.*/ 4700call assert_report ( 'should not get here' ) 4701finally 4702Xpath 'd' 4703break 4704endtry 4705call assert_report ( 'should not get here' ) 4706endwhile 4707call assert_equal ( 'abcd' , g: Xpath ) 4708endfunc 4709 4710func Test_exception_after_error_5 () 4711XpathINIT 4712while 1 4713try 4714try 4715let caught = 0 4716Xpath 'a' 4717endif " :endif without :if; throw error exception 4718if 1 4719" Missing :endif 4720catch /do_not_catch/ " ignore new error 4721call assert_report ( 'should not get here' ) 4722catch /^Vim(endif):/ 4723Xpath 'b' 4724let caught = 1 4725catch /^Vim(/ 4726call assert_report ( 'should not get here' ) 4727finally 4728Xpath 'c' 4729call assert_equal ( 1 , caught ) 4730endtry 4731catch /.*/ 4732call assert_report ( 'should not get here' ) 4733finally 4734Xpath 'd' 4735break 4736endtry 4737call assert_report ( 'should not get here' ) 4738endwhile 4739call assert_equal ( 'abcd' , g: Xpath ) 4740endfunc 4741 4742"------------------------------------------------------------------------------- 4743" Test 65: Errors in the /pattern/ argument of a :catch {{{1 4744" 4745" On an error in the /pattern/ argument of a :catch, the :catch does 4746" not match. Any following :catches of the same :try/:endtry don't 4747" match either. Finally clauses are executed. 4748"------------------------------------------------------------------------------- 4749 4750func Test_catch_pattern_error () 4751CheckEnglish 4752XpathINIT 4753 4754try 4755try 4756Xpath 'a' 4757throw "oops" 4758catch /^oops$/ 4759Xpath 'b' 4760catch /\)/ " not checked; exception has already been caught 4761call assert_report ( 'should not get here' ) 4762endtry 4763Xpath 'c' 4764catch /.*/ 4765call assert_report ( 'should not get here' ) 4766endtry 4767call assert_equal ( 'abc' , g: Xpath ) 4768 4769XpathINIT 4770func F () 4771try 4772try 4773try 4774Xpath 'a' 4775throw "ab" 4776catch /abc/ " does not catch 4777call assert_report ( 'should not get here' ) 4778catch /\)/ " error; discards exception 4779call assert_report ( 'should not get here' ) 4780catch /.*/ " not checked 4781call assert_report ( 'should not get here' ) 4782finally 4783Xpath 'b' 4784endtry 4785call assert_report ( 'should not get here' ) 4786catch /^ab$/ " checked, but original exception is discarded 4787call assert_report ( 'should not get here' ) 4788catch /^Vim(catch):/ 4789Xpath 'c' 4790call assert_match ( 'Vim(catch):E475: Invalid argument:' , v: exception ) 4791finally 4792Xpath 'd' 4793endtry 4794Xpath 'e' 4795catch /.*/ 4796call assert_report ( 'should not get here' ) 4797endtry 4798Xpath 'f' 4799endfunc 4800 4801call F () 4802call assert_equal ( 'abcdef' , g: Xpath ) 4803 4804delfunc F 4805endfunc 4806 4807"------------------------------------------------------------------------------- 4808" Test 66: Stop range :call on error, interrupt, or :throw {{{1 4809" 4810" When a function which is multiply called for a range since it 4811" doesn't handle the range itself has an error in a command 4812" dynamically enclosed by :try/:endtry or gets an interrupt or 4813" executes a :throw, no more calls for the remaining lines in the 4814" range are made. On an error in a command not dynamically enclosed 4815" by :try/:endtry, the function is executed again for the remaining 4816" lines in the range. 4817"------------------------------------------------------------------------------- 4818 4819func Test_stop_range_on_error () 4820let test =<< trim [CODE] 4821let file = tempname() 4822exec "edit" file 4823call setline(1, ['line 1', 'line 2', 'line 3']) 4824let taken = "" 4825let expected = "G1EF1E(1)F1E(2)F1E(3)G2EF2E(1)G3IF3I(1)G4TF4T(1)G5AF5A(1)" 4826 4827func F(reason, n) abort 4828let g:taken = g:taken .. "F" .. a:n .. 4829\ substitute(a:reason, '\(\l\).*', '\u\1', "") .. 4830\ "(" .. line(".") .. ")" 4831 4832if a:reason == "error" 4833asdf 4834elseif a:reason == "interrupt" 4835call interrupt() 4836elseif a:reason == "throw" 4837throw "xyz" 4838elseif a:reason == "aborting error" 4839XloopNEXT 4840call assert_equal(g:taken, g:expected) 4841try 4842bwipeout! 4843call delete(g:file) 4844asdf 4845endtry 4846endif 4847endfunc 4848 4849func G(reason, n) 4850let g:taken = g:taken .. "G" .. a:n .. 4851\ substitute(a:reason, '\(\l\).*', '\u\1', "") 48521,3call F(a:reason, a:n) 4853endfunc 4854 4855Xpath 'a' 4856call G("error", 1) 4857try 4858Xpath 'b' 4859try 4860call G("error", 2) 4861call assert_report('should not get here') 4862finally 4863Xpath 'c' 4864try 4865call G("interrupt", 3) 4866call assert_report('should not get here') 4867finally 4868Xpath 'd' 4869try 4870call G("throw", 4) 4871call assert_report('should not get here') 4872endtry 4873endtry 4874endtry 4875catch /xyz/ 4876Xpath 'e' 4877catch /.*/ 4878call assert_report('should not get here') 4879endtry 4880Xpath 'f' 4881call G("aborting error", 5) 4882call assert_report('should not get here') 4883[CODE] 4884let verify =<< trim [CODE] 4885call assert_equal('abcdef', g:Xpath) 4886[CODE] 4887call RunInNewVim ( test , verify ) 4888endfunc 4889 4890"------------------------------------------------------------------------------- 4891" Test 67: :throw across :call command {{{1 4892" 4893" On a call command, an exception might be thrown when evaluating the 4894" function name, during evaluation of the arguments, or when the 4895" function is being executed. The exception can be caught by the 4896" caller. 4897"------------------------------------------------------------------------------- 4898 4899func THROW ( x , n ) 4900if a: n == 1 4901Xpath 'A' 4902elseif a: n == 2 4903Xpath 'B' 4904elseif a: n == 3 4905Xpath 'C' 4906endif 4907throw a: x 4908endfunc 4909 4910func NAME ( x , n ) 4911if a: n == 1 4912call assert_report ( 'should not get here' ) 4913elseif a: n == 2 4914Xpath 'D' 4915elseif a: n == 3 4916Xpath 'E' 4917elseif a: n == 4 4918Xpath 'F' 4919endif 4920return a: x 4921endfunc 4922 4923func ARG ( x , n ) 4924if a: n == 1 4925call assert_report ( 'should not get here' ) 4926elseif a: n == 2 4927call assert_report ( 'should not get here' ) 4928elseif a: n == 3 4929Xpath 'G' 4930elseif a: n == 4 4931Xpath 'I' 4932endif 4933return a: x 4934endfunc 4935 4936func Test_throw_across_call_cmd () 4937XpathINIT 4938 4939func F ( x , n ) 4940if a: n == 2 4941call assert_report ( 'should not get here' ) 4942elseif a: n == 4 4943Xpath 'a' 4944endif 4945endfunc 4946 4947while 1 4948try 4949let v: errmsg = "" 4950 4951while 1 4952try 4953Xpath 'b' 4954call { NAME ( THROW ( "name" , 1 ), 1 )}( ARG ( 4711 , 1 ), 1 ) 4955call assert_report ( 'should not get here' ) 4956catch /^name$/ 4957Xpath 'c' 4958catch /.*/ 4959call assert_report ( 'should not get here' ) 4960finally 4961call assert_equal ( "" , v: errmsg ) 4962let v: errmsg = "" 4963break 4964endtry 4965endwhile 4966 4967while 1 4968try 4969Xpath 'd' 4970call { NAME ( "F" , 2 )}( ARG ( THROW ( "arg" , 2 ), 2 ), 2 ) 4971call assert_report ( 'should not get here' ) 4972catch /^arg$/ 4973Xpath 'e' 4974catch /.*/ 4975call assert_report ( 'should not get here' ) 4976finally 4977call assert_equal ( "" , v: errmsg ) 4978let v: errmsg = "" 4979break 4980endtry 4981endwhile 4982 4983while 1 4984try 4985Xpath 'f' 4986call { NAME ( "THROW" , 3 )}( ARG ( "call" , 3 ), 3 ) 4987call assert_report ( 'should not get here' ) 4988catch /^call$/ 4989Xpath 'g' 4990catch /^0$/ " default return value 4991call assert_report ( 'should not get here' ) 4992catch /.*/ 4993call assert_report ( 'should not get here' ) 4994finally 4995call assert_equal ( "" , v: errmsg ) 4996let v: errmsg = "" 4997break 4998endtry 4999endwhile 5000 5001while 1 5002try 5003Xpath 'h' 5004call { NAME ( "F" , 4 )}( ARG ( 4711 , 4 ), 4 ) 5005Xpath 'i' 5006catch /.*/ 5007call assert_report ( 'should not get here' ) 5008finally 5009call assert_equal ( "" , v: errmsg ) 5010let v: errmsg = "" 5011break 5012endtry 5013endwhile 5014 5015catch /^0$/ " default return value 5016call assert_report ( 'should not get here' ) 5017catch /.*/ 5018call assert_report ( 'should not get here' ) 5019finally 5020call assert_equal ( "" , v: errmsg ) 5021let v: errmsg = "" 5022break 5023endtry 5024endwhile 5025 5026call assert_equal ( 'bAcdDBefEGCghFIai' , g: Xpath ) 5027delfunction F 5028endfunc 5029 5030"------------------------------------------------------------------------------- 5031" Test 68: :throw across function calls in expressions {{{1 5032" 5033" On a function call within an expression, an exception might be 5034" thrown when evaluating the function name, during evaluation of the 5035" arguments, or when the function is being executed. The exception 5036" can be caught by the caller. 5037" 5038" This test reuses the functions THROW(), NAME(), and ARG() from the 5039" previous test. 5040"------------------------------------------------------------------------------- 5041 5042func Test_throw_across_call_expr () 5043XpathINIT 5044 5045func F ( x , n ) 5046if a: n == 2 5047call assert_report ( 'should not get here' ) 5048elseif a: n == 4 5049Xpath 'a' 5050endif 5051return a: x 5052endfunction 5053 5054while 1 5055try 5056let error = 0 5057let v: errmsg = "" 5058 5059while 1 5060try 5061Xpath 'b' 5062let var1 = { NAME ( THROW ( "name" , 1 ), 1 )}( ARG ( 4711 , 1 ), 1 ) 5063call assert_report ( 'should not get here' ) 5064catch /^name$/ 5065Xpath 'c' 5066catch /.*/ 5067call assert_report ( 'should not get here' ) 5068finally 5069call assert_equal ( "" , v: errmsg ) 5070let v: errmsg = "" 5071break 5072endtry 5073endwhile 5074call assert_true ( ! exists ( 'var1' )) 5075 5076while 1 5077try 5078Xpath 'd' 5079let var2 = { NAME ( "F" , 2 )}( ARG ( THROW ( "arg" , 2 ), 2 ), 2 ) 5080call assert_report ( 'should not get here' ) 5081catch /^arg$/ 5082Xpath 'e' 5083catch /.*/ 5084call assert_report ( 'should not get here' ) 5085finally 5086call assert_equal ( "" , v: errmsg ) 5087let v: errmsg = "" 5088break 5089endtry 5090endwhile 5091call assert_true ( ! exists ( 'var2' )) 5092 5093while 1 5094try 5095Xpath 'f' 5096let var3 = { NAME ( "THROW" , 3 )}( ARG ( "call" , 3 ), 3 ) 5097call assert_report ( 'should not get here' ) 5098catch /^call$/ 5099Xpath 'g' 5100catch /^0$/ " default return value 5101call assert_report ( 'should not get here' ) 5102catch /.*/ 5103call assert_report ( 'should not get here' ) 5104finally 5105call assert_equal ( "" , v: errmsg ) 5106let v: errmsg = "" 5107break 5108endtry 5109endwhile 5110call assert_true ( ! exists ( 'var3' )) 5111 5112while 1 5113try 5114Xpath 'h' 5115let var4 = { NAME ( "F" , 4 )}( ARG ( 4711 , 4 ), 4 ) 5116Xpath 'i' 5117catch /.*/ 5118call assert_report ( 'should not get here' ) 5119finally 5120call assert_equal ( "" , v: errmsg ) 5121let v: errmsg = "" 5122break 5123endtry 5124endwhile 5125call assert_true ( exists ( 'var4' ) && var4 == 4711 ) 5126 5127catch /^0$/ " default return value 5128call assert_report ( 'should not get here' ) 5129catch /.*/ 5130call assert_report ( 'should not get here' ) 5131finally 5132call assert_equal ( "" , v: errmsg ) 5133break 5134endtry 5135endwhile 5136 5137call assert_equal ( 'bAcdDBefEGCghFIai' , g: Xpath ) 5138delfunc F 5139endfunc 5140 5141"------------------------------------------------------------------------------- 5142" Test 76: Errors, interrupts, :throw during expression evaluation {{{1 5143" 5144" When a function call made during expression evaluation is aborted 5145" due to an error inside a :try/:endtry region or due to an interrupt 5146" or a :throw, the expression evaluation is aborted as well. No 5147" message is displayed for the cancelled expression evaluation. On an 5148" error not inside :try/:endtry, the expression evaluation continues. 5149"------------------------------------------------------------------------------- 5150 5151func Test_expr_eval_error () 5152let test =<< trim [CODE] 5153let taken = "" 5154 5155func ERR(n) 5156let g:taken = g:taken .. "E" .. a:n 5157asdf 5158endfunc 5159 5160func ERRabort(n) abort 5161let g:taken = g:taken .. "A" .. a:n 5162asdf 5163endfunc " returns -1; may cause follow-up msg for illegal var/func name 5164 5165func WRAP(n, arg) 5166let g:taken = g:taken .. "W" .. a:n 5167let g:saved_errmsg = v:errmsg 5168return arg 5169endfunc 5170 5171func INT(n) 5172let g:taken = g:taken .. "I" .. a:n 5173call interrupt() 5174endfunc 5175 5176func THR(n) 5177let g:taken = g:taken .. "T" .. a:n 5178throw "should not be caught" 5179endfunc 5180 5181func CONT(n) 5182let g:taken = g:taken .. "C" .. a:n 5183endfunc 5184 5185func MSG(n) 5186let g:taken = g:taken .. "M" .. a:n 5187let errmsg = (a:n >= 37 && a:n <= 44) ? g:saved_errmsg : v:errmsg 5188let msgptn = (a:n >= 10 && a:n <= 27) ? "^$" : "asdf" 5189call assert_match(msgptn, errmsg) 5190let v:errmsg = "" 5191let g:saved_errmsg = "" 5192endfunc 5193 5194let v:errmsg = "" 5195 5196try 5197let t = 1 5198while t <= 9 5199Xloop 'a' 5200try 5201if t == 1 5202let v{ERR(t) + CONT(t)} = 0 5203elseif t == 2 5204let v{ERR(t) + CONT(t)} 5205elseif t == 3 5206let var = exists('v{ERR(t) + CONT(t)}') 5207elseif t == 4 5208unlet v{ERR(t) + CONT(t)} 5209elseif t == 5 5210function F{ERR(t) + CONT(t)}() 5211endfunction 5212elseif t == 6 5213function F{ERR(t) + CONT(t)} 5214elseif t == 7 5215let var = exists('*F{ERR(t) + CONT(t)}') 5216elseif t == 8 5217delfunction F{ERR(t) + CONT(t)} 5218elseif t == 9 5219let var = ERR(t) + CONT(t) 5220endif 5221catch /asdf/ 5222" v:errmsg is not set when the error message is converted to an 5223" exception. Set it to the original error message. 5224let v:errmsg = substitute(v:exception, '^Vim:', '', "") 5225catch /^Vim\((\a\+)\)\=:/ 5226" An error exception has been thrown after the original error. 5227let v:errmsg = "" 5228finally 5229call MSG(t) 5230let t = t + 1 5231XloopNEXT 5232continue " discard an aborting error 5233endtry 5234endwhile 5235catch /.*/ 5236call assert_report('should not get here') 5237endtry 5238 5239try 5240let t = 10 5241while t <= 18 5242Xloop 'b' 5243try 5244if t == 10 5245let v{INT(t) + CONT(t)} = 0 5246elseif t == 11 5247let v{INT(t) + CONT(t)} 5248elseif t == 12 5249let var = exists('v{INT(t) + CONT(t)}') 5250elseif t == 13 5251unlet v{INT(t) + CONT(t)} 5252elseif t == 14 5253function F{INT(t) + CONT(t)}() 5254endfunction 5255elseif t == 15 5256function F{INT(t) + CONT(t)} 5257elseif t == 16 5258let var = exists('*F{INT(t) + CONT(t)}') 5259elseif t == 17 5260delfunction F{INT(t) + CONT(t)} 5261elseif t == 18 5262let var = INT(t) + CONT(t) 5263endif 5264catch /^Vim\((\a\+)\)\=:\(Interrupt\)\@!/ 5265" An error exception has been triggered after the interrupt. 5266let v:errmsg = substitute(v:exception, '^Vim\((\a\+)\)\=:', '', "") 5267finally 5268call MSG(t) 5269let t = t + 1 5270XloopNEXT 5271continue " discard interrupt 5272endtry 5273endwhile 5274catch /.*/ 5275call assert_report('should not get here') 5276endtry 5277 5278try 5279let t = 19 5280while t <= 27 5281Xloop 'c' 5282try 5283if t == 19 5284let v{THR(t) + CONT(t)} = 0 5285elseif t == 20 5286let v{THR(t) + CONT(t)} 5287elseif t == 21 5288let var = exists('v{THR(t) + CONT(t)}') 5289elseif t == 22 5290unlet v{THR(t) + CONT(t)} 5291elseif t == 23 5292function F{THR(t) + CONT(t)}() 5293endfunction 5294elseif t == 24 5295function F{THR(t) + CONT(t)} 5296elseif t == 25 5297let var = exists('*F{THR(t) + CONT(t)}') 5298elseif t == 26 5299delfunction F{THR(t) + CONT(t)} 5300elseif t == 27 5301let var = THR(t) + CONT(t) 5302endif 5303catch /^Vim\((\a\+)\)\=:/ 5304" An error exception has been triggered after the :throw. 5305let v:errmsg = substitute(v:exception, '^Vim\((\a\+)\)\=:', '', "") 5306finally 5307call MSG(t) 5308let t = t + 1 5309XloopNEXT 5310continue " discard exception 5311endtry 5312endwhile 5313catch /.*/ 5314call assert_report('should not get here') 5315endtry 5316 5317let v{ERR(28) + CONT(28)} = 0 5318call MSG(28) 5319let v{ERR(29) + CONT(29)} 5320call MSG(29) 5321let var = exists('v{ERR(30) + CONT(30)}') 5322call MSG(30) 5323unlet v{ERR(31) + CONT(31)} 5324call MSG(31) 5325function F{ERR(32) + CONT(32)}() 5326endfunction 5327call MSG(32) 5328function F{ERR(33) + CONT(33)} 5329call MSG(33) 5330let var = exists('*F{ERR(34) + CONT(34)}') 5331call MSG(34) 5332delfunction F{ERR(35) + CONT(35)} 5333call MSG(35) 5334let var = ERR(36) + CONT(36) 5335call MSG(36) 5336 5337let saved_errmsg = "" 5338 5339let v{WRAP(37, ERRabort(37)) + CONT(37)} = 0 5340call MSG(37) 5341let v{WRAP(38, ERRabort(38)) + CONT(38)} 5342call MSG(38) 5343let var = exists('v{WRAP(39, ERRabort(39)) + CONT(39)}') 5344call MSG(39) 5345unlet v{WRAP(40, ERRabort(40)) + CONT(40)} 5346call MSG(40) 5347function F{WRAP(41, ERRabort(41)) + CONT(41)}() 5348endfunction 5349call MSG(41) 5350function F{WRAP(42, ERRabort(42)) + CONT(42)} 5351call MSG(42) 5352let var = exists('*F{WRAP(43, ERRabort(43)) + CONT(43)}') 5353call MSG(43) 5354delfunction F{WRAP(44, ERRabort(44)) + CONT(44)} 5355call MSG(44) 5356let var = ERRabort(45) + CONT(45) 5357call MSG(45) 5358Xpath 'd' 5359 5360let expected = "" 5361\ .. "E1M1E2M2E3M3E4M4E5M5E6M6E7M7E8M8E9M9" 5362\ .. "I10M10I11M11I12M12I13M13I14M14I15M15I16M16I17M17I18M18" 5363\ .. "T19M19T20M20T21M21T22M22T23M23T24M24T25M25T26M26T27M27" 5364\ .. "E28C28M28E29C29M29E30C30M30E31C31M31E32C32M32E33C33M33" 5365\ .. "E34C34M34E35C35M35E36C36M36" 5366\ .. "A37W37C37M37A38W38C38M38A39W39C39M39A40W40C40M40A41W41C41M41" 5367\ .. "A42W42C42M42A43W43C43M43A44W44C44M44A45C45M45" 5368call assert_equal(expected, taken) 5369[CODE] 5370let verify =<< trim [CODE] 5371let expected = "a1a2a3a4a5a6a7a8a9" 5372\ .. "b10b11b12b13b14b15b16b17b18" 5373\ .. "c19c20c21c22c23c24c25c26c27d" 5374call assert_equal(expected, g:Xpath) 5375[CODE] 5376call RunInNewVim ( test , verify ) 5377endfunc 5378 5379"------------------------------------------------------------------------------- 5380" Test 77: Errors, interrupts, :throw in name{brace-expression} {{{1 5381" 5382" When a function call made during evaluation of an expression in 5383" braces as part of a function name after ":function" is aborted due 5384" to an error inside a :try/:endtry region or due to an interrupt or 5385" a :throw, the expression evaluation is aborted as well, and the 5386" function definition is ignored, skipping all commands to the 5387" ":endfunction". On an error not inside :try/:endtry, the expression 5388" evaluation continues and the function gets defined, and can be 5389" called and deleted. 5390"------------------------------------------------------------------------------- 5391func Test_brace_expr_error () 5392let test =<< trim [CODE] 5393func ERR() abort 5394Xloop 'a' 5395asdf 5396endfunc " returns -1 5397 5398func OK() 5399Xloop 'b' 5400let v:errmsg = "" 5401return 0 5402endfunc 5403 5404let v:errmsg = "" 5405 5406Xpath 'c' 5407func F{1 + ERR() + OK()}(arg) 5408" F0 should be defined. 5409if exists("a:arg") && a:arg == "calling" 5410Xpath 'd' 5411else 5412call assert_report('should not get here') 5413endif 5414endfunction 5415call assert_equal("", v:errmsg) 5416XloopNEXT 5417 5418Xpath 'e' 5419call F{1 + ERR() + OK()}("calling") 5420call assert_equal("", v:errmsg) 5421XloopNEXT 5422 5423Xpath 'f' 5424delfunction F{1 + ERR() + OK()} 5425call assert_equal("", v:errmsg) 5426XloopNEXT 5427 5428try 5429while 1 5430try 5431Xpath 'g' 5432func G{1 + ERR() + OK()}(arg) 5433" G0 should not be defined, and the function body should be 5434" skipped. 5435call assert_report('should not get here') 5436" Use an unmatched ":finally" to check whether the body is 5437" skipped when an error occurs in ERR(). This works whether or 5438" not the exception is converted to an exception. 5439finally 5440call assert_report('should not get here') 5441endtry 5442try 5443call assert_report('should not get here') 5444endfunction 5445 5446call assert_report('should not get here') 5447catch /asdf/ 5448" Jumped to when the function is not defined and the body is 5449" skipped. 5450Xpath 'h' 5451catch /.*/ 5452call assert_report('should not get here') 5453finally 5454Xpath 'i' 5455break 5456endtry " jumped to when the body is not skipped 5457endwhile 5458catch /.*/ 5459call assert_report('should not get here') 5460endtry 5461[CODE] 5462let verify =<< trim [CODE] 5463call assert_equal('ca1b1ea2b2dfa3b3ga4hi', g:Xpath) 5464[CODE] 5465call RunInNewVim ( test , verify ) 5466endfunc 5467 5468"------------------------------------------------------------------------------- 5469" Test 78: Messages on parsing errors in expression evaluation {{{1 5470" 5471" When an expression evaluation detects a parsing error, an error 5472" message is given and converted to an exception, and the expression 5473" evaluation is aborted. 5474"------------------------------------------------------------------------------- 5475func Test_expr_eval_error_msg () 5476CheckEnglish 5477 5478let test =<< trim [CODE] 5479let taken = "" 5480 5481func F(n) 5482let g:taken = g:taken . "F" . a:n 5483endfunc 5484 5485func MSG(n, enr, emsg) 5486let g:taken = g:taken . "M" . a:n 5487call assert_match('^' .. a:enr .. ':', v:errmsg) 5488call assert_match(a:emsg, v:errmsg) 5489endfunc 5490 5491func CONT(n) 5492let g:taken = g:taken . "C" . a:n 5493endfunc 5494 5495let v:errmsg = "" 5496try 5497let t = 1 5498while t <= 14 5499let g:taken = g:taken . "T" . t 5500let v:errmsg = "" 5501try 5502if t == 1 5503let v{novar + CONT(t)} = 0 5504elseif t == 2 5505let v{novar + CONT(t)} 5506elseif t == 3 5507let var = exists('v{novar + CONT(t)}') 5508elseif t == 4 5509unlet v{novar + CONT(t)} 5510elseif t == 5 5511function F{novar + CONT(t)}() 5512endfunction 5513elseif t == 6 5514function F{novar + CONT(t)} 5515elseif t == 7 5516let var = exists('*F{novar + CONT(t)}') 5517elseif t == 8 5518delfunction F{novar + CONT(t)} 5519elseif t == 9 5520echo novar + CONT(t) 5521elseif t == 10 5522echo v{novar + CONT(t)} 5523elseif t == 11 5524echo F{novar + CONT(t)} 5525elseif t == 12 5526let var = novar + CONT(t) 5527elseif t == 13 5528let var = v{novar + CONT(t)} 5529elseif t == 14 5530let var = F{novar + CONT(t)}() 5531endif 5532catch /^Vim\((\a\+)\)\=:/ 5533Xloop 'a' 5534" v:errmsg is not set when the error message is converted to an 5535" exception. Set it to the original error message. 5536let v:errmsg = substitute(v:exception, '^Vim\((\a\+)\)\=:', '', "") 5537finally 5538Xloop 'b' 5539if t <= 8 && t != 3 && t != 7 5540call MSG(t, 'E475', 'Invalid argument\>') 5541else 5542call MSG(t, 'E121', "Undefined variable") 5543endif 5544let t = t + 1 5545XloopNEXT 5546continue " discard an aborting error 5547endtry 5548endwhile 5549catch /.*/ 5550call assert_report('should not get here') 5551endtry 5552 5553func T(n, expr, enr, emsg) 5554try 5555let g:taken = g:taken . "T" . a:n 5556let v:errmsg = "" 5557try 5558execute "let var = " . a:expr 5559catch /^Vim\((\a\+)\)\=:/ 5560Xloop 'c' 5561" v:errmsg is not set when the error message is converted to an 5562" exception. Set it to the original error message. 5563let v:errmsg = substitute(v:exception, '^Vim\((\a\+)\)\=:', '', "") 5564finally 5565Xloop 'd' 5566call MSG(a:n, a:enr, a:emsg) 5567XloopNEXT 5568" Discard an aborting error: 5569return 5570endtry 5571catch /.*/ 5572call assert_report('should not get here') 5573endtry 5574endfunc 5575 5576call T(15, 'Nofunc() + CONT(15)', 'E117', "Unknown function") 5577call T(16, 'F(1 2 + CONT(16))', 'E116', "Invalid arguments") 5578call T(17, 'F(1, 2) + CONT(17)', 'E118', "Too many arguments") 5579call T(18, 'F() + CONT(18)', 'E119', "Not enough arguments") 5580call T(19, '{(1} + CONT(19)', 'E110', "Missing ')'") 5581call T(20, '("abc"[1) + CONT(20)', 'E111', "Missing ']'") 5582call T(21, '(1 +) + CONT(21)', 'E15', "Invalid expression") 5583call T(22, '1 2 + CONT(22)', 'E488', "Trailing characters: 2 +") 5584call T(23, '(1 ? 2) + CONT(23)', 'E109', "Missing ':' after '?'") 5585call T(24, '("abc) + CONT(24)', 'E114', "Missing quote") 5586call T(25, "('abc) + CONT(25)", 'E115', "Missing quote") 5587call T(26, '& + CONT(26)', 'E112', "Option name missing") 5588call T(27, '&asdf + CONT(27)', 'E113', "Unknown option") 5589 5590let expected = "" 5591\ .. "T1M1T2M2T3M3T4M4T5M5T6M6T7M7T8M8T9M9T10M10T11M11T12M12T13M13T14M14" 5592\ .. "T15M15T16M16T17M17T18M18T19M19T20M20T21M21T22M22T23M23T24M24T25M25" 5593\ .. "T26M26T27M27" 5594 5595call assert_equal(expected, taken) 5596[CODE] 5597let verify =<< trim [CODE] 5598let expected = "a1b1a2b2a3b3a4b4a5b5a6b6a7b7a8b8a9b9a10b10a11b11a12b12" 5599\ .. "a13b13a14b14c15d15c16d16c17d17c18d18c19d19c20d20" 5600\ .. "c21d21c22d22c23d23c24d24c25d25c26d26c27d27" 5601call assert_equal(expected, g:Xpath) 5602[CODE] 5603call RunInNewVim ( test , verify ) 5604endfunc 5605 5606"------------------------------------------------------------------------------- 5607" Test 79: Throwing one of several errors for the same command {{{1 5608" 5609" When several errors appear in a row (for instance during expression 5610" evaluation), the first as the most specific one is used when 5611" throwing an error exception. If, however, a syntax error is 5612" detected afterwards, this one is used for the error exception. 5613" On a syntax error, the next command is not executed, on a normal 5614" error, however, it is (relevant only in a function without the 5615" "abort" flag). v:errmsg is not set. 5616" 5617" If throwing error exceptions is configured off, v:errmsg is always 5618" set to the latest error message, that is, to the more general 5619" message or the syntax error, respectively. 5620"------------------------------------------------------------------------------- 5621func Test_throw_multi_error () 5622CheckEnglish 5623 5624let test =<< trim [CODE] 5625func NEXT(cmd) 5626exec a:cmd . " | Xloop 'a'" 5627endfun 5628 5629call NEXT('echo novar') " (checks nextcmd) 5630XloopNEXT 5631call NEXT('let novar #') " (skips nextcmd) 5632XloopNEXT 5633call NEXT('unlet novar #') " (skips nextcmd) 5634XloopNEXT 5635call NEXT('let {novar}') " (skips nextcmd) 5636XloopNEXT 5637call NEXT('unlet{ novar}') " (skips nextcmd) 5638 5639call assert_equal('a1', g:Xpath) 5640XpathINIT 5641XloopINIT 5642 5643func EXEC(cmd) 5644exec a:cmd 5645endfunc 5646 5647try 5648while 1 " dummy loop 5649try 5650let v:errmsg = "" 5651call EXEC('echo novar') " normal error 5652catch /^Vim\((\a\+)\)\=:/ 5653Xpath 'b' 5654call assert_match('E121: Undefined variable: novar', v:exception) 5655finally 5656Xpath 'c' 5657call assert_equal("", v:errmsg) 5658break 5659endtry 5660endwhile 5661 5662Xpath 'd' 5663let cmd = "let" 5664while cmd != "" 5665try 5666let v:errmsg = "" 5667call EXEC(cmd . ' novar #') " normal plus syntax error 5668catch /^Vim\((\a\+)\)\=:/ 5669Xloop 'e' 5670call assert_match('E488: Trailing characters', v:exception) 5671finally 5672Xloop 'f' 5673call assert_equal("", v:errmsg) 5674if cmd == "let" 5675let cmd = "unlet" 5676else 5677let cmd = "" 5678endif 5679XloopNEXT 5680continue 5681endtry 5682endwhile 5683 5684Xpath 'g' 5685let cmd = "let" 5686while cmd != "" 5687try 5688let v:errmsg = "" 5689call EXEC(cmd . ' {novar}') " normal plus syntax error 5690catch /^Vim\((\a\+)\)\=:/ 5691Xloop 'h' 5692call assert_match('E475: Invalid argument: {novar}', v:exception) 5693finally 5694Xloop 'i' 5695call assert_equal("", v:errmsg) 5696if cmd == "let" 5697let cmd = "unlet" 5698else 5699let cmd = "" 5700endif 5701XloopNEXT 5702continue 5703endtry 5704endwhile 5705catch /.*/ 5706call assert_report('should not get here') 5707endtry 5708Xpath 'j' 5709[CODE] 5710let verify =<< trim [CODE] 5711call assert_equal('bcde1f1e2f2gh3i3h4i4j', g:Xpath) 5712[CODE] 5713call RunInNewVim ( test , verify ) 5714endfunc 5715 5716"------------------------------------------------------------------------------- 5717" Test 80: Syntax error in expression for illegal :elseif {{{1 5718" 5719" If there is a syntax error in the expression after an illegal 5720" :elseif, an error message is given (or an error exception thrown) 5721" for the illegal :elseif rather than the expression error. 5722"------------------------------------------------------------------------------- 5723func Test_if_syntax_error () 5724CheckEnglish 5725 5726let test =<< trim [CODE] 5727let v:errmsg = "" 5728if 0 5729else 5730elseif 1 ||| 2 5731endif 5732Xpath 'a' 5733call assert_match('E584: :elseif after :else', v:errmsg) 5734 5735let v:errmsg = "" 5736if 1 5737else 5738elseif 1 ||| 2 5739endif 5740Xpath 'b' 5741call assert_match('E584: :elseif after :else', v:errmsg) 5742 5743let v:errmsg = "" 5744elseif 1 ||| 2 5745Xpath 'c' 5746call assert_match('E582: :elseif without :if', v:errmsg) 5747 5748let v:errmsg = "" 5749while 1 5750elseif 1 ||| 2 5751endwhile 5752Xpath 'd' 5753call assert_match('E582: :elseif without :if', v:errmsg) 5754 5755while 1 5756try 5757try 5758let v:errmsg = "" 5759if 0 5760else 5761elseif 1 ||| 2 5762endif 5763catch /^Vim\((\a\+)\)\=:/ 5764Xpath 'e' 5765call assert_match('E584: :elseif after :else', v:exception) 5766finally 5767Xpath 'f' 5768call assert_equal("", v:errmsg) 5769endtry 5770catch /.*/ 5771call assert_report('should not get here') 5772finally 5773Xpath 'g' 5774break 5775endtry 5776endwhile 5777 5778while 1 5779try 5780try 5781let v:errmsg = "" 5782if 1 5783else 5784elseif 1 ||| 2 5785endif 5786catch /^Vim\((\a\+)\)\=:/ 5787Xpath 'h' 5788call assert_match('E584: :elseif after :else', v:exception) 5789finally 5790Xpath 'i' 5791call assert_equal("", v:errmsg) 5792endtry 5793catch /.*/ 5794call assert_report('should not get here') 5795finally 5796Xpath 'j' 5797break 5798endtry 5799endwhile 5800 5801while 1 5802try 5803try 5804let v:errmsg = "" 5805elseif 1 ||| 2 5806catch /^Vim\((\a\+)\)\=:/ 5807Xpath 'k' 5808call assert_match('E582: :elseif without :if', v:exception) 5809finally 5810Xpath 'l' 5811call assert_equal("", v:errmsg) 5812endtry 5813catch /.*/ 5814call assert_report('should not get here') 5815finally 5816Xpath 'm' 5817break 5818endtry 5819endwhile 5820 5821while 1 5822try 5823try 5824let v:errmsg = "" 5825while 1 5826elseif 1 ||| 2 5827endwhile 5828catch /^Vim\((\a\+)\)\=:/ 5829Xpath 'n' 5830call assert_match('E582: :elseif without :if', v:exception) 5831finally 5832Xpath 'o' 5833call assert_equal("", v:errmsg) 5834endtry 5835catch /.*/ 5836call assert_report('should not get here') 5837finally 5838Xpath 'p' 5839break 5840endtry 5841endwhile 5842Xpath 'q' 5843[CODE] 5844let verify =<< trim [CODE] 5845call assert_equal('abcdefghijklmnopq', g:Xpath) 5846[CODE] 5847call RunInNewVim ( test , verify ) 5848endfunc 5849 5850"------------------------------------------------------------------------------- 5851" Test 81: Discarding exceptions after an error or interrupt {{{1 5852" 5853" When an exception is thrown from inside a :try conditional without 5854" :catch and :finally clauses and an error or interrupt occurs before 5855" the :endtry is reached, the exception is discarded. 5856"------------------------------------------------------------------------------- 5857 5858func Test_discard_exception_after_error_1 () 5859let test =<< trim [CODE] 5860try 5861Xpath 'a' 5862try 5863Xpath 'b' 5864throw "arrgh" 5865call assert_report('should not get here') 5866if 1 5867call assert_report('should not get here') 5868" error after :throw: missing :endif 5869endtry 5870call assert_report('should not get here') 5871catch /arrgh/ 5872call assert_report('should not get here') 5873endtry 5874call assert_report('should not get here') 5875[CODE] 5876let verify =<< trim [CODE] 5877call assert_equal('ab', g:Xpath) 5878[CODE] 5879call RunInNewVim ( test , verify ) 5880endfunc 5881 5882" interrupt the code before the endtry is invoked 5883func Test_discard_exception_after_error_2 () 5884XpathINIT 5885let lines =<< trim [CODE] 5886try 5887Xpath 'a' 5888try 5889Xpath 'b' 5890throw "arrgh" 5891call assert_report('should not get here') 5892endtry " interrupt here 5893call assert_report('should not get here') 5894catch /arrgh/ 5895call assert_report('should not get here') 5896endtry 5897call assert_report('should not get here') 5898[CODE] 5899call writefile ( lines , 'Xscript' ) 5900 5901breakadd file 7 Xscript 5902try 5903let caught_intr = 0 5904debuggreedy 5905call feedkeys ( ":source Xscript\<CR>quit\<CR>" , "xt" ) 5906catch /^Vim:Interrupt$/ 5907call assert_match ( 'Xscript, line 7' , v: throwpoint ) 5908let caught_intr = 1 5909endtry 59100 debuggreedy 5911call assert_equal ( 1 , caught_intr ) 5912call assert_equal ( 'ab' , g: Xpath ) 5913breakdel * 5914call delete ( 'Xscript' ) 5915endfunc 5916 5917"------------------------------------------------------------------------------- 5918" Test 82: Ignoring :catch clauses after an error or interrupt {{{1 5919" 5920" When an exception is thrown and an error or interrupt occurs before 5921" the matching :catch clause is reached, the exception is discarded 5922" and the :catch clause is ignored (also for the error or interrupt 5923" exception being thrown then). 5924"------------------------------------------------------------------------------- 5925 5926func Test_ignore_catch_after_error_1 () 5927let test =<< trim [CODE] 5928try 5929try 5930Xpath 'a' 5931throw "arrgh" 5932call assert_report('should not get here') 5933if 1 5934call assert_report('should not get here') 5935" error after :throw: missing :endif 5936catch /.*/ 5937call assert_report('should not get here') 5938catch /.*/ 5939call assert_report('should not get here') 5940endtry 5941call assert_report('should not get here') 5942catch /arrgh/ 5943call assert_report('should not get here') 5944endtry 5945call assert_report('should not get here') 5946[CODE] 5947let verify =<< trim [CODE] 5948call assert_equal('a', g:Xpath) 5949[CODE] 5950call RunInNewVim ( test , verify ) 5951endfunc 5952 5953func Test_ignore_catch_after_error_2 () 5954let test =<< trim [CODE] 5955func E() 5956try 5957try 5958Xpath 'a' 5959throw "arrgh" 5960call assert_report('should not get here') 5961if 1 5962call assert_report('should not get here') 5963" error after :throw: missing :endif 5964catch /.*/ 5965call assert_report('should not get here') 5966catch /.*/ 5967call assert_report('should not get here') 5968endtry 5969call assert_report('should not get here') 5970catch /arrgh/ 5971call assert_report('should not get here') 5972endtry 5973endfunc 5974 5975call E() 5976call assert_report('should not get here') 5977[CODE] 5978let verify =<< trim [CODE] 5979call assert_equal('a', g:Xpath) 5980[CODE] 5981call RunInNewVim ( test , verify ) 5982endfunc 5983 5984" interrupt right before a catch is invoked in a script 5985func Test_ignore_catch_after_intr_1 () 5986" for unknown reasons this test sometimes fails on MS-Windows. 5987let g: test_is_flaky = 1 5988 5989XpathINIT 5990let lines =<< trim [CODE] 5991try 5992try 5993Xpath 'a' 5994throw "arrgh" 5995call assert_report('should not get here') 5996catch /.*/ " interrupt here 5997call assert_report('should not get here') 5998catch /.*/ 5999call assert_report('should not get here') 6000endtry 6001call assert_report('should not get here') 6002catch /arrgh/ 6003call assert_report('should not get here') 6004endtry 6005call assert_report('should not get here') 6006[CODE] 6007call writefile ( lines , 'Xscript' ) 6008 6009breakadd file 6 Xscript 6010try 6011let caught_intr = 0 6012debuggreedy 6013call feedkeys ( ":source Xscript\<CR>quit\<CR>" , "xt" ) 6014catch /^Vim:Interrupt$/ 6015call assert_match ( 'Xscript, line 6' , v: throwpoint ) 6016let caught_intr = 1 6017endtry 60180 debuggreedy 6019call assert_equal ( 1 , caught_intr ) 6020call assert_equal ( 'a' , g: Xpath ) 6021breakdel * 6022call delete ( 'Xscript' ) 6023endfunc 6024 6025" interrupt right before a catch is invoked inside a function. 6026func Test_ignore_catch_after_intr_2 () 6027" for unknown reasons this test sometimes fails on MS-Windows. 6028let g: test_is_flaky = 1 6029 6030XpathINIT 6031func F () 6032try 6033try 6034Xpath 'a' 6035throw "arrgh" 6036call assert_report ( 'should not get here' ) 6037catch /.*/ " interrupt here 6038call assert_report ( 'should not get here' ) 6039catch /.*/ 6040call assert_report ( 'should not get here' ) 6041endtry 6042call assert_report ( 'should not get here' ) 6043catch /arrgh/ 6044call assert_report ( 'should not get here' ) 6045endtry 6046call assert_report ( 'should not get here' ) 6047endfunc 6048 6049breakadd func 6 F 6050try 6051let caught_intr = 0 6052debuggreedy 6053call feedkeys ( ":call F()\<CR>quit\<CR>" , "xt" ) 6054catch /^Vim:Interrupt$/ 6055call assert_match ( '\.F, line 6' , v: throwpoint ) 6056let caught_intr = 1 6057endtry 60580 debuggreedy 6059call assert_equal ( 1 , caught_intr ) 6060call assert_equal ( 'a' , g: Xpath ) 6061breakdel * 6062delfunc F 6063endfunc 6064 6065"------------------------------------------------------------------------------- 6066" Test 83: Executing :finally clauses after an error or interrupt {{{1 6067" 6068" When an exception is thrown and an error or interrupt occurs before 6069" the :finally of the innermost :try is reached, the exception is 6070" discarded and the :finally clause is executed. 6071"------------------------------------------------------------------------------- 6072 6073func Test_finally_after_error () 6074let test =<< trim [CODE] 6075try 6076Xpath 'a' 6077try 6078Xpath 'b' 6079throw "arrgh" 6080call assert_report('should not get here') 6081if 1 6082call assert_report('should not get here') 6083" error after :throw: missing :endif 6084finally 6085Xpath 'c' 6086endtry 6087call assert_report('should not get here') 6088catch /arrgh/ 6089call assert_report('should not get here') 6090endtry 6091call assert_report('should not get here') 6092[CODE] 6093let verify =<< trim [CODE] 6094call assert_equal('abc', g:Xpath) 6095[CODE] 6096call RunInNewVim ( test , verify ) 6097endfunc 6098 6099" interrupt the code right before the finally is invoked 6100func Test_finally_after_intr () 6101XpathINIT 6102let lines =<< trim [CODE] 6103try 6104Xpath 'a' 6105try 6106Xpath 'b' 6107throw "arrgh" 6108call assert_report('should not get here') 6109finally " interrupt here 6110Xpath 'c' 6111endtry 6112call assert_report('should not get here') 6113catch /arrgh/ 6114call assert_report('should not get here') 6115endtry 6116call assert_report('should not get here') 6117[CODE] 6118call writefile ( lines , 'Xscript' ) 6119 6120breakadd file 7 Xscript 6121try 6122let caught_intr = 0 6123debuggreedy 6124call feedkeys ( ":source Xscript\<CR>quit\<CR>" , "xt" ) 6125catch /^Vim:Interrupt$/ 6126call assert_match ( 'Xscript, line 7' , v: throwpoint ) 6127let caught_intr = 1 6128endtry 61290 debuggreedy 6130call assert_equal ( 1 , caught_intr ) 6131call assert_equal ( 'abc' , g: Xpath ) 6132breakdel * 6133call delete ( 'Xscript' ) 6134endfunc 6135 6136"------------------------------------------------------------------------------- 6137" Test 84: Exceptions in autocommand sequences. {{{1 6138" 6139" When an exception occurs in a sequence of autocommands for 6140" a specific event, the rest of the sequence is not executed. The 6141" command that triggered the autocommand execution aborts, and the 6142" exception is propagated to the caller. 6143" 6144" For the FuncUndefined event under a function call expression or 6145" :call command, the function is not executed, even when it has 6146" been defined by the autocommands before the exception occurred. 6147"------------------------------------------------------------------------------- 6148 6149func Test_autocmd_exception () 6150let test =<< trim [CODE] 6151func INT() 6152call interrupt() 6153endfunc 6154 6155aug TMP 6156autocmd! 6157 6158autocmd User x1 Xpath 'a' 6159autocmd User x1 throw "x1" 6160autocmd User x1 call assert_report('should not get here') 6161 6162autocmd User x2 Xpath 'b' 6163autocmd User x2 asdf 6164autocmd User x2 call assert_report('should not get here') 6165 6166autocmd User x3 Xpath 'c' 6167autocmd User x3 call INT() 6168autocmd User x3 call assert_report('should not get here') 6169 6170autocmd FuncUndefined U1 func U1() 6171autocmd FuncUndefined U1 call assert_report('should not get here') 6172autocmd FuncUndefined U1 endfunc 6173autocmd FuncUndefined U1 Xpath 'd' 6174autocmd FuncUndefined U1 throw "U1" 6175autocmd FuncUndefined U1 call assert_report('should not get here') 6176 6177autocmd FuncUndefined U2 func U2() 6178autocmd FuncUndefined U2 call assert_report('should not get here') 6179autocmd FuncUndefined U2 endfunc 6180autocmd FuncUndefined U2 Xpath 'e' 6181autocmd FuncUndefined U2 ASDF 6182autocmd FuncUndefined U2 call assert_report('should not get here') 6183 6184autocmd FuncUndefined U3 func U3() 6185autocmd FuncUndefined U3 call assert_report('should not get here') 6186autocmd FuncUndefined U3 endfunc 6187autocmd FuncUndefined U3 Xpath 'f' 6188autocmd FuncUndefined U3 call INT() 6189autocmd FuncUndefined U3 call assert_report('should not get here') 6190aug END 6191 6192try 6193try 6194Xpath 'g' 6195doautocmd User x1 6196catch /x1/ 6197Xpath 'h' 6198endtry 6199 6200while 1 6201try 6202Xpath 'i' 6203doautocmd User x2 6204catch /asdf/ 6205Xpath 'j' 6206finally 6207Xpath 'k' 6208break 6209endtry 6210endwhile 6211 6212while 1 6213try 6214Xpath 'l' 6215doautocmd User x3 6216catch /Vim:Interrupt/ 6217Xpath 'm' 6218finally 6219Xpath 'n' 6220" ... but break loop for caught interrupt exception, 6221" or discard interrupt and break loop if $VIMNOINTTHROW 6222break 6223endtry 6224endwhile 6225 6226if exists("*U1") | delfunction U1 | endif 6227if exists("*U2") | delfunction U2 | endif 6228if exists("*U3") | delfunction U3 | endif 6229 6230try 6231Xpath 'o' 6232call U1() 6233catch /U1/ 6234Xpath 'p' 6235endtry 6236 6237while 1 6238try 6239Xpath 'q' 6240call U2() 6241catch /ASDF/ 6242Xpath 'r' 6243finally 6244Xpath 's' 6245" ... but break loop for caught error exception, 6246" or discard error and break loop if $VIMNOERRTHROW 6247break 6248endtry 6249endwhile 6250 6251while 1 6252try 6253Xpath 't' 6254call U3() 6255catch /Vim:Interrupt/ 6256Xpath 'u' 6257finally 6258Xpath 'v' 6259" ... but break loop for caught interrupt exception, 6260" or discard interrupt and break loop if $VIMNOINTTHROW 6261break 6262endtry 6263endwhile 6264catch /.*/ 6265call assert_report('should not get here') 6266endtry 6267Xpath 'w' 6268[CODE] 6269let verify =<< trim [CODE] 6270call assert_equal('gahibjklcmnodpqerstfuvw', g:Xpath) 6271[CODE] 6272call RunInNewVim ( test , verify ) 6273endfunc 6274 6275"------------------------------------------------------------------------------- 6276" Test 85: Error exceptions in autocommands for I/O command events {{{1 6277" 6278" When an I/O command is inside :try/:endtry, autocommands to be 6279" executed after it should be skipped on an error (exception) in the 6280" command itself or in autocommands to be executed before the command. 6281" In the latter case, the I/O command should not be executed either. 6282" Example 1: BufWritePre, :write, BufWritePost 6283" Example 2: FileReadPre, :read, FileReadPost. 6284"------------------------------------------------------------------------------- 6285 6286func Test_autocmd_error_io_exception () 6287let test =<< trim [CODE] 6288" Remove the autocommands for the events specified as arguments in all used 6289" autogroups. 6290func Delete_autocommands(...) 6291let augfile = tempname() 6292while 1 6293try 6294exec "redir >" . augfile 6295aug 6296redir END 6297exec "edit" augfile 6298g/^$/d 6299norm G$ 6300let wrap = "w" 6301while search('\%( \|^\)\@<=.\{-}\%( \)\@=', wrap) > 0 6302let wrap = "W" 6303exec "norm y/ \n" 6304let argno = 1 6305while argno <= a:0 6306exec "au!" escape(@", " ") a:{argno} 6307let argno = argno + 1 6308endwhile 6309endwhile 6310catch /.*/ 6311finally 6312bwipeout! 6313call delete(augfile) 6314break 6315endtry 6316endwhile 6317endfunc 6318 6319call Delete_autocommands("BufWritePre", "BufWritePost") 6320 6321while 1 6322try 6323try 6324let post = 0 6325aug TMP 6326au! BufWritePost * let post = 1 6327aug END 6328write /n/o/n/e/x/i/s/t/e/n/t 6329catch /^Vim(write):/ 6330Xpath 'a' 6331call assert_match("E212: Can't open file for writing", v:exception) 6332finally 6333Xpath 'b' 6334call assert_equal(0, post) 6335au! TMP 6336aug! TMP 6337endtry 6338catch /.*/ 6339call assert_report('should not get here') 6340finally 6341Xpath 'c' 6342break 6343endtry 6344endwhile 6345 6346while 1 6347try 6348try 6349let post = 0 6350aug TMP 6351au! BufWritePre * asdf 6352au! BufWritePost * let post = 1 6353aug END 6354let tmpfile = tempname() 6355exec "write" tmpfile 6356catch /^Vim\((write)\)\=:/ 6357Xpath 'd' 6358call assert_match('E492: Not an editor command', v:exception) 6359finally 6360Xpath 'e' 6361if filereadable(tmpfile) 6362call assert_report('should not get here') 6363endif 6364call assert_equal(0, post) 6365au! TMP 6366aug! TMP 6367endtry 6368catch /.*/ 6369call assert_report('should not get here') 6370finally 6371Xpath 'f' 6372break 6373endtry 6374endwhile 6375 6376call delete(tmpfile) 6377 6378call Delete_autocommands("BufWritePre", "BufWritePost", 6379\ "BufReadPre", "BufReadPost", "FileReadPre", "FileReadPost") 6380 6381while 1 6382try 6383try 6384let post = 0 6385aug TMP 6386au! FileReadPost * let post = 1 6387aug END 6388let caught = 0 6389read /n/o/n/e/x/i/s/t/e/n/t 6390catch /^Vim(read):/ 6391Xpath 'g' 6392call assert_match("E484: Can't open file", v:exception) 6393finally 6394Xpath 'h' 6395call assert_equal(0, post) 6396au! TMP 6397aug! TMP 6398endtry 6399catch /.*/ 6400call assert_report('should not get here') 6401finally 6402Xpath 'i' 6403break 6404endtry 6405endwhile 6406 6407while 1 6408try 6409let infile = tempname() 6410let tmpfile = tempname() 6411call writefile(["XYZ"], infile) 6412exec "edit" tmpfile 6413try 6414Xpath 'j' 6415try 6416let post = 0 6417aug TMP 6418au! FileReadPre * asdf 6419au! FileReadPost * let post = 1 6420aug END 6421exec "0read" infile 6422catch /^Vim\((read)\)\=:/ 6423Xpath 'k' 6424call assert_match('E492: Not an editor command', v:exception) 6425finally 6426Xpath 'l' 6427if getline("1") == "XYZ" 6428call assert_report('should not get here') 6429endif 6430call assert_equal(0, post) 6431au! TMP 6432aug! TMP 6433endtry 6434finally 6435Xpath 'm' 6436bwipeout! 6437endtry 6438catch /.*/ 6439call assert_report('should not get here') 6440finally 6441Xpath 'n' 6442break 6443endtry 6444endwhile 6445 6446call delete(infile) 6447call delete(tmpfile) 6448[CODE] 6449let verify =<< trim [CODE] 6450call assert_equal('abcdefghijklmn', g:Xpath) 6451[CODE] 6452call RunInNewVim ( test , verify ) 6453endfunc 6454 6455"------------------------------------------------------------------------------- 6456" Test 87 using (expr) ? funcref : funcref {{{1 6457" 6458" Vim needs to correctly parse the funcref and even when it does 6459" not execute the funcref, it needs to consume the trailing () 6460"------------------------------------------------------------------------------- 6461 6462func Add2 ( x1 , x2 ) 6463return a: x1 + a: x2 6464endfu 6465 6466func GetStr () 6467return "abcdefghijklmnopqrstuvwxyp" 6468endfu 6469 6470func Test_funcref_with_condexpr () 6471call assert_equal ( 5 , function ( 'Add2' )( 2 , 3 )) 6472 6473call assert_equal ( 3 , 1 ? function ( 'Add2' )( 1 , 2 ) : function ( 'Add2' )( 2 , 3 )) 6474call assert_equal ( 5 , 0 ? function ( 'Add2' )( 1 , 2 ) : function ( 'Add2' )( 2 , 3 )) 6475" Make sure, GetStr() still works. 6476call assert_equal ( 'abcdefghijk' , GetStr ()[ 0 : 10 ]) 6477endfunc 6478 6479"------------------------------------------------------------------------------- 6480" Test 90: Recognizing {} in variable name. {{{1 6481"------------------------------------------------------------------------------- 6482 6483func Test_curlies () 6484let s: var = 66 6485let ns = 's' 6486call assert_equal ( 66 , { ns }: var ) 6487 6488let g: a = {} 6489let g: b = 't' 6490let g: a [ g: b ] = 77 6491call assert_equal ( 77 , g: a [ 't' ]) 6492endfunc 6493 6494"------------------------------------------------------------------------------- 6495" Test 91: using type(). {{{1 6496"------------------------------------------------------------------------------- 6497 6498func Test_type () 6499call assert_equal ( 0 , type ( 0 )) 6500call assert_equal ( 1 , type ( "" )) 6501call assert_equal ( 2 , type ( function ( "tr" ))) 6502call assert_equal ( 2 , type ( function ( "tr" , [ 8 ]))) 6503call assert_equal ( 3 , type ([])) 6504call assert_equal ( 4 , type ({})) 6505if has ( 'float' ) 6506call assert_equal ( 5 , type ( 0.0 )) 6507endif 6508call assert_equal ( 6 , type ( v: false )) 6509call assert_equal ( 6 , type ( v: true )) 6510" call assert_equal(7, type(v:none)) 6511call assert_equal ( 7 , type ( v: null )) 6512call assert_equal ( v: t_number , type ( 0 )) 6513call assert_equal ( v: t_string , type ( "" )) 6514call assert_equal ( v: t_func , type ( function ( "tr" ))) 6515call assert_equal ( v: t_func , type ( function ( "tr" , [ 8 ]))) 6516call assert_equal ( v: t_list , type ([])) 6517call assert_equal ( v: t_dict , type ({})) 6518if has ( 'float' ) 6519call assert_equal ( v: t_float , type ( 0.0 )) 6520endif 6521call assert_equal ( v: t_bool , type ( v: false )) 6522call assert_equal ( v: t_bool , type ( v: true )) 6523" call assert_equal(v:t_none, type(v:none)) 6524" call assert_equal(v:t_none, type(v:null)) 6525call assert_equal ( v: t_string , type ( v: _null_string )) 6526call assert_equal ( v: t_list , type ( v: _null_list )) 6527call assert_equal ( v: t_dict , type ( v: _null_dict )) 6528if has ( 'job' ) 6529call assert_equal ( v: t_job , type ( test_null_job ())) 6530endif 6531if has ( 'channel' ) 6532call assert_equal ( v: t_channel , type ( test_null_channel ())) 6533endif 6534call assert_equal ( v: t_blob , type ( v: _null_blob )) 6535 6536call assert_equal ( 0 , 0 + v: false ) 6537call assert_equal ( 1 , 0 + v: true ) 6538" call assert_equal(0, 0 + v:none) 6539call assert_equal ( 0 , 0 + v: null ) 6540 6541call assert_equal ( 'v:false' , '' . v: false ) 6542call assert_equal ( 'v:true' , '' . v: true ) 6543" call assert_equal('v:none', '' . v:none) 6544call assert_equal ( 'v:null' , '' . v: null ) 6545 6546call assert_true ( v: false == 0 ) 6547call assert_false ( v: false != 0 ) 6548call assert_true ( v: true == 1 ) 6549call assert_false ( v: true != 1 ) 6550call assert_false ( v: true == v: false ) 6551call assert_true ( v: true != v: false ) 6552 6553call assert_true ( v: null == 0 ) 6554call assert_false ( v: null != 0 ) 6555" call assert_true(v:none == 0) 6556" call assert_false(v:none != 0) 6557 6558call assert_true ( v: false is v: false ) 6559call assert_true ( v: true is v: true ) 6560" call assert_true(v:none is v:none) 6561call assert_true ( v: null is v: null ) 6562 6563call assert_false ( v: false isnot v: false ) 6564call assert_false ( v: true isnot v: true ) 6565" call assert_false(v:none isnot v:none) 6566call assert_false ( v: null isnot v: null ) 6567 6568call assert_false ( v: false is 0 ) 6569call assert_false ( v: true is 1 ) 6570call assert_false ( v: true is v: false ) 6571" call assert_false(v:none is 0) 6572" call assert_false(v:none is []) 6573" call assert_false(v:none is {}) 6574" call assert_false(v:none is 'text') 6575call assert_false ( v: null is 0 ) 6576" call assert_false(v:null is v:none) 6577 6578call assert_true ( v: false isnot 0 ) 6579call assert_true ( v: true isnot 1 ) 6580call assert_true ( v: true isnot v: false ) 6581" call assert_true(v:none isnot 0) 6582call assert_true ( v: null isnot 0 ) 6583" call assert_true(v:null isnot v:none) 6584 6585call assert_equal ( v: false , eval ( string ( v: false ))) 6586call assert_equal ( v: true , eval ( string ( v: true ))) 6587" call assert_equal(v:none, eval(string(v:none))) 6588call assert_equal ( v: null , eval ( string ( v: null ))) 6589 6590call assert_equal ( v: false , copy ( v: false )) 6591call assert_equal ( v: true , copy ( v: true )) 6592" call assert_equal(v:none, copy(v:none)) 6593call assert_equal ( v: null , copy ( v: null )) 6594 6595call assert_equal ([ v: false ], deepcopy ([ v: false ])) 6596call assert_equal ([ v: true ], deepcopy ([ v: true ])) 6597" call assert_equal([v:none], deepcopy([v:none])) 6598call assert_equal ([ v: null ], deepcopy ([ v: null ])) 6599 6600call assert_true ( empty ( v: false )) 6601call assert_false ( empty ( v: true )) 6602call assert_true ( empty ( v: null )) 6603" call assert_true(empty(v:none)) 6604 6605func ChangeYourMind () 6606try 6607return v: true 6608finally 6609return 'something else' 6610endtry 6611endfunc 6612 6613call ChangeYourMind () 6614endfunc 6615 6616"------------------------------------------------------------------------------- 6617" Test 92: skipping code {{{1 6618"------------------------------------------------------------------------------- 6619 6620func Test_skip () 6621let Fn = function ( 'Test_type' ) 6622call assert_false ( 0 && Fn [ 1 ]) 6623call assert_false ( 0 && string ( Fn )) 6624call assert_false ( 0 && len ( Fn )) 6625let l = [] 6626call assert_false ( 0 && l [ 1 ]) 6627call assert_false ( 0 && string ( l )) 6628call assert_false ( 0 && len ( l )) 6629let f = 1.0 6630call assert_false ( 0 && f [ 1 ]) 6631call assert_false ( 0 && string ( f )) 6632call assert_false ( 0 && len ( f )) 6633let sp = v: null 6634call assert_false ( 0 && sp [ 1 ]) 6635call assert_false ( 0 && string ( sp )) 6636call assert_false ( 0 && len ( sp )) 6637 6638endfunc 6639 6640"------------------------------------------------------------------------------- 6641" Test 93: :echo and string() {{{1 6642"------------------------------------------------------------------------------- 6643 6644func Test_echo_and_string () 6645" String 6646let a = 'foo bar' 6647redir => result 6648echo a 6649echo string ( a ) 6650redir END 6651let l = split ( result , "\n" ) 6652call assert_equal ([ "foo bar" , 6653\ "'foo bar'" ], l ) 6654 6655" Float 6656if has ( 'float' ) 6657let a = -1.2e0 6658redir => result 6659echo a 6660echo string ( a ) 6661redir END 6662let l = split ( result , "\n" ) 6663call assert_equal ([ "-1.2" , 6664\ "-1.2" ], l ) 6665endif 6666 6667" Funcref 6668redir => result 6669echo function ( 'string' ) 6670echo string ( function ( 'string' )) 6671redir END 6672let l = split ( result , "\n" ) 6673call assert_equal ([ "string" , 6674\ "function('string')" ], l ) 6675 6676" Empty dictionaries in a list 6677let a = {} 6678redir => result 6679echo [ a , a , a ] 6680echo string ([ a , a , a ]) 6681redir END 6682let l = split ( result , "\n" ) 6683call assert_equal ([ "[{}, {}, {}]" , 6684\ "[{}, {}, {}]" ], l ) 6685 6686" Empty dictionaries in a dictionary 6687let a = {} 6688let b = { "a" : a , "b" : a } 6689redir => result 6690echo b 6691echo string ( b ) 6692redir END 6693let l = split ( result , "\n" ) 6694call assert_equal ([ "{'a': {}, 'b': {}}" , 6695\ "{'a': {}, 'b': {}}" ], l ) 6696 6697" Empty lists in a list 6698let a = [] 6699redir => result 6700echo [ a , a , a ] 6701echo string ([ a , a , a ]) 6702redir END 6703let l = split ( result , "\n" ) 6704call assert_equal ([ "[[], [], []]" , 6705\ "[[], [], []]" ], l ) 6706 6707" Empty lists in a dictionary 6708let a = [] 6709let b = { "a" : a , "b" : a } 6710redir => result 6711echo b 6712echo string ( b ) 6713redir END 6714let l = split ( result , "\n" ) 6715call assert_equal ([ "{'a': [], 'b': []}" , 6716\ "{'a': [], 'b': []}" ], l ) 6717 6718call assert_fails ( 'echo &:' , 'E112:' ) 6719call assert_fails ( 'echo &g:' , 'E112:' ) 6720call assert_fails ( 'echo &l:' , 'E112:' ) 6721 6722endfunc 6723 6724"------------------------------------------------------------------------------- 6725" Test 94: 64-bit Numbers {{{1 6726"------------------------------------------------------------------------------- 6727 6728func Test_num64 () 6729call assert_notequal ( 4294967296 , 0 ) 6730call assert_notequal ( -4294967296 , 0 ) 6731call assert_equal ( 4294967296 , 0xFFFFffff + 1 ) 6732call assert_equal ( -4294967296 , -0xFFFFffff - 1 ) 6733 6734call assert_equal ( 9223372036854775807 , 1 / 0 ) 6735call assert_equal ( -9223372036854775807 , -1 / 0 ) 6736call assert_equal ( -9223372036854775807 - 1 , 0 / 0 ) 6737 6738if has ( 'float' ) 6739call assert_equal ( 0x7FFFffffFFFFffff , float2nr ( 1.0e150 )) 6740call assert_equal ( -0x7FFFffffFFFFffff , float2nr ( -1.0e150 )) 6741endif 6742 6743let rng = range ( 0xFFFFffff , 0x100000001 ) 6744call assert_equal ([ 0xFFFFffff , 0x100000000 , 0x100000001 ], rng ) 6745call assert_equal ( 0x100000001 , max ( rng )) 6746call assert_equal ( 0xFFFFffff , min ( rng )) 6747call assert_equal ( rng , sort ( range ( 0x100000001 , 0xFFFFffff , -1 ), 'N' )) 6748endfunc 6749 6750"------------------------------------------------------------------------------- 6751" Test 95: lines of :append, :change, :insert {{{1 6752"------------------------------------------------------------------------------- 6753 6754func DefineFunction ( name , body ) 6755let func = join ([ 'function! ' . a: name . '()' ] + a: body + [ 'endfunction' ], "\n" ) 6756exec func 6757endfunc 6758 6759func Test_script_lines () 6760" :append 6761try 6762call DefineFunction ( 'T_Append' , [ 6763\ 'append' , 6764\ 'py <<EOS' , 6765\ '.' , 6766\ ]) 6767catch 6768call assert_report ( "Can't define function" ) 6769endtry 6770try 6771call DefineFunction ( 'T_Append' , [ 6772\ 'append' , 6773\ 'abc' , 6774\ ]) 6775call assert_report ( "Shouldn't be able to define function" ) 6776catch 6777call assert_exception ( 'Vim(function):E1145: Missing heredoc end marker: .' ) 6778endtry 6779 6780" :change 6781try 6782call DefineFunction ( 'T_Change' , [ 6783\ 'change' , 6784\ 'py <<EOS' , 6785\ '.' , 6786\ ]) 6787catch 6788call assert_report ( "Can't define function" ) 6789endtry 6790try 6791call DefineFunction ( 'T_Change' , [ 6792\ 'change' , 6793\ 'abc' , 6794\ ]) 6795call assert_report ( "Shouldn't be able to define function" ) 6796catch 6797call assert_exception ( 'Vim(function):E1145: Missing heredoc end marker: .' ) 6798endtry 6799 6800" :insert 6801try 6802call DefineFunction ( 'T_Insert' , [ 6803\ 'insert' , 6804\ 'py <<EOS' , 6805\ '.' , 6806\ ]) 6807catch 6808call assert_report ( "Can't define function" ) 6809endtry 6810try 6811call DefineFunction ( 'T_Insert' , [ 6812\ 'insert' , 6813\ 'abc' , 6814\ ]) 6815call assert_report ( "Shouldn't be able to define function" ) 6816catch 6817call assert_exception ( 'Vim(function):E1145: Missing heredoc end marker: .' ) 6818endtry 6819 6820" More test for :append, :change, :insert 6821let cmds = [ "append" , "change" , "insert" ] 6822let suffixes = [ "" , "!" , "|" , "|xyz" , " " ] 6823 6824for c in cmds 6825" Single character (with some accepted trailing characters) 6826for s in suffixes 6827let cmd = c [: 0 ] .. s 6828let line = [ "func LinesCheck()" , cmd , "" , "endfunc" , "call LinesCheck()" ] 6829call writefile ( line , 'Xfunc' , 'D' ) 6830call assert_fails ( 'source Xfunc' , 'E1145: Missing heredoc end marker: .' , $ '"{cmd}"') 6831endfor 6832 6833" Unnecessary arguments 6834let cmd = c [: 2 ] .. " end" 6835let line [ 1 ] = cmd 6836call writefile ( line , 'Xfunc' , 'D' ) 6837call assert_fails ( 'source Xfunc' , 'E488: Trailing characters: end:' , $ '"{cmd}"') 6838 6839" Extra characters at the end (i.e., other commands) 6840let cmd = c .. "x" 6841let line [ 1 ] = cmd 6842call writefile ( line , 'Xfunc' , 'D' ) 6843call assert_fails ( 'source Xfunc' , 'E492: Not an editor command:' , $ '"{cmd}"') 6844endfor 6845 6846let line =<< trim END 6847func AppendCheck() 6848apple 6849endfunc 6850call AppendCheck() 6851END 6852call writefile ( line , 'Xfunc' , 'D' ) 6853call assert_fails ( 'source Xfunc' , 'E492: Not an editor command: apple' ) 6854 6855let line =<< trim END 6856func AppendCheck() 6857command! apple :echo "hello apple" 6858apple 6859endfunc 6860call AppendCheck() 6861END 6862call writefile ( line , 'Xfunc' , 'D' ) 6863call assert_fails ( 'source Xfunc' , 'E183: User defined commands must start with an uppercase letter' ) 6864 6865endfunc 6866 6867"------------------------------------------------------------------------------- 6868" Test 96: line continuation {{{1 6869" 6870" Undefined behavior was detected by ubsan with line continuation 6871" after an empty line. 6872"------------------------------------------------------------------------------- 6873func Test_script_empty_line_continuation () 6874 6875\ 6876endfunc 6877 6878"------------------------------------------------------------------------------- 6879" Test 97: bitwise functions {{{1 6880"------------------------------------------------------------------------------- 6881func Test_bitwise_functions () 6882" and 6883call assert_equal ( 127 , and ( 127 , 127 )) 6884call assert_equal ( 16 , and ( 127 , 16 )) 6885eval 127 -> and ( 16 ) -> assert_equal ( 16 ) 6886call assert_equal ( 0 , and ( 127 , 128 )) 6887call assert_fails ( "call and([], 1)" , 'E745:' ) 6888call assert_fails ( "call and({}, 1)" , 'E728:' ) 6889if has ( 'float' ) 6890call assert_fails ( "call and(1.0, 1)" , 'E805:' ) 6891call assert_fails ( "call and(1, 1.0)" , 'E805:' ) 6892endif 6893call assert_fails ( "call and(1, [])" , 'E745:' ) 6894call assert_fails ( "call and(1, {})" , 'E728:' ) 6895" or 6896call assert_equal ( 23 , or ( 16 , 7 )) 6897call assert_equal ( 15 , or ( 8 , 7 )) 6898eval 8 -> or ( 7 ) -> assert_equal ( 15 ) 6899call assert_equal ( 123 , or ( 0 , 123 )) 6900call assert_fails ( "call or([], 1)" , 'E745:' ) 6901call assert_fails ( "call or({}, 1)" , 'E728:' ) 6902if has ( 'float' ) 6903call assert_fails ( "call or(1.0, 1)" , 'E805:' ) 6904call assert_fails ( "call or(1, 1.0)" , 'E805:' ) 6905endif 6906call assert_fails ( "call or(1, [])" , 'E745:' ) 6907call assert_fails ( "call or(1, {})" , 'E728:' ) 6908" xor 6909call assert_equal ( 0 , xor ( 127 , 127 )) 6910call assert_equal ( 111 , xor ( 127 , 16 )) 6911eval 127 -> xor ( 16 ) -> assert_equal ( 111 ) 6912call assert_equal ( 255 , xor ( 127 , 128 )) 6913if has ( 'float' ) 6914call assert_fails ( "call xor(1.0, 1)" , 'E805:' ) 6915call assert_fails ( "call xor(1, 1.0)" , 'E805:' ) 6916endif 6917call assert_fails ( "call xor([], 1)" , 'E745:' ) 6918call assert_fails ( "call xor({}, 1)" , 'E728:' ) 6919call assert_fails ( "call xor(1, [])" , 'E745:' ) 6920call assert_fails ( "call xor(1, {})" , 'E728:' ) 6921" invert 6922call assert_equal ( 65408 , and ( invert ( 127 ), 65535 )) 6923eval 127 -> invert () -> and ( 65535 ) -> assert_equal ( 65408 ) 6924call assert_equal ( 65519 , and ( invert ( 16 ), 65535 )) 6925call assert_equal ( 65407 , and ( invert ( 128 ), 65535 )) 6926if has ( 'float' ) 6927call assert_fails ( "call invert(1.0)" , 'E805:' ) 6928endif 6929call assert_fails ( "call invert([])" , 'E745:' ) 6930call assert_fails ( "call invert({})" , 'E728:' ) 6931endfunc 6932 6933" Test using bang after user command {{{1 6934func Test_user_command_with_bang () 6935command -bang Nieuw let nieuw = 1 6936Ni ! 6937call assert_equal ( 1 , nieuw ) 6938unlet nieuw 6939delcommand Nieuw 6940endfunc 6941 6942func Test_script_expand_sfile () 6943let lines =<< trim END 6944func s:snr() 6945return expand('<sfile>') 6946endfunc 6947let g:result = s:snr() 6948END 6949call writefile ( lines , 'Xexpand' ) 6950source Xexpand 6951call assert_match ( '<SNR>\d\+_snr' , g: result ) 6952source Xexpand 6953call assert_match ( '<SNR>\d\+_snr' , g: result ) 6954 6955call delete ( 'Xexpand' ) 6956unlet g: result 6957endfunc 6958 6959func Test_compound_assignment_operators () 6960" Test for number 6961let x = 1 6962let x += 10 6963call assert_equal ( 11 , x ) 6964let x -= 5 6965call assert_equal ( 6 , x ) 6966let x *= 4 6967call assert_equal ( 24 , x ) 6968let x /= 3 6969call assert_equal ( 8 , x ) 6970let x %= 3 6971call assert_equal ( 2 , x ) 6972let x .= 'n' 6973call assert_equal ( '2n' , x ) 6974 6975" Test special cases: division or modulus with 0. 6976let x = 1 6977let x /= 0 6978call assert_equal ( 0x7FFFFFFFFFFFFFFF , x ) 6979 6980let x = -1 6981let x /= 0 6982call assert_equal ( -0x7FFFFFFFFFFFFFFF , x ) 6983 6984let x = 0 6985let x /= 0 6986call assert_equal ( -0x7FFFFFFFFFFFFFFF - 1 , x ) 6987 6988let x = 1 6989let x %= 0 6990call assert_equal ( 0 , x ) 6991 6992let x = -1 6993let x %= 0 6994call assert_equal ( 0 , x ) 6995 6996let x = 0 6997let x %= 0 6998call assert_equal ( 0 , x ) 6999 7000" Test for string 7001let x = 'str' 7002let x .= 'ing' 7003call assert_equal ( 'string' , x ) 7004let x += 1 7005call assert_equal ( 1 , x ) 7006 7007if has ( 'float' ) 7008" Test for float 7009let x -= 1.5 7010call assert_equal ( -0.5 , x ) 7011let x = 0.5 7012let x += 4.5 7013call assert_equal ( 5.0 , x ) 7014let x -= 1.5 7015call assert_equal ( 3.5 , x ) 7016let x *= 3.0 7017call assert_equal ( 10.5 , x ) 7018let x /= 2.5 7019call assert_equal ( 4.2 , x ) 7020call assert_fails ( 'let x %= 0.5' , 'E734:' ) 7021call assert_fails ( 'let x .= "f"' , 'E734:' ) 7022let x = ! 3.14 7023call assert_equal ( 0 , x ) 7024call assert_equal ( 1 , !! 1.0 ) 7025let x = ! 0.0 7026call assert_equal ( 1 , x ) 7027call assert_equal ( 0 , !! 0.0 ) 7028 7029" integer and float operations 7030let x = 1 7031let x *= 2.1 7032call assert_equal ( 2.1 , x ) 7033let x = 1 7034let x /= 0.25 7035call assert_equal ( 4.0 , x ) 7036let x = 1 7037call assert_fails ( 'let x %= 0.25' , 'E734:' ) 7038let x = 1 7039call assert_fails ( 'let x .= 0.25' , 'E734:' ) 7040let x = 1.0 7041call assert_fails ( 'let x += [1.1]' , 'E734:' ) 7042endif 7043 7044" Test for environment variable 7045let $ FOO = 1 7046call assert_fails ( 'let $FOO += 1' , 'E734' ) 7047call assert_fails ( 'let $FOO -= 1' , 'E734' ) 7048call assert_fails ( 'let $FOO *= 1' , 'E734' ) 7049call assert_fails ( 'let $FOO /= 1' , 'E734' ) 7050call assert_fails ( 'let $FOO %= 1' , 'E734' ) 7051let $ FOO .= 's' 7052call assert_equal ( '1s' , $ FOO ) 7053unlet $ FOO 7054 7055" Test for option variable (type: number) 7056let & scrolljump = 1 7057let & scrolljump += 5 7058call assert_equal ( 6 , & scrolljump ) 7059let & scrolljump -= 2 7060call assert_equal ( 4 , & scrolljump ) 7061let & scrolljump *= 3 7062call assert_equal ( 12 , & scrolljump ) 7063let & scrolljump /= 2 7064call assert_equal ( 6 , & scrolljump ) 7065let & scrolljump %= 5 7066call assert_equal ( 1 , & scrolljump ) 7067call assert_fails ( 'let &scrolljump .= "j"' , [ 'E734:' , 'E734:' ]) 7068set scrolljump & vim 7069 7070let & foldlevelstart = 2 7071let & foldlevelstart -= 1 7072call assert_equal ( 1 , & foldlevelstart ) 7073let & foldlevelstart -= 1 7074call assert_equal ( 0 , & foldlevelstart ) 7075let & foldlevelstart = 2 7076let & foldlevelstart -= 2 7077call assert_equal ( 0 , & foldlevelstart ) 7078 7079" Test for register 7080let @/ = 1 7081call assert_fails ( 'let @/ += 1' , 'E734:' ) 7082call assert_fails ( 'let @/ -= 1' , 'E734:' ) 7083call assert_fails ( 'let @/ *= 1' , 'E734:' ) 7084call assert_fails ( 'let @/ /= 1' , 'E734:' ) 7085call assert_fails ( 'let @/ %= 1' , 'E734:' ) 7086let @/ .= 's' 7087call assert_equal ( '1s' , @/ ) 7088let @/ = '' 7089endfunc 7090 7091func Test_unlet_env () 7092let $ TESTVAR = 'yes' 7093call assert_equal ( 'yes' , $ TESTVAR ) 7094call assert_fails ( 'lockvar $TESTVAR' , 'E940' ) 7095call assert_fails ( 'unlockvar $TESTVAR' , 'E940' ) 7096call assert_equal ( 'yes' , $ TESTVAR ) 7097if 0 7098unlet $ TESTVAR 7099endif 7100call assert_equal ( 'yes' , $ TESTVAR ) 7101unlet $ TESTVAR 7102call assert_equal ( '' , $ TESTVAR ) 7103endfunc 7104 7105func Test_refcount () 7106throw 'Skipped: Nvim does not support test_refcount()' 7107" Immediate values 7108call assert_equal ( -1 , test_refcount ( 1 )) 7109call assert_equal ( -1 , test_refcount ( 's' )) 7110call assert_equal ( -1 , test_refcount ( v: true )) 7111call assert_equal ( 0 , test_refcount ([])) 7112call assert_equal ( 0 , test_refcount ({})) 7113call assert_equal ( 0 , test_refcount ( 0 zff)) 7114call assert_equal ( 0 , test_refcount ({ -> line ( '.' )})) 7115call assert_equal ( -1 , test_refcount ( 0.1 )) 7116if has ( 'job' ) 7117call assert_equal ( 0 , test_refcount ( job_start ([ & shell , & shellcmdflag , 'echo .' ]))) 7118endif 7119 7120" No refcount types 7121let x = 1 7122call assert_equal ( -1 , test_refcount ( x )) 7123let x = 's' 7124call assert_equal ( -1 , test_refcount ( x )) 7125let x = v: true 7126call assert_equal ( -1 , test_refcount ( x )) 7127let x = 0.1 7128call assert_equal ( -1 , test_refcount ( x )) 7129 7130" Check refcount 7131let x = [] 7132call assert_equal ( 1 , test_refcount ( x )) 7133 7134let x = {} 7135call assert_equal ( 1 , x -> test_refcount ()) 7136 7137let x = 0 zff 7138call assert_equal ( 1 , test_refcount ( x )) 7139 7140let X = { -> line ( '.' )} 7141call assert_equal ( 1 , test_refcount ( X )) 7142let Y = X 7143call assert_equal ( 2 , test_refcount ( X )) 7144 7145if has ( 'job' ) 7146let job = job_start ([ & shell , & shellcmdflag , 'echo .' ]) 7147call assert_equal ( 1 , test_refcount ( job )) 7148call assert_equal ( 1 , test_refcount ( job_getchannel ( job ))) 7149call assert_equal ( 1 , test_refcount ( job )) 7150endif 7151 7152" Function arguments, copying and unassigning 7153func ExprCheck ( x , i ) 7154let i = a: i + 1 7155call assert_equal ( i , test_refcount ( a: x )) 7156let Y = a: x 7157call assert_equal ( i + 1 , test_refcount ( a: x )) 7158call assert_equal ( test_refcount ( a: x ), test_refcount ( Y )) 7159let Y = 0 7160call assert_equal ( i , test_refcount ( a: x )) 7161endfunc 7162call ExprCheck ([], 0 ) 7163call ExprCheck ({}, 0 ) 7164call ExprCheck ( 0 zff, 0) 7165call ExprCheck ({ -> line ( '.' )}, 0 ) 7166if has ( 'job' ) 7167call ExprCheck ( job , 1 ) 7168call ExprCheck ( job_getchannel ( job ), 1 ) 7169call job_stop ( job ) 7170endif 7171delfunc ExprCheck 7172 7173" Regarding function 7174func Func ( x ) abort 7175call assert_equal ( 2 , test_refcount ( function ( 'Func' ))) 7176call assert_equal ( 0 , test_refcount ( funcref ( 'Func' ))) 7177endfunc 7178call assert_equal ( 1 , test_refcount ( function ( 'Func' ))) 7179call assert_equal ( 0 , test_refcount ( function ( 'Func' , [ 1 ]))) 7180call assert_equal ( 0 , test_refcount ( funcref ( 'Func' ))) 7181call assert_equal ( 0 , test_refcount ( funcref ( 'Func' , [ 1 ]))) 7182let X = function ( 'Func' ) 7183let Y = X 7184call assert_equal ( 1 , test_refcount ( X )) 7185let X = function ( 'Func' , [ 1 ]) 7186let Y = X 7187call assert_equal ( 2 , test_refcount ( X )) 7188let X = funcref ( 'Func' ) 7189let Y = X 7190call assert_equal ( 2 , test_refcount ( X )) 7191let X = funcref ( 'Func' , [ 1 ]) 7192let Y = X 7193call assert_equal ( 2 , test_refcount ( X )) 7194unlet X 7195unlet Y 7196call Func ( 1 ) 7197delfunc Func 7198 7199" Function with dict 7200func DictFunc () dict 7201call assert_equal ( 3 , test_refcount ( self )) 7202endfunc 7203let d = { 'Func' : function ( 'DictFunc' )} 7204call assert_equal ( 1 , test_refcount ( d )) 7205call assert_equal ( 0 , test_refcount ( d . Func )) 7206call d . Func () 7207unlet d 7208delfunc DictFunc 7209 7210if has ( 'channel' ) 7211call assert_equal ( -1 , test_refcount ( test_null_job ())) 7212call assert_equal ( -1 , test_refcount ( test_null_channel ())) 7213endif 7214call assert_equal ( -1 , test_refcount ( test_null_function ())) 7215call assert_equal ( -1 , test_refcount ( test_null_partial ())) 7216call assert_equal ( -1 , test_refcount ( test_null_blob ())) 7217call assert_equal ( -1 , test_refcount ( test_null_list ())) 7218call assert_equal ( -1 , test_refcount ( test_null_dict ())) 7219endfunc 7220 7221" Test for missing :endif, :endfor, :endwhile and :endtry {{{1 7222func Test_missing_end () 7223call writefile ([ 'if 2 > 1' , 'echo ">"' ], 'Xscript' ) 7224call assert_fails ( 'source Xscript' , 'E171:' ) 7225call writefile ([ 'for i in range(5)' , 'echo i' ], 'Xscript' ) 7226call assert_fails ( 'source Xscript' , 'E170:' ) 7227call writefile ([ 'while v:true' , 'echo "."' ], 'Xscript' ) 7228call assert_fails ( 'source Xscript' , 'E170:' ) 7229call writefile ([ 'try' , 'echo "."' ], 'Xscript' ) 7230call assert_fails ( 'source Xscript' , 'E600:' ) 7231call delete ( 'Xscript' ) 7232 7233" Using endfor with :while 7234let caught_e732 = 0 7235try 7236while v: true 7237endfor 7238catch /E732:/ 7239let caught_e732 = 1 7240endtry 7241call assert_equal ( 1 , caught_e732 ) 7242 7243" Using endwhile with :for 7244let caught_e733 = 0 7245try 7246for i in range ( 1 ) 7247endwhile 7248catch /E733:/ 7249let caught_e733 = 1 7250endtry 7251call assert_equal ( 1 , caught_e733 ) 7252 7253" Using endfunc with :if 7254call assert_fails ( 'exe "if 1 | endfunc | endif"' , 'E193:' ) 7255 7256" Missing 'in' in a :for statement 7257call assert_fails ( 'for i range(1) | endfor' , 'E690:' ) 7258 7259" Incorrect number of variables in for 7260call assert_fails ( 'for [i,] in range(3) | endfor' , 'E475:' ) 7261endfunc 7262 7263" Test for deep nesting of if/for/while/try statements {{{1 7264func Test_deep_nest () 7265CheckRunVimInTerminal 7266 7267let lines =<< trim [SCRIPT] 7268" Deep nesting of if ... endif 7269func Test1() 7270let @a = join(repeat(['if v:true'], 51), "\n") 7271let @a ..= "\n" 7272let @a ..= join(repeat(['endif'], 51), "\n") 7273@a 7274let @a = '' 7275endfunc 7276 7277" Deep nesting of for ... endfor 7278func Test2() 7279let @a = join(repeat(['for i in [1]'], 51), "\n") 7280let @a ..= "\n" 7281let @a ..= join(repeat(['endfor'], 51), "\n") 7282@a 7283let @a = '' 7284endfunc 7285 7286" Deep nesting of while ... endwhile 7287func Test3() 7288let @a = join(repeat(['while v:true'], 51), "\n") 7289let @a ..= "\n" 7290let @a ..= join(repeat(['endwhile'], 51), "\n") 7291@a 7292let @a = '' 7293endfunc 7294 7295" Deep nesting of try ... endtry 7296func Test4() 7297let @a = join(repeat(['try'], 51), "\n") 7298let @a ..= "\necho v:true\n" 7299let @a ..= join(repeat(['endtry'], 51), "\n") 7300@a 7301let @a = '' 7302endfunc 7303 7304" Deep nesting of function ... endfunction 7305func Test5() 7306let @a = join(repeat(['function X()'], 51), "\n") 7307let @a ..= "\necho v:true\n" 7308let @a ..= join(repeat(['endfunction'], 51), "\n") 7309@a 7310let @a = '' 7311endfunc 7312[SCRIPT] 7313call writefile ( lines , 'Xscript' ) 7314 7315let buf = RunVimInTerminal ( '-S Xscript' , { 'rows' : 6 }) 7316 7317" Deep nesting of if ... endif 7318call term_sendkeys ( buf , ":call Test1()\n" ) 7319call TermWait ( buf ) 7320call WaitForAssert ({ -> assert_match ( '^E579:' , term_getline ( buf , 5 ))}) 7321 7322" Deep nesting of for ... endfor 7323call term_sendkeys ( buf , ":call Test2()\n" ) 7324call TermWait ( buf ) 7325call WaitForAssert ({ -> assert_match ( '^E585:' , term_getline ( buf , 5 ))}) 7326 7327" Deep nesting of while ... endwhile 7328call term_sendkeys ( buf , ":call Test3()\n" ) 7329call TermWait ( buf ) 7330call WaitForAssert ({ -> assert_match ( '^E585:' , term_getline ( buf , 5 ))}) 7331 7332" Deep nesting of try ... endtry 7333call term_sendkeys ( buf , ":call Test4()\n" ) 7334call TermWait ( buf ) 7335call WaitForAssert ({ -> assert_match ( '^E601:' , term_getline ( buf , 5 ))}) 7336 7337" Deep nesting of function ... endfunction 7338call term_sendkeys ( buf , ":call Test5()\n" ) 7339call TermWait ( buf ) 7340call WaitForAssert ({ -> assert_match ( '^E1058:' , term_getline ( buf , 4 ))}) 7341call term_sendkeys ( buf , "\<C-C>\n" ) 7342call TermWait ( buf ) 7343 7344"let l = '' 7345"for i in range(1, 6) 7346" let l ..= term_getline(buf, i) . "\n" 7347"endfor 7348"call assert_report(l) 7349 7350call StopVimInTerminal ( buf ) 7351call delete ( 'Xscript' ) 7352endfunc 7353 7354" Test for errors in converting to float from various types {{{1 7355func Test_float_conversion_errors () 7356if has ( 'float' ) 7357call assert_fails ( 'let x = 4.0 % 2.0' , 'E804' ) 7358call assert_fails ( 'echo 1.1[0]' , 'E806' ) 7359call assert_fails ( 'echo sort([function("min"), 1], "f")' , 'E891:' ) 7360call assert_fails ( 'echo 3.2 == "vim"' , 'E892:' ) 7361call assert_fails ( 'echo sort([[], 1], "f")' , 'E893:' ) 7362call assert_fails ( 'echo sort([{}, 1], "f")' , 'E894:' ) 7363call assert_fails ( 'echo 3.2 == v:true' , 'E362:' ) 7364" call assert_fails('echo 3.2 == v:none', 'E907:') 7365endif 7366endfunc 7367 7368" invalid function names {{{1 7369func Test_invalid_function_names () 7370" function name not starting with capital 7371let caught_e128 = 0 7372try 7373func ! g: test () 7374echo "test" 7375endfunc 7376catch /E128:/ 7377let caught_e128 = 1 7378endtry 7379call assert_equal ( 1 , caught_e128 ) 7380 7381" function name includes a colon 7382let caught_e884 = 0 7383try 7384func ! b: test () 7385echo "test" 7386endfunc 7387catch /E884:/ 7388let caught_e884 = 1 7389endtry 7390call assert_equal ( 1 , caught_e884 ) 7391 7392" function name followed by # 7393let caught_e128 = 0 7394try 7395func ! test2 () "# 7396echo "test2" 7397endfunc 7398catch /E128:/ 7399let caught_e128 = 1 7400endtry 7401call assert_equal ( 1 , caught_e128 ) 7402 7403" function name starting with/without "g:", buffer-local funcref. 7404function ! g: Foo ( n ) 7405return 'called Foo(' . a: n . ')' 7406endfunction 7407let b: my_func = function ( 'Foo' ) 7408call assert_equal ( 'called Foo(1)' , b: my_func ( 1 )) 7409call assert_equal ( 'called Foo(2)' , g: Foo ( 2 )) 7410call assert_equal ( 'called Foo(3)' , Foo ( 3 )) 7411delfunc g:Foo 7412 7413" script-local function used in Funcref must exist. 7414let lines =<< trim END 7415func s:Testje() 7416return "foo" 7417endfunc 7418let Bar = function('s:Testje') 7419call assert_equal(0, exists('s:Testje')) 7420call assert_equal(1, exists('*s:Testje')) 7421call assert_equal(1, exists('Bar')) 7422call assert_equal(1, exists('*Bar')) 7423END 7424call writefile ( lines , 'Xscript' ) 7425source Xscript 7426call delete ( 'Xscript' ) 7427endfunc 7428 7429" substring and variable name {{{1 7430func Test_substring_var () 7431let str = 'abcdef' 7432let n = 3 7433call assert_equal ( 'def' , str [ n :]) 7434call assert_equal ( 'abcd' , str [: n ]) 7435call assert_equal ( 'd' , str [ n : n ]) 7436unlet n 7437let nn = 3 7438call assert_equal ( 'def' , str [ nn :]) 7439call assert_equal ( 'abcd' , str [: nn ]) 7440call assert_equal ( 'd' , str [ nn : nn ]) 7441unlet nn 7442let b: nn = 4 7443call assert_equal ( 'ef' , str [ b: nn :]) 7444call assert_equal ( 'abcde' , str [: b: nn ]) 7445call assert_equal ( 'e' , str [ b: nn : b: nn ]) 7446unlet b: nn 7447endfunc 7448 7449" Test using s: with a typed command {{{1 7450func Test_typed_script_var () 7451CheckRunVimInTerminal 7452 7453let buf = RunVimInTerminal ( '' , { 'rows' : 6 }) 7454 7455" Deep nesting of if ... endif 7456call term_sendkeys ( buf , ":echo get(s:, 'foo', 'x')\n" ) 7457call TermWait ( buf ) 7458call WaitForAssert ({ -> assert_match ( '^E116:' , term_getline ( buf , 5 ))}) 7459 7460call StopVimInTerminal ( buf ) 7461endfunc 7462 7463" Test for issue6776 {{{1 7464func Test_ternary_expression () 7465try 7466call eval ( '0 ? 0' ) 7467catch 7468endtry 7469" previous failure should not cause next expression to fail 7470call assert_equal ( v: false , eval ( string ( v: false ))) 7471 7472try 7473call eval ( '0 ? "burp' ) 7474catch 7475endtry 7476" previous failure should not cause next expression to fail 7477call assert_equal ( v: false , eval ( string ( v: false ))) 7478 7479try 7480call eval ( '1 ? 0 : "burp' ) 7481catch 7482endtry 7483" previous failure should not cause next expression to fail 7484call assert_equal ( v: false , eval ( string ( v: false ))) 7485endfunction 7486 7487func Test_for_over_string () 7488let res = '' 7489for c in 'aéc̀d' 7490let res ..= c .. '-' 7491endfor 7492call assert_equal ( 'a-é-c̀-d-' , res ) 7493 7494let res = '' 7495for c in '' 7496let res ..= c .. '-' 7497endfor 7498call assert_equal ( '' , res ) 7499 7500let res = '' 7501for c in v: _null_string 7502let res ..= c .. '-' 7503endfor 7504call assert_equal ( '' , res ) 7505 7506" Test for using "_" as the loop variable 7507let i = 0 7508let s = 'abc' 7509for _ in s 7510call assert_equal ( s [ i ], _ ) 7511let i += 1 7512endfor 7513endfunc 7514 7515" Test for deeply nested :source command {{{1 7516func Test_deeply_nested_source () 7517throw 'Skipped: Vim9 script is N/A' 7518let lines =<< trim END 7519 7520so 7521sil 0scr 7522delete 7523so 75240 7525END 7526call writefile ([ "vim9 silent! @0 \n/" ] + lines , 'Xnested.vim' , 'D' ) 7527 7528" this must not crash 7529let cmd = GetVimCommand () .. " -e -s -S Xnested.vim -c qa!" 7530call system ( cmd ) 7531endfunc 7532 7533" Test for impact of silent! on an exception {{{1 7534func Test_exception_silent () 7535XpathINIT 7536let lines =<< trim END 7537func Throw() 7538Xpath 'a' 7539throw "Uncaught" 7540" This line is not executed. 7541Xpath 'b' 7542endfunc 7543" The exception is suppressed due to the presence of silent!. 7544silent! call Throw() 7545try 7546call DoesNotExist() 7547catch /E117:/ 7548Xpath 'c' 7549endtry 7550Xpath 'd' 7551END 7552let verify =<< trim END 7553call assert_equal('acd', g:Xpath) 7554END 7555 7556call RunInNewVim ( lines , verify ) 7557endfunc 7558 7559" Test for an error message starting with "Vim E123: " {{{1 7560func Test_skip_prefix_in_exception () 7561let emsg = '' 7562try 7563echoerr "Vim E123:" 7564catch 7565let emsg = v: exception 7566endtry 7567call assert_equal ( 'Vim(echoerr):Vim E123:' , emsg ) 7568 7569let emsg = '' 7570try 7571echoerr "Vim E123: abc" 7572catch 7573let emsg = v: exception 7574endtry 7575call assert_equal ( 'Vim(echoerr):E123: abc' , emsg ) 7576endfunc 7577 7578" Test for try/except messages displayed with 'verbose' level set to 13 {{{1 7579func Test_verbose_try_except_messages () 7580let msgs = '' 7581redir => msgs 7582set verbose = 13 7583try 7584echoerr 'foo' 7585catch 7586echo v: exception 7587endtry 7588set verbose = 0 7589redir END 7590let expected =<< trim END 7591Exception thrown: Vim(echoerr):foo 7592 7593Exception caught: Vim(echoerr):foo 7594 7595Vim(echoerr):foo 7596Exception finished: Vim(echoerr):foo 7597END 7598call assert_equal ( expected , msgs -> split ( "\n" )) 7599endfunc 7600 7601" Test for trailing characters after a catch pattern {{{1 7602func Test_catch_pattern_trailing_chars () 7603let lines =<< trim END 7604try 7605echoerr 'foo' 7606catch /foo/xxx 7607echo 'caught foo' 7608endtry 7609END 7610 7611new 7612call setline ( 1 , lines ) 7613let caught_exception = v: false 7614try 7615source 7616catch /E488: Trailing characters: \/xxx/ 7617let caught_exception = v: true 7618endtry 7619call assert_true ( caught_exception ) 7620bw ! 7621endfunc 7622 7623" Test using fullcommand() {{{1 7624func Test_builtin_fullcommand () 7625" :hor is the minimum abbreviation of :horizontal; :ho is invalid 7626call assert_equal ( '' , fullcommand ( 'ho' )) 7627call assert_equal ( 'horizontal' , fullcommand ( 'hor' )) 7628 7629" :k takes one {a-zA-Z'} mark argument and optional whitespace 7630call assert_equal ( 'k' , fullcommand ( 'k' )) 7631call assert_equal ( 'k' , fullcommand ( ':k' )) 7632call assert_equal ( 'k' , fullcommand ( 'karrrrrgh!' )) 7633 7634" :dl is "delete and list" in a legacy Vim script scope 7635call assert_equal ( 'delete' , fullcommand ( 'dl' )) 7636 7637" :s two and three letter commands 7638call assert_equal ( 'substitute' , fullcommand ( 'sIr' )) 7639call assert_equal ( 'substitute' , fullcommand ( 'sIrarrrrrgh!' )) 7640 7641" :finally 7642call assert_equal ( 'finally' , fullcommand ( 'fina' )) 7643" 'final' - returns 'final', a Vim9 script-exclusive keyword 7644" - is a valid shortening of :finally in legacy Vim script 7645"call assert_equal('final', fullcommand('final')) 7646call assert_equal ( 'finally' , fullcommand ( 'finall' )) 7647 7648endfunc 7649 7650"------------------------------------------------------------------------------- 7651" Modelines {{{1 7652" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 7653"-------------------------------------------------------------------------------