tcstatements.pas 54 KB

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