tcstatements.pas 54 KB

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