1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903 |
- {
- Examples:
- ./testpassrc --suite=TTestStatementParser.TestCallQualified2
- }
- unit tcstatements;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, fpcunit, pastree, pscanner, pparser,
- tcbaseparser, testregistry;
- Type
- { TTestStatementParser }
- TTestStatementParser = Class(TTestParser)
- private
- FStatement: TPasImplBlock;
- FVariables : TStrings;
- procedure DoTestCallOtherFormat;
- procedure TestCallFormat(FN: String; AddPrecision: Boolean; AddSecondParam: boolean = false);
- Protected
- Procedure SetUp; override;
- Procedure TearDown; override;
- procedure AddStatements(ASource : Array of string);
- Procedure DeclareVar(Const AVarType : String; Const AVarName : String = 'A');
- function TestStatement(ASource : string) : TPasImplElement;
- function TestStatement(ASource : Array of string) : TPasImplElement;
- Procedure ExpectParserError(Const Msg : string);
- Procedure ExpectParserError(Const Msg : string; ASource : Array of string);
- Function AssertStatement(Msg : String; AClass : TClass;AIndex : Integer = 0) : TPasImplBlock;
- Property Statement: TPasImplBlock Read FStatement;
- Published
- Procedure TestEmpty;
- Procedure TestEmptyStatement;
- Procedure TestEmptyStatements;
- Procedure TestBlock;
- Procedure TestBlockComment;
- Procedure TestBlock2Comments;
- Procedure TestAssignment;
- Procedure TestAssignmentAdd;
- Procedure TestAssignmentMinus;
- Procedure TestAssignmentMul;
- Procedure TestAssignmentDivision;
- Procedure TestAssignmentMissingSemicolonError;
- Procedure TestCall;
- Procedure TestCallComment;
- Procedure TestCallQualified;
- Procedure TestCallQualified2;
- Procedure TestCallNoArgs;
- Procedure TestCallOneArg;
- procedure TestCallWriteFormat1;
- procedure TestCallWriteFormat2;
- procedure TestCallWriteFormat3;
- procedure TestCallWriteFormat4;
- procedure TestCallWritelnFormat1;
- procedure TestCallWritelnFormat2;
- procedure TestCallStrFormat1;
- procedure TestCallStrFormat2;
- procedure TestCallOtherFormat;
- Procedure TestIf;
- Procedure TestIfBlock;
- Procedure TestIfAssignment;
- Procedure TestIfElse;
- Procedure TestIfElseBlock;
- Procedure TestIfSemiColonElseError;
- procedure TestIfforElseBlock;
- procedure TestIfRaiseElseBlock;
- procedure TestIfWithBlock;
- Procedure TestNestedIf;
- Procedure TestNestedIfElse;
- Procedure TestWhile;
- Procedure TestWhileBlock;
- Procedure TestWhileNested;
- Procedure TestRepeat;
- Procedure TestRepeatBlock;
- procedure TestRepeatBlockNosemicolon;
- Procedure TestRepeatNested;
- Procedure TestFor;
- Procedure TestForIn;
- Procedure TestForExpr;
- Procedure TestForBlock;
- procedure TestDowntoBlock;
- Procedure TestForNested;
- Procedure TestWith;
- Procedure TestWithMultiple;
- Procedure TestCaseEmpty;
- Procedure TestCaseOneInteger;
- Procedure TestCaseTwoIntegers;
- Procedure TestCaseRange;
- Procedure TestCaseRangeSeparate;
- Procedure TestCase2Cases;
- Procedure TestCaseBlock;
- Procedure TestCaseElseBlockEmpty;
- procedure TestCaseOtherwiseBlockEmpty;
- Procedure TestCaseElseBlockAssignment;
- Procedure TestCaseElseBlock2Assignments;
- Procedure TestCaseIfCaseElse;
- Procedure TestCaseIfElse;
- Procedure TestCaseElseNoSemicolon;
- Procedure TestCaseIfElseNoSemicolon;
- procedure TestCaseIfOtherwiseNoSemicolon;
- Procedure TestRaise;
- Procedure TestRaiseEmpty;
- Procedure TestRaiseAt;
- Procedure TestTryFinally;
- Procedure TestTryFinallyEmpty;
- Procedure TestTryFinallyNested;
- procedure TestTryExcept;
- procedure TestTryExceptNested;
- procedure TestTryExceptEmpty;
- Procedure TestTryExceptOn;
- Procedure TestTryExceptOn2;
- Procedure TestTryExceptOnElse;
- Procedure TestTryExceptOnIfElse;
- Procedure TestTryExceptOnElseNoSemicolo;
- procedure TestTryExceptRaise;
- Procedure TestAsm;
- Procedure TestAsmBlock;
- Procedure TestAsmBlockWithEndLabel;
- Procedure TestAsmBlockInIfThen;
- Procedure TestGotoInIfThen;
- procedure TestAssignToAddress;
- procedure TestFinalizationNoSemicolon;
- procedure TestMacroComment;
- Procedure TestPlatformIdentifier;
- Procedure TestPlatformIdentifier2;
- Procedure TestArgumentNameOn;
- end;
- implementation
- { TTestStatementParser }
- procedure TTestStatementParser.SetUp;
- begin
- inherited SetUp;
- FVariables:=TStringList.Create;
- end;
- procedure TTestStatementParser.TearDown;
- begin
- FreeAndNil(FVariables);
- inherited TearDown;
- end;
- procedure TTestStatementParser.AddStatements(ASource: array of string);
- Var
- I :Integer;
- begin
- StartProgram(ExtractFileUnitName(MainFilename));
- if FVariables.Count>0 then
- begin
- Add('Var');
- For I:=0 to FVariables.Count-1 do
- Add(' '+Fvariables[I]);
- end;
- Add('begin');
- For I:=Low(ASource) to High(ASource) do
- Add(' '+ASource[i]);
- end;
- procedure TTestStatementParser.DeclareVar(const AVarType: String;
- const AVarName: String);
- begin
- FVariables.Add(AVarName+' : '+AVarType+';');
- end;
- function TTestStatementParser.TestStatement(ASource: string): TPasImplElement;
- begin
- Result:=TestStatement([ASource]);
- end;
- function TTestStatementParser.TestStatement(ASource: array of string
- ): TPasImplElement;
- begin
- Result:=Nil;
- FStatement:=Nil;
- AddStatements(ASource);
- ParseModule;
- AssertEquals('Have program',TPasProgram,Module.ClassType);
- AssertNotNull('Have program section',PasProgram.ProgramSection);
- AssertNotNull('Have initialization section',PasProgram.InitializationSection);
- if (PasProgram.InitializationSection.Elements.Count>0) then
- if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
- FStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
- Result:=FStatement;
- end;
- procedure TTestStatementParser.ExpectParserError(const Msg: string);
- begin
- AssertException(Msg,EParserError,@ParseModule);
- end;
- procedure TTestStatementParser.ExpectParserError(const Msg: string;
- ASource: array of string);
- begin
- AddStatements(ASource);
- ExpectParserError(Msg);
- end;
- function TTestStatementParser.AssertStatement(Msg: String; AClass: TClass;
- AIndex: Integer): TPasImplBlock;
- begin
- if not (AIndex<PasProgram.InitializationSection.Elements.Count) then
- Fail(Msg+': No such statement : '+intTostr(AIndex));
- AssertNotNull(Msg+' Have statement',PasProgram.InitializationSection.Elements[AIndex]);
- AssertEquals(Msg+' statement class',AClass,TObject(PasProgram.InitializationSection.Elements[AIndex]).ClassType);
- Result:=TObject(PasProgram.InitializationSection.Elements[AIndex]) as TPasImplBlock;
- end;
- procedure TTestStatementParser.TestEmpty;
- begin
- //TestStatement(';');
- TestStatement('');
- AssertEquals('No statements',0,PasProgram.InitializationSection.Elements.Count);
- end;
- procedure TTestStatementParser.TestEmptyStatement;
- begin
- TestStatement(';');
- AssertEquals('0 statement',0,PasProgram.InitializationSection.Elements.Count);
- end;
- procedure TTestStatementParser.TestEmptyStatements;
- begin
- TestStatement(';;');
- AssertEquals('0 statement',0,PasProgram.InitializationSection.Elements.Count);
- end;
- procedure TTestStatementParser.TestBlock;
- Var
- B : TPasImplBeginBlock;
- begin
- TestStatement(['begin','end']);
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertNotNull('Statement assigned',PasProgram.InitializationSection.Elements[0]);
- AssertEquals('Block statement',TPasImplBeginBlock,Statement.ClassType);
- B:= Statement as TPasImplBeginBlock;
- AssertEquals('Empty block',0,B.Elements.Count);
- end;
- procedure TTestStatementParser.TestBlockComment;
- Var
- B : TPasImplBeginBlock;
- begin
- Engine.NeedComments:=True;
- TestStatement(['{ This is a comment }','begin','end']);
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertNotNull('Statement assigned',PasProgram.InitializationSection.Elements[0]);
- AssertEquals('Block statement',TPasImplBeginBlock,Statement.ClassType);
- B:= Statement as TPasImplBeginBlock;
- AssertEquals('Empty block',0,B.Elements.Count);
- AssertEquals('No DocComment','',B.DocComment);
- end;
- procedure TTestStatementParser.TestBlock2Comments;
- Var
- B : TPasImplBeginBlock;
- begin
- Engine.NeedComments:=True;
- TestStatement(['{ This is a comment }','// Another comment','begin','end']);
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertNotNull('Statement assigned',PasProgram.InitializationSection.Elements[0]);
- AssertEquals('Block statement',TPasImplBeginBlock,Statement.ClassType);
- B:= Statement as TPasImplBeginBlock;
- AssertEquals('Empty block',0,B.Elements.Count);
- AssertEquals('No DocComment','',B.DocComment);
- end;
- procedure TTestStatementParser.TestAssignment;
- Var
- A : TPasImplAssign;
- begin
- DeclareVar('integer');
- TestStatement(['a:=1;']);
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Assignment statement',TPasImplAssign,Statement.ClassType);
- A:=Statement as TPasImplAssign;
- AssertEquals('Normal assignment',akDefault,A.Kind);
- AssertExpression('Right side is constant',A.Right,pekNumber,'1');
- AssertExpression('Left side is variable',A.Left,pekIdent,'a');
- end;
- procedure TTestStatementParser.TestAssignmentAdd;
- Var
- A : TPasImplAssign;
- begin
- Parser.Scanner.Options:=[po_cassignments];
- DeclareVar('integer');
- TestStatement(['a+=1;']);
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Assignment statement',TPasImplAssign,Statement.ClassType);
- A:=Statement as TPasImplAssign;
- AssertEquals('Add assignment',akAdd,A.Kind);
- AssertExpression('Right side is constant',A.Right,pekNumber,'1');
- AssertExpression('Left side is variable',A.Left,pekIdent,'a');
- end;
- procedure TTestStatementParser.TestAssignmentMinus;
- Var
- A : TPasImplAssign;
- begin
- Parser.Scanner.Options:=[po_cassignments];
- DeclareVar('integer');
- TestStatement(['a-=1;']);
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Assignment statement',TPasImplAssign,Statement.ClassType);
- A:=Statement as TPasImplAssign;
- AssertEquals('Minus assignment',akMinus,A.Kind);
- AssertExpression('Right side is constant',A.Right,pekNumber,'1');
- AssertExpression('Left side is variable',A.Left,pekIdent,'a');
- end;
- procedure TTestStatementParser.TestAssignmentMul;
- Var
- A : TPasImplAssign;
- begin
- Parser.Scanner.Options:=[po_cassignments];
- DeclareVar('integer');
- TestStatement(['a*=1;']);
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Assignment statement',TPasImplAssign,Statement.ClassType);
- A:=Statement as TPasImplAssign;
- AssertEquals('Mul assignment',akMul,A.Kind);
- AssertExpression('Right side is constant',A.Right,pekNumber,'1');
- AssertExpression('Left side is variable',A.Left,pekIdent,'a');
- end;
- procedure TTestStatementParser.TestAssignmentDivision;
- Var
- A : TPasImplAssign;
- begin
- Parser.Scanner.Options:=[po_cassignments];
- DeclareVar('integer');
- TestStatement(['a/=1;']);
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Assignment statement',TPasImplAssign,Statement.ClassType);
- A:=Statement as TPasImplAssign;
- AssertEquals('Division assignment',akDivision,A.Kind);
- AssertExpression('Right side is constant',A.Right,pekNumber,'1');
- AssertExpression('Left side is variable',A.Left,pekIdent,'a');
- end;
- procedure TTestStatementParser.TestAssignmentMissingSemicolonError;
- begin
- DeclareVar('integer');
- ExpectParserError('Semicolon expected, but "a" found',['a:=1','a:=2']);
- end;
- procedure TTestStatementParser.TestCall;
- Var
- S : TPasImplSimple;
- begin
- TestStatement('Doit;');
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
- S:=Statement as TPasImplSimple;
- AssertExpression('Doit call',S.Expr,pekIdent,'Doit');
- end;
- procedure TTestStatementParser.TestCallComment;
- Var
- S : TPasImplSimple;
- begin
- Engine.NeedComments:=True;
- TestStatement(['//comment line','Doit;']);
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- S:=Statement as TPasImplSimple;
- AssertExpression('Doit call',S.Expr,pekIdent,'Doit');
- AssertEquals('No DocComment','',S.DocComment);
- end;
- procedure TTestStatementParser.TestCallQualified;
- Var
- S : TPasImplSimple;
- B : TBinaryExpr;
- begin
- TestStatement('Unita.Doit;');
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
- S:=Statement as TPasImplSimple;
- AssertExpression('Doit call',S.Expr,pekBinary,TBinaryExpr);
- B:=S.Expr as TBinaryExpr;
- TAssert.AssertSame('B.left.Parent=B',B,B.left.Parent);
- TAssert.AssertSame('B.right.Parent=B',B,B.right.Parent);
- AssertExpression('Unit name',B.Left,pekIdent,'Unita');
- AssertExpression('Doit call',B.Right,pekIdent,'Doit');
- end;
- procedure TTestStatementParser.TestCallQualified2;
- Var
- S : TPasImplSimple;
- B : TBinaryExpr;
- begin
- TestStatement('Unita.ClassB.Doit;');
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
- S:=Statement as TPasImplSimple;
- AssertExpression('Doit call',S.Expr,pekBinary,TBinaryExpr);
- B:=S.Expr as TBinaryExpr;
- AssertExpression('Doit call',B.Right,pekIdent,'Doit');
- AssertExpression('First two parts of unit name',B.left,pekBinary,TBinaryExpr);
- B:=B.left as TBinaryExpr;
- AssertExpression('Unit name part 1',B.Left,pekIdent,'Unita');
- AssertExpression('Unit name part 2',B.right,pekIdent,'ClassB');
- end;
- procedure TTestStatementParser.TestCallNoArgs;
- Var
- S : TPasImplSimple;
- P : TParamsExpr;
- begin
- TestStatement('Doit();');
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
- S:=Statement as TPasImplSimple;
- AssertExpression('Doit call',S.Expr,pekFuncParams,TParamsExpr);
- P:=S.Expr as TParamsExpr;
- AssertExpression('Correct function call name',P.Value,pekIdent,'Doit');
- AssertEquals('No params',0,Length(P.Params));
- end;
- procedure TTestStatementParser.TestCallOneArg;
- Var
- S : TPasImplSimple;
- P : TParamsExpr;
- begin
- TestStatement('Doit(1);');
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
- S:=Statement as TPasImplSimple;
- AssertExpression('Doit call',S.Expr,pekFuncParams,TParamsExpr);
- P:=S.Expr as TParamsExpr;
- AssertExpression('Correct function call name',P.Value,pekIdent,'Doit');
- AssertEquals('One param',1,Length(P.Params));
- AssertExpression('Parameter is constant',P.Params[0],pekNumber,'1');
- end;
- procedure TTestStatementParser.TestCallFormat(FN: String;
- AddPrecision: Boolean; AddSecondParam: boolean);
- var
- P : TParamsExpr;
- procedure CheckParam(Index: integer; const aParamName: string);
- begin
- AssertExpression('Parameter['+IntToStr(Index)+'] is identifier',P.Params[Index],pekIdent,aParamName);
- AssertExpression('Parameter['+IntToStr(Index)+'] has formatting constant 1' ,P.Params[Index].format1,pekNumber,'3');
- if AddPrecision then
- AssertExpression('Parameter['+IntToStr(Index)+'] has formatting constant 2',P.Params[Index].format2,pekNumber,'2');
- end;
- Var
- S : TPasImplSimple;
- N : String;
- ArgCnt: Integer;
- begin
- N:=fn+'(a:3';
- if AddPrecision then
- N:=N+':2';
- ArgCnt:=1;
- if AddSecondParam then
- begin
- ArgCnt:=2;
- N:=N+',b:3';
- if AddPrecision then
- N:=N+':2';
- end;
- N:=N+');';
- TestStatement(N);
- AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
- S:=Statement as TPasImplSimple;
- AssertExpression('Doit call',S.Expr,pekFuncParams,TParamsExpr);
- P:=S.Expr as TParamsExpr;
- AssertExpression('Correct function call name',P.Value,pekIdent,FN);
- AssertEquals(IntToStr(ArgCnt)+' param',ArgCnt,Length(P.Params));
- CheckParam(0,'a');
- if AddSecondParam then
- CheckParam(1,'b');
- end;
- procedure TTestStatementParser.TestCallWriteFormat1;
- begin
- TestCallFormat('write',False);
- end;
- procedure TTestStatementParser.TestCallWriteFormat2;
- begin
- TestCallFormat('write',True);
- end;
- procedure TTestStatementParser.TestCallWriteFormat3;
- begin
- TestCallFormat('write',false,true);
- end;
- procedure TTestStatementParser.TestCallWriteFormat4;
- begin
- TestCallFormat('write',true,true);
- end;
- procedure TTestStatementParser.TestCallWritelnFormat1;
- begin
- TestCallFormat('writeln',False);
- end;
- procedure TTestStatementParser.TestCallWritelnFormat2;
- begin
- TestCallFormat('writeln',True);
- end;
- procedure TTestStatementParser.TestCallStrFormat1;
- begin
- TestCallFormat('str',False);
- end;
- procedure TTestStatementParser.TestCallStrFormat2;
- begin
- TestCallFormat('str',True);
- end;
- procedure TTestStatementParser.DoTestCallOtherFormat;
- begin
- TestCallFormat('nono',False);
- end;
- procedure TTestStatementParser.TestCallOtherFormat;
- begin
- AssertException('Only Write(ln) and str allow format',EParserError,@DoTestCallOtherFormat);
- end;
- procedure TTestStatementParser.TestIf;
- Var
- I : TPasImplIfElse;
- begin
- DeclareVar('boolean');
- TestStatement(['if a then',';']);
- I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
- AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
- AssertNull('No else',i.ElseBranch);
- AssertNull('No if branch',I.IfBranch);
- end;
- procedure TTestStatementParser.TestIfBlock;
- Var
- I : TPasImplIfElse;
- begin
- DeclareVar('boolean');
- TestStatement(['if a then',' begin',' end']);
- I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
- AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
- AssertNull('No else',i.ElseBranch);
- AssertNotNull('if branch',I.IfBranch);
- AssertEquals('begin end block',TPasImplBeginBlock,I.ifBranch.ClassType);
- end;
- procedure TTestStatementParser.TestIfAssignment;
- Var
- I : TPasImplIfElse;
- begin
- DeclareVar('boolean');
- TestStatement(['if a then',' a:=False;']);
- I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
- AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
- AssertNull('No else',i.ElseBranch);
- AssertNotNull('if branch',I.IfBranch);
- AssertEquals('assignment statement',TPasImplAssign,I.ifBranch.ClassType);
- end;
- procedure TTestStatementParser.TestIfElse;
- Var
- I : TPasImplIfElse;
- begin
- DeclareVar('boolean');
- TestStatement(['if a then',' begin',' end','else',';']);
- I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
- AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
- AssertNull('No else',i.ElseBranch);
- AssertNotNull('if branch',I.IfBranch);
- AssertEquals('begin end block',TPasImplBeginBlock,I.ifBranch.ClassType);
- end;
- procedure TTestStatementParser.TestIfElseBlock;
- Var
- I : TPasImplIfElse;
- begin
- DeclareVar('boolean');
- TestStatement(['if a then',' begin',' end','else',' begin',' end']);
- I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
- AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
- AssertNotNull('if branch',I.IfBranch);
- AssertEquals('begin end block',TPasImplBeginBlock,I.ifBranch.ClassType);
- AssertNotNull('Else branch',i.ElseBranch);
- AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
- end;
- procedure TTestStatementParser.TestIfforElseBlock;
- Var
- I : TPasImplIfElse;
- begin
- TestStatement(['if a then','for X := 1 downto 0 do Writeln(X)','else', 'for X := 0 to 1 do Writeln(X)']);
- I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
- AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
- AssertEquals('For statement',TPasImplForLoop,I.ifBranch.ClassType);
- AssertEquals('For statement',TPasImplForLoop,I.ElseBranch.ClassType);
- end;
- procedure TTestStatementParser.TestIfRaiseElseBlock;
- Var
- I : TPasImplIfElse;
- begin
- TestStatement(['if a then','raise','else', 'for X := 0 to 1 do Writeln(X)']);
- I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
- AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
- AssertEquals('For statement',TPasImplRaise,I.ifBranch.ClassType);
- AssertEquals('For statement',TPasImplForLoop,I.ElseBranch.ClassType);
- end;
- procedure TTestStatementParser.TestIfWithBlock;
- Var
- I : TPasImplIfElse;
- begin
- TestStatement(['if a then','with b do something','else', 'for X := 0 to 1 do Writeln(X)']);
- I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
- AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
- AssertEquals('For statement',TPasImplWithDo,I.ifBranch.ClassType);
- AssertEquals('For statement',TPasImplForLoop,I.ElseBranch.ClassType);
- end;
- procedure TTestStatementParser.TestIfSemiColonElseError;
- begin
- DeclareVar('boolean');
- ExpectParserError('No semicolon before else',['if a then',' begin',' end;','else',' begin',' end']);
- end;
- procedure TTestStatementParser.TestNestedIf;
- Var
- I : TPasImplIfElse;
- begin
- DeclareVar('boolean');
- DeclareVar('boolean','b');
- TestStatement(['if a then',' if b then',' begin',' end','else',' begin',' end']);
- I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
- AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
- AssertNotNull('if branch',I.IfBranch);
- AssertNull('Else branch',i.ElseBranch);
- AssertEquals('if in if branch',TPasImplIfElse,I.ifBranch.ClassType);
- I:=I.Ifbranch as TPasImplIfElse;
- AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
- end;
- procedure TTestStatementParser.TestNestedIfElse;
- Var
- I : TPasImplIfElse;
- begin
- DeclareVar('boolean');
- TestStatement(['if a then',' if b then',' begin',' end',' else',' begin',' end','else',' begin','end']);
- I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
- AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
- AssertNotNull('if branch',I.IfBranch);
- AssertNotNull('Else branch',i.ElseBranch);
- AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
- AssertEquals('if in if branch',TPasImplIfElse,I.ifBranch.ClassType);
- I:=I.Ifbranch as TPasImplIfElse;
- AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
- end;
- procedure TTestStatementParser.TestWhile;
- Var
- W : TPasImplWhileDo;
- begin
- DeclareVar('boolean');
- TestStatement(['While a do ;']);
- W:=AssertStatement('While statement',TPasImplWhileDo) as TPasImplWhileDo;
- AssertExpression('While condition',W.ConditionExpr,pekIdent,'a');
- AssertNull('Empty body',W.Body);
- end;
- procedure TTestStatementParser.TestWhileBlock;
- Var
- W : TPasImplWhileDo;
- begin
- DeclareVar('boolean');
- TestStatement(['While a do',' begin',' end']);
- W:=AssertStatement('While statement',TPasImplWhileDo) as TPasImplWhileDo;
- AssertExpression('While condition',W.ConditionExpr,pekIdent,'a');
- AssertNotNull('Have while body',W.Body);
- AssertEquals('begin end block',TPasImplBeginBlock,W.Body.ClassType);
- AssertEquals('Empty block',0,TPasImplBeginBlock(W.Body).ELements.Count);
- end;
- procedure TTestStatementParser.TestWhileNested;
- Var
- W : TPasImplWhileDo;
- begin
- DeclareVar('boolean');
- DeclareVar('boolean','b');
- TestStatement(['While a do',' while b do',' begin',' end']);
- W:=AssertStatement('While statement',TPasImplWhileDo) as TPasImplWhileDo;
- AssertExpression('While condition',W.ConditionExpr,pekIdent,'a');
- AssertNotNull('Have while body',W.Body);
- AssertEquals('Nested while',TPasImplWhileDo,W.Body.ClassType);
- W:=W.Body as TPasImplWhileDo;
- AssertExpression('While condition',W.ConditionExpr,pekIdent,'b');
- AssertNotNull('Have nested while body',W.Body);
- AssertEquals('Nested begin end block',TPasImplBeginBlock,W.Body.ClassType);
- AssertEquals('Empty nested block',0,TPasImplBeginBlock(W.Body).ELements.Count);
- end;
- procedure TTestStatementParser.TestRepeat;
- Var
- R : TPasImplRepeatUntil;
- begin
- DeclareVar('boolean');
- TestStatement(['Repeat','Until a;']);
- R:=AssertStatement('Repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
- AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
- AssertEquals('Empty body',0,R.Elements.Count);
- end;
- procedure TTestStatementParser.TestRepeatBlock;
- Var
- R : TPasImplRepeatUntil;
- begin
- DeclareVar('boolean');
- TestStatement(['Repeat','begin','end;','Until a;']);
- R:=AssertStatement('repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
- AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
- AssertEquals('Have statement',1,R.Elements.Count);
- AssertEquals('begin end block',TPasImplBeginBlock,TObject(R.Elements[0]).ClassType);
- AssertEquals('Empty block',0,TPasImplBeginBlock(R.Elements[0]).ELements.Count);
- end;
- procedure TTestStatementParser.TestRepeatBlockNosemicolon;
- Var
- R : TPasImplRepeatUntil;
- begin
- DeclareVar('boolean');
- TestStatement(['Repeat','begin','end','Until a;']);
- R:=AssertStatement('repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
- AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
- AssertEquals('Have statement',1,R.Elements.Count);
- AssertEquals('begin end block',TPasImplBeginBlock,TObject(R.Elements[0]).ClassType);
- AssertEquals('Empty block',0,TPasImplBeginBlock(R.Elements[0]).ELements.Count);
- end;
- procedure TTestStatementParser.TestRepeatNested;
- Var
- R : TPasImplRepeatUntil;
- begin
- DeclareVar('boolean');
- DeclareVar('boolean','b');
- TestStatement(['Repeat','repeat','begin','end','until b','Until a;']);
- R:=AssertStatement('repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
- AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
- AssertEquals('Have statement',1,R.Elements.Count);
- AssertEquals('Nested repeat',TPasImplRepeatUntil,TObject(R.Elements[0]).ClassType);
- R:=TPasImplRepeatUntil(R.Elements[0]);
- AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'b');
- AssertEquals('Have statement',1,R.Elements.Count);
- AssertEquals('begin end block',TPasImplBeginBlock,TObject(R.Elements[0]).ClassType);
- AssertEquals('Empty block',0,TPasImplBeginBlock(R.Elements[0]).ELements.Count);
- end;
- procedure TTestStatementParser.TestFor;
- Var
- F : TPasImplForLoop;
- begin
- DeclareVar('integer');
- TestStatement(['For a:=1 to 10 do',';']);
- F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
- AssertExpression('Loop variable name',F.VariableName,pekIdent,'a');
- AssertEquals('Loop type',ltNormal,F.Looptype);
- AssertEquals('Up loop',False,F.Down);
- AssertExpression('Start value',F.StartExpr,pekNumber,'1');
- AssertExpression('End value',F.EndExpr,pekNumber,'10');
- AssertNull('Empty body',F.Body);
- end;
- procedure TTestStatementParser.TestForIn;
- Var
- F : TPasImplForLoop;
- begin
- DeclareVar('integer');
- TestStatement(['For a in SomeSet Do',';']);
- F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
- AssertExpression('Loop variable name',F.VariableName,pekIdent,'a');
- AssertEquals('Loop type',ltIn,F.Looptype);
- AssertEquals('In loop',False,F.Down);
- AssertExpression('Start value',F.StartExpr,pekIdent,'SomeSet');
- AssertNull('Loop type',F.EndExpr);
- AssertNull('Empty body',F.Body);
- end;
- procedure TTestStatementParser.TestForExpr;
- Var
- F : TPasImplForLoop;
- B : TBinaryExpr;
- begin
- DeclareVar('integer');
- TestStatement(['For a:=1+1 to 5+5 do',';']);
- F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
- AssertExpression('Loop variable name',F.VariableName,pekIdent,'a');
- AssertEquals('Up loop',False,F.Down);
- AssertExpression('Start expression',F.StartExpr,pekBinary,TBinaryExpr);
- B:=F.StartExpr as TBinaryExpr;
- AssertExpression('Start value left',B.left,pekNumber,'1');
- AssertExpression('Start value right',B.right,pekNumber,'1');
- AssertExpression('Start expression',F.StartExpr,pekBinary,TBinaryExpr);
- B:=F.EndExpr as TBinaryExpr;
- AssertExpression('End value left',B.left,pekNumber,'5');
- AssertExpression('End value right',B.right,pekNumber,'5');
- AssertNull('Empty body',F.Body);
- end;
- procedure TTestStatementParser.TestForBlock;
- Var
- F : TPasImplForLoop;
- begin
- DeclareVar('integer');
- TestStatement(['For a:=1 to 10 do','begin','end']);
- F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
- AssertExpression('Loop variable name',F.VariableName,pekIdent,'a');
- AssertEquals('Up loop',False,F.Down);
- AssertExpression('Start value',F.StartExpr,pekNumber,'1');
- AssertExpression('End value',F.EndExpr,pekNumber,'10');
- AssertNotNull('Have for body',F.Body);
- AssertEquals('begin end block',TPasImplBeginBlock,F.Body.ClassType);
- AssertEquals('Empty block',0,TPasImplBeginBlock(F.Body).ELements.Count);
- end;
- procedure TTestStatementParser.TestDowntoBlock;
- Var
- F : TPasImplForLoop;
- begin
- DeclareVar('integer');
- TestStatement(['For a:=10 downto 1 do','begin','end']);
- F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
- AssertExpression('Loop variable name',F.VariableName,pekIdent,'a');
- AssertEquals('Down loop',True,F.Down);
- AssertExpression('Start value',F.StartExpr,pekNumber,'10');
- AssertExpression('End value',F.EndExpr,pekNumber,'1');
- AssertNotNull('Have for body',F.Body);
- AssertEquals('begin end block',TPasImplBeginBlock,F.Body.ClassType);
- AssertEquals('Empty block',0,TPasImplBeginBlock(F.Body).ELements.Count);
- end;
- procedure TTestStatementParser.TestForNested;
- Var
- F : TPasImplForLoop;
- begin
- DeclareVar('integer');
- DeclareVar('integer','b');
- TestStatement(['For a:=1 to 10 do','For b:=11 to 20 do','begin','end']);
- F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
- AssertExpression('Loop variable name',F.VariableName,pekIdent,'a');
- AssertEquals('Up loop',False,F.Down);
- AssertExpression('Start value',F.StartExpr,pekNumber,'1');
- AssertExpression('End value',F.EndExpr,pekNumber,'10');
- AssertNotNull('Have while body',F.Body);
- AssertEquals('begin end block',TPasImplForLoop,F.Body.ClassType);
- F:=F.Body as TPasImplForLoop;
- AssertExpression('Loop variable name',F.VariableName,pekIdent,'b');
- AssertEquals('Up loop',False,F.Down);
- AssertExpression('Start value',F.StartExpr,pekNumber,'11');
- AssertExpression('End value',F.EndExpr,pekNumber,'20');
- AssertNotNull('Have for body',F.Body);
- AssertEquals('begin end block',TPasImplBeginBlock,F.Body.ClassType);
- AssertEquals('Empty block',0,TPasImplBeginBlock(F.Body).ELements.Count);
- end;
- procedure TTestStatementParser.TestWith;
- Var
- W : TpasImplWithDo;
- begin
- DeclareVar('record X,Y : Integer; end');
- TestStatement(['With a do','begin','end']);
- W:=AssertStatement('For statement',TpasImplWithDo) as TpasImplWithDo;
- AssertEquals('1 expression',1,W.Expressions.Count);
- AssertExpression('With identifier',TPasExpr(W.Expressions[0]),pekIdent,'a');
- AssertNotNull('Have with body',W.Body);
- AssertEquals('begin end block',TPasImplBeginBlock,W.Body.ClassType);
- AssertEquals('Empty block',0,TPasImplBeginBlock(W.Body).ELements.Count);
- end;
- procedure TTestStatementParser.TestWithMultiple;
- Var
- W : TpasImplWithDo;
- begin
- DeclareVar('record X,Y : Integer; end');
- DeclareVar('record W,Z : Integer; end','b');
- TestStatement(['With a,b do','begin','end']);
- W:=AssertStatement('For statement',TpasImplWithDo) as TpasImplWithDo;
- AssertEquals('2 expressions',2,W.Expressions.Count);
- AssertExpression('With identifier 1',TPasExpr(W.Expressions[0]),pekIdent,'a');
- AssertExpression('With identifier 2',TPasExpr(W.Expressions[1]),pekIdent,'b');
- AssertNotNull('Have with body',W.Body);
- AssertEquals('begin end block',TPasImplBeginBlock,W.Body.ClassType);
- AssertEquals('Empty block',0,TPasImplBeginBlock(W.Body).ELements.Count);
- end;
- procedure TTestStatementParser.TestCaseEmpty;
- begin
- DeclareVar('integer');
- AddStatements(['case a of','end;']);
- ExpectParserError('Empty case not allowed');
- end;
- procedure TTestStatementParser.TestCaseOneInteger;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1 : ;','end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertNull('No else branch',C.ElseBranch);
- AssertEquals('One case label',1,C.Elements.Count);
- AssertEquals('Correct case for case label',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('1 expression for case',1,S.Expressions.Count);
- AssertExpression('With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
- AssertEquals('Empty case label statement',0,S.Elements.Count);
- AssertNull('Empty case label statement',S.Body);
- end;
- procedure TTestStatementParser.TestCaseTwoIntegers;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1,2 : ;','end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertNull('No else branch',C.ElseBranch);
- AssertEquals('One case label',1,C.Elements.Count);
- AssertEquals('Correct case for case label',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('2 expressions for case',2,S.Expressions.Count);
- AssertExpression('With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
- AssertExpression('With identifier 2',TPasExpr(S.Expressions[1]),pekNumber,'2');
- AssertEquals('Empty case label statement',0,S.Elements.Count);
- AssertNull('Empty case label statement',S.Body);
- end;
- procedure TTestStatementParser.TestCaseRange;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1..3 : ;','end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertNull('No else branch',C.ElseBranch);
- AssertEquals('One case label',1,C.Elements.Count);
- AssertEquals('Correct case for case label',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('1 expression for case',1,S.Expressions.Count);
- AssertExpression('With identifier 1',TPasExpr(S.Expressions[0]),pekRange,TBinaryExpr);
- AssertEquals('Empty case label statement',0,S.Elements.Count);
- AssertNull('Empty case label statement',S.Body);
- end;
- procedure TTestStatementParser.TestCaseRangeSeparate;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1..3,5 : ;','end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertNull('No else branch',C.ElseBranch);
- AssertEquals('One case label',1,C.Elements.Count);
- AssertEquals('Correct case for case label',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('2 expressions for case',2,S.Expressions.Count);
- AssertExpression('With identifier 1',TPasExpr(S.Expressions[0]),pekRange,TBinaryExpr);
- AssertExpression('With identifier 2',TPasExpr(S.Expressions[1]),pekNumber,'5');
- AssertEquals('Empty case label statement',0,S.Elements.Count);
- AssertNull('Empty case label statement',S.Body);
- end;
- procedure TTestStatementParser.TestCase2Cases;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1 : ;','2 : ;','end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertNull('No else branch',C.ElseBranch);
- AssertEquals('Two case labels',2,C.Elements.Count);
- AssertEquals('Correct case for case label 1',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
- AssertExpression('Case 1 With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
- AssertEquals('Empty case label statement 1',0,S.Elements.Count);
- AssertNull('Empty case label statement 1',S.Body);
- // Two
- AssertEquals('Correct case for case label 2',TPasImplCaseStatement,TPasElement(C.Elements[1]).ClassType);
- S:=TPasImplCaseStatement(C.Elements[1]);
- AssertEquals('2 expressions for case 2',1,S.Expressions.Count);
- AssertExpression('Case 2 With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'2');
- AssertEquals('Empty case label statement 2',0,S.Elements.Count);
- AssertNull('Empty case label statement 2',S.Body);
- end;
- procedure TTestStatementParser.TestCaseBlock;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- B : TPasImplbeginBlock;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1 : begin end;','end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertNull('No else branch',C.ElseBranch);
- AssertEquals('Two case labels',1,C.Elements.Count);
- AssertEquals('Correct case for case label 1',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
- AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
- AssertEquals('1 case label statement',1,S.Elements.Count);
- AssertEquals('Correct case for case label 1',TPasImplbeginBlock,TPasElement(S.Elements[0]).ClassType);
- B:=TPasImplbeginBlock(S.Elements[0]);
- AssertEquals('0 statements in block',0,B.Elements.Count);
- end;
- procedure TTestStatementParser.TestCaseElseBlockEmpty;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- B : TPasImplbeginBlock;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1 : begin end;','else',' end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertEquals('Two case labels',2,C.Elements.Count);
- AssertEquals('Correct case for case label 1',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
- AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
- AssertEquals('1 case label statement',1,S.Elements.Count);
- AssertEquals('Correct case for case label 1',TPasImplbeginBlock,TPasElement(S.Elements[0]).ClassType);
- B:=TPasImplbeginBlock(S.Elements[0]);
- AssertEquals('0 statements in block',0,B.Elements.Count);
- AssertNotNull('Have else branch',C.ElseBranch);
- AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
- AssertEquals('Zero statements ',0,TPasImplCaseElse(C.ElseBranch).Elements.Count);
- end;
- procedure TTestStatementParser.TestCaseOtherwiseBlockEmpty;
- Var
- C : TPasImplCaseOf;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1 : begin end;','otherwise',' end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertNotNull('Have else branch',C.ElseBranch);
- AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
- AssertEquals('Zero statements ',0,TPasImplCaseElse(C.ElseBranch).Elements.Count);
- end;
- procedure TTestStatementParser.TestCaseElseBlockAssignment;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- B : TPasImplbeginBlock;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1 : begin end;','else','a:=1',' end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertEquals('Two case labels',2,C.Elements.Count);
- AssertEquals('Correct case for case label 1',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
- AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
- AssertEquals('1 case label statement',1,S.Elements.Count);
- AssertEquals('Correct case for case label 1',TPasImplbeginBlock,TPasElement(S.Elements[0]).ClassType);
- B:=TPasImplbeginBlock(S.Elements[0]);
- AssertEquals('0 statements in block',0,B.Elements.Count);
- AssertNotNull('Have else branch',C.ElseBranch);
- AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
- AssertEquals('1 statement in else branch ',1,TPasImplCaseElse(C.ElseBranch).Elements.Count);
- end;
- procedure TTestStatementParser.TestCaseElseBlock2Assignments;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- B : TPasImplbeginBlock;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1 : begin end;','else','a:=1;','a:=32;',' end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertEquals('Two case labels',2,C.Elements.Count);
- AssertEquals('Correct case for case label 1',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
- AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
- AssertEquals('1 case label statement',1,S.Elements.Count);
- AssertEquals('Correct case for case label 1',TPasImplbeginBlock,TPasElement(S.Elements[0]).ClassType);
- B:=TPasImplbeginBlock(S.Elements[0]);
- AssertEquals('0 statements in block',0,B.Elements.Count);
- AssertNotNull('Have else branch',C.ElseBranch);
- AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
- AssertEquals('2 statements in else branch ',2,TPasImplCaseElse(C.ElseBranch).Elements.Count);
- end;
- procedure TTestStatementParser.TestCaseIfCaseElse;
- Var
- C : TPasImplCaseOf;
- begin
- DeclareVar('integer');
- DeclareVar('boolean','b');
- TestStatement(['case a of','1 : if b then',' begin end;','else',' end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertEquals('Two case labels',2,C.Elements.Count);
- AssertNotNull('Have else branch',C.ElseBranch);
- AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
- AssertEquals('0 statement in else branch ',0,TPasImplCaseElse(C.ElseBranch).Elements.Count);
- end;
- procedure TTestStatementParser.TestCaseIfElse;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- begin
- DeclareVar('integer');
- DeclareVar('boolean','b');
- TestStatement(['case a of','1 : if b then',' begin end','else','begin','end',' end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertEquals('Two case labels',1,C.Elements.Count);
- AssertNull('Have no else branch',C.ElseBranch);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
- AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
- AssertEquals('1 case label statement',1,S.Elements.Count);
- AssertEquals('If statement in case label 1',TPasImplIfElse,TPasElement(S.Elements[0]).ClassType);
- AssertNotNull('If statement has else block',TPasImplIfElse(S.Elements[0]).ElseBranch);
- end;
- procedure TTestStatementParser.TestCaseElseNoSemicolon;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1 : dosomething;','2 : dosomethingmore','else','a:=1;','end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertEquals('case label count',3,C.Elements.Count);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('case 1',1,S.Expressions.Count);
- AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
- S:=TPasImplCaseStatement(C.Elements[1]);
- AssertEquals('case 2',1,S.Expressions.Count);
- AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'2');
- AssertEquals('third is else',TPasImplCaseElse,TObject(C.Elements[2]).ClassType);
- AssertNotNull('Have else branch',C.ElseBranch);
- AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
- AssertEquals('1 statements in else branch ',1,TPasImplCaseElse(C.ElseBranch).Elements.Count);
- end;
- procedure TTestStatementParser.TestCaseIfElseNoSemicolon;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1 : dosomething;','2: if b then',' dosomething','else dosomethingmore','else','a:=1;','end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertEquals('case label count',3,C.Elements.Count);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('case 1',1,S.Expressions.Count);
- AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
- S:=TPasImplCaseStatement(C.Elements[1]);
- AssertEquals('case 2',1,S.Expressions.Count);
- AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'2');
- AssertEquals('third is else',TPasImplCaseElse,TObject(C.Elements[2]).ClassType);
- AssertNotNull('Have else branch',C.ElseBranch);
- AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
- AssertEquals('1 statements in else branch ',1,TPasImplCaseElse(C.ElseBranch).Elements.Count);
- end;
- procedure TTestStatementParser.TestCaseIfOtherwiseNoSemicolon;
- Var
- C : TPasImplCaseOf;
- S : TPasImplCaseStatement;
- begin
- DeclareVar('integer');
- TestStatement(['case a of','1 : dosomething;','2: if b then',' dosomething','else dosomethingmore','otherwise','a:=1;','end;']);
- C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
- AssertNotNull('Have case expression',C.CaseExpr);
- AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
- AssertEquals('case label count',3,C.Elements.Count);
- S:=TPasImplCaseStatement(C.Elements[0]);
- AssertEquals('case 1',1,S.Expressions.Count);
- AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
- S:=TPasImplCaseStatement(C.Elements[1]);
- AssertEquals('case 2',1,S.Expressions.Count);
- AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'2');
- AssertEquals('third is else',TPasImplCaseElse,TObject(C.Elements[2]).ClassType);
- AssertNotNull('Have else branch',C.ElseBranch);
- AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
- AssertEquals('1 statements in else branch ',1,TPasImplCaseElse(C.ElseBranch).Elements.Count);
- end;
- procedure TTestStatementParser.TestRaise;
- Var
- R : TPasImplRaise;
- begin
- DeclareVar('Exception');
- TestStatement('Raise A;');
- R:=AssertStatement('Raise statement',TPasImplRaise) as TPasImplRaise;
- AssertEquals(0,R.Elements.Count);
- AssertNotNull(R.ExceptObject);
- AssertNull(R.ExceptAddr);
- AssertExpression('Expression object',R.ExceptObject,pekIdent,'A');
- end;
- procedure TTestStatementParser.TestRaiseEmpty;
- Var
- R : TPasImplRaise;
- begin
- TestStatement('Raise;');
- R:=AssertStatement('Raise statement',TPasImplRaise) as TPasImplRaise;
- AssertEquals(0,R.Elements.Count);
- AssertNull(R.ExceptObject);
- AssertNull(R.ExceptAddr);
- end;
- procedure TTestStatementParser.TestRaiseAt;
- Var
- R : TPasImplRaise;
- begin
- DeclareVar('Exception');
- DeclareVar('Pointer','B');
- TestStatement('Raise A at B;');
- R:=AssertStatement('Raise statement',TPasImplRaise) as TPasImplRaise;
- AssertEquals(0,R.Elements.Count);
- AssertNotNull(R.ExceptObject);
- AssertNotNull(R.ExceptAddr);
- AssertExpression('Expression object',R.ExceptAddr,pekIdent,'B');
- end;
- procedure TTestStatementParser.TestTryFinally;
- Var
- T : TPasImplTry;
- S : TPasImplSimple;
- F : TPasImplTryFinally;
- begin
- TestStatement(['Try',' DoSomething;','finally',' DoSomethingElse','end']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(1,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Finally statement',TPasImplTryFinally,T.FinallyExcept.ClassType);
- F:=TPasImplTryFinally(T.FinallyExcept);
- AssertEquals(1,F.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(F.Elements[0]).ClassType);
- S:=TPasImplSimple(F.Elements[0]);
- AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
- end;
- procedure TTestStatementParser.TestTryFinallyEmpty;
- Var
- T : TPasImplTry;
- F : TPasImplTryFinally;
- begin
- TestStatement(['Try','finally','end;']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(0,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNull(T.ElseBranch);
- AssertEquals('Finally statement',TPasImplTryFinally,T.FinallyExcept.ClassType);
- F:=TPasImplTryFinally(T.FinallyExcept);
- AssertEquals(0,F.Elements.Count);
- end;
- procedure TTestStatementParser.TestTryFinallyNested;
- Var
- T : TPasImplTry;
- S : TPasImplSimple;
- F : TPasImplTryFinally;
- begin
- TestStatement(['Try',' DoSomething1;',' Try',' DoSomething2;',' finally',' DoSomethingElse2',' end;','Finally',' DoSomethingElse1','end']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(2,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething1');
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Finally statement',TPasImplTryFinally,T.FinallyExcept.ClassType);
- F:=TPasImplTryFinally(T.FinallyExcept);
- AssertEquals(1,F.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(F.Elements[0]).ClassType);
- S:=TPasImplSimple(F.Elements[0]);
- AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse1');
- // inner statement
- AssertNotNull(T.Elements[1]);
- AssertEquals('Nested try statement',TPasImplTry,TPasElement(T.Elements[1]).ClassType);
- T:=TPasImplTry(T.Elements[1]);
- AssertEquals(1,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething2');
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Finally statement',TPasImplTryFinally,T.FinallyExcept.ClassType);
- F:=TPasImplTryFinally(T.FinallyExcept);
- AssertEquals(1,F.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(F.Elements[0]).ClassType);
- S:=TPasImplSimple(F.Elements[0]);
- AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse2');
- end;
- procedure TTestStatementParser.TestTryExcept;
- Var
- T : TPasImplTry;
- S : TPasImplSimple;
- E : TPasImplTryExcept;
- begin
- TestStatement(['Try',' DoSomething;','except',' DoSomethingElse','end']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(1,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
- E:=TPasImplTryExcept(T.FinallyExcept);
- AssertEquals(1,E.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(E.Elements[0]).ClassType);
- S:=TPasImplSimple(E.Elements[0]);
- AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
- end;
- procedure TTestStatementParser.TestTryExceptNested;
- Var
- T : TPasImplTry;
- S : TPasImplSimple;
- E : TPasImplTryExcept;
- begin
- TestStatement(['Try',' DoSomething1;',' try',' DoSomething2;',' except',' DoSomethingElse2',' end','except',' DoSomethingElse1','end']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(2,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething1');
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
- E:=TPasImplTryExcept(T.FinallyExcept);
- AssertEquals(1,E.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(E.Elements[0]).ClassType);
- S:=TPasImplSimple(E.Elements[0]);
- AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse1');
- AssertNotNull(T.Elements[1]);
- AssertEquals('Simple statement',TPasImplTry,TPasElement(T.Elements[1]).ClassType);
- T:=TPasImplTry(T.Elements[1]);
- AssertEquals(1,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement 2',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething2 call ',S.Expr,pekIdent,'DoSomething2');
- AssertEquals('Simple statement2',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Except statement2',TPasImplTryExcept,T.FinallyExcept.ClassType);
- E:=TPasImplTryExcept(T.FinallyExcept);
- AssertEquals(1,E.Elements.Count);
- AssertEquals('Simple statement2',TPasImplSimple,TPasElement(E.Elements[0]).ClassType);
- S:=TPasImplSimple(E.Elements[0]);
- AssertExpression('DoSomethingElse2 call',S.Expr,pekIdent,'DoSomethingElse2');
- end;
- procedure TTestStatementParser.TestTryExceptEmpty;
- Var
- T : TPasImplTry;
- E : TPasImplTryExcept;
- begin
- TestStatement(['Try','except','end;']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(0,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNull(T.ElseBranch);
- AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
- E:=TPasImplTryExcept(T.FinallyExcept);
- AssertEquals(0,E.Elements.Count);
- end;
- procedure TTestStatementParser.TestTryExceptOn;
- Var
- T : TPasImplTry;
- S : TPasImplSimple;
- E : TPasImplTryExcept;
- O : TPasImplExceptOn;
- begin
- TestStatement(['Try',' DoSomething;','except','On E : Exception do','DoSomethingElse;','end']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(1,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
- E:=TPasImplTryExcept(T.FinallyExcept);
- AssertEquals(1,E.Elements.Count);
- AssertEquals('Except on handler',TPasImplExceptOn,TPasElement(E.Elements[0]).ClassType);
- O:=TPasImplExceptOn(E.Elements[0]);
- AssertEquals(1,O.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(O.Elements[0]).ClassType);
- AssertEquals('Exception Variable name','E',O.VariableName);
- AssertEquals('Exception Type name','Exception',O.TypeName);
- S:=TPasImplSimple(O.Elements[0]);
- AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
- // AssertEquals('Variable name',
- end;
- procedure TTestStatementParser.TestTryExceptOn2;
- Var
- T : TPasImplTry;
- S : TPasImplSimple;
- E : TPasImplTryExcept;
- O : TPasImplExceptOn;
- begin
- TestStatement(['Try',' DoSomething;','except',
- 'On E : Exception do','DoSomethingElse;',
- 'On Y : Exception2 do','DoSomethingElse2;',
- 'end']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(1,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
- E:=TPasImplTryExcept(T.FinallyExcept);
- AssertEquals(2,E.Elements.Count);
- // Exception handler 1
- AssertEquals('Except on handler',TPasImplExceptOn,TPasElement(E.Elements[0]).ClassType);
- O:=TPasImplExceptOn(E.Elements[0]);
- AssertEquals(1,O.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(O.Elements[0]).ClassType);
- AssertEquals('Exception Variable name','E',O.VariableName);
- AssertEquals('Exception Type name','Exception',O.TypeName);
- S:=TPasImplSimple(O.Elements[0]);
- AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
- // Exception handler 2
- AssertEquals('Except on handler',TPasImplExceptOn,TPasElement(E.Elements[1]).ClassType);
- O:=TPasImplExceptOn(E.Elements[1]);
- AssertEquals(1,O.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(O.Elements[0]).ClassType);
- AssertEquals('Exception Variable name','Y',O.VariableName);
- AssertEquals('Exception Type name','Exception2',O.TypeName);
- S:=TPasImplSimple(O.Elements[0]);
- AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse2');
- end;
- procedure TTestStatementParser.TestTryExceptOnElse;
- Var
- T : TPasImplTry;
- S : TPasImplSimple;
- E : TPasImplTryExcept;
- O : TPasImplExceptOn;
- EE : TPasImplTryExceptElse;
- I : TPasImplIfElse;
- begin
- DeclareVar('Boolean','b');
- // Check that Else belongs to Except, not to IF
- TestStatement(['Try',' DoSomething;','except','On E : Exception do','if b then','DoSomethingElse;','else','DoSomethingMore;','end']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(1,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNotNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
- E:=TPasImplTryExcept(T.FinallyExcept);
- AssertEquals(1,E.Elements.Count);
- AssertEquals('Except on handler',TPasImplExceptOn,TPasElement(E.Elements[0]).ClassType);
- O:=TPasImplExceptOn(E.Elements[0]);
- AssertEquals('Exception Variable name','E',O.VariableName);
- AssertEquals('Exception Type name','Exception',O.TypeName);
- AssertEquals(1,O.Elements.Count);
- AssertEquals('Simple statement',TPasImplIfElse,TPasElement(O.Elements[0]).ClassType);
- I:=TPasImplIfElse(O.Elements[0]);
- AssertEquals(1,I.Elements.Count);
- AssertNull('No else barcnh for if',I.ElseBranch);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(I.Elements[0]).ClassType);
- S:=TPasImplSimple(I.Elements[0]);
- AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
- AssertEquals('Except Else statement',TPasImplTryExceptElse,T.ElseBranch.ClassType);
- EE:=TPasImplTryExceptElse(T.ElseBranch);
- AssertEquals(1,EE.Elements.Count);
- AssertNotNull(EE.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(EE.Elements[0]).ClassType);
- S:=TPasImplSimple(EE.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomethingMore');
- end;
- procedure TTestStatementParser.TestTryExceptOnIfElse;
- Var
- T : TPasImplTry;
- S : TPasImplSimple;
- E : TPasImplTryExcept;
- O : TPasImplExceptOn;
- EE : TPasImplTryExceptElse;
- begin
- TestStatement(['Try',' DoSomething;','except','On E : Exception do','DoSomethingElse;','else','DoSomethingMore;','end']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(1,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNotNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
- E:=TPasImplTryExcept(T.FinallyExcept);
- AssertEquals(1,E.Elements.Count);
- AssertEquals('Except on handler',TPasImplExceptOn,TPasElement(E.Elements[0]).ClassType);
- O:=TPasImplExceptOn(E.Elements[0]);
- AssertEquals('Exception Variable name','E',O.VariableName);
- AssertEquals('Exception Type name','Exception',O.TypeName);
- AssertEquals(1,O.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(O.Elements[0]).ClassType);
- S:=TPasImplSimple(O.Elements[0]);
- AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
- AssertEquals('Except Else statement',TPasImplTryExceptElse,T.ElseBranch.ClassType);
- EE:=TPasImplTryExceptElse(T.ElseBranch);
- AssertEquals(1,EE.Elements.Count);
- AssertNotNull(EE.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(EE.Elements[0]).ClassType);
- S:=TPasImplSimple(EE.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomethingMore');
- end;
- procedure TTestStatementParser.TestTryExceptOnElseNoSemicolo;
- Var
- T : TPasImplTry;
- S : TPasImplSimple;
- E : TPasImplTryExcept;
- O : TPasImplExceptOn;
- EE : TPasImplTryExceptElse;
- begin
- TestStatement(['Try',' DoSomething;','except','On E : Exception do','DoSomethingElse','else','DoSomethingMore','end']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(1,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNotNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
- E:=TPasImplTryExcept(T.FinallyExcept);
- AssertEquals(1,E.Elements.Count);
- AssertEquals('Except on handler',TPasImplExceptOn,TPasElement(E.Elements[0]).ClassType);
- O:=TPasImplExceptOn(E.Elements[0]);
- AssertEquals('Exception Variable name','E',O.VariableName);
- AssertEquals('Exception Type name','Exception',O.TypeName);
- AssertEquals(1,O.Elements.Count);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(O.Elements[0]).ClassType);
- S:=TPasImplSimple(O.Elements[0]);
- AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
- AssertEquals('Except Else statement',TPasImplTryExceptElse,T.ElseBranch.ClassType);
- EE:=TPasImplTryExceptElse(T.ElseBranch);
- AssertEquals(1,EE.Elements.Count);
- AssertNotNull(EE.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(EE.Elements[0]).ClassType);
- S:=TPasImplSimple(EE.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomethingMore');
- end;
- procedure TTestStatementParser.TestTryExceptRaise;
- Var
- T : TPasImplTry;
- S : TPasImplSimple;
- E : TPasImplTryExcept;
- begin
- TestStatement(['Try',' DoSomething;','except',' raise','end']);
- T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
- AssertEquals(1,T.Elements.Count);
- AssertNotNull(T.FinallyExcept);
- AssertNull(T.ElseBranch);
- AssertNotNull(T.Elements[0]);
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- S:=TPasImplSimple(T.Elements[0]);
- AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
- AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
- AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
- E:=TPasImplTryExcept(T.FinallyExcept);
- AssertEquals(1,E.Elements.Count);
- AssertEquals('Raise statement',TPasImplRaise,TPasElement(E.Elements[0]).ClassType);
- end;
- procedure TTestStatementParser.TestAsm;
- Var
- T : TPasImplAsmStatement;
- begin
- TestStatement(['asm',' mov eax,1','end;']);
- T:=AssertStatement('Asm statement',TPasImplAsmStatement) as TPasImplAsmStatement;
- AssertEquals('Asm tokens',4,T.Tokens.Count);
- AssertEquals('token 1 ','mov',T.Tokens[0]);
- AssertEquals('token 2 ','eax',T.Tokens[1]);
- AssertEquals('token 3 ',',',T.Tokens[2]);
- AssertEquals('token 4 ','1',T.Tokens[3]);
- end;
- procedure TTestStatementParser.TestAsmBlock;
- begin
- Source.Add('{$MODE DELPHI}');
- Source.Add('function BitsHighest(X: Cardinal): Integer;');
- Source.Add('asm');
- Source.Add('end;');
- Source.Add('begin');
- Source.Add('end.');
- ParseModule;
- end;
- procedure TTestStatementParser.TestAsmBlockWithEndLabel;
- begin
- Source.Add('{$MODE DELPHI}');
- Source.Add('function BitsHighest(X: Cardinal): Integer;');
- Source.Add('asm');
- Source.Add(' MOV ECX, EAX');
- Source.Add(' MOV EAX, -1');
- Source.Add(' BSR EAX, ECX');
- Source.Add(' JNZ @@End');
- Source.Add(' MOV EAX, -1');
- Source.Add('@@End:');
- Source.Add('end;');
- Source.Add('begin');
- Source.Add('end.');
- ParseModule;
- end;
- procedure TTestStatementParser.TestAsmBlockInIfThen;
- begin
- Source.Add('{$MODE DELPHI}');
- Source.Add('function Get8087StatusWord(ClearExceptions: Boolean): Word;');
- Source.Add(' begin');
- Source.Add(' if ClearExceptions then');
- Source.Add(' asm');
- Source.Add(' end');
- Source.Add(' else');
- Source.Add(' asm');
- Source.Add(' end;');
- Source.Add(' end;');
- Source.Add(' begin');
- Source.Add(' end.');
- ParseModule;
- end;
- procedure TTestStatementParser.TestAssignToAddress;
- begin
- AddStatements(['@Proc:=Nil']);
- ParseModule;
- end;
- procedure TTestStatementParser.TestFinalizationNoSemicolon;
- begin
- Source.Add('unit afile;');
- Source.Add('{$mode objfpc}');
- Source.Add('interface');
- Source.Add('implementation');
- Source.Add('initialization');
- Source.Add(' writeln(''qqq'')');
- Source.Add('finalization');
- Source.Add(' write(''rrr'')');
- ParseModule;
- end;
- procedure TTestStatementParser.TestMacroComment;
- begin
- AddStatements(['{$MACRO ON}',
- '{$DEFINE func := //}',
- ' calltest;',
- ' func (''1'',''2'',''3'');',
- 'CallTest2;'
- ]);
- ParseModule;
- end;
- procedure TTestStatementParser.TestPlatformIdentifier;
- begin
- AddStatements(['write(platform);']);
- ParseModule;
- end;
- procedure TTestStatementParser.TestPlatformIdentifier2;
- begin
- AddStatements(['write(libs+platform);']);
- ParseModule;
- end;
- procedure TTestStatementParser.TestArgumentNameOn;
- begin
- Source.Add('function TryOn(const on: boolean): boolean;');
- Source.Add(' begin');
- Source.Add(' end;');
- Source.Add(' begin');
- Source.Add(' end.');
- ParseModule;
- end;
- procedure TTestStatementParser.TestGotoInIfThen;
- begin
- AddStatements([
- '{$goto on}',
- 'if expr then',
- ' dosomething',
- ' else if expr2 then',
- ' goto try_qword',
- ' else',
- ' dosomething;',
- ' try_qword:',
- ' dosomething;']);
- ParseModule;
- end;
- initialization
- RegisterTests([TTestStatementParser]);
- end.
|