tconstparser.pas 15 KB

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