robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
933b99a2b1
master
1" Test try-catch-finally exception handling 2" Most of this was formerly in test49. 3 4source check.vim 5source shared.vim 6source vim9.vim 7 8"------------------------------------------------------------------------------- 9" Test environment {{{1 10"------------------------------------------------------------------------------- 11 12com ! XpathINIT let g:Xpath = '' 13com ! -nargs = 1 -bar Xpath let g:Xpath = g:Xpath . <args> 14 15" Test 25: Executing :finally clauses on normal control flow {{{1 16" 17" Control flow in a :try conditional should always fall through to its 18" :finally clause. A :finally clause of a :try conditional inside an 19" inactive conditional should never be executed. 20"------------------------------------------------------------------------------- 21 22func T25_F () 23let loops = 3 24while loops > 0 25Xpath 'a' . loops 26if loops >= 2 27try 28Xpath 'b' . loops 29if loops == 2 30try 31Xpath 'c' . loops 32finally 33Xpath 'd' . loops 34endtry 35endif 36finally 37Xpath 'e' . loops 38if loops == 2 39try 40Xpath 'f' . loops 41final 42Xpath 'g' . loops 43endtry 44endif 45endtry 46endif 47Xpath 'h' . loops 48let loops = loops - 1 49endwhile 50Xpath 'i' 51endfunc 52 53" Also try using "fina" and "final" and "finall" as abbreviations. 54func T25_G () 55if 1 56try 57Xpath 'A' 58call T25_F () 59Xpath 'B' 60fina 61Xpath 'C' 62endtry 63else 64try 65Xpath 'D' 66finall 67Xpath 'E' 68endtry 69endif 70endfunc 71 72func Test_finally () 73XpathINIT 74call T25_G () 75call assert_equal ( 'Aa3b3e3h3a2b2c2d2e2f2g2h2a1h1iBC' , g: Xpath ) 76endfunc 77 78 79"------------------------------------------------------------------------------- 80" Test 26: Executing :finally clauses after :continue or :break {{{1 81" 82" For a :continue or :break dynamically enclosed in a :try/:endtry 83" region inside the next surrounding :while/:endwhile, if the 84" :continue/:break is before the :finally, the :finally clause is 85" executed first. If the :continue/:break is after the :finally, the 86" :finally clause is broken (like an :if/:endif region). 87"------------------------------------------------------------------------------- 88 89func T26_F () 90try 91let loops = 3 92while loops > 0 93try 94try 95if loops == 2 96Xpath 'a' . loops 97let loops = loops - 1 98continue 99elseif loops == 1 100Xpath 'b' . loops 101break 102finish 103endif 104Xpath 'c' . loops 105endtry 106finally 107Xpath 'd' . loops 108endtry 109Xpath 'e' . loops 110let loops = loops - 1 111endwhile 112Xpath 'f' 113finally 114Xpath 'g' 115let loops = 3 116while loops > 0 117try 118finally 119try 120if loops == 2 121Xpath 'h' . loops 122let loops = loops - 1 123continue 124elseif loops == 1 125Xpath 'i' . loops 126break 127finish 128endif 129endtry 130Xpath 'j' . loops 131endtry 132Xpath 'k' . loops 133let loops = loops - 1 134endwhile 135Xpath 'l' 136endtry 137Xpath 'm' 138endfunc 139 140func Test_finally_after_continue () 141XpathINIT 142call T26_F () 143call assert_equal ( 'c3d3e3a2d1b1d1fgj3k3h2i1lm' , g: Xpath ) 144endfunc 145 146 147"------------------------------------------------------------------------------- 148" Test 32: Remembering the :return value on :finally {{{1 149" 150" If a :finally clause is executed due to a :return specifying 151" a value, this is the value visible to the caller if not overwritten 152" by a new :return in the :finally clause. A :return without a value 153" in the :finally clause overwrites with value 0. 154"------------------------------------------------------------------------------- 155 156func T32_F () 157try 158Xpath 'a' 159try 160Xpath 'b' 161return "ABCD" 162Xpath 'c' 163finally 164Xpath 'd' 165endtry 166Xpath 'e' 167finally 168Xpath 'f' 169endtry 170Xpath 'g' 171endfunc 172 173func T32_G () 174try 175Xpath 'h' 176return 8 177Xpath 'i' 178finally 179Xpath 'j' 180return 16 + strlen ( T32_F ()) 181Xpath 'k' 182endtry 183Xpath 'l' 184endfunc 185 186func T32_H () 187try 188Xpath 'm' 189return 32 190Xpath 'n' 191finally 192Xpath 'o' 193return 194Xpath 'p' 195endtry 196Xpath 'q' 197endfunc 198 199func T32_I () 200try 201Xpath 'r' 202finally 203Xpath 's' 204return T32_G () + T32_H () + 64 205Xpath 't' 206endtry 207Xpath 'u' 208endfunc 209 210func Test_finally_return () 211XpathINIT 212call assert_equal ( 84 , T32_I ()) 213call assert_equal ( 'rshjabdfmo' , g: Xpath ) 214endfunc 215 216"------------------------------------------------------------------------------- 217" Test 33: :return under :execute or user command and :finally {{{1 218" 219" A :return command may be executed under an ":execute" or from 220" a user command. Executing of :finally clauses and passing through 221" the return code works also then. 222"------------------------------------------------------------------------------- 223 224func T33_F () 225try 226RETURN 10 227Xpath 'a' 228finally 229Xpath 'b' 230endtry 231Xpath 'c' 232endfunc 233 234func T33_G () 235try 236RETURN 20 237Xpath 'd' 238finally 239Xpath 'e' 240RETURN 30 241Xpath 'f' 242endtry 243Xpath 'g' 244endfunc 245 246func T33_H () 247try 248execute "try | return 40 | finally | return 50 | endtry" 249Xpath 'h' 250finally 251Xpath 'i' 252endtry 253Xpath 'j' 254endfunc 255 256func T33_I () 257try 258execute "try | return 60 | finally | return 70 | endtry" 259Xpath 'k' 260finally 261Xpath 'l' 262execute "try | return 80 | finally | return 90 | endtry" 263Xpath 'm' 264endtry 265Xpath 'n' 266endfunc 267 268func T33_J () 269try 270RETURN 100 271Xpath 'o' 272finally 273Xpath 'p' 274return 275Xpath 'q' 276endtry 277Xpath 'r' 278endfunc 279 280func T33_K () 281try 282execute "try | return 110 | finally | return 120 | endtry" 283Xpath 's' 284finally 285Xpath 't' 286execute "try | return 130 | finally | return | endtry" 287Xpath 'u' 288endtry 289Xpath 'v' 290endfunc 291 292func T33_L () 293try 294return 295Xpath 'w' 296finally 297Xpath 'x' 298RETURN 140 299Xpath 'y' 300endtry 301Xpath 'z' 302endfunc 303 304func T33_M () 305try 306return 307Xpath 'A' 308finally 309Xpath 'B' 310execute "try | return 150 | finally | return 160 | endtry" 311Xpath 'C' 312endtry 313Xpath 'D' 314endfunc 315 316func T33_N () 317RETURN 170 318endfunc 319 320func T33_O () 321execute "try | return 180 | finally | return 190 | endtry" 322endfunc 323 324func Test_finally_cmd_return () 325command ! -nargs = ? RETURN 326 \try | return <args> | finally | return <args> * 2 | endtry 327XpathINIT 328call assert_equal ( 20 , T33_F ()) 329call assert_equal ( 60 , T33_G ()) 330call assert_equal ( 50 , T33_H ()) 331call assert_equal ( 90 , T33_I ()) 332call assert_equal ( 0 , T33_J ()) 333call assert_equal ( 0 , T33_K ()) 334call assert_equal ( 280 , T33_L ()) 335call assert_equal ( 160 , T33_M ()) 336call assert_equal ( 340 , T33_N ()) 337call assert_equal ( 190 , T33_O ()) 338call assert_equal ( 'beilptxB' , g: Xpath ) 339delcommand RETURN 340endfunc 341 342 343"------------------------------------------------------------------------------- 344" Test 41: Skipped :throw finding next command {{{1 345" 346" A :throw in an inactive conditional must not hide a following 347" command. 348"------------------------------------------------------------------------------- 349 350func T41_F () 351Xpath 'a' 352if 0 |throw 'never' |endif |Xpath 'b' 353Xpath 'c' 354endfunc 355 356func T41_G () 357Xpath 'd' 358while 0 |throw 'never' |endwhile |Xpath 'e' 359Xpath 'f' 360endfunc 361 362func T41_H () 363Xpath 'g' 364if 0 |try |throw 'never' |endtry |endif |Xpath 'h' 365Xpath 'i' 366endfunc 367 368func Test_throw_inactive_cond () 369XpathINIT 370try 371Xpath 'j' 372call T41_F () 373Xpath 'k' 374catch /.*/ 375Xpath 'l' 376call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 377endtry 378 379try 380Xpath 'm' 381call T41_G () 382Xpath 'n' 383catch /.*/ 384Xpath 'o' 385call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 386endtry 387 388try 389Xpath 'p' 390call T41_H () 391Xpath 'q' 392catch /.*/ 393Xpath 'r' 394call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 395endtry 396 397call assert_equal ( 'jabckmdefnpghiq' , g: Xpath ) 398endfunc 399 400 401"------------------------------------------------------------------------------- 402" Test 42: Catching number and string exceptions {{{1 403" 404" When a number is thrown, it is converted to a string exception. 405" Numbers and strings may be caught by specifying a regular exception 406" as argument to the :catch command. 407"------------------------------------------------------------------------------- 408 409 410func T42_F () 411try 412 413try 414Xpath 'a' 415throw 4711 416Xpath 'b' 417catch /4711/ 418Xpath 'c' 419endtry 420 421try 422Xpath 'd' 423throw 4711 424Xpath 'e' 425catch /^4711$/ 426Xpath 'f' 427endtry 428 429try 430Xpath 'g' 431throw 4711 432Xpath 'h' 433catch /\d/ 434Xpath 'i' 435endtry 436 437try 438Xpath 'j' 439throw 4711 440Xpath 'k' 441catch /^\d\+$/ 442Xpath 'l' 443endtry 444 445try 446Xpath 'm' 447throw "arrgh" 448Xpath 'n' 449catch /arrgh/ 450Xpath 'o' 451endtry 452 453try 454Xpath 'p' 455throw "arrgh" 456Xpath 'q' 457catch /^arrgh$/ 458Xpath 'r' 459endtry 460 461try 462Xpath 's' 463throw "arrgh" 464Xpath 't' 465catch /\l/ 466Xpath 'u' 467endtry 468 469try 470Xpath 'v' 471throw "arrgh" 472Xpath 'w' 473catch /^\l\+$/ 474Xpath 'x' 475endtry 476 477try 478try 479Xpath 'y' 480throw "ARRGH" 481Xpath 'z' 482catch /^arrgh$/ 483Xpath 'A' 484endtry 485catch /^\carrgh$/ 486Xpath 'B' 487endtry 488 489try 490Xpath 'C' 491throw "" 492Xpath 'D' 493catch /^$/ 494Xpath 'E' 495endtry 496 497catch /.*/ 498Xpath 'F' 499call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 500endtry 501endfunc 502 503func Test_catch_number_string () 504XpathINIT 505call T42_F () 506call assert_equal ( 'acdfgijlmoprsuvxyBCE' , g: Xpath ) 507endfunc 508 509 510"------------------------------------------------------------------------------- 511" Test 43: Selecting the correct :catch clause {{{1 512" 513" When an exception is thrown and there are multiple :catch clauses, 514" the first matching one is taken. 515"------------------------------------------------------------------------------- 516 517func T43_F () 518let loops = 3 519while loops > 0 520try 521if loops == 3 522Xpath 'a' . loops 523throw "a" 524Xpath 'b' . loops 525elseif loops == 2 526Xpath 'c' . loops 527throw "ab" 528Xpath 'd' . loops 529elseif loops == 1 530Xpath 'e' . loops 531throw "abc" 532Xpath 'f' . loops 533endif 534catch /abc/ 535Xpath 'g' . loops 536catch /ab/ 537Xpath 'h' . loops 538catch /.*/ 539Xpath 'i' . loops 540catch /a/ 541Xpath 'j' . loops 542endtry 543 544let loops = loops - 1 545endwhile 546Xpath 'k' 547endfunc 548 549func Test_multi_catch () 550XpathINIT 551call T43_F () 552call assert_equal ( 'a3i3c2h2e1g1k' , g: Xpath ) 553endfunc 554 555 556"------------------------------------------------------------------------------- 557" Test 44: Missing or empty :catch patterns {{{1 558" 559" A missing or empty :catch pattern means the same as /.*/, that is, 560" catches everything. To catch only empty exceptions, /^$/ must be 561" used. A :catch with missing, empty, or /.*/ argument also works 562" when followed by another command separated by a bar on the same 563" line. :catch patterns cannot be specified between ||. But other 564" pattern separators can be used instead of //. 565"------------------------------------------------------------------------------- 566 567func T44_F () 568try 569try 570Xpath 'a' 571throw "" 572catch /^$/ 573Xpath 'b' 574endtry 575 576try 577Xpath 'c' 578throw "" 579catch /.*/ 580Xpath 'd' 581endtry 582 583try 584Xpath 'e' 585throw "" 586catch // 587Xpath 'f' 588endtry 589 590try 591Xpath 'g' 592throw "" 593catch 594Xpath 'h' 595endtry 596 597try 598Xpath 'i' 599throw "oops" 600catch /^$/ 601Xpath 'j' 602catch /.*/ 603Xpath 'k' 604endtry 605 606try 607Xpath 'l' 608throw "arrgh" 609catch /^$/ 610Xpath 'm' 611catch // 612Xpath 'n' 613endtry 614 615try 616Xpath 'o' 617throw "brrr" 618catch /^$/ 619Xpath 'p' 620catch 621Xpath 'q' 622endtry 623 624try |Xpath 'r' |throw "x" |catch /.*/ |Xpath 's' |endtry 625 626try |Xpath 't' |throw "y" |catch // |Xpath 'u' |endtry 627 628while 1 629try 630let caught = 0 631let v: errmsg = "" 632" Extra try level: if ":catch" without arguments below raises 633" a syntax error because it misinterprets the "Xpath" as a pattern, 634" let it be caught by the ":catch /.*/" below. 635try 636try |Xpath 'v' |throw "z" |catch |Xpath 'w' |: 637endtry 638endtry 639catch /.*/ 640let caught = 1 641call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 642finally 643if $ VIMNOERRTHROW && v: errmsg != "" 644call assert_report ( v: errmsg ) 645endif 646if caught || $ VIMNOERRTHROW && v: errmsg != "" 647Xpath 'x' 648endif 649break " discard error for $VIMNOERRTHROW 650endtry 651endwhile 652 653let cologne = 4711 654try 655try 656Xpath 'y' 657throw "throw cologne" 658" Next lines catches all and throws 4711: 659catch |throw cologne | 660Xpath 'z' 661endtry 662catch /4711/ 663Xpath 'A' 664endtry 665 666try 667Xpath 'B' 668throw "plus" 669catch +plus+ 670Xpath 'C' 671endtry 672 673Xpath 'D' 674catch /.*/ 675Xpath 'E' 676call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 677endtry 678endfunc 679 680func Test_empty_catch () 681XpathINIT 682call T44_F () 683call assert_equal ( 'abcdefghiklnoqrstuvwyABCD' , g: Xpath ) 684endfunc 685 686 687"------------------------------------------------------------------------------- 688" Test 45: Catching exceptions from nested :try blocks {{{1 689" 690" When :try blocks are nested, an exception is caught by the innermost 691" try conditional that has a matching :catch clause. 692"------------------------------------------------------------------------------- 693 694func T45_F () 695let loops = 3 696while loops > 0 697try 698try 699try 700try 701if loops == 3 702Xpath 'a' . loops 703throw "a" 704Xpath 'b' . loops 705elseif loops == 2 706Xpath 'c' . loops 707throw "ab" 708Xpath 'd' . loops 709elseif loops == 1 710Xpath 'e' . loops 711throw "abc" 712Xpath 'f' . loops 713endif 714catch /abc/ 715Xpath 'g' . loops 716endtry 717catch /ab/ 718Xpath 'h' . loops 719endtry 720catch /.*/ 721Xpath 'i' . loops 722endtry 723catch /a/ 724Xpath 'j' . loops 725endtry 726 727let loops = loops - 1 728endwhile 729Xpath 'k' 730endfunc 731 732func Test_catch_from_nested_try () 733XpathINIT 734call T45_F () 735call assert_equal ( 'a3i3c2h2e1g1k' , g: Xpath ) 736endfunc 737 738 739"------------------------------------------------------------------------------- 740" Test 46: Executing :finally after a :throw in nested :try {{{1 741" 742" When an exception is thrown from within nested :try blocks, the 743" :finally clauses of the non-catching try conditionals should be 744" executed before the matching :catch of the next surrounding :try 745" gets the control. If this also has a :finally clause, it is 746" executed afterwards. 747"------------------------------------------------------------------------------- 748 749func T46_F () 750let sum = 0 751 752try 753Xpath 'a' 754try 755Xpath 'b' 756try 757Xpath 'c' 758try 759Xpath 'd' 760throw "ABC" 761Xpath 'e' 762catch /xyz/ 763Xpath 'f' 764finally 765Xpath 'g' 766if sum != 0 767Xpath 'h' 768endif 769let sum = sum + 1 770endtry 771Xpath 'i' 772catch /123/ 773Xpath 'j' 774catch /321/ 775Xpath 'k' 776finally 777Xpath 'l' 778if sum != 1 779Xpath 'm' 780endif 781let sum = sum + 2 782endtry 783Xpath 'n' 784finally 785Xpath 'o' 786if sum != 3 787Xpath 'p' 788endif 789let sum = sum + 4 790endtry 791Xpath 'q' 792catch /ABC/ 793Xpath 'r' 794if sum != 7 795Xpath 's' 796endif 797let sum = sum + 8 798finally 799Xpath 't' 800if sum != 15 801Xpath 'u' 802endif 803let sum = sum + 16 804endtry 805Xpath 'v' 806if sum != 31 807Xpath 'w' 808endif 809endfunc 810 811func Test_finally_after_throw () 812XpathINIT 813call T46_F () 814call assert_equal ( 'abcdglortv' , g: Xpath ) 815endfunc 816 817 818"------------------------------------------------------------------------------- 819" Test 47: Throwing exceptions from a :catch clause {{{1 820" 821" When an exception is thrown from a :catch clause, it should not be 822" caught by a :catch of the same :try conditional. After executing 823" the :finally clause (if present), surrounding try conditionals 824" should be checked for a matching :catch. 825"------------------------------------------------------------------------------- 826 827func T47_F () 828Xpath 'a' 829try 830Xpath 'b' 831try 832Xpath 'c' 833try 834Xpath 'd' 835throw "x1" 836Xpath 'e' 837catch /x1/ 838Xpath 'f' 839try 840Xpath 'g' 841throw "x2" 842Xpath 'h' 843catch /x1/ 844Xpath 'i' 845catch /x2/ 846Xpath 'j' 847try 848Xpath 'k' 849throw "x3" 850Xpath 'l' 851catch /x1/ 852Xpath 'm' 853catch /x2/ 854Xpath 'n' 855finally 856Xpath 'o' 857endtry 858Xpath 'p' 859catch /x3/ 860Xpath 'q' 861endtry 862Xpath 'r' 863catch /x1/ 864Xpath 's' 865catch /x2/ 866Xpath 't' 867catch /x3/ 868Xpath 'u' 869finally 870Xpath 'v' 871endtry 872Xpath 'w' 873catch /x1/ 874Xpath 'x' 875catch /x2/ 876Xpath 'y' 877catch /x3/ 878Xpath 'z' 879endtry 880Xpath 'A' 881catch /.*/ 882Xpath 'B' 883call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 884endtry 885Xpath 'C' 886endfunc 887 888func Test_throw_from_catch () 889XpathINIT 890call T47_F () 891call assert_equal ( 'abcdfgjkovzAC' , g: Xpath ) 892endfunc 893 894 895"------------------------------------------------------------------------------- 896" Test 48: Throwing exceptions from a :finally clause {{{1 897" 898" When an exception is thrown from a :finally clause, it should not be 899" caught by a :catch of the same :try conditional. Surrounding try 900" conditionals should be checked for a matching :catch. A previously 901" thrown exception is discarded. 902"------------------------------------------------------------------------------- 903 904func T48_F () 905try 906 907try 908try 909Xpath 'a' 910catch /x1/ 911Xpath 'b' 912finally 913Xpath 'c' 914throw "x1" 915Xpath 'd' 916endtry 917Xpath 'e' 918catch /x1/ 919Xpath 'f' 920endtry 921Xpath 'g' 922 923try 924try 925Xpath 'h' 926throw "x2" 927Xpath 'i' 928catch /x2/ 929Xpath 'j' 930catch /x3/ 931Xpath 'k' 932finally 933Xpath 'l' 934throw "x3" 935Xpath 'm' 936endtry 937Xpath 'n' 938catch /x2/ 939Xpath 'o' 940catch /x3/ 941Xpath 'p' 942endtry 943Xpath 'q' 944 945try 946try 947try 948Xpath 'r' 949throw "x4" 950Xpath 's' 951catch /x5/ 952Xpath 't' 953finally 954Xpath 'u' 955throw "x5" " discards 'x4' 956Xpath 'v' 957endtry 958Xpath 'w' 959catch /x4/ 960Xpath 'x' 961finally 962Xpath 'y' 963endtry 964Xpath 'z' 965catch /x5/ 966Xpath 'A' 967endtry 968Xpath 'B' 969 970catch /.*/ 971Xpath 'C' 972call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 973endtry 974Xpath 'D' 975endfunc 976 977func Test_throw_from_finally () 978XpathINIT 979call T48_F () 980call assert_equal ( 'acfghjlpqruyABD' , g: Xpath ) 981endfunc 982 983 984"------------------------------------------------------------------------------- 985" Test 51: Throwing exceptions across :execute and user commands {{{1 986" 987" A :throw command may be executed under an ":execute" or from 988" a user command. 989"------------------------------------------------------------------------------- 990 991func T51_F () 992command ! -nargs = ? THROW1 throw <args> | throw 1 993command ! -nargs = ? THROW2 try | throw <args> | endtry | throw 2 994command ! -nargs = ? THROW3 try | throw 3 | catch /3/ | throw <args> | endtry 995command ! -nargs = ? THROW4 try | throw 4 | finally | throw <args> | endtry 996 997try 998 999try 1000try 1001Xpath 'a' 1002THROW1 "A" 1003catch /A/ 1004Xpath 'b' 1005endtry 1006catch /1/ 1007Xpath 'c' 1008endtry 1009 1010try 1011try 1012Xpath 'd' 1013THROW2 "B" 1014catch /B/ 1015Xpath 'e' 1016endtry 1017catch /2/ 1018Xpath 'f' 1019endtry 1020 1021try 1022try 1023Xpath 'g' 1024THROW3 "C" 1025catch /C/ 1026Xpath 'h' 1027endtry 1028catch /3/ 1029Xpath 'i' 1030endtry 1031 1032try 1033try 1034Xpath 'j' 1035THROW4 "D" 1036catch /D/ 1037Xpath 'k' 1038endtry 1039catch /4/ 1040Xpath 'l' 1041endtry 1042 1043try 1044try 1045Xpath 'm' 1046execute 'throw "E" | throw 5' 1047catch /E/ 1048Xpath 'n' 1049endtry 1050catch /5/ 1051Xpath 'o' 1052endtry 1053 1054try 1055try 1056Xpath 'p' 1057execute 'try | throw "F" | endtry | throw 6' 1058catch /F/ 1059Xpath 'q' 1060endtry 1061catch /6/ 1062Xpath 'r' 1063endtry 1064 1065try 1066try 1067Xpath 's' 1068execute 'try | throw 7 | catch /7/ | throw "G" | endtry' 1069catch /G/ 1070Xpath 't' 1071endtry 1072catch /7/ 1073Xpath 'u' 1074endtry 1075 1076try 1077try 1078Xpath 'v' 1079execute 'try | throw 8 | finally | throw "H" | endtry' 1080catch /H/ 1081Xpath 'w' 1082endtry 1083catch /8/ 1084Xpath 'x' 1085endtry 1086 1087catch /.*/ 1088Xpath 'y' 1089call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 1090endtry 1091 1092Xpath 'z' 1093 1094delcommand THROW1 1095delcommand THROW2 1096delcommand THROW3 1097delcommand THROW4 1098endfunc 1099 1100func Test_throw_across_commands () 1101XpathINIT 1102call T51_F () 1103call assert_equal ( 'abdeghjkmnpqstvwz' , g: Xpath ) 1104endfunc 1105 1106 1107 1108"------------------------------------------------------------------------------- 1109" Test 69: :throw across :if, :elseif, :while {{{1 1110" 1111" On an :if, :elseif, or :while command, an exception might be thrown 1112" during evaluation of the expression to test. The exception can be 1113" caught by the script. 1114"------------------------------------------------------------------------------- 1115 1116func T69_throw ( x ) 1117Xpath 'x' 1118throw a: x 1119endfunc 1120 1121func Test_throw_ifelsewhile () 1122XpathINIT 1123 1124try 1125try 1126Xpath 'a' 1127if 111 == T69_throw ( "if" ) + 111 1128Xpath 'b' 1129else 1130Xpath 'c' 1131endif 1132Xpath 'd' 1133catch /^if$/ 1134Xpath 'e' 1135catch /.*/ 1136Xpath 'f' 1137call assert_report ( "if: " . v: exception . " in " . v: throwpoint ) 1138endtry 1139 1140try 1141Xpath 'g' 1142if v: false 1143Xpath 'h' 1144elseif 222 == T69_throw ( "elseif" ) + 222 1145Xpath 'i' 1146else 1147Xpath 'j' 1148endif 1149Xpath 'k' 1150catch /^elseif$/ 1151Xpath 'l' 1152catch /.*/ 1153Xpath 'm' 1154call assert_report ( "elseif: " . v: exception . " in " . v: throwpoint ) 1155endtry 1156 1157try 1158Xpath 'n' 1159while 333 == T69_throw ( "while" ) + 333 1160Xpath 'o' 1161break 1162endwhile 1163Xpath 'p' 1164catch /^while$/ 1165Xpath 'q' 1166catch /.*/ 1167Xpath 'r' 1168call assert_report ( "while: " .. v: exception .. " in " .. v: throwpoint ) 1169endtry 1170catch /^0$/ " default return value 1171Xpath 's' 1172call assert_report ( v: throwpoint ) 1173catch /.*/ 1174call assert_report ( v: exception .. " in " .. v: throwpoint ) 1175Xpath 't' 1176endtry 1177 1178call assert_equal ( 'axegxlnxq' , g: Xpath ) 1179endfunc 1180 1181 1182"------------------------------------------------------------------------------- 1183" Test 70: :throw across :return or :throw {{{1 1184" 1185" On a :return or :throw command, an exception might be thrown during 1186" evaluation of the expression to return or throw, respectively. The 1187" exception can be caught by the script. 1188"------------------------------------------------------------------------------- 1189 1190let T70_taken = "" 1191 1192func T70_throw ( x , n ) 1193let g: T70_taken = g: T70_taken . "T" . a: n 1194throw a: x 1195endfunc 1196 1197func T70_F ( x , y , n ) 1198let g: T70_taken = g: T70_taken . "F" . a: n 1199return a: x + T70_throw ( a: y , a: n ) 1200endfunc 1201 1202func T70_G ( x , y , n ) 1203let g: T70_taken = g: T70_taken . "G" . a: n 1204throw a: x . T70_throw ( a: y , a: n ) 1205return a: x 1206endfunc 1207 1208func Test_throwreturn () 1209XpathINIT 1210 1211try 1212try 1213Xpath 'a' 1214call T70_F ( 4711 , "return" , 1 ) 1215Xpath 'b' 1216catch /^return$/ 1217Xpath 'c' 1218catch /.*/ 1219Xpath 'd' 1220call assert_report ( "return: " .. v: exception .. " in " .. v: throwpoint ) 1221endtry 1222 1223try 1224Xpath 'e' 1225let var = T70_F ( 4712 , "return-var" , 2 ) 1226Xpath 'f' 1227catch /^return-var$/ 1228Xpath 'g' 1229catch /.*/ 1230Xpath 'h' 1231call assert_report ( "return-var: " . v: exception . " in " . v: throwpoint ) 1232finally 1233unlet ! var 1234endtry 1235 1236try 1237Xpath 'i' 1238throw "except1" . T70_throw ( "throw1" , 3 ) 1239Xpath 'j' 1240catch /^except1/ 1241Xpath 'k' 1242catch /^throw1$/ 1243Xpath 'l' 1244catch /.*/ 1245Xpath 'm' 1246call assert_report ( "throw1: " .. v: exception .. " in " .. v: throwpoint ) 1247endtry 1248 1249try 1250Xpath 'n' 1251call T70_G ( "except2" , "throw2" , 4 ) 1252Xpath 'o' 1253catch /^except2/ 1254Xpath 'p' 1255catch /^throw2$/ 1256Xpath 'q' 1257catch /.*/ 1258Xpath 'r' 1259call assert_report ( "throw2: " .. v: exception .. " in " .. v: throwpoint ) 1260endtry 1261 1262try 1263Xpath 's' 1264let var = T70_G ( "except3" , "throw3" , 5 ) 1265Xpath 't' 1266catch /^except3/ 1267Xpath 'u' 1268catch /^throw3$/ 1269Xpath 'v' 1270catch /.*/ 1271Xpath 'w' 1272call assert_report ( "throw3: " .. v: exception .. " in " .. v: throwpoint ) 1273finally 1274unlet ! var 1275endtry 1276 1277call assert_equal ( 'F1T1F2T2T3G4T4G5T5' , g: T70_taken ) 1278Xpath 'x' 1279catch /^0$/ " default return value 1280Xpath 'y' 1281call assert_report ( v: throwpoint ) 1282catch /.*/ 1283Xpath 'z' 1284call assert_report ( 'Caught' .. v: exception .. ' in ' .. v: throwpoint ) 1285endtry 1286 1287call assert_equal ( 'acegilnqsvx' , g: Xpath ) 1288endfunc 1289 1290"------------------------------------------------------------------------------- 1291" Test 71: :throw across :echo variants and :execute {{{1 1292" 1293" On an :echo, :echon, :echomsg, :echoerr, or :execute command, an 1294" exception might be thrown during evaluation of the arguments to 1295" be displayed or executed as a command, respectively. Any following 1296" arguments are not evaluated, then. The exception can be caught by 1297" the script. 1298"------------------------------------------------------------------------------- 1299 1300let T71_taken = "" 1301 1302func T71_throw ( x , n ) 1303let g: T71_taken = g: T71_taken . "T" . a: n 1304throw a: x 1305endfunc 1306 1307func T71_F ( n ) 1308let g: T71_taken = g: T71_taken . "F" . a: n 1309return "F" . a: n 1310endfunc 1311 1312func Test_throw_echo () 1313XpathINIT 1314 1315try 1316try 1317Xpath 'a' 1318echo 'echo ' . T71_throw ( "echo-except" , 1 ) . T71_F ( 1 ) 1319Xpath 'b' 1320catch /^echo-except$/ 1321Xpath 'c' 1322catch /.*/ 1323Xpath 'd' 1324call assert_report ( "echo: " .. v: exception .. " in " .. v: throwpoint ) 1325endtry 1326 1327try 1328Xpath 'e' 1329echon "echon " . T71_throw ( "echon-except" , 2 ) . T71_F ( 2 ) 1330Xpath 'f' 1331catch /^echon-except$/ 1332Xpath 'g' 1333catch /.*/ 1334Xpath 'h' 1335call assert_report ( 'echon: ' . v: exception . ' in ' . v: throwpoint ) 1336endtry 1337 1338try 1339Xpath 'i' 1340echomsg "echomsg " . T71_throw ( "echomsg-except" , 3 ) . T71_F ( 3 ) 1341Xpath 'j' 1342catch /^echomsg-except$/ 1343Xpath 'k' 1344catch /.*/ 1345Xpath 'l' 1346call assert_report ( 'echomsg: ' . v: exception . ' in ' . v: throwpoint ) 1347endtry 1348 1349try 1350Xpath 'm' 1351echoerr "echoerr " . T71_throw ( "echoerr-except" , 4 ) . T71_F ( 4 ) 1352Xpath 'n' 1353catch /^echoerr-except$/ 1354Xpath 'o' 1355catch /Vim/ 1356Xpath 'p' 1357catch /echoerr/ 1358Xpath 'q' 1359catch /.*/ 1360Xpath 'r' 1361call assert_report ( 'echoerr: ' . v: exception . ' in ' . v: throwpoint ) 1362endtry 1363 1364try 1365Xpath 's' 1366execute "echo 'execute " . T71_throw ( "execute-except" , 5 ) . T71_F ( 5 ) "'" 1367Xpath 't' 1368catch /^execute-except$/ 1369Xpath 'u' 1370catch /.*/ 1371Xpath 'v' 1372call assert_report ( 'execute: ' . v: exception . ' in ' . v: throwpoint ) 1373endtry 1374 1375call assert_equal ( 'T1T2T3T4T5' , g: T71_taken ) 1376Xpath 'w' 1377catch /^0$/ " default return value 1378Xpath 'x' 1379call assert_report ( v: throwpoint ) 1380catch /.*/ 1381Xpath 'y' 1382call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 1383endtry 1384 1385call assert_equal ( 'acegikmosuw' , g: Xpath ) 1386endfunc 1387 1388 1389"------------------------------------------------------------------------------- 1390" Test 72: :throw across :let or :unlet {{{1 1391" 1392" On a :let command, an exception might be thrown during evaluation 1393" of the expression to assign. On an :let or :unlet command, the 1394" evaluation of the name of the variable to be assigned or list or 1395" deleted, respectively, may throw an exception. Any following 1396" arguments are not evaluated, then. The exception can be caught by 1397" the script. 1398"------------------------------------------------------------------------------- 1399 1400let throwcount = 0 1401 1402func T72_throw ( x ) 1403let g: throwcount = g: throwcount + 1 1404throw a: x 1405endfunc 1406 1407let T72_addpath = '' 1408 1409func T72_addpath ( p ) 1410let g: T72_addpath = g: T72_addpath . a: p 1411endfunc 1412 1413func Test_throw_let () 1414XpathINIT 1415 1416try 1417try 1418let $ VAR = 'old_value' 1419Xpath 'a' 1420let $ VAR = 'let(' . T72_throw ( 'var' ) . ')' 1421Xpath 'b' 1422catch /^var$/ 1423Xpath 'c' 1424finally 1425call assert_equal ( 'old_value' , $ VAR ) 1426endtry 1427 1428try 1429let @a= 'old_value' 1430Xpath 'd' 1431let @a= 'let(' . T72_throw ( 'reg' ) . ')' 1432Xpath 'e' 1433catch /^reg$/ 1434try 1435Xpath 'f' 1436let @A= 'let(' . T72_throw ( 'REG' ) . ')' 1437Xpath 'g' 1438catch /^REG$/ 1439Xpath 'h' 1440endtry 1441finally 1442call assert_equal ( 'old_value' , @a) 1443call assert_equal ( 'old_value' , @A) 1444endtry 1445 1446try 1447let saved_gpath = & g: path 1448let saved_lpath = & l: path 1449Xpath 'i' 1450let & path = 'let(' . T72_throw ( 'opt' ) . ')' 1451Xpath 'j' 1452catch /^opt$/ 1453try 1454Xpath 'k' 1455let & g: path = 'let(' . T72_throw ( 'gopt' ) . ')' 1456Xpath 'l' 1457catch /^gopt$/ 1458try 1459Xpath 'm' 1460let & l: path = 'let(' . T72_throw ( 'lopt' ) . ')' 1461Xpath 'n' 1462catch /^lopt$/ 1463Xpath 'o' 1464endtry 1465endtry 1466finally 1467call assert_equal ( saved_gpath , & g: path ) 1468call assert_equal ( saved_lpath , & l: path ) 1469let & g: path = saved_gpath 1470let & l: path = saved_lpath 1471endtry 1472 1473unlet ! var1 var2 var3 1474 1475try 1476Xpath 'p' 1477let var1 = 'let(' . T72_throw ( 'var1' ) . ')' 1478Xpath 'q' 1479catch /^var1$/ 1480Xpath 'r' 1481finally 1482call assert_true ( ! exists ( 'var1' )) 1483endtry 1484 1485try 1486let var2 = 'old_value' 1487Xpath 's' 1488let var2 = 'let(' . T72_throw ( 'var2' ) . ')' 1489Xpath 't' 1490catch /^var2$/ 1491Xpath 'u' 1492finally 1493call assert_equal ( 'old_value' , var2 ) 1494endtry 1495 1496try 1497Xpath 'v' 1498let var { T72_throw ( 'var3' )} = 4711 1499Xpath 'w' 1500catch /^var3$/ 1501Xpath 'x' 1502endtry 1503 1504try 1505call T72_addpath ( 'T1' ) 1506let var { T72_throw ( 'var4' )} var { T72_addpath ( 'T2' )} |call T72_addpath ( 'T3' ) 1507call T72_addpath ( 'T4' ) 1508catch /^var4$/ 1509call T72_addpath ( 'T5' ) 1510endtry 1511 1512try 1513call T72_addpath ( 'T6' ) 1514unlet var { T72_throw ( 'var5' )} var { T72_addpath ( 'T7' )} 1515 \ |call T72_addpath ( 'T8' ) 1516call T72_addpath ( 'T9' ) 1517catch /^var5$/ 1518call T72_addpath ( 'T10' ) 1519endtry 1520 1521call assert_equal ( 'T1T5T6T10' , g: T72_addpath ) 1522call assert_equal ( 11 , g: throwcount ) 1523catch /.*/ 1524call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 1525endtry 1526 1527call assert_equal ( 'acdfhikmoprsuvx' , g: Xpath ) 1528endfunc 1529 1530 1531"------------------------------------------------------------------------------- 1532" Test 73: :throw across :function, :delfunction {{{1 1533" 1534" The :function and :delfunction commands may cause an expression 1535" specified in braces to be evaluated. During evaluation, an 1536" exception might be thrown. The exception can be caught by the 1537" script. 1538"------------------------------------------------------------------------------- 1539 1540let T73_taken = '' 1541 1542func T73_throw ( x , n ) 1543let g: T73_taken = g: T73_taken . 'T' . a: n 1544throw a: x 1545endfunc 1546 1547func T73_expr ( x , n ) 1548let g: T73_taken = g: T73_taken . 'E' . a: n 1549if a: n % 2 == 0 1550call T73_throw ( a: x , a: n ) 1551endif 1552return 2 - a: n % 2 1553endfunc 1554 1555func Test_throw_func () 1556XpathINIT 1557 1558try 1559try 1560" Define function. 1561Xpath 'a' 1562function ! F0 () 1563endfunction 1564Xpath 'b' 1565function ! F { T73_expr ( 'function-def-ok' , 1 )}() 1566endfunction 1567Xpath 'c' 1568function ! F { T73_expr ( 'function-def' , 2 )}() 1569endfunction 1570Xpath 'd' 1571catch /^function-def-ok$/ 1572Xpath 'e' 1573catch /^function-def$/ 1574Xpath 'f' 1575catch /.*/ 1576call assert_report ( 'def: ' . v: exception . ' in ' . v: throwpoint ) 1577endtry 1578 1579try 1580" List function. 1581Xpath 'g' 1582function F0 1583Xpath 'h' 1584function F { T73_expr ( 'function-lst-ok' , 3 )} 1585Xpath 'i' 1586function F { T73_expr ( 'function-lst' , 4 )} 1587Xpath 'j' 1588catch /^function-lst-ok$/ 1589Xpath 'k' 1590catch /^function-lst$/ 1591Xpath 'l' 1592catch /.*/ 1593call assert_report ( 'lst: ' . v: exception . ' in ' . v: throwpoint ) 1594endtry 1595 1596try 1597" Delete function 1598Xpath 'm' 1599delfunction F0 1600Xpath 'n' 1601delfunction F{T73_expr('function-del-ok', 5)} 1602Xpath 'o' 1603delfunction F{T73_expr('function-del', 6)} 1604Xpath 'p' 1605catch /^function-del-ok$/ 1606Xpath 'q' 1607catch /^function-del$/ 1608Xpath 'r' 1609catch /.*/ 1610call assert_report ( 'del: ' . v: exception . ' in ' . v: throwpoint ) 1611endtry 1612call assert_equal ( 'E1E2T2E3E4T4E5E6T6' , g: T73_taken ) 1613catch /.*/ 1614call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 1615endtry 1616 1617call assert_equal ( 'abcfghilmnor' , g: Xpath ) 1618endfunc 1619 1620 1621"------------------------------------------------------------------------------- 1622" Test 74: :throw across builtin functions and commands {{{1 1623" 1624" Some functions like exists(), searchpair() take expression 1625" arguments, other functions or commands like substitute() or 1626" :substitute cause an expression (specified in the regular 1627" expression) to be evaluated. During evaluation an exception 1628" might be thrown. The exception can be caught by the script. 1629"------------------------------------------------------------------------------- 1630 1631let T74_taken = "" 1632 1633func T74_throw ( x , n ) 1634let g: T74_taken = g: T74_taken . "T" . a: n 1635throw a: x 1636endfunc 1637 1638func T74_expr ( x , n ) 1639let g: T74_taken = g: T74_taken . "E" . a: n 1640call T74_throw ( a: x . a: n , a: n ) 1641return "EXPR" 1642endfunc 1643 1644func T74_skip ( x , n ) 1645let g: T74_taken = g: T74_taken . "S" . a: n . "(" . line ( "." ) 1646let theline = getline ( "." ) 1647if theline =~ "skip" 1648let g: T74_taken = g: T74_taken . "s)" 1649return 1 1650elseif theline =~ "throw" 1651let g: T74_taken = g: T74_taken . "t)" 1652call T74_throw ( a: x . a: n , a: n ) 1653else 1654let g: T74_taken = g: T74_taken . ")" 1655return 0 1656endif 1657endfunc 1658 1659func T74_subst ( x , n ) 1660let g: T74_taken = g: T74_taken . "U" . a: n . "(" . line ( "." ) 1661let theline = getline ( "." ) 1662if theline =~ "not" " T74_subst() should not be called for this line 1663let g: T74_taken = g: T74_taken . "n)" 1664call T74_throw ( a: x . a: n , a: n ) 1665elseif theline =~ "throw" 1666let g: T74_taken = g: T74_taken . "t)" 1667call T74_throw ( a: x . a: n , a: n ) 1668else 1669let g: T74_taken = g: T74_taken . ")" 1670return "replaced" 1671endif 1672endfunc 1673 1674func Test_throw_builtin_func () 1675XpathINIT 1676 1677try 1678try 1679Xpath 'a' 1680let result = exists ( '*{T74_expr("exists", 1)}' ) 1681Xpath 'b' 1682catch /^exists1$/ 1683Xpath 'c' 1684try 1685let result = exists ( '{T74_expr("exists", 2)}' ) 1686Xpath 'd' 1687catch /^exists2$/ 1688Xpath 'e' 1689catch /.*/ 1690call assert_report ( 'exists2: ' . v: exception . ' in ' . v: throwpoint ) 1691endtry 1692catch /.*/ 1693call assert_report ( 'exists1: ' . v: exception . ' in ' . v: throwpoint ) 1694endtry 1695 1696try 1697let file = tempname () 1698exec "edit" file 1699call append ( 0 , [ 1700 \'begin' , 1701 \'xx' , 1702 \'middle 3' , 1703 \'xx' , 1704 \'middle 5 skip' , 1705 \'xx' , 1706 \'middle 7 throw' , 1707 \'xx' , 1708 \'end' ]) 1709normal ! gg 1710Xpath 'f' 1711let result = searchpair ( "begin" , "middle" , "end" , '' , 1712 \'T74_skip("searchpair", 3)' ) 1713Xpath 'g' 1714let result = searchpair ( "begin" , "middle" , "end" , '' , 1715 \'T74_skip("searchpair", 4)' ) 1716Xpath 'h' 1717let result = searchpair ( "begin" , "middle" , "end" , '' , 1718 \'T74_skip("searchpair", 5)' ) 1719Xpath 'i' 1720catch /^searchpair[35]$/ 1721Xpath 'j' 1722catch /^searchpair4$/ 1723Xpath 'k' 1724catch /.*/ 1725call assert_report ( 'searchpair: ' . v: exception . ' in ' . v: throwpoint ) 1726finally 1727bwipeout ! 1728call delete ( file ) 1729endtry 1730 1731try 1732let file = tempname () 1733exec "edit" file 1734call append ( 0 , [ 1735 \'subst 1' , 1736 \'subst 2' , 1737 \'not' , 1738 \'subst 4' , 1739 \'subst throw' , 1740 \'subst 6' ]) 1741normal ! gg 1742Xpath 'l' 17431 , 2 substitute /subst/\=T74_subst( "substitute", 6)/ 1744try 1745Xpath 'm' 1746try 1747let v: errmsg = "" 17483 substitute /subst/\=T74_subst( "substitute", 7)/ 1749finally 1750if v: errmsg != "" 1751" If exceptions are not thrown on errors, fake the error 1752" exception in order to get the same execution path. 1753throw "faked Vim(substitute)" 1754endif 1755endtry 1756catch /Vim(substitute)/ " Pattern not found ('e' flag missing) 1757Xpath 'n' 17583 substitute /subst/\=T74_subst( "substitute", 8)/e 1759Xpath 'o' 1760endtry 1761Xpath 'p' 17624 , 6 substitute /subst/\=T74_subst( "substitute", 9)/ 1763Xpath 'q' 1764catch /^substitute[678]/ 1765Xpath 'r' 1766catch /^substitute9/ 1767Xpath 's' 1768finally 1769bwipeout ! 1770call delete ( file ) 1771endtry 1772 1773try 1774Xpath 't' 1775let var = substitute ( "sub" , "sub" , '\=T74_throw("substitute()y", 10)' , '' ) 1776Xpath 'u' 1777catch /substitute()y/ 1778Xpath 'v' 1779catch /.*/ 1780call assert_report ( 'substitute()y: ' . v: exception . ' in ' 1781 \. v: throwpoint ) 1782endtry 1783 1784try 1785Xpath 'w' 1786let var = substitute ( "not" , "sub" , '\=T74_throw("substitute()n", 11)' , '' ) 1787Xpath 'x' 1788catch /substitute()n/ 1789Xpath 'y' 1790catch /.*/ 1791call assert_report ( 'substitute()n: ' . v: exception . ' in ' 1792 \. v: throwpoint ) 1793endtry 1794 1795call assert_equal ( 'E1T1E2T2S3(3)S4(5s)S4(7t)T4U6(1)U6(2)U9(4)U9(5t)T9T10' , 1796 \g: T74_taken ) 1797 1798catch /.*/ 1799call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 1800endtry 1801 1802call assert_equal ( 'acefgklmnopstvwx' , g: Xpath ) 1803endfunc 1804 1805 1806"------------------------------------------------------------------------------- 1807" Test 75: Errors in builtin functions. {{{1 1808" 1809" On an error in a builtin function called inside a :try/:endtry 1810" region, the evaluation of the expression calling that function and 1811" the command containing that expression are abandoned. The error can 1812" be caught as an exception. 1813" 1814" A simple :call of the builtin function is a trivial case. If the 1815" builtin function is called in the argument list of another function, 1816" no further arguments are evaluated, and the other function is not 1817" executed. If the builtin function is called from the argument of 1818" a :return command, the :return command is not executed. If the 1819" builtin function is called from the argument of a :throw command, 1820" the :throw command is not executed. The evaluation of the 1821" expression calling the builtin function is abandoned. 1822"------------------------------------------------------------------------------- 1823 1824func T75_F1 ( arg1 ) 1825Xpath 'a' 1826endfunc 1827 1828func T75_F2 ( arg1 , arg2 ) 1829Xpath 'b' 1830endfunc 1831 1832func T75_G () 1833Xpath 'c' 1834endfunc 1835 1836func T75_H () 1837Xpath 'd' 1838endfunc 1839 1840func T75_R () 1841while 1 1842try 1843let caught = 0 1844let v: errmsg = "" 1845Xpath 'e' 1846return append ( 1 , "s" ) 1847catch /E21/ 1848let caught = 1 1849catch /.*/ 1850Xpath 'f' 1851finally 1852Xpath 'g' 1853if caught || $ VIMNOERRTHROW && v: errmsg =~ 'E21:' 1854Xpath 'h' 1855endif 1856break " discard error for $VIMNOERRTHROW 1857endtry 1858endwhile 1859Xpath 'i' 1860endfunc 1861 1862func Test_builtin_func_error () 1863XpathINIT 1864 1865try 1866set noma " let append() fail with "E21" 1867 1868while 1 1869try 1870let caught = 0 1871let v: errmsg = "" 1872Xpath 'j' 1873call append ( 1 , "s" ) 1874catch /E21/ 1875let caught = 1 1876catch /.*/ 1877Xpath 'k' 1878finally 1879Xpath 'l' 1880if caught || $ VIMNOERRTHROW && v: errmsg =~ 'E21:' 1881Xpath 'm' 1882endif 1883break " discard error for $VIMNOERRTHROW 1884endtry 1885endwhile 1886 1887while 1 1888try 1889let caught = 0 1890let v: errmsg = "" 1891Xpath 'n' 1892call T75_F1 ( 'x' . append ( 1 , "s" )) 1893catch /E21/ 1894let caught = 1 1895catch /.*/ 1896Xpath 'o' 1897finally 1898Xpath 'p' 1899if caught || $ VIMNOERRTHROW && v: errmsg =~ 'E21:' 1900Xpath 'q' 1901endif 1902break " discard error for $VIMNOERRTHROW 1903endtry 1904endwhile 1905 1906while 1 1907try 1908let caught = 0 1909let v: errmsg = "" 1910Xpath 'r' 1911call T75_F2 ( 'x' . append ( 1 , "s" ), T75_G ()) 1912catch /E21/ 1913let caught = 1 1914catch /.*/ 1915Xpath 's' 1916finally 1917Xpath 't' 1918if caught || $ VIMNOERRTHROW && v: errmsg =~ 'E21:' 1919Xpath 'u' 1920endif 1921break " discard error for $VIMNOERRTHROW 1922endtry 1923endwhile 1924 1925call T75_R () 1926 1927while 1 1928try 1929let caught = 0 1930let v: errmsg = "" 1931Xpath 'v' 1932throw "T" . append ( 1 , "s" ) 1933catch /E21/ 1934let caught = 1 1935catch /^T.*/ 1936Xpath 'w' 1937catch /.*/ 1938Xpath 'x' 1939finally 1940Xpath 'y' 1941if caught || $ VIMNOERRTHROW && v: errmsg =~ 'E21:' 1942Xpath 'z' 1943endif 1944break " discard error for $VIMNOERRTHROW 1945endtry 1946endwhile 1947 1948while 1 1949try 1950let caught = 0 1951let v: errmsg = "" 1952Xpath 'A' 1953let x = "a" 1954let x = x . "b" . append ( 1 , "s" ) . T75_H () 1955catch /E21/ 1956let caught = 1 1957catch /.*/ 1958Xpath 'B' 1959finally 1960Xpath 'C' 1961if caught || $ VIMNOERRTHROW && v: errmsg =~ 'E21:' 1962Xpath 'D' 1963endif 1964call assert_equal ( 'a' , x ) 1965break " discard error for $VIMNOERRTHROW 1966endtry 1967endwhile 1968catch /.*/ 1969call assert_report ( 'Caught ' . v: exception . ' in ' . v: throwpoint ) 1970finally 1971set ma & 1972endtry 1973 1974call assert_equal ( 'jlmnpqrtueghivyzACD' , g: Xpath ) 1975endfunc 1976 1977func Test_reload_in_try_catch () 1978call writefile ([ 'x' ], 'Xreload' ) 1979set autoread 1980edit Xreload 1981tabnew 1982call writefile ([ 'xx' ], 'Xreload' ) 1983augroup ReLoad 1984au FileReadPost Xreload let x = doesnotexist 1985au BufReadPost Xreload let x = doesnotexist 1986augroup END 1987try 1988edit Xreload 1989catch 1990endtry 1991tabnew 1992 1993tabclose 1994tabclose 1995autocmd ! ReLoad 1996set noautoread 1997bwipe ! Xreload 1998call delete ( 'Xreload' ) 1999endfunc 2000 2001" Test for errors with :catch, :throw, :finally {{{1 2002func Test_try_catch_errors () 2003call assert_fails ( 'throw |' , 'E471:' ) 2004call assert_fails ( "throw \n " , 'E471:' ) 2005call assert_fails ( 'catch abc' , 'E654:' ) 2006call assert_fails ( 'try | let i = 1| finally | catch | endtry' , 'E604:' ) 2007call assert_fails ( 'finally' , 'E606:' ) 2008call assert_fails ( 'try | finally | finally | endtry' , 'E607:' ) 2009call assert_fails ( 'try | for i in range(5) | endif | endtry' , 'E580:' ) 2010call assert_fails ( 'try | while v:true | endtry' , 'E170:' ) 2011call assert_fails ( 'try | if v:true | endtry' , 'E171:' ) 2012 2013" this was using a negative index in cstack[] 2014let lines =<< trim END 2015try 2016for 2017if 2018endwhile 2019if 2020finally 2021END 2022call CheckScriptFailure ( lines , 'E690:' ) 2023 2024let lines =<< trim END 2025try 2026for 2027if 2028endwhile 2029if 2030endtry 2031END 2032call CheckScriptFailure ( lines , 'E690:' ) 2033endfunc 2034 2035" Test for verbose messages with :try :catch, and :finally {{{1 2036func Test_try_catch_verbose () 2037" This test works only when the language is English 2038CheckEnglish 2039 2040set verbose = 14 2041 2042" Test for verbose messages displayed when an exception is caught 2043redir => msg 2044try 2045echo i 2046catch /E121:/ 2047finally 2048endtry 2049redir END 2050let expected = [ 2051 \'Exception thrown: Vim(echo):E121: Undefined variable: i' , '' , 2052 \'Exception caught: Vim(echo):E121: Undefined variable: i' , '' , 2053 \'Exception finished: Vim(echo):E121: Undefined variable: i' ] 2054call assert_equal ( expected , split ( msg , "\n" )) 2055 2056" Test for verbose messages displayed when an exception is discarded 2057redir => msg 2058try 2059try 2060throw 'abc' 2061finally 2062throw 'xyz' 2063endtry 2064catch 2065endtry 2066redir END 2067let expected = [ 2068 \'Exception thrown: abc' , '' , 2069 \'Exception made pending: abc' , '' , 2070 \'Exception thrown: xyz' , '' , 2071 \'Exception discarded: abc' , '' , 2072 \'Exception caught: xyz' , '' , 2073 \'Exception finished: xyz' ] 2074call assert_equal ( expected , split ( msg , "\n" )) 2075 2076" Test for messages displayed when :throw is resumed after :finally 2077redir => msg 2078try 2079try 2080throw 'abc' 2081finally 2082endtry 2083catch 2084endtry 2085redir END 2086let expected = [ 2087 \'Exception thrown: abc' , '' , 2088 \'Exception made pending: abc' , '' , 2089 \'Exception resumed: abc' , '' , 2090 \'Exception caught: abc' , '' , 2091 \'Exception finished: abc' ] 2092call assert_equal ( expected , split ( msg , "\n" )) 2093 2094" Test for messages displayed when :break is resumed after :finally 2095redir => msg 2096for i in range ( 1 ) 2097try 2098break 2099finally 2100endtry 2101endfor 2102redir END 2103let expected = [ ':break made pending' , '' , ':break resumed' ] 2104call assert_equal ( expected , split ( msg , "\n" )) 2105 2106" Test for messages displayed when :continue is resumed after :finally 2107redir => msg 2108for i in range ( 1 ) 2109try 2110continue 2111finally 2112endtry 2113endfor 2114redir END 2115let expected = [ ':continue made pending' , '' , ':continue resumed' ] 2116call assert_equal ( expected , split ( msg , "\n" )) 2117 2118" Test for messages displayed when :return is resumed after :finally 2119func Xtest () 2120try 2121return 'vim' 2122finally 2123endtry 2124endfunc 2125redir => msg 2126call Xtest () 2127redir END 2128let expected = [ 2129 \'calling Xtest()' , '' , 2130 \':return vim made pending' , '' , 2131 \':return vim resumed' , '' , 2132 \'Xtest returning ''vim''' , '' , 2133 \'continuing in Test_try_catch_verbose' ] 2134call assert_equal ( expected , split ( msg , "\n" )) 2135delfunc Xtest 2136 2137" Test for messages displayed when :finish is resumed after :finally 2138call writefile ([ 'try' , 'finish' , 'finally' , 'endtry' ], 'Xscript' ) 2139redir => msg 2140source Xscript 2141redir END 2142let expected = [ 2143 \':finish made pending' , '' , 2144 \':finish resumed' , '' , 2145 \'finished sourcing Xscript' , 2146 \'continuing in Test_try_catch_verbose' ] 2147call assert_equal ( expected , split ( msg , "\n" )[ 1 :]) 2148call delete ( 'Xscript' ) 2149 2150" Test for messages displayed when a pending :continue is discarded by an 2151" exception in a finally handler 2152redir => msg 2153try 2154for i in range ( 1 ) 2155try 2156continue 2157finally 2158throw 'abc' 2159endtry 2160endfor 2161catch 2162endtry 2163redir END 2164let expected = [ 2165 \':continue made pending' , '' , 2166 \'Exception thrown: abc' , '' , 2167 \':continue discarded' , '' , 2168 \'Exception caught: abc' , '' , 2169 \'Exception finished: abc' ] 2170call assert_equal ( expected , split ( msg , "\n" )) 2171 2172set verbose & 2173endfunc 2174 2175" Test for throwing an exception from a BufEnter autocmd {{{1 2176func Test_BufEnter_exception () 2177augroup bufenter_exception 2178au ! 2179autocmd BufEnter Xfile1 throw 'abc' 2180augroup END 2181 2182let caught_abc = 0 2183try 2184sp Xfile1 2185catch /^abc/ 2186let caught_abc = 1 2187endtry 2188call assert_equal ( 1 , caught_abc ) 2189call assert_equal ( 1 , winnr ( '$' )) 2190 2191augroup bufenter_exception 2192au ! 2193augroup END 2194augroup ! bufenter_exception 2195 %bwipe ! 2196 2197" Test for recursively throwing exceptions in autocmds 2198augroup bufenter_exception 2199au ! 2200autocmd BufEnter Xfile1 throw 'bufenter' 2201autocmd BufLeave Xfile1 throw 'bufleave' 2202augroup END 2203 2204let ex_count = 0 2205try 2206try 2207sp Xfile1 2208catch /^bufenter/ 2209let ex_count += 1 2210endtry 2211catch /^bufleave/ 2212let ex_count += 10 2213endtry 2214call assert_equal ( 10 , ex_count ) 2215call assert_equal ( 2 , winnr ( '$' )) 2216 2217augroup bufenter_exception 2218au ! 2219augroup END 2220augroup ! bufenter_exception 2221 %bwipe ! 2222endfunc 2223 2224" Test for using try/catch when lines are joined by "|" or "\n" {{{1 2225func Test_try_catch_nextcmd () 2226func Throw () 2227throw "Failure" 2228endfunc 2229 2230let lines =<< trim END 2231try 2232let s:x = Throw() 2233catch 2234let g:caught = 1 2235endtry 2236END 2237 2238let g: caught = 0 2239call execute ( lines ) 2240call assert_equal ( 1 , g: caught ) 2241 2242let g: caught = 0 2243call execute ( join ( lines , '|' )) 2244call assert_equal ( 1 , g: caught ) 2245 2246let g: caught = 0 2247call execute ( join ( lines , "\n" )) 2248call assert_equal ( 1 , g: caught ) 2249 2250unlet g: caught 2251delfunc Throw 2252endfunc 2253 2254" Test for using try/catch in a user command with a failing expression {{{1 2255func Test_user_command_try_catch () 2256let lines =<< trim END 2257function s:throw() abort 2258throw 'error' 2259endfunction 2260 2261command! Execute 2262\ try 2263\ | let s:x = s:throw() 2264\ | catch 2265\ | let g:caught = 'caught' 2266\ | endtry 2267 2268let g:caught = 'no' 2269Execute 2270call assert_equal('caught', g:caught) 2271END 2272call writefile ( lines , 'XtestTryCatch' ) 2273source XtestTryCatch 2274 2275call delete ( 'XtestTryCatch' ) 2276unlet g: caught 2277endfunc 2278 2279" Test for using throw in a called function with following error {{{1 2280func Test_user_command_throw_in_function_call () 2281let lines =<< trim END 2282function s:get_dict() abort 2283throw 'my_error' 2284endfunction 2285 2286try 2287call s:get_dict().foo() 2288catch /my_error/ 2289let caught = 'yes' 2290catch 2291let caught = v:exception 2292endtry 2293call assert_equal('yes', caught) 2294END 2295call writefile ( lines , 'XtestThrow' ) 2296source XtestThrow 2297 2298call delete ( 'XtestThrow' ) 2299unlet g: caught 2300endfunc 2301 2302" Test that after reporting an uncaught exception there is no error for a 2303" missing :endif 2304func Test_after_exception_no_endif_error () 2305function Throw () 2306throw "Failure" 2307endfunction 2308 2309function Foo () 2310if 1 2311call Throw () 2312endif 2313endfunction 2314call assert_fails ( 'call Foo()' , [ 'E605:' , 'E605:' ]) 2315delfunc Throw 2316delfunc Foo 2317endfunc 2318 2319" Test for using throw in a called function with following endtry {{{1 2320func Test_user_command_function_call_with_endtry () 2321let lines =<< trim END 2322funct s:throw(msg) abort 2323throw a:msg 2324endfunc 2325func s:main() abort 2326try 2327try 2328throw 'err1' 2329catch 2330call s:throw('err2') | endtry 2331catch 2332let s:caught = 'yes' 2333endtry 2334endfunc 2335 2336call s:main() 2337call assert_equal('yes', s:caught) 2338END 2339call writefile ( lines , 'XtestThrow' ) 2340source XtestThrow 2341 2342call delete ( 'XtestThrow' ) 2343endfunc 2344 2345func ThisWillFail () 2346 2347endfunc 2348 2349" This was crashing prior to the fix in 8.2.3478. 2350func Test_error_in_catch_and_finally () 2351let lines =<< trim END 2352try 2353echo x 2354catch 2355for l in [] 2356finally 2357END 2358call writefile ( lines , 'XtestCatchAndFinally' ) 2359try 2360source XtestCatchAndFinally 2361catch /E600:/ 2362endtry 2363 2364call delete ( 'XtestCatchAndFinally' ) 2365endfunc 2366 2367" This was causing an illegal memory access 2368func Test_leave_block_in_endtry_not_called () 2369let lines =<< trim END 2370" vim9script 2371" try # 2372try " 2373for x in [] 2374if 2375endwhile 2376if 2377endtry 2378END 2379call writefile ( lines , 'XtestEndtry' ) 2380try 2381source XtestEndtry 2382catch /E171:/ 2383endtry 2384 2385call delete ( 'XtestEndtry' ) 2386endfunc 2387 2388" Modeline {{{1 2389" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker