tconstparser.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. unit tconstparser;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, fpcunit, pastree, pscanner, tcbaseparser, testregistry, pparser;
  6. Type
  7. { TTestConstParser }
  8. TTestConstParser = Class(TTestParser)
  9. private
  10. FConst: TPasConst;
  11. FExpr: TPasExpr;
  12. FHint : string;
  13. FTyped: String;
  14. procedure DoParseConstUnTypedRange;
  15. Protected
  16. Function ParseConst(ASource : String) : TPasConst;
  17. Procedure CheckExprNameKindClass(AKind : TPasExprKind; AClass : TClass);
  18. Property TheConst : TPasConst Read FConst;
  19. Property TheExpr : TPasExpr Read FExpr;
  20. Property Hint : string Read FHint Write FHint;
  21. Property Typed : String Read FTyped Write FTyped;
  22. procedure SetUp; override;
  23. Public
  24. Procedure DoTestSimpleIntConst;
  25. Procedure DoTestSimpleFloatConst;
  26. Procedure DoTestSimpleStringConst;
  27. Procedure DoTestSimpleNilConst;
  28. Procedure DoTestSimpleBoolConst;
  29. Procedure DoTestSimpleIdentifierConst;
  30. Procedure DoTestSimpleSetConst;
  31. Procedure DoTestSimpleExprConst;
  32. Published
  33. Procedure TestSimpleIntConst;
  34. Procedure TestSimpleFloatConst;
  35. Procedure TestSimpleStringConst;
  36. Procedure TestSimpleNilConst;
  37. Procedure TestSimpleBoolConst;
  38. Procedure TestSimpleIdentifierConst;
  39. Procedure TestSimpleSetConst;
  40. Procedure TestSimpleExprConst;
  41. Procedure TestSimpleAbsoluteConst;
  42. Procedure TestSimpleIntConstDeprecatedMsg;
  43. Procedure TestSimpleIntConstDeprecated;
  44. Procedure TestSimpleFloatConstDeprecated;
  45. Procedure TestSimpleStringConstDeprecated;
  46. Procedure TestSimpleNilConstDeprecated;
  47. Procedure TestSimpleBoolConstDeprecated;
  48. Procedure TestSimpleIdentifierConstDeprecated;
  49. Procedure TestSimpleSetConstDeprecated;
  50. Procedure TestSimpleExprConstDeprecated;
  51. Procedure TestSimpleIntConstPlatform;
  52. Procedure TestSimpleFloatConstPlatform;
  53. Procedure TestSimpleStringConstPlatform;
  54. Procedure TestSimpleNilConstPlatform;
  55. Procedure TestSimpleBoolConstPlatform;
  56. Procedure TestSimpleIdentifierConstPlatform;
  57. Procedure TestSimpleSetConstPlatform;
  58. Procedure TestSimpleExprConstPlatform;
  59. Procedure TestSimpleIntConstExperimental;
  60. Procedure TestSimpleFloatConstExperimental;
  61. Procedure TestSimpleStringConstExperimental;
  62. Procedure TestSimpleNilConstExperimental;
  63. Procedure TestSimpleBoolConstExperimental;
  64. Procedure TestSimpleIdentifierConstExperimental;
  65. Procedure TestSimpleSetConstExperimental;
  66. Procedure TestSimpleExprConstExperimental;
  67. Procedure TestTypedIntConst;
  68. Procedure TestTypedFloatConst;
  69. Procedure TestTypedStringConst;
  70. Procedure TestTypedNilConst;
  71. Procedure TestTypedBoolConst;
  72. Procedure TestTypedIdentifierConst;
  73. Procedure TestTypedSetConst;
  74. Procedure TestTypedExprConst;
  75. Procedure TestRecordConst;
  76. Procedure TestRecordConstEmpty;
  77. Procedure TestArrayConst;
  78. Procedure TestRangeConst;
  79. Procedure TestRangeConstUnTyped;
  80. Procedure TestArrayOfRangeConst;
  81. Procedure TestConstErrorRecovery;
  82. end;
  83. { TTestResourcestringParser }
  84. TTestResourcestringParser = Class(TTestParser)
  85. private
  86. FExpr: TPasExpr;
  87. FHint : string;
  88. FTheStr: TPasResString;
  89. Protected
  90. Function ParseResourcestring(ASource : String) : TPasResString;
  91. Procedure CheckExprNameKindClass(AKind : TPasExprKind; AClass : TClass);
  92. Property Hint : string Read FHint Write FHint;
  93. Property TheStr : TPasResString Read FTheStr;
  94. Property TheExpr : TPasExpr Read FExpr;
  95. Public
  96. Procedure DoTestSimple;
  97. Procedure DoTestSum;
  98. Procedure DoTestSum2;
  99. Published
  100. Procedure TestSimple;
  101. Procedure TestSimpleDeprecated;
  102. Procedure TestSimplePlatform;
  103. Procedure TestSum1;
  104. Procedure TestSum1Deprecated;
  105. Procedure TestSum1Platform;
  106. Procedure TestSum2;
  107. Procedure TestSum2Deprecated;
  108. Procedure TestSum2Platform;
  109. end;
  110. { TTestLabelParser }
  111. TTestLabelParser = Class(TTestParser)
  112. private
  113. FHint : string;
  114. Protected
  115. Function ParseLabel(ASource : String) : TPasLabels;
  116. Property Hint : string Read FHint Write FHint;
  117. Published
  118. Procedure TestSimple;
  119. Procedure TestSimpleNumber;
  120. end;
  121. implementation
  122. { TTestLabelParser }
  123. function TTestLabelParser.ParseLabel(ASource: String): TPasLabels;
  124. Var
  125. D : String;
  126. begin
  127. UseImplementation:=True;
  128. Add('label');
  129. D:=ASource;
  130. If Hint<>'' then
  131. D:=D+' '+Hint;
  132. Add(' '+D+';');
  133. Add('end.');
  134. //Writeln(source.text);
  135. ParseDeclarations;
  136. AssertEquals('One labels section',1,Declarations.Labels.Count);
  137. AssertEquals('First declaration is label section.',TPasLabels,TObject(Declarations.Labels[0]).ClassType);
  138. Result:=TPasLabels(Declarations.Labels[0]);
  139. end;
  140. procedure TTestLabelParser.TestSimple;
  141. Var
  142. Res : TPasLabels;
  143. begin
  144. Res:=ParseLabel('a');
  145. AssertEquals('One label definition',1,Res.Labels.Count);
  146. AssertEquals('One label definition','a',Res.Labels[0]);
  147. end;
  148. procedure TTestLabelParser.TestSimpleNumber;
  149. Var
  150. Res : TPasLabels;
  151. begin
  152. Res:=ParseLabel('100');
  153. AssertEquals('One label definition',1,Res.Labels.Count);
  154. AssertEquals('One label definition','100',Res.Labels[0]);
  155. end;
  156. { TTestConstParser }
  157. function TTestConstParser.ParseConst(ASource: String): TPasConst;
  158. Var
  159. D : String;
  160. begin
  161. Add('Const');
  162. D:=' A ';
  163. If (Typed<>'') then
  164. D:=D+' : '+Typed+' ';
  165. D:=D+' = '+ASource;
  166. If Hint<>'' then
  167. D:=D+' '+Hint;
  168. Add(' '+D+';');
  169. ParseDeclarations;
  170. AssertEquals('One constant definition',1,Declarations.Consts.Count);
  171. AssertEquals('First declaration is constant definition.',TPasConst,TObject(Declarations.Consts[0]).ClassType);
  172. Result:=TPasConst(Declarations.Consts[0]);
  173. AssertNotNull(Result.Expr);
  174. FExpr:=Result.Expr;
  175. FConst:=Result;
  176. Definition:=Result;
  177. end;
  178. procedure TTestConstParser.CheckExprNameKindClass(
  179. AKind: TPasExprKind; AClass : TClass);
  180. begin
  181. AssertEquals('Correct name','A',TheConst.Name);
  182. AssertExpression('Const', TheExpr,aKind,AClass);
  183. end;
  184. procedure TTestConstParser.SetUp;
  185. begin
  186. inherited SetUp;
  187. Hint:='';
  188. end;
  189. procedure TTestConstParser.DoTestSimpleIntConst;
  190. begin
  191. ParseConst('1');
  192. AssertExpression('Integer Const',TheExpr,pekNumber,'1');
  193. end;
  194. procedure TTestConstParser.DoTestSimpleFloatConst;
  195. begin
  196. ParseConst('1.2');
  197. AssertExpression('Float const', TheExpr,pekNumber,'1.2');
  198. end;
  199. procedure TTestConstParser.DoTestSimpleStringConst;
  200. begin
  201. ParseConst('''test''');
  202. AssertExpression('String const', TheExpr,pekString,'''test''');
  203. end;
  204. procedure TTestConstParser.DoTestSimpleNilConst;
  205. begin
  206. ParseConst('Nil');
  207. CheckExprNameKindClass(pekNil,TNilExpr);
  208. end;
  209. procedure TTestConstParser.DoTestSimpleBoolConst;
  210. begin
  211. ParseConst('True');
  212. CheckExprNameKindClass(pekBoolConst,TBoolconstExpr);
  213. AssertEquals('Correct expression value',True,TBoolconstExpr(TheExpr).Value);
  214. end;
  215. procedure TTestConstParser.DoTestSimpleIdentifierConst;
  216. begin
  217. ParseConst('taCenter');
  218. AssertExpression('Enumeration const', theExpr,pekIdent,'taCenter');
  219. end;
  220. procedure TTestConstParser.DoTestSimpleSetConst;
  221. begin
  222. ParseConst('[taLeftJustify,taRightJustify]');
  223. CheckExprNameKindClass(pekSet,TParamsExpr);
  224. AssertEquals('Correct set count',2,Length(TParamsExpr(TheExpr).Params));
  225. AssertExpression('Set element 1',TParamsExpr(TheExpr).Params[0],pekIdent,'taLeftJustify');
  226. AssertExpression('Set element 2',TParamsExpr(TheExpr).Params[1],pekIdent,'taRightJustify');
  227. end;
  228. procedure TTestConstParser.DoTestSimpleExprConst;
  229. Var
  230. B : TBinaryExpr;
  231. begin
  232. ParseConst('1 + 2');
  233. CheckExprNameKindClass(pekBinary,TBinaryExpr);
  234. B:=TBinaryExpr(TheExpr);
  235. TAssert.AssertSame('B.Left.Parent=B',B,B.Left.Parent);
  236. TAssert.AssertSame('B.right.Parent=B',B,B.Right.Parent);
  237. AssertExpression('Left expression',B.Left,pekNumber,'1');
  238. AssertExpression('Right expression',B.Right,pekNumber,'2');
  239. end;
  240. procedure TTestConstParser.TestSimpleIntConst;
  241. begin
  242. DoTestSimpleIntConst
  243. end;
  244. procedure TTestConstParser.TestSimpleFloatConst;
  245. begin
  246. DoTestSimpleFloatConst
  247. end;
  248. procedure TTestConstParser.TestSimpleStringConst;
  249. begin
  250. DoTestSimpleStringConst
  251. end;
  252. procedure TTestConstParser.TestSimpleNilConst;
  253. begin
  254. DoTestSimpleNilConst
  255. end;
  256. procedure TTestConstParser.TestSimpleBoolConst;
  257. begin
  258. DoTestSimpleBoolConst
  259. end;
  260. procedure TTestConstParser.TestSimpleIdentifierConst;
  261. begin
  262. DoTestSimpleIdentifierConst
  263. end;
  264. procedure TTestConstParser.TestSimpleSetConst;
  265. begin
  266. DoTestSimpleSetConst
  267. end;
  268. procedure TTestConstParser.TestSimpleExprConst;
  269. begin
  270. DoTestSimpleExprConst;
  271. end;
  272. procedure TTestConstParser.TestSimpleAbsoluteConst;
  273. // Found in xi.pp
  274. begin
  275. Add('Const');
  276. Add(' Absolute = 1;');
  277. ParseDeclarations;
  278. AssertEquals('One constant definition',1,Declarations.Consts.Count);
  279. AssertEquals('First declaration is constant definition.',TPasConst,TObject(Declarations.Consts[0]).ClassType);
  280. end;
  281. procedure TTestConstParser.TestSimpleIntConstDeprecatedMsg;
  282. begin
  283. Hint:='deprecated ''this is old''' ;
  284. DoTestSimpleIntConst;
  285. CheckHint(hDeprecated);
  286. end;
  287. procedure TTestConstParser.TestSimpleIntConstDeprecated;
  288. begin
  289. Hint:='deprecated';
  290. DoTestSimpleIntConst;
  291. CheckHint(hDeprecated);
  292. end;
  293. procedure TTestConstParser.TestSimpleFloatConstDeprecated;
  294. begin
  295. Hint:='deprecated';
  296. DoTestSimpleIntConst;
  297. CheckHint(hDeprecated);
  298. end;
  299. procedure TTestConstParser.TestSimpleStringConstDeprecated;
  300. begin
  301. Hint:='deprecated';
  302. DoTestSimpleStringConst;
  303. CheckHint(hDeprecated);
  304. end;
  305. procedure TTestConstParser.TestSimpleNilConstDeprecated;
  306. begin
  307. Hint:='deprecated';
  308. DoTestSimpleNilConst;
  309. CheckHint(hDeprecated);
  310. end;
  311. procedure TTestConstParser.TestSimpleBoolConstDeprecated;
  312. begin
  313. Hint:='deprecated';
  314. DoTestSimpleBoolConst;
  315. CheckHint(hDeprecated);
  316. end;
  317. procedure TTestConstParser.TestSimpleIdentifierConstDeprecated;
  318. begin
  319. Hint:='deprecated';
  320. DoTestSimpleIdentifierConst;
  321. CheckHint(hDeprecated);
  322. end;
  323. procedure TTestConstParser.TestSimpleSetConstDeprecated;
  324. begin
  325. Hint:='deprecated';
  326. DoTestSimpleSetConst;
  327. CheckHint(hDeprecated);
  328. end;
  329. procedure TTestConstParser.TestSimpleExprConstDeprecated;
  330. begin
  331. Hint:='deprecated';
  332. DoTestSimpleExprConst;
  333. CheckHint(hDeprecated);
  334. end;
  335. procedure TTestConstParser.TestSimpleIntConstPlatform;
  336. begin
  337. Hint:='Platform';
  338. DoTestSimpleIntConst;
  339. CheckHint(hPlatform);
  340. end;
  341. procedure TTestConstParser.TestSimpleFloatConstPlatform;
  342. begin
  343. Hint:='Platform';
  344. DoTestSimpleIntConst;
  345. CheckHint(hPlatform);
  346. end;
  347. procedure TTestConstParser.TestSimpleStringConstPlatform;
  348. begin
  349. Hint:='Platform';
  350. DoTestSimpleStringConst;
  351. CheckHint(hPlatform);
  352. end;
  353. procedure TTestConstParser.TestSimpleNilConstPlatform;
  354. begin
  355. Hint:='Platform';
  356. DoTestSimpleNilConst;
  357. CheckHint(hPlatform);
  358. end;
  359. procedure TTestConstParser.TestSimpleBoolConstPlatform;
  360. begin
  361. Hint:='Platform';
  362. DoTestSimpleBoolConst;
  363. CheckHint(hPlatform);
  364. end;
  365. procedure TTestConstParser.TestSimpleIdentifierConstPlatform;
  366. begin
  367. Hint:='Platform';
  368. DoTestSimpleIdentifierConst;
  369. CheckHint(hPlatform);
  370. end;
  371. procedure TTestConstParser.TestSimpleExprConstPlatform;
  372. begin
  373. Hint:='Platform';
  374. DoTestSimpleExprConst;
  375. CheckHint(hPlatform);
  376. end;
  377. procedure TTestConstParser.TestSimpleSetConstPlatform;
  378. begin
  379. Hint:='Platform';
  380. DoTestSimpleSetConst;
  381. CheckHint(hPlatform);
  382. end;
  383. procedure TTestConstParser.TestSimpleIntConstExperimental;
  384. begin
  385. Hint:='Experimental';
  386. DoTestSimpleIntConst;
  387. CheckHint(hExperimental);
  388. end;
  389. procedure TTestConstParser.TestSimpleFloatConstExperimental;
  390. begin
  391. Hint:='Experimental';
  392. DoTestSimpleIntConst;
  393. CheckHint(hExperimental);
  394. end;
  395. procedure TTestConstParser.TestSimpleStringConstExperimental;
  396. begin
  397. Hint:='Experimental';
  398. DoTestSimpleStringConst;
  399. CheckHint(hExperimental);
  400. end;
  401. procedure TTestConstParser.TestSimpleNilConstExperimental;
  402. begin
  403. Hint:='Experimental';
  404. DoTestSimpleNilConst;
  405. CheckHint(hExperimental);
  406. end;
  407. procedure TTestConstParser.TestSimpleBoolConstExperimental;
  408. begin
  409. Hint:='Experimental';
  410. DoTestSimpleBoolConst;
  411. CheckHint(hExperimental);
  412. end;
  413. procedure TTestConstParser.TestSimpleIdentifierConstExperimental;
  414. begin
  415. Hint:='Experimental';
  416. DoTestSimpleIdentifierConst;
  417. CheckHint(hExperimental);
  418. end;
  419. procedure TTestConstParser.TestSimpleSetConstExperimental;
  420. begin
  421. Hint:='Experimental';
  422. DoTestSimpleSetConst;
  423. CheckHint(hExperimental);
  424. end;
  425. procedure TTestConstParser.TestSimpleExprConstExperimental;
  426. begin
  427. Hint:='Experimental';
  428. DoTestSimpleExprConst;
  429. CheckHint(hExperimental);
  430. end;
  431. procedure TTestConstParser.TestTypedIntConst;
  432. begin
  433. Typed:='Integer';
  434. DoTestSimpleIntConst
  435. end;
  436. procedure TTestConstParser.TestTypedFloatConst;
  437. begin
  438. Typed:='Double';
  439. DoTestSimpleFloatConst
  440. end;
  441. procedure TTestConstParser.TestTypedStringConst;
  442. begin
  443. Typed:='shortstring';
  444. DoTestSimpleStringConst
  445. end;
  446. procedure TTestConstParser.TestTypedNilConst;
  447. begin
  448. Typed:='PAnsiChar';
  449. DoTestSimpleNilConst
  450. end;
  451. procedure TTestConstParser.TestTypedBoolConst;
  452. begin
  453. Typed:='Boolean';
  454. DoTestSimpleBoolConst
  455. end;
  456. procedure TTestConstParser.TestTypedIdentifierConst;
  457. begin
  458. Typed:='TAlign';
  459. DoTestSimpleIdentifierConst
  460. end;
  461. procedure TTestConstParser.TestTypedSetConst;
  462. begin
  463. Typed:='TAligns';
  464. DoTestSimpleSetConst
  465. end;
  466. procedure TTestConstParser.TestTypedExprConst;
  467. begin
  468. Typed:='ShortInt';
  469. DoTestSimpleExprConst;
  470. end;
  471. procedure TTestConstParser.TestRecordConst;
  472. Var
  473. R : TRecordValues;
  474. Fi : TRecordValuesItem;
  475. begin
  476. Typed := 'TPoint';
  477. ParseConst('(x:1;y: 2)');
  478. AssertEquals('Record Values',TRecordValues,TheExpr.ClassType);
  479. R:=TheExpr as TRecordValues;
  480. AssertEquals('Expression list of ',pekListOfExp,TheExpr.Kind);
  481. AssertEquals('2 elements',2,Length(R.Fields));
  482. FI:=R.Fields[0];
  483. AssertEquals('Name field 1','x',Fi.Name);
  484. AssertExpression('Field 1 value',Fi.ValueExp,pekNumber,'1');
  485. FI:=R.Fields[1];
  486. AssertEquals('Name field 2','y',Fi.Name);
  487. AssertExpression('Field 2 value',Fi.ValueExp,pekNumber,'2');
  488. end;
  489. procedure TTestConstParser.TestRecordConstEmpty;
  490. Var
  491. R : TRecordValues;
  492. //Fi : TRecordValuesItem;
  493. begin
  494. Typed := 'TPoint';
  495. ParseConst('()');
  496. AssertEquals('Record Values',TRecordValues,TheExpr.ClassType);
  497. R:=TheExpr as TRecordValues;
  498. AssertEquals('Expression list of ',pekListOfExp,TheExpr.Kind);
  499. AssertEquals('0 elements',0,Length(R.Fields));
  500. (*
  501. FI:=R.Fields[0];
  502. AssertEquals('Name field 1','x',Fi.Name);
  503. AssertExpression('Field 1 value',Fi.ValueExp,pekNumber,'1');
  504. FI:=R.Fields[1];
  505. AssertEquals('Name field 2','y',Fi.Name);
  506. AssertExpression('Field 2 value',Fi.ValueExp,pekNumber,'2');
  507. *)
  508. end;
  509. procedure TTestConstParser.TestArrayConst;
  510. Var
  511. R : TArrayValues;
  512. begin
  513. Typed := 'TMyArray';
  514. ParseConst('(1 , 2)');
  515. AssertEquals('Array Values',TArrayValues,TheExpr.ClassType);
  516. R:=TheExpr as TArrayValues;
  517. AssertEquals('Expression list of ',pekListOfExp,TheExpr.Kind);
  518. AssertEquals('2 elements',2,Length(R.Values));
  519. AssertExpression('Element 1 value',R.Values[0],pekNumber,'1');
  520. AssertExpression('Element 2 value',R.Values[1],pekNumber,'2');
  521. end;
  522. procedure TTestConstParser.TestRangeConst;
  523. begin
  524. Typed:='0..1';
  525. ParseConst('1');
  526. AssertEquals('Range type',TPasRangeType,TheConst.VarType.ClassType);
  527. AssertExpression('Float const', TheExpr,pekNumber,'1');
  528. end;
  529. procedure TTestConstParser.DoParseConstUnTypedRange;
  530. begin
  531. ParseConst('1..2');
  532. end;
  533. procedure TTestConstParser.TestRangeConstUnTyped;
  534. begin
  535. AssertException('Range const is not allowed',EParserError,@DoParseConstUnTypedRange);
  536. end;
  537. procedure TTestConstParser.TestArrayOfRangeConst;
  538. Var
  539. R : TArrayValues;
  540. begin
  541. Typed:='array [0..7] of 0..1';
  542. ParseConst('(0, 0, 0, 0, 0, 0, 0, 0)');
  543. AssertEquals('Array Values',TArrayValues,TheExpr.ClassType);
  544. R:=TheExpr as TArrayValues;
  545. AssertEquals('Expression list of ',pekListOfExp,TheExpr.Kind);
  546. AssertEquals('elements',8,Length(R.Values));
  547. // AssertEquals('Range type',TPasRangeType,TheConst.VarType.ClassType);
  548. // AssertExpression('Float const', TheExpr,pekNumber,'1');
  549. end;
  550. procedure TTestConstParser.TestConstErrorRecovery;
  551. begin
  552. Add('Const');
  553. Add(' A : 1;');
  554. Add(' B : 2;');
  555. try
  556. Parser.MaxErrorCount:=3;
  557. Parser.OnLog:=@DoParserLog;
  558. ParseDeclarations;
  559. except
  560. On E : Exception do
  561. begin
  562. AssertEquals('Correct class',E.ClassType,EParserError);
  563. end;
  564. end;
  565. AssertErrorCount(2);
  566. end;
  567. { TTestResourcestringParser }
  568. function TTestResourcestringParser.ParseResourcestring(ASource: String
  569. ): TPasResString;
  570. Var
  571. D : String;
  572. begin
  573. Add('Resourcestring');
  574. D:=' A = '+ASource;
  575. If Hint<>'' then
  576. D:=D+' '+Hint;
  577. Add(' '+D+';');
  578. Add('end.');
  579. //Writeln(source.text);
  580. ParseDeclarations;
  581. AssertEquals('One resourcestring definition',1,Declarations.ResStrings.Count);
  582. AssertEquals('First declaration is constant definition.',TPasResString,TObject(Declarations.ResStrings[0]).ClassType);
  583. Result:=TPasResString(Declarations.ResStrings[0]);
  584. FTheStr:=Result;
  585. FExpr:=Result.Expr;
  586. Definition:=Result;
  587. end;
  588. procedure TTestResourcestringParser.CheckExprNameKindClass(AKind: TPasExprKind;
  589. AClass: TClass);
  590. begin
  591. AssertEquals('Correct name','A',TheStr.Name);
  592. AssertEquals('Correct expression kind',aKind,TheExpr.Kind);
  593. AssertEquals('Correct expression class',AClass,TheExpr.ClassType);
  594. // Writeln('Delcaration : ',TheStr.GetDeclaration(True));
  595. end;
  596. procedure TTestResourcestringParser.DoTestSimple;
  597. begin
  598. ParseResourcestring('''Something''');
  599. CheckExprNameKindClass(pekString,TPrimitiveExpr);
  600. AssertEquals('Correct expression value','''Something''',TPrimitiveExpr(TheExpr).Value);
  601. end;
  602. procedure TTestResourcestringParser.DoTestSum;
  603. var
  604. B: TBinaryExpr;
  605. begin
  606. ParseResourcestring('''Something''+'' else''');
  607. CheckExprNameKindClass(pekBinary,TBinaryExpr);
  608. B:=TBinaryExpr(TheExpr);
  609. TAssert.AssertSame('B.left.parent=B',B,B.Left.Parent);
  610. TAssert.AssertSame('B.right.parent=B',B,B.Right.Parent);
  611. AssertEquals('Correct left',TPrimitiveExpr,B.Left.ClassType);
  612. AssertEquals('Correct right',TPrimitiveExpr,B.Right.ClassType);
  613. AssertEquals('Correct left expression value','''Something''',TPrimitiveExpr(B.Left).Value);
  614. AssertEquals('Correct right expression value',''' else''',TPrimitiveExpr(B.Right).Value);
  615. end;
  616. procedure TTestResourcestringParser.DoTestSum2;
  617. var
  618. B: TBinaryExpr;
  619. begin
  620. ParseResourcestring('''Something''+different');
  621. CheckExprNameKindClass(pekBinary,TBinaryExpr);
  622. B:=TBinaryExpr(TheExpr);
  623. TAssert.AssertSame('B.left.parent=B',B,B.Left.Parent);
  624. TAssert.AssertSame('B.right.parent=B',B,B.Right.Parent);
  625. AssertEquals('Correct left',TPrimitiveExpr,B.Left.ClassType);
  626. AssertEquals('Correct right',TPrimitiveExpr,B.Right.ClassType);
  627. AssertEquals('Correct left expression value','''Something''',TPrimitiveExpr(B.Left).Value);
  628. AssertEquals('Correct right expression value','different',TPrimitiveExpr(B.Right).Value);
  629. end;
  630. procedure TTestResourcestringParser.TestSimple;
  631. begin
  632. DoTestSimple;
  633. end;
  634. procedure TTestResourcestringParser.TestSimpleDeprecated;
  635. begin
  636. Hint:='deprecated';
  637. DoTestSimple;
  638. CheckHint(hDeprecated);
  639. end;
  640. procedure TTestResourcestringParser.TestSimplePlatform;
  641. begin
  642. Hint:='platform';
  643. DoTestSimple;
  644. CheckHint(hPlatform);
  645. end;
  646. procedure TTestResourcestringParser.TestSum2;
  647. begin
  648. DoTestSum2;
  649. end;
  650. procedure TTestResourcestringParser.TestSum2Deprecated;
  651. begin
  652. Hint:='deprecated';
  653. DoTestSum2;
  654. CheckHint(hDeprecated);
  655. end;
  656. procedure TTestResourcestringParser.TestSum2Platform;
  657. begin
  658. Hint:='platform';
  659. DoTestSum2;
  660. CheckHint(hplatform);
  661. end;
  662. procedure TTestResourcestringParser.TestSum1;
  663. begin
  664. DoTestSum;
  665. end;
  666. procedure TTestResourcestringParser.TestSum1Deprecated;
  667. begin
  668. Hint:='deprecated';
  669. DoTestSum;
  670. CheckHint(hDeprecated);
  671. end;
  672. procedure TTestResourcestringParser.TestSum1Platform;
  673. begin
  674. Hint:='platform';
  675. DoTestSum;
  676. CheckHint(hplatform);
  677. end;
  678. initialization
  679. RegisterTests([TTestConstParser,TTestResourcestringParser,TTestLabelParser]);
  680. end.