tcstatements.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. unit tcstatements;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, testutils, 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 TestAssignment;
  30. Procedure TestCall;
  31. Procedure TestCallQualified;
  32. Procedure TestCallQualified2;
  33. Procedure TestCallNoArgs;
  34. Procedure TestCallOneArg;
  35. Procedure TestIf;
  36. Procedure TestIfBlock;
  37. Procedure TestIfAssignment;
  38. Procedure TestIfElse;
  39. Procedure TestIfElseBlock;
  40. Procedure TestIfSemiColonElseError;
  41. Procedure TestNestedIf;
  42. Procedure TestNestedIfElse;
  43. Procedure TestWhile;
  44. Procedure TestWhileBlock;
  45. Procedure TestWhileNested;
  46. Procedure TestRepeat;
  47. Procedure TestRepeatBlock;
  48. procedure TestRepeatBlockNosemicolon;
  49. Procedure TestRepeatNested;
  50. Procedure TestFor;
  51. Procedure TestForExpr;
  52. Procedure TestForBlock;
  53. procedure TestDowntoBlock;
  54. Procedure TestForNested;
  55. end;
  56. implementation
  57. { TTestStatementParser }
  58. procedure TTestStatementParser.SetUp;
  59. begin
  60. inherited SetUp;
  61. FVariables:=TStringList.Create;
  62. end;
  63. procedure TTestStatementParser.TearDown;
  64. begin
  65. FreeAndNil(FVariables);
  66. inherited TearDown;
  67. end;
  68. procedure TTestStatementParser.AddStatements(ASource: array of string);
  69. Var
  70. I :Integer;
  71. begin
  72. StartProgram('afile');
  73. if FVariables.Count>0 then
  74. begin
  75. Add('Var');
  76. For I:=0 to FVariables.Count-1 do
  77. Add(' '+Fvariables[I]);
  78. end;
  79. Add('begin');
  80. For I:=Low(ASource) to High(ASource) do
  81. Add(' '+ASource[i]);
  82. end;
  83. procedure TTestStatementParser.DeclareVar(const AVarType: String;
  84. const AVarName: String);
  85. begin
  86. FVariables.Add(AVarName+' : '+AVarType+';');
  87. end;
  88. function TTestStatementParser.TestStatement(ASource: string): TPasImplElement;
  89. begin
  90. Result:=TestStatement([ASource]);
  91. end;
  92. function TTestStatementParser.TestStatement(ASource: array of string): TPasImplElement;
  93. Var
  94. i : Integer;
  95. begin
  96. FStatement:=Nil;
  97. AddStatements(ASource);
  98. ParseModule;
  99. AssertEquals('Have program',TPasProgram,Module.ClassType);
  100. AssertNotNull('Have program section',PasProgram.ProgramSection);
  101. AssertNotNull('Have program section',PasProgram.InitializationSection);
  102. if (PasProgram.InitializationSection.Elements.Count>0) then
  103. if TObject(PasProgram.InitializationSection.Elements[0]) is TPasImplBlock then
  104. FStatement:=TPasImplBlock(PasProgram.InitializationSection.Elements[0]);
  105. end;
  106. procedure TTestStatementParser.ExpectParserError(Const Msg : string);
  107. begin
  108. AssertException(Msg,EParserError,@ParseModule);
  109. end;
  110. procedure TTestStatementParser.ExpectParserError(const Msg: string;
  111. ASource: array of string);
  112. begin
  113. AddStatements(ASource);
  114. ExpectParserError(Msg);
  115. end;
  116. function TTestStatementParser.AssertStatement(Msg: String; AClass: TClass;
  117. AIndex: Integer): TPasImplBlock;
  118. begin
  119. if not (AIndex<PasProgram.InitializationSection.Elements.Count) then
  120. Fail(Msg+': No such statement : '+intTostr(AIndex));
  121. AssertNotNull(Msg+' Have statement',PasProgram.InitializationSection.Elements[AIndex]);
  122. AssertEquals(Msg+' statement class',AClass,TObject(PasProgram.InitializationSection.Elements[AIndex]).ClassType);
  123. Result:=TObject(PasProgram.InitializationSection.Elements[AIndex]) as TPasImplBlock;
  124. end;
  125. procedure TTestStatementParser.TestEmpty;
  126. begin
  127. //TestStatement(';');
  128. TestStatement('');
  129. AssertEquals('No statements',0,PasProgram.InitializationSection.Elements.Count);
  130. end;
  131. procedure TTestStatementParser.TestEmptyStatement;
  132. begin
  133. TestStatement(';');
  134. AssertEquals('0 statement',0,PasProgram.InitializationSection.Elements.Count);
  135. end;
  136. procedure TTestStatementParser.TestEmptyStatements;
  137. begin
  138. TestStatement(';;');
  139. AssertEquals('0 statement',0,PasProgram.InitializationSection.Elements.Count);
  140. end;
  141. procedure TTestStatementParser.TestBlock;
  142. Var
  143. B : TPasImplBeginBlock;
  144. begin
  145. TestStatement(['begin','end']);
  146. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  147. AssertNotNull('Statement assigned',PasProgram.InitializationSection.Elements[0]);
  148. AssertEquals('Block statement',TPasImplBeginBlock,Statement.ClassType);
  149. B:= Statement as TPasImplBeginBlock;
  150. AssertEquals('Empty block',0,B.Elements.Count);
  151. end;
  152. procedure TTestStatementParser.TestAssignment;
  153. Var
  154. A : TPasImplAssign;
  155. begin
  156. DeclareVar('integer');
  157. TestStatement(['a:=1;']);
  158. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  159. AssertEquals('Assignment statement',TPasImplAssign,Statement.ClassType);
  160. A:=Statement as TPasImplAssign;
  161. AssertExpression('Right side is constant',A.Right,pekNumber,'1');
  162. AssertExpression('Left side is variable',A.Left,pekIdent,'a');
  163. end;
  164. procedure TTestStatementParser.TestCall;
  165. Var
  166. S : TPasImplSimple;
  167. begin
  168. TestStatement('Doit;');
  169. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  170. AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
  171. S:=Statement as TPasImplSimple;
  172. AssertExpression('Doit call',S.Expr,pekIdent,'Doit');
  173. end;
  174. procedure TTestStatementParser.TestCallQualified;
  175. Var
  176. S : TPasImplSimple;
  177. B : TBinaryExpr;
  178. begin
  179. TestStatement('Unita.Doit;');
  180. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  181. AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
  182. S:=Statement as TPasImplSimple;
  183. AssertExpression('Doit call',S.Expr,pekBinary,TBinaryExpr);
  184. B:=S.Expr as TBinaryExpr;
  185. AssertExpression('Unit name',B.Left,pekIdent,'Unita');
  186. AssertExpression('Doit call',B.Right,pekIdent,'Doit');
  187. end;
  188. procedure TTestStatementParser.TestCallQualified2;
  189. Var
  190. S : TPasImplSimple;
  191. B : TBinaryExpr;
  192. begin
  193. TestStatement('Unita.ClassB.Doit;');
  194. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  195. AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
  196. S:=Statement as TPasImplSimple;
  197. AssertExpression('Doit call',S.Expr,pekBinary,TBinaryExpr);
  198. B:=S.Expr as TBinaryExpr;
  199. AssertExpression('Unit name',B.Left,pekIdent,'Unita');
  200. AssertExpression('Doit call',B.Right,pekBinary,TBinaryExpr);
  201. B:=B.Right as TBinaryExpr;
  202. AssertExpression('Unit name',B.Left,pekIdent,'ClassB');
  203. AssertExpression('Doit call',B.Right,pekIdent,'Doit');
  204. end;
  205. procedure TTestStatementParser.TestCallNoArgs;
  206. Var
  207. S : TPasImplSimple;
  208. P : TParamsExpr;
  209. begin
  210. TestStatement('Doit();');
  211. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  212. AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
  213. S:=Statement as TPasImplSimple;
  214. AssertExpression('Doit call',S.Expr,pekFuncParams,TParamsExpr);
  215. P:=S.Expr as TParamsExpr;
  216. AssertExpression('Correct function call name',P.Value,pekIdent,'Doit');
  217. AssertEquals('No params',0,Length(P.Params));
  218. end;
  219. procedure TTestStatementParser.TestCallOneArg;
  220. Var
  221. S : TPasImplSimple;
  222. P : TParamsExpr;
  223. begin
  224. TestStatement('Doit(1);');
  225. AssertEquals('1 statement',1,PasProgram.InitializationSection.Elements.Count);
  226. AssertEquals('Simple statement',TPasImplSimple,Statement.ClassType);
  227. S:=Statement as TPasImplSimple;
  228. AssertExpression('Doit call',S.Expr,pekFuncParams,TParamsExpr);
  229. P:=S.Expr as TParamsExpr;
  230. AssertExpression('Correct function call name',P.Value,pekIdent,'Doit');
  231. AssertEquals('One param',1,Length(P.Params));
  232. AssertExpression('Parameter is constant',P.Params[0],pekNumber,'1');
  233. end;
  234. procedure TTestStatementParser.TestIf;
  235. Var
  236. I : TPasImplIfElse;
  237. begin
  238. DeclareVar('boolean');
  239. TestStatement(['if a then',';']);
  240. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  241. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  242. AssertNull('No else',i.ElseBranch);
  243. AssertNull('No if branch',I.IfBranch);
  244. end;
  245. procedure TTestStatementParser.TestIfBlock;
  246. Var
  247. I : TPasImplIfElse;
  248. begin
  249. DeclareVar('boolean');
  250. TestStatement(['if a then',' begin',' end']);
  251. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  252. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  253. AssertNull('No else',i.ElseBranch);
  254. AssertNotNull('if branch',I.IfBranch);
  255. AssertEquals('begin end block',TPasImplBeginBlock,I.ifBranch.ClassType);
  256. end;
  257. procedure TTestStatementParser.TestIfAssignment;
  258. Var
  259. I : TPasImplIfElse;
  260. begin
  261. DeclareVar('boolean');
  262. TestStatement(['if a then',' a:=False;']);
  263. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  264. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  265. AssertNull('No else',i.ElseBranch);
  266. AssertNotNull('if branch',I.IfBranch);
  267. AssertEquals('assignment statement',TPasImplAssign,I.ifBranch.ClassType);
  268. end;
  269. procedure TTestStatementParser.TestIfElse;
  270. Var
  271. I : TPasImplIfElse;
  272. begin
  273. DeclareVar('boolean');
  274. TestStatement(['if a then',' begin',' end','else',';']);
  275. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  276. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  277. AssertNull('No else',i.ElseBranch);
  278. AssertNotNull('if branch',I.IfBranch);
  279. AssertEquals('begin end block',TPasImplBeginBlock,I.ifBranch.ClassType);
  280. end;
  281. procedure TTestStatementParser.TestIfElseBlock;
  282. Var
  283. I : TPasImplIfElse;
  284. begin
  285. DeclareVar('boolean');
  286. TestStatement(['if a then',' begin',' end','else',' begin',' end']);
  287. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  288. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  289. AssertNotNull('if branch',I.IfBranch);
  290. AssertEquals('begin end block',TPasImplBeginBlock,I.ifBranch.ClassType);
  291. AssertNotNull('Else branch',i.ElseBranch);
  292. AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
  293. end;
  294. procedure TTestStatementParser.TestIfSemiColonElseError;
  295. Var
  296. I : TPasImplIfElse;
  297. begin
  298. DeclareVar('boolean');
  299. ExpectParserError('No semicolon before else',['if a then',' begin',' end;','else',' begin',' end']);
  300. end;
  301. procedure TTestStatementParser.TestNestedIf;
  302. Var
  303. I,I2 : TPasImplIfElse;
  304. begin
  305. DeclareVar('boolean');
  306. DeclareVar('boolean','b');
  307. TestStatement(['if a then',' if b then',' begin',' end','else',' begin',' end']);
  308. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  309. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  310. AssertNotNull('if branch',I.IfBranch);
  311. AssertNull('Else branch',i.ElseBranch);
  312. AssertEquals('if in if branch',TPasImplIfElse,I.ifBranch.ClassType);
  313. I:=I.Ifbranch as TPasImplIfElse;
  314. AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
  315. end;
  316. procedure TTestStatementParser.TestNestedIfElse;
  317. Var
  318. I,I2 : TPasImplIfElse;
  319. begin
  320. DeclareVar('boolean');
  321. TestStatement(['if a then',' if b then',' begin',' end',' else',' begin',' end','else',' begin','end']);
  322. I:=AssertStatement('If statement',TPasImplIfElse) as TPasImplIfElse;
  323. AssertExpression('IF condition',I.ConditionExpr,pekIdent,'a');
  324. AssertNotNull('if branch',I.IfBranch);
  325. AssertNotNull('Else branch',i.ElseBranch);
  326. AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
  327. AssertEquals('if in if branch',TPasImplIfElse,I.ifBranch.ClassType);
  328. I:=I.Ifbranch as TPasImplIfElse;
  329. AssertEquals('begin end block',TPasImplBeginBlock,I.ElseBranch.ClassType);
  330. end;
  331. procedure TTestStatementParser.TestWhile;
  332. Var
  333. W : TPasImplWhileDo;
  334. begin
  335. DeclareVar('boolean');
  336. TestStatement(['While a do ;']);
  337. W:=AssertStatement('While statement',TPasImplWhileDo) as TPasImplWhileDo;
  338. AssertExpression('While condition',W.ConditionExpr,pekIdent,'a');
  339. AssertNull('Empty body',W.Body);
  340. end;
  341. procedure TTestStatementParser.TestWhileBlock;
  342. Var
  343. W : TPasImplWhileDo;
  344. begin
  345. DeclareVar('boolean');
  346. TestStatement(['While a do',' begin',' end']);
  347. W:=AssertStatement('While statement',TPasImplWhileDo) as TPasImplWhileDo;
  348. AssertExpression('While condition',W.ConditionExpr,pekIdent,'a');
  349. AssertNotNull('Have while body',W.Body);
  350. AssertEquals('begin end block',TPasImplBeginBlock,W.Body.ClassType);
  351. AssertEquals('Empty block',0,TPasImplBeginBlock(W.Body).ELements.Count);
  352. end;
  353. procedure TTestStatementParser.TestWhileNested;
  354. Var
  355. W : TPasImplWhileDo;
  356. begin
  357. DeclareVar('boolean');
  358. DeclareVar('boolean','b');
  359. TestStatement(['While a do',' while b do',' begin',' end']);
  360. W:=AssertStatement('While statement',TPasImplWhileDo) as TPasImplWhileDo;
  361. AssertExpression('While condition',W.ConditionExpr,pekIdent,'a');
  362. AssertNotNull('Have while body',W.Body);
  363. AssertEquals('Nested while',TPasImplWhileDo,W.Body.ClassType);
  364. W:=W.Body as TPasImplWhileDo;
  365. AssertExpression('While condition',W.ConditionExpr,pekIdent,'b');
  366. AssertNotNull('Have nested while body',W.Body);
  367. AssertEquals('Nested begin end block',TPasImplBeginBlock,W.Body.ClassType);
  368. AssertEquals('Empty nested block',0,TPasImplBeginBlock(W.Body).ELements.Count);
  369. end;
  370. procedure TTestStatementParser.TestRepeat;
  371. Var
  372. R : TPasImplRepeatUntil;
  373. begin
  374. DeclareVar('boolean');
  375. TestStatement(['Repeat','Until a;']);
  376. R:=AssertStatement('Repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
  377. AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
  378. AssertEquals('Empty body',0,R.Elements.Count);
  379. end;
  380. procedure TTestStatementParser.TestRepeatBlock;
  381. Var
  382. R : TPasImplRepeatUntil;
  383. begin
  384. DeclareVar('boolean');
  385. TestStatement(['Repeat','begin','end;','Until a;']);
  386. R:=AssertStatement('repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
  387. AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
  388. AssertEquals('Have statement',1,R.Elements.Count);
  389. AssertEquals('begin end block',TPasImplBeginBlock,TObject(R.Elements[0]).ClassType);
  390. AssertEquals('Empty block',0,TPasImplBeginBlock(R.Elements[0]).ELements.Count);
  391. end;
  392. procedure TTestStatementParser.TestRepeatBlockNosemicolon;
  393. Var
  394. R : TPasImplRepeatUntil;
  395. begin
  396. DeclareVar('boolean');
  397. TestStatement(['Repeat','begin','end','Until a;']);
  398. R:=AssertStatement('repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
  399. AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
  400. AssertEquals('Have statement',1,R.Elements.Count);
  401. AssertEquals('begin end block',TPasImplBeginBlock,TObject(R.Elements[0]).ClassType);
  402. AssertEquals('Empty block',0,TPasImplBeginBlock(R.Elements[0]).ELements.Count);
  403. end;
  404. procedure TTestStatementParser.TestRepeatNested;
  405. Var
  406. R : TPasImplRepeatUntil;
  407. begin
  408. DeclareVar('boolean');
  409. DeclareVar('boolean','b');
  410. TestStatement(['Repeat','repeat','begin','end','until b','Until a;']);
  411. R:=AssertStatement('repeat statement',TPasImplRepeatUntil) as TPasImplRepeatUntil;
  412. AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'a');
  413. AssertEquals('Have statement',1,R.Elements.Count);
  414. AssertEquals('Nested repeat',TPasImplRepeatUntil,TObject(R.Elements[0]).ClassType);
  415. R:=TPasImplRepeatUntil(R.Elements[0]);
  416. AssertExpression('repeat condition',R.ConditionExpr,pekIdent,'b');
  417. AssertEquals('Have statement',1,R.Elements.Count);
  418. AssertEquals('begin end block',TPasImplBeginBlock,TObject(R.Elements[0]).ClassType);
  419. AssertEquals('Empty block',0,TPasImplBeginBlock(R.Elements[0]).ELements.Count);
  420. end;
  421. procedure TTestStatementParser.TestFor;
  422. Var
  423. F : TPasImplForLoop;
  424. begin
  425. DeclareVar('integer');
  426. TestStatement(['For a:=1 to 10 do',';']);
  427. F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
  428. AssertEquals('Loop variable name','a',F.VariableName);
  429. AssertEquals('Up loop',False,F.Down);
  430. AssertExpression('Start value',F.StartExpr,pekNumber,'1');
  431. AssertExpression('End value',F.EndExpr,pekNumber,'10');
  432. AssertNull('Empty body',F.Body);
  433. end;
  434. procedure TTestStatementParser.TestForExpr;
  435. Var
  436. F : TPasImplForLoop;
  437. B : TBinaryExpr;
  438. begin
  439. DeclareVar('integer');
  440. TestStatement(['For a:=1+1 to 5+5 do',';']);
  441. F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
  442. AssertEquals('Loop variable name','a',F.VariableName);
  443. AssertEquals('Up loop',False,F.Down);
  444. AssertExpression('Start expression',F.StartExpr,pekBinary,TBinaryExpr);
  445. B:=F.StartExpr as TBinaryExpr;
  446. AssertExpression('Start value left',B.left,pekNumber,'1');
  447. AssertExpression('Start value right',B.right,pekNumber,'1');
  448. AssertExpression('Start expression',F.StartExpr,pekBinary,TBinaryExpr);
  449. B:=F.EndExpr as TBinaryExpr;
  450. AssertExpression('End value left',B.left,pekNumber,'5');
  451. AssertExpression('End value right',B.right,pekNumber,'5');
  452. AssertNull('Empty body',F.Body);
  453. end;
  454. procedure TTestStatementParser.TestForBlock;
  455. Var
  456. F : TPasImplForLoop;
  457. begin
  458. DeclareVar('integer');
  459. TestStatement(['For a:=1 to 10 do','begin','end']);
  460. F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
  461. AssertEquals('Loop variable name','a',F.VariableName);
  462. AssertEquals('Up loop',False,F.Down);
  463. AssertExpression('Start value',F.StartExpr,pekNumber,'1');
  464. AssertExpression('End value',F.EndExpr,pekNumber,'10');
  465. AssertNotNull('Have for body',F.Body);
  466. AssertEquals('begin end block',TPasImplBeginBlock,F.Body.ClassType);
  467. AssertEquals('Empty block',0,TPasImplBeginBlock(F.Body).ELements.Count);
  468. end;
  469. procedure TTestStatementParser.TestDowntoBlock;
  470. Var
  471. F : TPasImplForLoop;
  472. begin
  473. DeclareVar('integer');
  474. TestStatement(['For a:=10 downto 1 do','begin','end']);
  475. F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
  476. AssertEquals('Loop variable name','a',F.VariableName);
  477. AssertEquals('Down loop',True,F.Down);
  478. AssertExpression('Start value',F.StartExpr,pekNumber,'10');
  479. AssertExpression('End value',F.EndExpr,pekNumber,'1');
  480. AssertNotNull('Have for body',F.Body);
  481. AssertEquals('begin end block',TPasImplBeginBlock,F.Body.ClassType);
  482. AssertEquals('Empty block',0,TPasImplBeginBlock(F.Body).ELements.Count);
  483. end;
  484. procedure TTestStatementParser.TestForNested;
  485. Var
  486. F : TPasImplForLoop;
  487. begin
  488. DeclareVar('integer');
  489. DeclareVar('integer','b');
  490. TestStatement(['For a:=1 to 10 do','For b:=11 to 20 do','begin','end']);
  491. F:=AssertStatement('For statement',TPasImplForLoop) as TPasImplForLoop;
  492. AssertEquals('Loop variable name','a',F.VariableName);
  493. AssertEquals('Up loop',False,F.Down);
  494. AssertExpression('Start value',F.StartExpr,pekNumber,'1');
  495. AssertExpression('End value',F.EndExpr,pekNumber,'10');
  496. AssertNotNull('Have while body',F.Body);
  497. AssertEquals('begin end block',TPasImplForLoop,F.Body.ClassType);
  498. F:=F.Body as TPasImplForLoop;
  499. AssertEquals('Loop variable name','b',F.VariableName);
  500. AssertEquals('Up loop',False,F.Down);
  501. AssertExpression('Start value',F.StartExpr,pekNumber,'11');
  502. AssertExpression('End value',F.EndExpr,pekNumber,'20');
  503. AssertNotNull('Have for body',F.Body);
  504. AssertEquals('begin end block',TPasImplBeginBlock,F.Body.ClassType);
  505. AssertEquals('Empty block',0,TPasImplBeginBlock(F.Body).ELements.Count);
  506. end;
  507. initialization
  508. RegisterTests([TTestStatementParser]);
  509. end.