tcstatements.pas 53 KB

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