test_vimscript.vim 189 KB

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