tcstatements.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. unit tcstatements;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, pastree, pscanner, pparser,
  6. tcbaseparser, testregistry;
  7. Type
  8. { TTestStatementParser }
  9. TTestStatementParser = Class(TTestParser)
  10. private
  11. FStatement: TPasImplBlock;
  12. FVariables : TStrings;
  13. Protected
  14. Procedure SetUp; override;
  15. Procedure TearDown; override;
  16. procedure AddStatements(ASource : Array of string);
  17. Procedure DeclareVar(Const AVarType : String; Const AVarName : String = 'A');
  18. function TestStatement(ASource : string) : TPasImplElement;
  19. function TestStatement(ASource : Array of string) : TPasImplElement;
  20. Procedure ExpectParserError(Const Msg : string);
  21. Procedure ExpectParserError(Const Msg : string; ASource : Array of string);
  22. Function AssertStatement(Msg : String; AClass : TClass;AIndex : Integer = 0) : TPasImplBlock;
  23. Property Statement: TPasImplBlock Read FStatement;
  24. Published
  25. Procedure TestEmpty;
  26. Procedure TestEmptyStatement;
  27. Procedure TestEmptyStatements;
  28. Procedure TestBlock;
  29. Procedure TestBlockComment;
  30. Procedure TestBlock2Comments;
  31. Procedure TestAssignment;
  32. Procedure TestAssignmentAdd;
  33. Procedure TestAssignmentMinus;
  34. Procedure TestAssignmentMul;
  35. Procedure TestAssignmentDivision;
  36. Procedure TestCall;
  37. Procedure TestCallComment;
  38. Procedure TestCallQualified;
  39. Procedure TestCallQualified2;
  40. Procedure TestCallNoArgs;
  41. Procedure TestCallOneArg;
  42. Procedure TestIf;
  43. Procedure TestIfBlock;
  44. Procedure TestIfAssignment;
  45. Procedure TestIfElse;
  46. Procedure TestIfElseBlock;
  47. Procedure TestIfSemiColonElseError;
  48. Procedure TestNestedIf;
  49. Procedure TestNestedIfElse;
  50. Procedure TestWhile;
  51. Procedure TestWhileBlock;
  52. Procedure TestWhileNested;
  53. Procedure TestRepeat;
  54. Procedure TestRepeatBlock;
  55. procedure TestRepeatBlockNosemicolon;
  56. Procedure TestRepeatNested;
  57. Procedure TestFor;
  58. Procedure TestForIn;
  59. Procedure TestForExpr;
  60. Procedure TestForBlock;
  61. procedure TestDowntoBlock;
  62. Procedure TestForNested;
  63. Procedure TestWith;
  64. Procedure TestWithMultiple;
  65. Procedure TestCaseEmpty;
  66. Procedure TestCaseOneInteger;
  67. Procedure TestCaseTwoIntegers;
  68. Procedure TestCaseRange;
  69. Procedure TestCaseRangeSeparate;
  70. Procedure TestCase2Cases;
  71. Procedure TestCaseBlock;
  72. Procedure TestCaseElseBlockEmpty;
  73. Procedure TestCaseElseBlockAssignment;
  74. Procedure TestCaseElseBlock2Assignments;
  75. Procedure TestCaseIfCaseElse;
  76. Procedure TestCaseIfElse;
  77. Procedure TestRaise;
  78. Procedure TestRaiseEmpty;
  79. Procedure TestRaiseAt;
  80. Procedure TestTryFinally;
  81. Procedure TestTryFinallyEmpty;
  82. Procedure TestTryFinallyNested;
  83. procedure TestTryExcept;
  84. procedure TestTryExceptNested;
  85. procedure TestTryExceptEmpty;
  86. Procedure TestTryExceptOn;
  87. Procedure TestTryExceptOn2;
  88. Procedure TestTryExceptOnElse;
  89. Procedure TestTryExceptOnIfElse;
  90. Procedure TestAsm;
  91. end;
  92. implementation
  93. { TTestStatementParser }
  94. procedure TTestStatementParser.SetUp;
  95. begin
  96. inherited SetUp;
  97. FVariables:=TStringList.Create;
  98. end;
  99. procedure TTestStatementParser.TearDown;
  100. begin
  101. FreeAndNil(FVariables);
  102. inherited TearDown;
  103. end;
  104. procedure TTestStatementParser.AddStatements(ASource: array of string);
  105. Var
  106. I :Integer;
  107. begin
  108. StartProgram('afile');
  109. if FVariables.Count>0 then
  110. begin
  111. Add('Var');
  112. For I:=0 to FVariables.Count-1 do
  113. Add(' '+Fvariables[I]);
  114. end;
  115. Add('begin');
  116. For I:=Low(ASource) to High(ASource) do
  117. Add(' '+ASource[i]);
  118. end;
  119. procedure TTestStatementParser.DeclareVar(const AVarType: String;
  120. const AVarName: String);
  121. begin
  122. FVariables.Add(AVarName+' : '+AVarType+';');
  123. end;
  124. function TTestStatementParser.TestStatement(ASource: string): TPasImplElement;
  125. begin
  126. Result:=TestStatement([ASource]);
  127. end;
  128. function TTestStatementParser.TestStatement(ASource: array of string
  129. ): TPasImplElement;
  130. begin
  131. Result:=Nil;
  132. FStatement:=Nil;
  133. AddStatements(ASource);
  134. ParseModule;
  135. AssertEquals('Have program',TPasProgram,Module.ClassType);
  136. AssertNotNull('Have program section',PasProgram.ProgramSection);
  137. AssertNotNull('Have initialization section',PasProgram.InitializationSection);
  138. if (PasProgram.InitializationSection.Elements.Count>0) then
  139. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  140. FStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  141. Result:=FStatement;
  142. end;
  143. procedure TTestStatementParser.ExpectParserError(const Msg: string);
  144. begin
  145. AssertException(Msg,EParserError,@ParseModule);
  146. end;
  147. procedure TTestStatementParser.ExpectParserError(const Msg: string;
  148. ASource: array of string);
  149. begin
  150. AddStatements(ASource);
  151. ExpectParserError(Msg);
  152. end;
  153. function TTestStatementParser.AssertStatement(Msg: String; AClass: TClass;
  154. AIndex: Integer): TPasImplBlock;
  155. begin
  156. if not (AIndex<PasProgram.InitializationSection.Elements.Count) then
  157. Fail(Msg+': No such statement : '+intTostr(AIndex));
  158. AssertNotNull(Msg+' Have statement',PasProgram.InitializationSection.Elements[AIndex]);
  159. AssertEquals(Msg+' statement class',AClass,TObject(PasProgram.InitializationSection.Elements[AIndex]).ClassType);
  160. Result:=TObject(PasProgram.InitializationSection.Elements[AIndex]) as TPasImplBlock;
  161. end;
  162. procedure TTestStatementParser.TestEmpty;
  163. begin
  164. //TestStatement(';');
  165. TestStatement('');
  166. AssertEquals('No statements',0,PasProgram.InitializationSection.Elements.Count);
  167. end;
  168. procedure TTestStatementParser.TestEmptyStatement;
  169. begin
  170. TestStatement(';');
  171. AssertEquals('0 statement',0,PasProgram.InitializationSection.Elements.Count);
  172. end;
  173. procedure TTestStatementParser.TestEmptyStatements;
  174. begin
  175. TestStatement(';;');
  176. AssertEquals('0 statement',0,PasProgram.InitializationSection.Elements.Count);
  177. end;
  178. procedure TTestStatementParser.TestBlock;
  179. Var
  180. B : TPasImplBeginBlock;
  181. begin
  182. TestStatement(['begin','end']);
  183. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  184. AssertNotNull('Statement assigned',PasProgram.InitializationSection.Elements[0]);
  185. AssertEquals('Block statement',TPasImplBeginBlock,Statement.ClassType);
  186. B:= Statement as TPasImplBeginBlock;
  187. AssertEquals('Empty block',0,B.Elements.Count);
  188. end;
  189. procedure TTestStatementParser.TestBlockComment;
  190. Var
  191. B : TPasImplBeginBlock;
  192. begin
  193. Engine.NeedComments:=True;
  194. TestStatement(['{ This is a comment }','begin','end']);
  195. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  196. AssertNotNull('Statement assigned',PasProgram.InitializationSection.Elements[0]);
  197. AssertEquals('Block statement',TPasImplBeginBlock,Statement.ClassType);
  198. B:= Statement as TPasImplBeginBlock;
  199. AssertEquals('Empty block',0,B.Elements.Count);
  200. AssertEquals('No DocComment','',B.DocComment);
  201. end;
  202. procedure TTestStatementParser.TestBlock2Comments;
  203. Var
  204. B : TPasImplBeginBlock;
  205. begin
  206. Engine.NeedComments:=True;
  207. TestStatement(['{ This is a comment }','// Another comment','begin','end']);
  208. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  209. AssertNotNull('Statement assigned',PasProgram.InitializationSection.Elements[0]);
  210. AssertEquals('Block statement',TPasImplBeginBlock,Statement.ClassType);
  211. B:= Statement as TPasImplBeginBlock;
  212. AssertEquals('Empty block',0,B.Elements.Count);
  213. AssertEquals('No DocComment','',B.DocComment);
  214. end;
  215. procedure TTestStatementParser.TestAssignment;
  216. Var
  217. A : TPasImplAssign;
  218. begin
  219. DeclareVar('integer');
  220. TestStatement(['a:=1;']);
  221. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  222. AssertEquals('Assignment statement',TPasImplAssign,Statement.ClassType);
  223. A:=Statement as TPasImplAssign;
  224. AssertEquals('Normal assignment',akDefault,A.Kind);
  225. AssertExpression('Right side is constant',A.Right,pekNumber,'1');
  226. AssertExpression('Left side is variable',A.Left,pekIdent,'a');
  227. end;
  228. procedure TTestStatementParser.TestAssignmentAdd;
  229. Var
  230. A : TPasImplAssign;
  231. begin
  232. Parser.Scanner.Options:=[po_cassignments];
  233. DeclareVar('integer');
  234. TestStatement(['a+=1;']);
  235. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  236. AssertEquals('Assignment statement',TPasImplAssign,Statement.ClassType);
  237. A:=Statement as TPasImplAssign;
  238. AssertEquals('Add assignment',akAdd,A.Kind);
  239. AssertExpression('Right side is constant',A.Right,pekNumber,'1');
  240. AssertExpression('Left side is variable',A.Left,pekIdent,'a');
  241. end;
  242. procedure TTestStatementParser.TestAssignmentMinus;
  243. Var
  244. A : TPasImplAssign;
  245. begin
  246. Parser.Scanner.Options:=[po_cassignments];
  247. DeclareVar('integer');
  248. TestStatement(['a-=1;']);
  249. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  250. AssertEquals('Assignment statement',TPasImplAssign,Statement.ClassType);
  251. A:=Statement as TPasImplAssign;
  252. AssertEquals('Minus assignment',akMinus,A.Kind);
  253. AssertExpression('Right side is constant',A.Right,pekNumber,'1');
  254. AssertExpression('Left side is variable',A.Left,pekIdent,'a');
  255. end;
  256. procedure TTestStatementParser.TestAssignmentMul;
  257. Var
  258. A : TPasImplAssign;
  259. begin
  260. Parser.Scanner.Options:=[po_cassignments];
  261. DeclareVar('integer');
  262. TestStatement(['a*=1;']);
  263. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  264. AssertEquals('Assignment statement',TPasImplAssign,Statement.ClassType);
  265. A:=Statement as TPasImplAssign;
  266. AssertEquals('Mul assignment',akMul,A.Kind);
  267. AssertExpression('Right side is constant',A.Right,pekNumber,'1');
  268. AssertExpression('Left side is variable',A.Left,pekIdent,'a');
  269. end;
  270. procedure TTestStatementParser.TestAssignmentDivision;
  271. Var
  272. A : TPasImplAssign;
  273. begin
  274. Parser.Scanner.Options:=[po_cassignments];
  275. DeclareVar('integer');
  276. TestStatement(['a/=1;']);
  277. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  278. AssertEquals('Assignment statement',TPasImplAssign,Statement.ClassType);
  279. A:=Statement as TPasImplAssign;
  280. AssertEquals('Division assignment',akDivision,A.Kind);
  281. AssertExpression('Right side is constant',A.Right,pekNumber,'1');
  282. AssertExpression('Left side is variable',A.Left,pekIdent,'a');
  283. end;
  284. procedure TTestStatementParser.TestCall;
  285. Var
  286. S : TPasImplSimple;
  287. begin
  288. TestStatement('Doit;');
  289. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  290. AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
  291. S:=Statement as TPasImplSimple;
  292. AssertExpression('Doit call',S.Expr,pekIdent,'Doit');
  293. end;
  294. procedure TTestStatementParser.TestCallComment;
  295. Var
  296. S : TPasImplSimple;
  297. begin
  298. Engine.NeedComments:=True;
  299. TestStatement(['//comment line','Doit;']);
  300. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  301. AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
  302. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  303. S:=Statement as TPasImplSimple;
  304. AssertExpression('Doit call',S.Expr,pekIdent,'Doit');
  305. AssertEquals('No DocComment','',S.DocComment);
  306. end;
  307. procedure TTestStatementParser.TestCallQualified;
  308. Var
  309. S : TPasImplSimple;
  310. B : TBinaryExpr;
  311. begin
  312. TestStatement('Unita.Doit;');
  313. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  314. AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
  315. S:=Statement as TPasImplSimple;
  316. AssertExpression('Doit call',S.Expr,pekBinary,TBinaryExpr);
  317. B:=S.Expr as TBinaryExpr;
  318. AssertExpression('Unit name',B.Left,pekIdent,'Unita');
  319. AssertExpression('Doit call',B.Right,pekIdent,'Doit');
  320. end;
  321. procedure TTestStatementParser.TestCallQualified2;
  322. Var
  323. S : TPasImplSimple;
  324. B : TBinaryExpr;
  325. begin
  326. TestStatement('Unita.ClassB.Doit;');
  327. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  328. AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
  329. S:=Statement as TPasImplSimple;
  330. AssertExpression('Doit call',S.Expr,pekBinary,TBinaryExpr);
  331. B:=S.Expr as TBinaryExpr;
  332. AssertExpression('Unit name',B.Left,pekIdent,'Unita');
  333. AssertExpression('Doit call',B.Right,pekBinary,TBinaryExpr);
  334. B:=B.Right as TBinaryExpr;
  335. AssertExpression('Unit name',B.Left,pekIdent,'ClassB');
  336. AssertExpression('Doit call',B.Right,pekIdent,'Doit');
  337. end;
  338. procedure TTestStatementParser.TestCallNoArgs;
  339. Var
  340. S : TPasImplSimple;
  341. P : TParamsExpr;
  342. begin
  343. TestStatement('Doit();');
  344. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  345. AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
  346. S:=Statement as TPasImplSimple;
  347. AssertExpression('Doit call',S.Expr,pekFuncParams,TParamsExpr);
  348. P:=S.Expr as TParamsExpr;
  349. AssertExpression('Correct function call name',P.Value,pekIdent,'Doit');
  350. AssertEquals('No params',0,Length(P.Params));
  351. end;
  352. procedure TTestStatementParser.TestCallOneArg;
  353. Var
  354. S : TPasImplSimple;
  355. P : TParamsExpr;
  356. begin
  357. TestStatement('Doit(1);');
  358. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  359. AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
  360. S:=Statement as TPasImplSimple;
  361. AssertExpression('Doit call',S.Expr,pekFuncParams,TParamsExpr);
  362. P:=S.Expr as TParamsExpr;
  363. AssertExpression('Correct function call name',P.Value,pekIdent,'Doit');
  364. AssertEquals('One param',1,Length(P.Params));
  365. AssertExpression('Parameter is constant',P.Params[0],pekNumber,'1');
  366. end;
  367. procedure TTestStatementParser.TestIf;
  368. Var
  369. I : TPasImplIfElse;
  370. begin
  371. DeclareVar('boolean');
  372. TestStatement(['if a then',';']);
  373. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  374. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  375. AssertNull('No else',i.ElseBranch);
  376. AssertNull('No if branch',I.IfBranch);
  377. end;
  378. procedure TTestStatementParser.TestIfBlock;
  379. Var
  380. I : TPasImplIfElse;
  381. begin
  382. DeclareVar('boolean');
  383. TestStatement(['if a then',' begin',' end']);
  384. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  385. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  386. AssertNull('No else',i.ElseBranch);
  387. AssertNotNull('if branch',I.IfBranch);
  388. AssertEquals('begin end block',TPasImplBeginBlock,I.ifBranch.ClassType);
  389. end;
  390. procedure TTestStatementParser.TestIfAssignment;
  391. Var
  392. I : TPasImplIfElse;
  393. begin
  394. DeclareVar('boolean');
  395. TestStatement(['if a then',' a:=False;']);
  396. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  397. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  398. AssertNull('No else',i.ElseBranch);
  399. AssertNotNull('if branch',I.IfBranch);
  400. AssertEquals('assignment statement',TPasImplAssign,I.ifBranch.ClassType);
  401. end;
  402. procedure TTestStatementParser.TestIfElse;
  403. Var
  404. I : TPasImplIfElse;
  405. begin
  406. DeclareVar('boolean');
  407. TestStatement(['if a then',' begin',' end','else',';']);
  408. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  409. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  410. AssertNull('No else',i.ElseBranch);
  411. AssertNotNull('if branch',I.IfBranch);
  412. AssertEquals('begin end block',TPasImplBeginBlock,I.ifBranch.ClassType);
  413. end;
  414. procedure TTestStatementParser.TestIfElseBlock;
  415. Var
  416. I : TPasImplIfElse;
  417. begin
  418. DeclareVar('boolean');
  419. TestStatement(['if a then',' begin',' end','else',' begin',' end']);
  420. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  421. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  422. AssertNotNull('if branch',I.IfBranch);
  423. AssertEquals('begin end block',TPasImplBeginBlock,I.ifBranch.ClassType);
  424. AssertNotNull('Else branch',i.ElseBranch);
  425. AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
  426. end;
  427. procedure TTestStatementParser.TestIfSemiColonElseError;
  428. begin
  429. DeclareVar('boolean');
  430. ExpectParserError('No semicolon before else',['if a then',' begin',' end;','else',' begin',' end']);
  431. end;
  432. procedure TTestStatementParser.TestNestedIf;
  433. Var
  434. I : TPasImplIfElse;
  435. begin
  436. DeclareVar('boolean');
  437. DeclareVar('boolean','b');
  438. TestStatement(['if a then',' if b then',' begin',' end','else',' begin',' end']);
  439. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  440. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  441. AssertNotNull('if branch',I.IfBranch);
  442. AssertNull('Else branch',i.ElseBranch);
  443. AssertEquals('if in if branch',TPasImplIfElse,I.ifBranch.ClassType);
  444. I:=I.Ifbranch as TPasImplIfElse;
  445. AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
  446. end;
  447. procedure TTestStatementParser.TestNestedIfElse;
  448. Var
  449. I : TPasImplIfElse;
  450. begin
  451. DeclareVar('boolean');
  452. TestStatement(['if a then',' if b then',' begin',' end',' else',' begin',' end','else',' begin','end']);
  453. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  454. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  455. AssertNotNull('if branch',I.IfBranch);
  456. AssertNotNull('Else branch',i.ElseBranch);
  457. AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
  458. AssertEquals('if in if branch',TPasImplIfElse,I.ifBranch.ClassType);
  459. I:=I.Ifbranch as TPasImplIfElse;
  460. AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
  461. end;
  462. procedure TTestStatementParser.TestWhile;
  463. Var
  464. W : TPasImplWhileDo;
  465. begin
  466. DeclareVar('boolean');
  467. TestStatement(['While a do ;']);
  468. W:=AssertStatement('While statement',TPasImplWhileDo) as TPasImplWhileDo;
  469. AssertExpression('While condition',W.ConditionExpr,pekIdent,'a');
  470. AssertNull('Empty body',W.Body);
  471. end;
  472. procedure TTestStatementParser.TestWhileBlock;
  473. Var
  474. W : TPasImplWhileDo;
  475. begin
  476. DeclareVar('boolean');
  477. TestStatement(['While a do',' begin',' end']);
  478. W:=AssertStatement('While statement',TPasImplWhileDo) as TPasImplWhileDo;
  479. AssertExpression('While condition',W.ConditionExpr,pekIdent,'a');
  480. AssertNotNull('Have while body',W.Body);
  481. AssertEquals('begin end block',TPasImplBeginBlock,W.Body.ClassType);
  482. AssertEquals('Empty block',0,TPasImplBeginBlock(W.Body).ELements.Count);
  483. end;
  484. procedure TTestStatementParser.TestWhileNested;
  485. Var
  486. W : TPasImplWhileDo;
  487. begin
  488. DeclareVar('boolean');
  489. DeclareVar('boolean','b');
  490. TestStatement(['While a do',' while b do',' begin',' end']);
  491. W:=AssertStatement('While statement',TPasImplWhileDo) as TPasImplWhileDo;
  492. AssertExpression('While condition',W.ConditionExpr,pekIdent,'a');
  493. AssertNotNull('Have while body',W.Body);
  494. AssertEquals('Nested while',TPasImplWhileDo,W.Body.ClassType);
  495. W:=W.Body as TPasImplWhileDo;
  496. AssertExpression('While condition',W.ConditionExpr,pekIdent,'b');
  497. AssertNotNull('Have nested while body',W.Body);
  498. AssertEquals('Nested begin end block',TPasImplBeginBlock,W.Body.ClassType);
  499. AssertEquals('Empty nested block',0,TPasImplBeginBlock(W.Body).ELements.Count);
  500. end;
  501. procedure TTestStatementParser.TestRepeat;
  502. Var
  503. R : TPasImplRepeatUntil;
  504. begin
  505. DeclareVar('boolean');
  506. TestStatement(['Repeat','Until a;']);
  507. R:=AssertStatement('Repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
  508. AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
  509. AssertEquals('Empty body',0,R.Elements.Count);
  510. end;
  511. procedure TTestStatementParser.TestRepeatBlock;
  512. Var
  513. R : TPasImplRepeatUntil;
  514. begin
  515. DeclareVar('boolean');
  516. TestStatement(['Repeat','begin','end;','Until a;']);
  517. R:=AssertStatement('repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
  518. AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
  519. AssertEquals('Have statement',1,R.Elements.Count);
  520. AssertEquals('begin end block',TPasImplBeginBlock,TObject(R.Elements[0]).ClassType);
  521. AssertEquals('Empty block',0,TPasImplBeginBlock(R.Elements[0]).ELements.Count);
  522. end;
  523. procedure TTestStatementParser.TestRepeatBlockNosemicolon;
  524. Var
  525. R : TPasImplRepeatUntil;
  526. begin
  527. DeclareVar('boolean');
  528. TestStatement(['Repeat','begin','end','Until a;']);
  529. R:=AssertStatement('repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
  530. AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
  531. AssertEquals('Have statement',1,R.Elements.Count);
  532. AssertEquals('begin end block',TPasImplBeginBlock,TObject(R.Elements[0]).ClassType);
  533. AssertEquals('Empty block',0,TPasImplBeginBlock(R.Elements[0]).ELements.Count);
  534. end;
  535. procedure TTestStatementParser.TestRepeatNested;
  536. Var
  537. R : TPasImplRepeatUntil;
  538. begin
  539. DeclareVar('boolean');
  540. DeclareVar('boolean','b');
  541. TestStatement(['Repeat','repeat','begin','end','until b','Until a;']);
  542. R:=AssertStatement('repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
  543. AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
  544. AssertEquals('Have statement',1,R.Elements.Count);
  545. AssertEquals('Nested repeat',TPasImplRepeatUntil,TObject(R.Elements[0]).ClassType);
  546. R:=TPasImplRepeatUntil(R.Elements[0]);
  547. AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'b');
  548. AssertEquals('Have statement',1,R.Elements.Count);
  549. AssertEquals('begin end block',TPasImplBeginBlock,TObject(R.Elements[0]).ClassType);
  550. AssertEquals('Empty block',0,TPasImplBeginBlock(R.Elements[0]).ELements.Count);
  551. end;
  552. procedure TTestStatementParser.TestFor;
  553. Var
  554. F : TPasImplForLoop;
  555. begin
  556. DeclareVar('integer');
  557. TestStatement(['For a:=1 to 10 do',';']);
  558. F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
  559. AssertEquals('Loop variable name','a',F.VariableName);
  560. AssertEquals('Loop type',ltNormal,F.Looptype);
  561. AssertEquals('Up loop',False,F.Down);
  562. AssertExpression('Start value',F.StartExpr,pekNumber,'1');
  563. AssertExpression('End value',F.EndExpr,pekNumber,'10');
  564. AssertNull('Empty body',F.Body);
  565. end;
  566. procedure TTestStatementParser.TestForIn;
  567. Var
  568. F : TPasImplForLoop;
  569. begin
  570. DeclareVar('integer');
  571. TestStatement(['For a in SomeSet Do',';']);
  572. F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
  573. AssertEquals('Loop variable name','a',F.VariableName);
  574. AssertEquals('Loop type',ltIn,F.Looptype);
  575. AssertEquals('In loop',False,F.Down);
  576. AssertExpression('Start value',F.StartExpr,pekIdent,'SomeSet');
  577. AssertNull('Loop type',F.EndExpr);
  578. AssertNull('Empty body',F.Body);
  579. end;
  580. procedure TTestStatementParser.TestForExpr;
  581. Var
  582. F : TPasImplForLoop;
  583. B : TBinaryExpr;
  584. begin
  585. DeclareVar('integer');
  586. TestStatement(['For a:=1+1 to 5+5 do',';']);
  587. F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
  588. AssertEquals('Loop variable name','a',F.VariableName);
  589. AssertEquals('Up loop',False,F.Down);
  590. AssertExpression('Start expression',F.StartExpr,pekBinary,TBinaryExpr);
  591. B:=F.StartExpr as TBinaryExpr;
  592. AssertExpression('Start value left',B.left,pekNumber,'1');
  593. AssertExpression('Start value right',B.right,pekNumber,'1');
  594. AssertExpression('Start expression',F.StartExpr,pekBinary,TBinaryExpr);
  595. B:=F.EndExpr as TBinaryExpr;
  596. AssertExpression('End value left',B.left,pekNumber,'5');
  597. AssertExpression('End value right',B.right,pekNumber,'5');
  598. AssertNull('Empty body',F.Body);
  599. end;
  600. procedure TTestStatementParser.TestForBlock;
  601. Var
  602. F : TPasImplForLoop;
  603. begin
  604. DeclareVar('integer');
  605. TestStatement(['For a:=1 to 10 do','begin','end']);
  606. F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
  607. AssertEquals('Loop variable name','a',F.VariableName);
  608. AssertEquals('Up loop',False,F.Down);
  609. AssertExpression('Start value',F.StartExpr,pekNumber,'1');
  610. AssertExpression('End value',F.EndExpr,pekNumber,'10');
  611. AssertNotNull('Have for body',F.Body);
  612. AssertEquals('begin end block',TPasImplBeginBlock,F.Body.ClassType);
  613. AssertEquals('Empty block',0,TPasImplBeginBlock(F.Body).ELements.Count);
  614. end;
  615. procedure TTestStatementParser.TestDowntoBlock;
  616. Var
  617. F : TPasImplForLoop;
  618. begin
  619. DeclareVar('integer');
  620. TestStatement(['For a:=10 downto 1 do','begin','end']);
  621. F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
  622. AssertEquals('Loop variable name','a',F.VariableName);
  623. AssertEquals('Down loop',True,F.Down);
  624. AssertExpression('Start value',F.StartExpr,pekNumber,'10');
  625. AssertExpression('End value',F.EndExpr,pekNumber,'1');
  626. AssertNotNull('Have for body',F.Body);
  627. AssertEquals('begin end block',TPasImplBeginBlock,F.Body.ClassType);
  628. AssertEquals('Empty block',0,TPasImplBeginBlock(F.Body).ELements.Count);
  629. end;
  630. procedure TTestStatementParser.TestForNested;
  631. Var
  632. F : TPasImplForLoop;
  633. begin
  634. DeclareVar('integer');
  635. DeclareVar('integer','b');
  636. TestStatement(['For a:=1 to 10 do','For b:=11 to 20 do','begin','end']);
  637. F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
  638. AssertEquals('Loop variable name','a',F.VariableName);
  639. AssertEquals('Up loop',False,F.Down);
  640. AssertExpression('Start value',F.StartExpr,pekNumber,'1');
  641. AssertExpression('End value',F.EndExpr,pekNumber,'10');
  642. AssertNotNull('Have while body',F.Body);
  643. AssertEquals('begin end block',TPasImplForLoop,F.Body.ClassType);
  644. F:=F.Body as TPasImplForLoop;
  645. AssertEquals('Loop variable name','b',F.VariableName);
  646. AssertEquals('Up loop',False,F.Down);
  647. AssertExpression('Start value',F.StartExpr,pekNumber,'11');
  648. AssertExpression('End value',F.EndExpr,pekNumber,'20');
  649. AssertNotNull('Have for body',F.Body);
  650. AssertEquals('begin end block',TPasImplBeginBlock,F.Body.ClassType);
  651. AssertEquals('Empty block',0,TPasImplBeginBlock(F.Body).ELements.Count);
  652. end;
  653. procedure TTestStatementParser.TestWith;
  654. Var
  655. W : TpasImplWithDo;
  656. begin
  657. DeclareVar('record X,Y : Integer; end');
  658. TestStatement(['With a do','begin','end']);
  659. W:=AssertStatement('For statement',TpasImplWithDo) as TpasImplWithDo;
  660. AssertEquals('1 expression',1,W.Expressions.Count);
  661. AssertExpression('With identifier',TPasExpr(W.Expressions[0]),pekIdent,'a');
  662. AssertNotNull('Have with body',W.Body);
  663. AssertEquals('begin end block',TPasImplBeginBlock,W.Body.ClassType);
  664. AssertEquals('Empty block',0,TPasImplBeginBlock(W.Body).ELements.Count);
  665. end;
  666. procedure TTestStatementParser.TestWithMultiple;
  667. Var
  668. W : TpasImplWithDo;
  669. begin
  670. DeclareVar('record X,Y : Integer; end');
  671. DeclareVar('record W,Z : Integer; end','b');
  672. TestStatement(['With a,b do','begin','end']);
  673. W:=AssertStatement('For statement',TpasImplWithDo) as TpasImplWithDo;
  674. AssertEquals('2 expressions',2,W.Expressions.Count);
  675. AssertExpression('With identifier 1',TPasExpr(W.Expressions[0]),pekIdent,'a');
  676. AssertExpression('With identifier 2',TPasExpr(W.Expressions[1]),pekIdent,'b');
  677. AssertNotNull('Have with body',W.Body);
  678. AssertEquals('begin end block',TPasImplBeginBlock,W.Body.ClassType);
  679. AssertEquals('Empty block',0,TPasImplBeginBlock(W.Body).ELements.Count);
  680. end;
  681. procedure TTestStatementParser.TestCaseEmpty;
  682. begin
  683. DeclareVar('integer');
  684. AddStatements(['case a of','end;']);
  685. ExpectParserError('Empty case not allowed');
  686. end;
  687. procedure TTestStatementParser.TestCaseOneInteger;
  688. Var
  689. C : TPasImplCaseOf;
  690. S : TPasImplCaseStatement;
  691. begin
  692. DeclareVar('integer');
  693. TestStatement(['case a of','1 : ;','end;']);
  694. C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
  695. AssertNotNull('Have case expression',C.CaseExpr);
  696. AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
  697. AssertNull('No else branch',C.ElseBranch);
  698. AssertEquals('One case label',1,C.Elements.Count);
  699. AssertEquals('Correct case for case label',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
  700. S:=TPasImplCaseStatement(C.Elements[0]);
  701. AssertEquals('1 expression for case',1,S.Expressions.Count);
  702. AssertExpression('With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
  703. AssertEquals('Empty case label statement',0,S.Elements.Count);
  704. AssertNull('Empty case label statement',S.Body);
  705. end;
  706. procedure TTestStatementParser.TestCaseTwoIntegers;
  707. Var
  708. C : TPasImplCaseOf;
  709. S : TPasImplCaseStatement;
  710. begin
  711. DeclareVar('integer');
  712. TestStatement(['case a of','1,2 : ;','end;']);
  713. C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
  714. AssertNotNull('Have case expression',C.CaseExpr);
  715. AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
  716. AssertNull('No else branch',C.ElseBranch);
  717. AssertEquals('One case label',1,C.Elements.Count);
  718. AssertEquals('Correct case for case label',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
  719. S:=TPasImplCaseStatement(C.Elements[0]);
  720. AssertEquals('2 expressions for case',2,S.Expressions.Count);
  721. AssertExpression('With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
  722. AssertExpression('With identifier 2',TPasExpr(S.Expressions[1]),pekNumber,'2');
  723. AssertEquals('Empty case label statement',0,S.Elements.Count);
  724. AssertNull('Empty case label statement',S.Body);
  725. end;
  726. procedure TTestStatementParser.TestCaseRange;
  727. Var
  728. C : TPasImplCaseOf;
  729. S : TPasImplCaseStatement;
  730. begin
  731. DeclareVar('integer');
  732. TestStatement(['case a of','1..3 : ;','end;']);
  733. C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
  734. AssertNotNull('Have case expression',C.CaseExpr);
  735. AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
  736. AssertNull('No else branch',C.ElseBranch);
  737. AssertEquals('One case label',1,C.Elements.Count);
  738. AssertEquals('Correct case for case label',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
  739. S:=TPasImplCaseStatement(C.Elements[0]);
  740. AssertEquals('1 expression for case',1,S.Expressions.Count);
  741. AssertExpression('With identifier 1',TPasExpr(S.Expressions[0]),pekRange,TBinaryExpr);
  742. AssertEquals('Empty case label statement',0,S.Elements.Count);
  743. AssertNull('Empty case label statement',S.Body);
  744. end;
  745. procedure TTestStatementParser.TestCaseRangeSeparate;
  746. Var
  747. C : TPasImplCaseOf;
  748. S : TPasImplCaseStatement;
  749. begin
  750. DeclareVar('integer');
  751. TestStatement(['case a of','1..3,5 : ;','end;']);
  752. C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
  753. AssertNotNull('Have case expression',C.CaseExpr);
  754. AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
  755. AssertNull('No else branch',C.ElseBranch);
  756. AssertEquals('One case label',1,C.Elements.Count);
  757. AssertEquals('Correct case for case label',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
  758. S:=TPasImplCaseStatement(C.Elements[0]);
  759. AssertEquals('2 expressions for case',2,S.Expressions.Count);
  760. AssertExpression('With identifier 1',TPasExpr(S.Expressions[0]),pekRange,TBinaryExpr);
  761. AssertExpression('With identifier 2',TPasExpr(S.Expressions[1]),pekNumber,'5');
  762. AssertEquals('Empty case label statement',0,S.Elements.Count);
  763. AssertNull('Empty case label statement',S.Body);
  764. end;
  765. procedure TTestStatementParser.TestCase2Cases;
  766. Var
  767. C : TPasImplCaseOf;
  768. S : TPasImplCaseStatement;
  769. begin
  770. DeclareVar('integer');
  771. TestStatement(['case a of','1 : ;','2 : ;','end;']);
  772. C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
  773. AssertNotNull('Have case expression',C.CaseExpr);
  774. AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
  775. AssertNull('No else branch',C.ElseBranch);
  776. AssertEquals('Two case labels',2,C.Elements.Count);
  777. AssertEquals('Correct case for case label 1',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
  778. S:=TPasImplCaseStatement(C.Elements[0]);
  779. AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
  780. AssertExpression('Case 1 With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
  781. AssertEquals('Empty case label statement 1',0,S.Elements.Count);
  782. AssertNull('Empty case label statement 1',S.Body);
  783. // Two
  784. AssertEquals('Correct case for case label 2',TPasImplCaseStatement,TPasElement(C.Elements[1]).ClassType);
  785. S:=TPasImplCaseStatement(C.Elements[1]);
  786. AssertEquals('2 expressions for case 2',1,S.Expressions.Count);
  787. AssertExpression('Case 2 With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'2');
  788. AssertEquals('Empty case label statement 2',0,S.Elements.Count);
  789. AssertNull('Empty case label statement 2',S.Body);
  790. end;
  791. procedure TTestStatementParser.TestCaseBlock;
  792. Var
  793. C : TPasImplCaseOf;
  794. S : TPasImplCaseStatement;
  795. B : TPasImplbeginBlock;
  796. begin
  797. DeclareVar('integer');
  798. TestStatement(['case a of','1 : begin end;','end;']);
  799. C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
  800. AssertNotNull('Have case expression',C.CaseExpr);
  801. AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
  802. AssertNull('No else branch',C.ElseBranch);
  803. AssertEquals('Two case labels',1,C.Elements.Count);
  804. AssertEquals('Correct case for case label 1',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
  805. S:=TPasImplCaseStatement(C.Elements[0]);
  806. AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
  807. AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
  808. AssertEquals('1 case label statement',1,S.Elements.Count);
  809. AssertEquals('Correct case for case label 1',TPasImplbeginBlock,TPasElement(S.Elements[0]).ClassType);
  810. B:=TPasImplbeginBlock(S.Elements[0]);
  811. AssertEquals('0 statements in block',0,B.Elements.Count);
  812. end;
  813. procedure TTestStatementParser.TestCaseElseBlockEmpty;
  814. Var
  815. C : TPasImplCaseOf;
  816. S : TPasImplCaseStatement;
  817. B : TPasImplbeginBlock;
  818. begin
  819. DeclareVar('integer');
  820. TestStatement(['case a of','1 : begin end;','else',' end;']);
  821. C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
  822. AssertNotNull('Have case expression',C.CaseExpr);
  823. AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
  824. AssertEquals('Two case labels',2,C.Elements.Count);
  825. AssertEquals('Correct case for case label 1',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
  826. S:=TPasImplCaseStatement(C.Elements[0]);
  827. AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
  828. AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
  829. AssertEquals('1 case label statement',1,S.Elements.Count);
  830. AssertEquals('Correct case for case label 1',TPasImplbeginBlock,TPasElement(S.Elements[0]).ClassType);
  831. B:=TPasImplbeginBlock(S.Elements[0]);
  832. AssertEquals('0 statements in block',0,B.Elements.Count);
  833. AssertNotNull('Have else branch',C.ElseBranch);
  834. AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
  835. AssertEquals('Zero statements ',0,TPasImplCaseElse(C.ElseBranch).Elements.Count);
  836. end;
  837. procedure TTestStatementParser.TestCaseElseBlockAssignment;
  838. Var
  839. C : TPasImplCaseOf;
  840. S : TPasImplCaseStatement;
  841. B : TPasImplbeginBlock;
  842. begin
  843. DeclareVar('integer');
  844. TestStatement(['case a of','1 : begin end;','else','a:=1',' end;']);
  845. C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
  846. AssertNotNull('Have case expression',C.CaseExpr);
  847. AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
  848. AssertEquals('Two case labels',2,C.Elements.Count);
  849. AssertEquals('Correct case for case label 1',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
  850. S:=TPasImplCaseStatement(C.Elements[0]);
  851. AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
  852. AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
  853. AssertEquals('1 case label statement',1,S.Elements.Count);
  854. AssertEquals('Correct case for case label 1',TPasImplbeginBlock,TPasElement(S.Elements[0]).ClassType);
  855. B:=TPasImplbeginBlock(S.Elements[0]);
  856. AssertEquals('0 statements in block',0,B.Elements.Count);
  857. AssertNotNull('Have else branch',C.ElseBranch);
  858. AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
  859. AssertEquals('1 statement in else branch ',1,TPasImplCaseElse(C.ElseBranch).Elements.Count);
  860. end;
  861. procedure TTestStatementParser.TestCaseElseBlock2Assignments;
  862. Var
  863. C : TPasImplCaseOf;
  864. S : TPasImplCaseStatement;
  865. B : TPasImplbeginBlock;
  866. begin
  867. DeclareVar('integer');
  868. TestStatement(['case a of','1 : begin end;','else','a:=1;','a:=32;',' end;']);
  869. C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
  870. AssertNotNull('Have case expression',C.CaseExpr);
  871. AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
  872. AssertEquals('Two case labels',2,C.Elements.Count);
  873. AssertEquals('Correct case for case label 1',TPasImplCaseStatement,TPasElement(C.Elements[0]).ClassType);
  874. S:=TPasImplCaseStatement(C.Elements[0]);
  875. AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
  876. AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
  877. AssertEquals('1 case label statement',1,S.Elements.Count);
  878. AssertEquals('Correct case for case label 1',TPasImplbeginBlock,TPasElement(S.Elements[0]).ClassType);
  879. B:=TPasImplbeginBlock(S.Elements[0]);
  880. AssertEquals('0 statements in block',0,B.Elements.Count);
  881. AssertNotNull('Have else branch',C.ElseBranch);
  882. AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
  883. AssertEquals('2 statements in else branch ',2,TPasImplCaseElse(C.ElseBranch).Elements.Count);
  884. end;
  885. procedure TTestStatementParser.TestCaseIfCaseElse;
  886. Var
  887. C : TPasImplCaseOf;
  888. begin
  889. DeclareVar('integer');
  890. DeclareVar('boolean','b');
  891. TestStatement(['case a of','1 : if b then',' begin end;','else',' end;']);
  892. C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
  893. AssertNotNull('Have case expression',C.CaseExpr);
  894. AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
  895. AssertEquals('Two case labels',2,C.Elements.Count);
  896. AssertNotNull('Have else branch',C.ElseBranch);
  897. AssertEquals('Correct else branch class',TPasImplCaseElse,C.ElseBranch.ClassType);
  898. AssertEquals('0 statement in else branch ',0,TPasImplCaseElse(C.ElseBranch).Elements.Count);
  899. end;
  900. procedure TTestStatementParser.TestCaseIfElse;
  901. Var
  902. C : TPasImplCaseOf;
  903. S : TPasImplCaseStatement;
  904. begin
  905. DeclareVar('integer');
  906. DeclareVar('boolean','b');
  907. TestStatement(['case a of','1 : if b then',' begin end','else','begin','end',' end;']);
  908. C:=AssertStatement('Case statement',TpasImplCaseOf) as TpasImplCaseOf;
  909. AssertNotNull('Have case expression',C.CaseExpr);
  910. AssertExpression('Case expression',C.CaseExpr,pekIdent,'a');
  911. AssertEquals('Two case labels',1,C.Elements.Count);
  912. AssertNull('Have no else branch',C.ElseBranch);
  913. S:=TPasImplCaseStatement(C.Elements[0]);
  914. AssertEquals('2 expressions for case 1',1,S.Expressions.Count);
  915. AssertExpression('Case With identifier 1',TPasExpr(S.Expressions[0]),pekNumber,'1');
  916. AssertEquals('1 case label statement',1,S.Elements.Count);
  917. AssertEquals('If statement in case label 1',TPasImplIfElse,TPasElement(S.Elements[0]).ClassType);
  918. AssertNotNull('If statement has else block',TPasImplIfElse(S.Elements[0]).ElseBranch);
  919. end;
  920. procedure TTestStatementParser.TestRaise;
  921. Var
  922. R : TPasImplRaise;
  923. begin
  924. DeclareVar('Exception');
  925. TestStatement('Raise A;');
  926. R:=AssertStatement('Raise statement',TPasImplRaise) as TPasImplRaise;
  927. AssertEquals(0,R.Elements.Count);
  928. AssertNotNull(R.ExceptObject);
  929. AssertNull(R.ExceptAddr);
  930. AssertExpression('Expression object',R.ExceptObject,pekIdent,'A');
  931. end;
  932. procedure TTestStatementParser.TestRaiseEmpty;
  933. Var
  934. R : TPasImplRaise;
  935. begin
  936. TestStatement('Raise;');
  937. R:=AssertStatement('Raise statement',TPasImplRaise) as TPasImplRaise;
  938. AssertEquals(0,R.Elements.Count);
  939. AssertNull(R.ExceptObject);
  940. AssertNull(R.ExceptAddr);
  941. end;
  942. procedure TTestStatementParser.TestRaiseAt;
  943. Var
  944. R : TPasImplRaise;
  945. begin
  946. DeclareVar('Exception');
  947. DeclareVar('Pointer','B');
  948. TestStatement('Raise A at B;');
  949. R:=AssertStatement('Raise statement',TPasImplRaise) as TPasImplRaise;
  950. AssertEquals(0,R.Elements.Count);
  951. AssertNotNull(R.ExceptObject);
  952. AssertNotNull(R.ExceptAddr);
  953. AssertExpression('Expression object',R.ExceptAddr,pekIdent,'B');
  954. end;
  955. procedure TTestStatementParser.TestTryFinally;
  956. Var
  957. T : TPasImplTry;
  958. S : TPasImplSimple;
  959. F : TPasImplTryFinally;
  960. begin
  961. TestStatement(['Try',' DoSomething;','finally',' DoSomethingElse','end']);
  962. T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
  963. AssertEquals(1,T.Elements.Count);
  964. AssertNotNull(T.FinallyExcept);
  965. AssertNull(T.ElseBranch);
  966. AssertNotNull(T.Elements[0]);
  967. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  968. S:=TPasImplSimple(T.Elements[0]);
  969. AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
  970. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  971. AssertEquals('Finally statement',TPasImplTryFinally,T.FinallyExcept.ClassType);
  972. F:=TPasImplTryFinally(T.FinallyExcept);
  973. AssertEquals(1,F.Elements.Count);
  974. AssertEquals('Simple statement',TPasImplSimple,TPasElement(F.Elements[0]).ClassType);
  975. S:=TPasImplSimple(F.Elements[0]);
  976. AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
  977. end;
  978. procedure TTestStatementParser.TestTryFinallyEmpty;
  979. Var
  980. T : TPasImplTry;
  981. F : TPasImplTryFinally;
  982. begin
  983. TestStatement(['Try','finally','end;']);
  984. T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
  985. AssertEquals(0,T.Elements.Count);
  986. AssertNotNull(T.FinallyExcept);
  987. AssertNull(T.ElseBranch);
  988. AssertEquals('Finally statement',TPasImplTryFinally,T.FinallyExcept.ClassType);
  989. F:=TPasImplTryFinally(T.FinallyExcept);
  990. AssertEquals(0,F.Elements.Count);
  991. end;
  992. procedure TTestStatementParser.TestTryFinallyNested;
  993. Var
  994. T : TPasImplTry;
  995. S : TPasImplSimple;
  996. F : TPasImplTryFinally;
  997. begin
  998. TestStatement(['Try',' DoSomething1;',' Try',' DoSomething2;',' finally',' DoSomethingElse2',' end;','Finally',' DoSomethingElse1','end']);
  999. T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
  1000. AssertEquals(2,T.Elements.Count);
  1001. AssertNotNull(T.FinallyExcept);
  1002. AssertNull(T.ElseBranch);
  1003. AssertNotNull(T.Elements[0]);
  1004. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1005. S:=TPasImplSimple(T.Elements[0]);
  1006. AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething1');
  1007. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1008. AssertEquals('Finally statement',TPasImplTryFinally,T.FinallyExcept.ClassType);
  1009. F:=TPasImplTryFinally(T.FinallyExcept);
  1010. AssertEquals(1,F.Elements.Count);
  1011. AssertEquals('Simple statement',TPasImplSimple,TPasElement(F.Elements[0]).ClassType);
  1012. S:=TPasImplSimple(F.Elements[0]);
  1013. AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse1');
  1014. // inner statement
  1015. AssertNotNull(T.Elements[1]);
  1016. AssertEquals('Nested try statement',TPasImplTry,TPasElement(T.Elements[1]).ClassType);
  1017. T:=TPasImplTry(T.Elements[1]);
  1018. AssertEquals(1,T.Elements.Count);
  1019. AssertNotNull(T.FinallyExcept);
  1020. AssertNull(T.ElseBranch);
  1021. AssertNotNull(T.Elements[0]);
  1022. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1023. S:=TPasImplSimple(T.Elements[0]);
  1024. AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething2');
  1025. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1026. AssertEquals('Finally statement',TPasImplTryFinally,T.FinallyExcept.ClassType);
  1027. F:=TPasImplTryFinally(T.FinallyExcept);
  1028. AssertEquals(1,F.Elements.Count);
  1029. AssertEquals('Simple statement',TPasImplSimple,TPasElement(F.Elements[0]).ClassType);
  1030. S:=TPasImplSimple(F.Elements[0]);
  1031. AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse2');
  1032. end;
  1033. procedure TTestStatementParser.TestTryExcept;
  1034. Var
  1035. T : TPasImplTry;
  1036. S : TPasImplSimple;
  1037. E : TPasImplTryExcept;
  1038. begin
  1039. TestStatement(['Try',' DoSomething;','except',' DoSomethingElse','end']);
  1040. T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
  1041. AssertEquals(1,T.Elements.Count);
  1042. AssertNotNull(T.FinallyExcept);
  1043. AssertNull(T.ElseBranch);
  1044. AssertNotNull(T.Elements[0]);
  1045. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1046. S:=TPasImplSimple(T.Elements[0]);
  1047. AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
  1048. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1049. AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
  1050. E:=TPasImplTryExcept(T.FinallyExcept);
  1051. AssertEquals(1,E.Elements.Count);
  1052. AssertEquals('Simple statement',TPasImplSimple,TPasElement(E.Elements[0]).ClassType);
  1053. S:=TPasImplSimple(E.Elements[0]);
  1054. AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
  1055. end;
  1056. procedure TTestStatementParser.TestTryExceptNested;
  1057. Var
  1058. T : TPasImplTry;
  1059. S : TPasImplSimple;
  1060. E : TPasImplTryExcept;
  1061. begin
  1062. TestStatement(['Try',' DoSomething1;',' try',' DoSomething2;',' except',' DoSomethingElse2',' end','except',' DoSomethingElse1','end']);
  1063. T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
  1064. AssertEquals(2,T.Elements.Count);
  1065. AssertNotNull(T.FinallyExcept);
  1066. AssertNull(T.ElseBranch);
  1067. AssertNotNull(T.Elements[0]);
  1068. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1069. S:=TPasImplSimple(T.Elements[0]);
  1070. AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething1');
  1071. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1072. AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
  1073. E:=TPasImplTryExcept(T.FinallyExcept);
  1074. AssertEquals(1,E.Elements.Count);
  1075. AssertEquals('Simple statement',TPasImplSimple,TPasElement(E.Elements[0]).ClassType);
  1076. S:=TPasImplSimple(E.Elements[0]);
  1077. AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse1');
  1078. AssertNotNull(T.Elements[1]);
  1079. AssertEquals('Simple statement',TPasImplTry,TPasElement(T.Elements[1]).ClassType);
  1080. T:=TPasImplTry(T.Elements[1]);
  1081. AssertEquals(1,T.Elements.Count);
  1082. AssertNotNull(T.FinallyExcept);
  1083. AssertNull(T.ElseBranch);
  1084. AssertNotNull(T.Elements[0]);
  1085. AssertEquals('Simple statement 2',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1086. S:=TPasImplSimple(T.Elements[0]);
  1087. AssertExpression('DoSomething2 call ',S.Expr,pekIdent,'DoSomething2');
  1088. AssertEquals('Simple statement2',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1089. AssertEquals('Except statement2',TPasImplTryExcept,T.FinallyExcept.ClassType);
  1090. E:=TPasImplTryExcept(T.FinallyExcept);
  1091. AssertEquals(1,E.Elements.Count);
  1092. AssertEquals('Simple statement2',TPasImplSimple,TPasElement(E.Elements[0]).ClassType);
  1093. S:=TPasImplSimple(E.Elements[0]);
  1094. AssertExpression('DoSomethingElse2 call',S.Expr,pekIdent,'DoSomethingElse2');
  1095. end;
  1096. procedure TTestStatementParser.TestTryExceptEmpty;
  1097. Var
  1098. T : TPasImplTry;
  1099. E : TPasImplTryExcept;
  1100. begin
  1101. TestStatement(['Try','except','end;']);
  1102. T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
  1103. AssertEquals(0,T.Elements.Count);
  1104. AssertNotNull(T.FinallyExcept);
  1105. AssertNull(T.ElseBranch);
  1106. AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
  1107. E:=TPasImplTryExcept(T.FinallyExcept);
  1108. AssertEquals(0,E.Elements.Count);
  1109. end;
  1110. procedure TTestStatementParser.TestTryExceptOn;
  1111. Var
  1112. T : TPasImplTry;
  1113. S : TPasImplSimple;
  1114. E : TPasImplTryExcept;
  1115. O : TPasImplExceptOn;
  1116. begin
  1117. TestStatement(['Try',' DoSomething;','except','On E : Exception do','DoSomethingElse;','end']);
  1118. T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
  1119. AssertEquals(1,T.Elements.Count);
  1120. AssertNotNull(T.FinallyExcept);
  1121. AssertNull(T.ElseBranch);
  1122. AssertNotNull(T.Elements[0]);
  1123. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1124. S:=TPasImplSimple(T.Elements[0]);
  1125. AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
  1126. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1127. AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
  1128. E:=TPasImplTryExcept(T.FinallyExcept);
  1129. AssertEquals(1,E.Elements.Count);
  1130. AssertEquals('Except on handler',TPasImplExceptOn,TPasElement(E.Elements[0]).ClassType);
  1131. O:=TPasImplExceptOn(E.Elements[0]);
  1132. AssertEquals(1,O.Elements.Count);
  1133. AssertEquals('Simple statement',TPasImplSimple,TPasElement(O.Elements[0]).ClassType);
  1134. AssertExpression('Exception Variable name',O.VarExpr,pekIdent,'E');
  1135. AssertExpression('Exception Type name',O.TypeExpr,pekIdent,'Exception');
  1136. S:=TPasImplSimple(O.Elements[0]);
  1137. AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
  1138. // AssertEquals('Variable name',
  1139. end;
  1140. procedure TTestStatementParser.TestTryExceptOn2;
  1141. Var
  1142. T : TPasImplTry;
  1143. S : TPasImplSimple;
  1144. E : TPasImplTryExcept;
  1145. O : TPasImplExceptOn;
  1146. begin
  1147. TestStatement(['Try',' DoSomething;','except',
  1148. 'On E : Exception do','DoSomethingElse;',
  1149. 'On Y : Exception2 do','DoSomethingElse2;',
  1150. 'end']);
  1151. T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
  1152. AssertEquals(1,T.Elements.Count);
  1153. AssertNotNull(T.FinallyExcept);
  1154. AssertNull(T.ElseBranch);
  1155. AssertNotNull(T.Elements[0]);
  1156. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1157. S:=TPasImplSimple(T.Elements[0]);
  1158. AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
  1159. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1160. AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
  1161. E:=TPasImplTryExcept(T.FinallyExcept);
  1162. AssertEquals(2,E.Elements.Count);
  1163. // Exception handler 1
  1164. AssertEquals('Except on handler',TPasImplExceptOn,TPasElement(E.Elements[0]).ClassType);
  1165. O:=TPasImplExceptOn(E.Elements[0]);
  1166. AssertEquals(1,O.Elements.Count);
  1167. AssertEquals('Simple statement',TPasImplSimple,TPasElement(O.Elements[0]).ClassType);
  1168. AssertExpression('Exception Variable name',O.VarExpr,pekIdent,'E');
  1169. AssertExpression('Exception Type name',O.TypeExpr,pekIdent,'Exception');
  1170. S:=TPasImplSimple(O.Elements[0]);
  1171. AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
  1172. // Exception handler 2
  1173. AssertEquals('Except on handler',TPasImplExceptOn,TPasElement(E.Elements[1]).ClassType);
  1174. O:=TPasImplExceptOn(E.Elements[1]);
  1175. AssertEquals(1,O.Elements.Count);
  1176. AssertEquals('Simple statement',TPasImplSimple,TPasElement(O.Elements[0]).ClassType);
  1177. AssertExpression('Exception Variable name',O.VarExpr,pekIdent,'Y');
  1178. AssertExpression('Exception Type name',O.TypeExpr,pekIdent,'Exception2');
  1179. S:=TPasImplSimple(O.Elements[0]);
  1180. AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse2');
  1181. end;
  1182. procedure TTestStatementParser.TestTryExceptOnElse;
  1183. Var
  1184. T : TPasImplTry;
  1185. S : TPasImplSimple;
  1186. E : TPasImplTryExcept;
  1187. O : TPasImplExceptOn;
  1188. EE : TPasImplTryExceptElse;
  1189. I : TPasImplIfElse;
  1190. begin
  1191. DeclareVar('Boolean','b');
  1192. // Check that Else belongs to Except, not to IF
  1193. TestStatement(['Try',' DoSomething;','except','On E : Exception do','if b then','DoSomethingElse;','else','DoSomethingMore;','end']);
  1194. T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
  1195. AssertEquals(1,T.Elements.Count);
  1196. AssertNotNull(T.FinallyExcept);
  1197. AssertNotNull(T.ElseBranch);
  1198. AssertNotNull(T.Elements[0]);
  1199. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1200. S:=TPasImplSimple(T.Elements[0]);
  1201. AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
  1202. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1203. AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
  1204. E:=TPasImplTryExcept(T.FinallyExcept);
  1205. AssertEquals(1,E.Elements.Count);
  1206. AssertEquals('Except on handler',TPasImplExceptOn,TPasElement(E.Elements[0]).ClassType);
  1207. O:=TPasImplExceptOn(E.Elements[0]);
  1208. AssertExpression('Exception Variable name',O.VarExpr,pekIdent,'E');
  1209. AssertExpression('Exception Type name',O.TypeExpr,pekIdent,'Exception');
  1210. AssertEquals(1,O.Elements.Count);
  1211. AssertEquals('Simple statement',TPasImplIfElse,TPasElement(O.Elements[0]).ClassType);
  1212. I:=TPasImplIfElse(O.Elements[0]);
  1213. AssertEquals(1,I.Elements.Count);
  1214. AssertNull('No else barcnh for if',I.ElseBranch);
  1215. AssertEquals('Simple statement',TPasImplSimple,TPasElement(I.Elements[0]).ClassType);
  1216. S:=TPasImplSimple(I.Elements[0]);
  1217. AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
  1218. AssertEquals('Except Else statement',TPasImplTryExceptElse,T.ElseBranch.ClassType);
  1219. EE:=TPasImplTryExceptElse(T.ElseBranch);
  1220. AssertEquals(1,EE.Elements.Count);
  1221. AssertNotNull(EE.Elements[0]);
  1222. AssertEquals('Simple statement',TPasImplSimple,TPasElement(EE.Elements[0]).ClassType);
  1223. S:=TPasImplSimple(EE.Elements[0]);
  1224. AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomethingMore');
  1225. end;
  1226. procedure TTestStatementParser.TestTryExceptOnIfElse;
  1227. Var
  1228. T : TPasImplTry;
  1229. S : TPasImplSimple;
  1230. E : TPasImplTryExcept;
  1231. O : TPasImplExceptOn;
  1232. EE : TPasImplTryExceptElse;
  1233. begin
  1234. TestStatement(['Try',' DoSomething;','except','On E : Exception do','DoSomethingElse;','else','DoSomethingMore;','end']);
  1235. T:=AssertStatement('Try statement',TPasImplTry) as TPasImplTry;
  1236. AssertEquals(1,T.Elements.Count);
  1237. AssertNotNull(T.FinallyExcept);
  1238. AssertNotNull(T.ElseBranch);
  1239. AssertNotNull(T.Elements[0]);
  1240. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1241. S:=TPasImplSimple(T.Elements[0]);
  1242. AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomething');
  1243. AssertEquals('Simple statement',TPasImplSimple,TPasElement(T.Elements[0]).ClassType);
  1244. AssertEquals('Except statement',TPasImplTryExcept,T.FinallyExcept.ClassType);
  1245. E:=TPasImplTryExcept(T.FinallyExcept);
  1246. AssertEquals(1,E.Elements.Count);
  1247. AssertEquals('Except on handler',TPasImplExceptOn,TPasElement(E.Elements[0]).ClassType);
  1248. O:=TPasImplExceptOn(E.Elements[0]);
  1249. AssertExpression('Exception Variable name',O.VarExpr,pekIdent,'E');
  1250. AssertExpression('Exception Type name',O.TypeExpr,pekIdent,'Exception');
  1251. AssertEquals(1,O.Elements.Count);
  1252. AssertEquals('Simple statement',TPasImplSimple,TPasElement(O.Elements[0]).ClassType);
  1253. S:=TPasImplSimple(O.Elements[0]);
  1254. AssertExpression('DoSomethingElse call',S.Expr,pekIdent,'DoSomethingElse');
  1255. AssertEquals('Except Else statement',TPasImplTryExceptElse,T.ElseBranch.ClassType);
  1256. EE:=TPasImplTryExceptElse(T.ElseBranch);
  1257. AssertEquals(1,EE.Elements.Count);
  1258. AssertNotNull(EE.Elements[0]);
  1259. AssertEquals('Simple statement',TPasImplSimple,TPasElement(EE.Elements[0]).ClassType);
  1260. S:=TPasImplSimple(EE.Elements[0]);
  1261. AssertExpression('DoSomething call',S.Expr,pekIdent,'DoSomethingMore');
  1262. end;
  1263. procedure TTestStatementParser.TestAsm;
  1264. Var
  1265. T : TPasImplAsmStatement;
  1266. begin
  1267. TestStatement(['asm',' mov eax,1','end;']);
  1268. T:=AssertStatement('Asm statement',TPasImplAsmStatement) as TPasImplAsmStatement;
  1269. AssertEquals('Asm tokens',4,T.Tokens.Count);
  1270. AssertEquals('token 1 ','mov',T.Tokens[0]);
  1271. AssertEquals('token 2 ','eax',T.Tokens[1]);
  1272. AssertEquals('token 3 ',',',T.Tokens[2]);
  1273. AssertEquals('token 4 ','1',T.Tokens[3]);
  1274. end;
  1275. initialization
  1276. RegisterTests([TTestStatementParser]);
  1277. end.