tconstparser.pas 19 KB

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