tests.rtti.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. unit tests.rtti;
  2. {$ifdef fpc}
  3. {$mode objfpc}{$H+}
  4. {$modeswitch advancedrecords}
  5. {$endif}
  6. interface
  7. uses
  8. {$IFDEF FPC}
  9. fpcunit,testregistry, testutils,
  10. {$ELSE FPC}
  11. TestFramework,
  12. {$ENDIF FPC}
  13. Classes, SysUtils, typinfo,
  14. Rtti;
  15. type
  16. { TTestCase1 }
  17. TTestCase1= class(TTestCase)
  18. published
  19. //procedure GetTypes;
  20. procedure GetTypeInteger;
  21. procedure GetClassProperties;
  22. procedure GetClassPropertiesValue;
  23. procedure TestTRttiTypeProperties;
  24. procedure TestPropGetValueString;
  25. procedure TestPropGetValueInteger;
  26. procedure TestPropGetValueBoolean;
  27. procedure TestPropGetValueShortString;
  28. procedure TestPropGetValueProcString;
  29. procedure TestPropGetValueProcInteger;
  30. procedure TestPropGetValueProcBoolean;
  31. procedure TestPropGetValueProcShortString;
  32. procedure TestPropSetValueString;
  33. procedure TestPropSetValueInteger;
  34. procedure TestPropSetValueBoolean;
  35. procedure TestPropSetValueShortString;
  36. procedure TestGetValueStringCastError;
  37. procedure TestMakeObject;
  38. procedure TestGetIsReadable;
  39. procedure TestIsWritable;
  40. procedure TestIsManaged;
  41. end;
  42. implementation
  43. type
  44. TGetClassProperties = class
  45. private
  46. FPubPropRO: integer;
  47. FPubPropRW: integer;
  48. published
  49. property PubPropRO: integer read FPubPropRO;
  50. property PubPropRW: integer read FPubPropRW write FPubPropRW;
  51. property PubPropSetRO: integer read FPubPropRO;
  52. property PubPropSetRW: integer read FPubPropRW write FPubPropRW;
  53. end;
  54. { TTestValueClass }
  55. TTestValueClass = class
  56. private
  57. FAInteger: integer;
  58. FAString: string;
  59. FABoolean: boolean;
  60. FAShortString: ShortString;
  61. function GetAInteger: integer;
  62. function GetAString: string;
  63. function GetABoolean: boolean;
  64. function GetAShortString: ShortString;
  65. procedure SetWriteOnly(AValue: integer);
  66. published
  67. property AInteger: Integer read FAInteger write FAInteger;
  68. property AString: string read FAString write FAString;
  69. property ABoolean: boolean read FABoolean write FABoolean;
  70. property AShortString: ShortString read FAShortString write FAShortString;
  71. property AGetInteger: Integer read GetAInteger;
  72. property AGetString: string read GetAString;
  73. property AGetBoolean: boolean read GetABoolean;
  74. property AGetShortString: ShortString read GetAShortString;
  75. property AWriteOnly: integer write SetWriteOnly;
  76. end;
  77. TManagedRec = record
  78. s: string;
  79. end;
  80. {$ifdef fpc}
  81. TManagedRecOp = record
  82. class operator AddRef(var a: TManagedRecOp);
  83. end;
  84. {$endif}
  85. TNonManagedRec = record
  86. i: Integer;
  87. end;
  88. TManagedObj = object
  89. i: IInterface;
  90. end;
  91. TNonManagedObj = object
  92. d: double;
  93. end;
  94. TTestEnum = (te1, te2, te3, te4, te5);
  95. TTestSet = set of TTestEnum;
  96. TTestProc = procedure;
  97. TTestMethod = procedure of object;
  98. TTestHelper = class helper for TObject
  99. end;
  100. TArrayOfString = array[0..0] of string;
  101. TArrayOfManagedRec = array[0..0] of TManagedRec;
  102. TArrayOfNonManagedRec = array[0..0] of TNonManagedRec;
  103. TArrayOfByte = array[0..0] of byte;
  104. {$ifdef fpc}
  105. {$PUSH}
  106. {$INTERFACES CORBA}
  107. ICORBATest = interface
  108. end;
  109. {$POP}
  110. {$endif}
  111. {$ifdef fpc}
  112. class operator TManagedRecOp.AddRef(var a: TManagedRecOp);
  113. begin
  114. end;
  115. {$endif}
  116. { TTestValueClass }
  117. function TTestValueClass.GetAInteger: integer;
  118. begin
  119. result := FAInteger;
  120. end;
  121. function TTestValueClass.GetAString: string;
  122. begin
  123. result := FAString;
  124. end;
  125. function TTestValueClass.GetABoolean: boolean;
  126. begin
  127. result := FABoolean;
  128. end;
  129. function TTestValueClass.GetAShortString: ShortString;
  130. begin
  131. Result := FAShortString;
  132. end;
  133. procedure TTestValueClass.SetWriteOnly(AValue: integer);
  134. begin
  135. // Do nothing
  136. end;
  137. { Note: GetTypes currently only returns those types that had been acquired using
  138. GetType, so GetTypes itself can't be really tested currently }
  139. (*procedure TTestCase1.GetTypes;
  140. var
  141. LContext: TRttiContext;
  142. LType: TRttiType;
  143. IsTestCaseClassFound: boolean;
  144. begin
  145. LContext := TRttiContext.Create;
  146. { Enumerate all types declared in the application }
  147. for LType in LContext.GetTypes() do
  148. begin
  149. if LType.Name='TTestCase1' then
  150. IsTestCaseClassFound:=true;
  151. end;
  152. LContext.Free;
  153. CheckTrue(IsTestCaseClassFound, 'RTTI information does not contain class of testcase.');
  154. end;*)
  155. procedure TTestCase1.TestGetValueStringCastError;
  156. var
  157. ATestClass : TTestValueClass;
  158. c: TRttiContext;
  159. ARttiType: TRttiType;
  160. AValue: TValue;
  161. i: integer;
  162. HadException: boolean;
  163. begin
  164. c := TRttiContext.Create;
  165. try
  166. ATestClass := TTestValueClass.Create;
  167. ATestClass.AString := '12';
  168. try
  169. ARttiType := c.GetType(ATestClass.ClassInfo);
  170. AValue := ARttiType.GetProperty('astring').GetValue(ATestClass);
  171. HadException := false;
  172. try
  173. i := AValue.AsInteger;
  174. except
  175. on E: Exception do
  176. if E.ClassType=EInvalidCast then
  177. HadException := true;
  178. end;
  179. Check(HadException, 'No or invalid exception on invalid cast');
  180. finally
  181. AtestClass.Free;
  182. end;
  183. finally
  184. c.Free;
  185. end;
  186. end;
  187. procedure TTestCase1.TestMakeObject;
  188. var
  189. AValue: TValue;
  190. ATestClass: TTestValueClass;
  191. begin
  192. ATestClass := TTestValueClass.Create;
  193. ATestClass.AInteger := 54329;
  194. TValue.Make(@ATestClass, TypeInfo(TTestValueClass),AValue);
  195. CheckEquals(AValue.IsClass, False);
  196. CheckEquals(AValue.IsObject, True);
  197. Check(AValue.AsObject=ATestClass);
  198. CheckEquals(TTestValueClass(AValue.AsObject).AInteger, 54329);
  199. ATestClass.Free;
  200. end;
  201. procedure TTestCase1.TestGetIsReadable;
  202. var
  203. c: TRttiContext;
  204. ARttiType: TRttiType;
  205. AProperty: TRttiProperty;
  206. begin
  207. c := TRttiContext.Create;
  208. try
  209. ARttiType := c.GetType(TTestValueClass);
  210. AProperty := ARttiType.GetProperty('aBoolean');
  211. CheckEquals(AProperty.IsReadable, true);
  212. AProperty := ARttiType.GetProperty('aGetBoolean');
  213. CheckEquals(AProperty.IsReadable, true);
  214. AProperty := ARttiType.GetProperty('aWriteOnly');
  215. CheckEquals(AProperty.IsReadable, False);
  216. finally
  217. c.Free;
  218. end;
  219. end;
  220. procedure TTestCase1.TestIsWritable;
  221. var
  222. c: TRttiContext;
  223. ARttiType: TRttiType;
  224. AProperty: TRttiProperty;
  225. begin
  226. c := TRttiContext.Create;
  227. try
  228. ARttiType := c.GetType(TTestValueClass);
  229. AProperty := ARttiType.GetProperty('aBoolean');
  230. CheckEquals(AProperty.IsWritable, true);
  231. AProperty := ARttiType.GetProperty('aGetBoolean');
  232. CheckEquals(AProperty.IsWritable, false);
  233. AProperty := ARttiType.GetProperty('aWriteOnly');
  234. CheckEquals(AProperty.IsWritable, True);
  235. finally
  236. c.Free;
  237. end;
  238. end;
  239. procedure TTestCase1.TestPropGetValueBoolean;
  240. var
  241. ATestClass : TTestValueClass;
  242. c: TRttiContext;
  243. ARttiType: TRttiType;
  244. AProperty: TRttiProperty;
  245. AValue: TValue;
  246. begin
  247. c := TRttiContext.Create;
  248. try
  249. ATestClass := TTestValueClass.Create;
  250. ATestClass.ABoolean := true;
  251. try
  252. ARttiType := c.GetType(ATestClass.ClassInfo);
  253. Check(assigned(ARttiType));
  254. AProperty := ARttiType.GetProperty('aBoolean');
  255. AValue := AProperty.GetValue(ATestClass);
  256. CheckEquals(true,AValue.AsBoolean);
  257. ATestClass.ABoolean := false;
  258. CheckEquals(true, AValue.AsBoolean);
  259. CheckEquals('True', AValue.ToString);
  260. CheckEquals(True, AValue.IsOrdinal);
  261. CheckEquals(1, AValue.AsOrdinal);
  262. finally
  263. AtestClass.Free;
  264. end;
  265. CheckEquals(True,AValue.AsBoolean);
  266. finally
  267. c.Free;
  268. end;
  269. end;
  270. procedure TTestCase1.TestPropGetValueShortString;
  271. var
  272. ATestClass : TTestValueClass;
  273. c: TRttiContext;
  274. ARttiType: TRttiType;
  275. AProperty: TRttiProperty;
  276. AValue: TValue;
  277. begin
  278. c := TRttiContext.Create;
  279. try
  280. ATestClass := TTestValueClass.Create;
  281. ATestClass.AShortString := 'Hello World';
  282. try
  283. ARttiType := c.GetType(ATestClass.ClassInfo);
  284. Check(assigned(ARttiType));
  285. AProperty := ARttiType.GetProperty('aShortString');
  286. AValue := AProperty.GetValue(ATestClass);
  287. CheckEquals('Hello World',AValue.AsString);
  288. ATestClass.AShortString := 'Foobar';
  289. CheckEquals('Hello World', AValue.AsString);
  290. CheckEquals(False, AValue.IsOrdinal);
  291. CheckEquals(False, AValue.IsObject);
  292. CheckEquals(False, AValue.IsArray);
  293. CheckEquals(False, AValue.IsClass);
  294. finally
  295. AtestClass.Free;
  296. end;
  297. CheckEquals('Hello World',AValue.AsString);
  298. finally
  299. c.Free;
  300. end;
  301. end;
  302. procedure TTestCase1.TestPropGetValueInteger;
  303. var
  304. ATestClass : TTestValueClass;
  305. c: TRttiContext;
  306. ARttiType: TRttiType;
  307. AProperty: TRttiProperty;
  308. AValue: TValue;
  309. begin
  310. c := TRttiContext.Create;
  311. try
  312. ATestClass := TTestValueClass.Create;
  313. ATestClass.AInteger := 472349;
  314. try
  315. ARttiType := c.GetType(ATestClass.ClassInfo);
  316. Check(assigned(ARttiType));
  317. AProperty := ARttiType.GetProperty('ainteger');
  318. AValue := AProperty.GetValue(ATestClass);
  319. CheckEquals(472349,AValue.AsInteger);
  320. ATestClass.AInteger := 12;
  321. CheckEquals(472349, AValue.AsInteger);
  322. CheckEquals('472349', AValue.ToString);
  323. CheckEquals(True, AValue.IsOrdinal);
  324. finally
  325. AtestClass.Free;
  326. end;
  327. CheckEquals(472349,AValue.AsInteger);
  328. finally
  329. c.Free;
  330. end;
  331. end;
  332. procedure TTestCase1.TestPropGetValueString;
  333. var
  334. ATestClass : TTestValueClass;
  335. c: TRttiContext;
  336. ARttiType: TRttiType;
  337. AProperty: TRttiProperty;
  338. AValue: TValue;
  339. i: int64;
  340. begin
  341. c := TRttiContext.Create;
  342. try
  343. ATestClass := TTestValueClass.Create;
  344. ATestClass.AString := 'Hello World';
  345. try
  346. ARttiType := c.GetType(ATestClass.ClassInfo);
  347. Check(assigned(ARttiType));
  348. AProperty := ARttiType.GetProperty('astring');
  349. AValue := AProperty.GetValue(ATestClass);
  350. CheckEquals('Hello World',AValue.AsString);
  351. ATestClass.AString := 'Goodbye World';
  352. CheckEquals('Hello World',AValue.AsString);
  353. CheckEquals('Hello World',AValue.ToString);
  354. Check(TypeInfo(string)=AValue.TypeInfo);
  355. Check(AValue.TypeData=GetTypeData(AValue.TypeInfo));
  356. Check(AValue.IsEmpty=false);
  357. Check(AValue.IsObject=false);
  358. Check(AValue.IsClass=false);
  359. CheckEquals(AValue.IsOrdinal, false);
  360. CheckEquals(AValue.TryAsOrdinal(i), false);
  361. CheckEquals(AValue.IsType(TypeInfo(string)), true);
  362. CheckEquals(AValue.IsType(TypeInfo(integer)), false);
  363. CheckEquals(AValue.IsArray, false);
  364. finally
  365. AtestClass.Free;
  366. end;
  367. CheckEquals('Hello World',AValue.AsString);
  368. finally
  369. c.Free;
  370. end;
  371. end;
  372. procedure TTestCase1.TestPropGetValueProcBoolean;
  373. var
  374. ATestClass : TTestValueClass;
  375. c: TRttiContext;
  376. ARttiType: TRttiType;
  377. AProperty: TRttiProperty;
  378. AValue: TValue;
  379. begin
  380. c := TRttiContext.Create;
  381. try
  382. ATestClass := TTestValueClass.Create;
  383. ATestClass.ABoolean := true;
  384. try
  385. ARttiType := c.GetType(ATestClass.ClassInfo);
  386. Check(assigned(ARttiType));
  387. AProperty := ARttiType.GetProperty('aGetBoolean');
  388. AValue := AProperty.GetValue(ATestClass);
  389. CheckEquals(true,AValue.AsBoolean);
  390. finally
  391. AtestClass.Free;
  392. end;
  393. CheckEquals(True,AValue.AsBoolean);
  394. finally
  395. c.Free;
  396. end;
  397. end;
  398. procedure TTestCase1.TestPropGetValueProcShortString;
  399. var
  400. ATestClass : TTestValueClass;
  401. c: TRttiContext;
  402. ARttiType: TRttiType;
  403. AProperty: TRttiProperty;
  404. AValue: TValue;
  405. begin
  406. c := TRttiContext.Create;
  407. try
  408. ATestClass := TTestValueClass.Create;
  409. ATestClass.AShortString := 'Hello World';
  410. try
  411. ARttiType := c.GetType(ATestClass.ClassInfo);
  412. Check(assigned(ARttiType));
  413. AProperty := ARttiType.GetProperty('aGetShortString');
  414. AValue := AProperty.GetValue(ATestClass);
  415. CheckEquals('Hello World',AValue.AsString);
  416. finally
  417. AtestClass.Free;
  418. end;
  419. CheckEquals('Hello World',AValue.AsString);
  420. finally
  421. c.Free;
  422. end;
  423. end;
  424. procedure TTestCase1.TestPropSetValueString;
  425. var
  426. ATestClass : TTestValueClass;
  427. c: TRttiContext;
  428. ARttiType: TRttiType;
  429. AProperty: TRttiProperty;
  430. AValue: TValue;
  431. s: string;
  432. begin
  433. c := TRttiContext.Create;
  434. try
  435. ATestClass := TTestValueClass.Create;
  436. try
  437. ARttiType := c.GetType(ATestClass.ClassInfo);
  438. AProperty := ARttiType.GetProperty('astring');
  439. s := 'ipse lorem or something like that';
  440. TValue.Make(@s, TypeInfo(string), AValue);
  441. AProperty.SetValue(ATestClass, AValue);
  442. CheckEquals(ATestClass.AString, s);
  443. s := 'Another string';
  444. CheckEquals(ATestClass.AString, 'ipse lorem or something like that');
  445. finally
  446. AtestClass.Free;
  447. end;
  448. finally
  449. c.Free;
  450. end;
  451. end;
  452. procedure TTestCase1.TestPropSetValueInteger;
  453. var
  454. ATestClass : TTestValueClass;
  455. c: TRttiContext;
  456. ARttiType: TRttiType;
  457. AProperty: TRttiProperty;
  458. AValue: TValue;
  459. i: integer;
  460. begin
  461. c := TRttiContext.Create;
  462. try
  463. ATestClass := TTestValueClass.Create;
  464. try
  465. ARttiType := c.GetType(ATestClass.ClassInfo);
  466. AProperty := ARttiType.GetProperty('aInteger');
  467. i := -43573;
  468. TValue.Make(@i, TypeInfo(Integer), AValue);
  469. AProperty.SetValue(ATestClass, AValue);
  470. CheckEquals(ATestClass.AInteger, i);
  471. i := 1;
  472. CheckEquals(ATestClass.AInteger, -43573);
  473. finally
  474. AtestClass.Free;
  475. end;
  476. finally
  477. c.Free;
  478. end;
  479. end;
  480. procedure TTestCase1.TestPropSetValueBoolean;
  481. var
  482. ATestClass : TTestValueClass;
  483. c: TRttiContext;
  484. ARttiType: TRttiType;
  485. AProperty: TRttiProperty;
  486. AValue: TValue;
  487. b: boolean;
  488. begin
  489. c := TRttiContext.Create;
  490. try
  491. ATestClass := TTestValueClass.Create;
  492. try
  493. ARttiType := c.GetType(ATestClass.ClassInfo);
  494. AProperty := ARttiType.GetProperty('aboolean');
  495. b := true;
  496. TValue.Make(@b, TypeInfo(Boolean), AValue);
  497. AProperty.SetValue(ATestClass, AValue);
  498. CheckEquals(ATestClass.ABoolean, b);
  499. b := false;
  500. CheckEquals(ATestClass.ABoolean, true);
  501. TValue.Make(@b, TypeInfo(Boolean), AValue);
  502. AProperty.SetValue(ATestClass, AValue);
  503. CheckEquals(ATestClass.ABoolean, false);
  504. finally
  505. AtestClass.Free;
  506. end;
  507. finally
  508. c.Free;
  509. end;
  510. end;
  511. procedure TTestCase1.TestPropSetValueShortString;
  512. var
  513. ATestClass : TTestValueClass;
  514. c: TRttiContext;
  515. ARttiType: TRttiType;
  516. AProperty: TRttiProperty;
  517. AValue: TValue;
  518. s: string;
  519. ss: ShortString;
  520. begin
  521. c := TRttiContext.Create;
  522. try
  523. ATestClass := TTestValueClass.Create;
  524. try
  525. ARttiType := c.GetType(ATestClass.ClassInfo);
  526. AProperty := ARttiType.GetProperty('aShortString');
  527. s := 'ipse lorem or something like that';
  528. TValue.Make(@s, TypeInfo(String), AValue);
  529. AProperty.SetValue(ATestClass, AValue);
  530. CheckEquals(ATestClass.AShortString, s);
  531. s := 'Another string';
  532. CheckEquals(ATestClass.AShortString, 'ipse lorem or something like that');
  533. ss := 'Hello World';
  534. TValue.Make(@ss, TypeInfo(ShortString), AValue);
  535. AProperty.SetValue(ATestClass, AValue);
  536. CheckEquals(ATestClass.AShortString, ss);
  537. ss := 'Foobar';
  538. CheckEquals(ATestClass.AShortString, 'Hello World');
  539. finally
  540. AtestClass.Free;
  541. end;
  542. finally
  543. c.Free;
  544. end;
  545. end;
  546. procedure TTestCase1.TestPropGetValueProcInteger;
  547. var
  548. ATestClass : TTestValueClass;
  549. c: TRttiContext;
  550. ARttiType: TRttiType;
  551. AProperty: TRttiProperty;
  552. AValue: TValue;
  553. begin
  554. c := TRttiContext.Create;
  555. try
  556. ATestClass := TTestValueClass.Create;
  557. ATestClass.AInteger := 472349;
  558. try
  559. ARttiType := c.GetType(ATestClass.ClassInfo);
  560. Check(assigned(ARttiType));
  561. AProperty := ARttiType.GetProperty('agetinteger');
  562. AValue := AProperty.GetValue(ATestClass);
  563. CheckEquals(472349,AValue.AsInteger);
  564. finally
  565. AtestClass.Free;
  566. end;
  567. CheckEquals(472349,AValue.AsInteger);
  568. finally
  569. c.Free;
  570. end;
  571. end;
  572. procedure TTestCase1.TestPropGetValueProcString;
  573. var
  574. ATestClass : TTestValueClass;
  575. c: TRttiContext;
  576. ARttiType: TRttiType;
  577. AProperty: TRttiProperty;
  578. AValue: TValue;
  579. begin
  580. c := TRttiContext.Create;
  581. try
  582. ATestClass := TTestValueClass.Create;
  583. ATestClass.AString := 'Hello World';
  584. try
  585. ARttiType := c.GetType(ATestClass.ClassInfo);
  586. Check(assigned(ARttiType));
  587. AProperty := ARttiType.GetProperty('agetstring');
  588. AValue := AProperty.GetValue(ATestClass);
  589. CheckEquals('Hello World',AValue.AsString);
  590. finally
  591. AtestClass.Free;
  592. end;
  593. CheckEquals('Hello World',AValue.AsString);
  594. finally
  595. c.Free;
  596. end;
  597. end;
  598. procedure TTestCase1.TestTRttiTypeProperties;
  599. var
  600. c: TRttiContext;
  601. ARttiType: TRttiType;
  602. begin
  603. c := TRttiContext.Create;
  604. try
  605. ARttiType := c.GetType(TTestValueClass);
  606. Check(assigned(ARttiType));
  607. CheckEquals(ARttiType.Name,'TTestValueClass');
  608. Check(ARttiType.TypeKind=tkClass);
  609. // CheckEquals(ARttiType.IsPublicType,false);
  610. CheckEquals(ARttiType.TypeSize,SizeOf(TObject));
  611. CheckEquals(ARttiType.IsManaged,false);
  612. CheckEquals(ARttiType.BaseType.classname,'TRttiInstanceType');
  613. CheckEquals(ARttiType.IsInstance,True);
  614. CheckEquals(ARttiType.AsInstance.DeclaringUnitName,'tests.rtti');
  615. Check(ARttiType.BaseType.Name='TObject');
  616. Check(ARttiType.AsInstance.BaseType.Name='TObject');
  617. CheckEquals(ARttiType.IsOrdinal,False);
  618. CheckEquals(ARttiType.IsRecord,False);
  619. CheckEquals(ARttiType.IsSet,False);
  620. finally
  621. c.Free;
  622. end;
  623. end;
  624. procedure TTestCase1.GetTypeInteger;
  625. var
  626. LContext: TRttiContext;
  627. LType: TRttiType;
  628. begin
  629. LContext := TRttiContext.Create;
  630. LType := LContext.GetType(TypeInfo(integer));
  631. CheckEquals(LType.Name, 'LongInt');
  632. LContext.Free;
  633. end;
  634. procedure TTestCase1.GetClassProperties;
  635. var
  636. LContext: TRttiContext;
  637. LType: TRttiType;
  638. PropList: {$ifdef fpc}specialize{$endif} TArray<TRttiProperty>;
  639. begin
  640. LContext := TRttiContext.Create;
  641. LType := LContext.GetType(TypeInfo(TGetClassProperties));
  642. PropList := LType.GetProperties;
  643. CheckEquals(4, length(PropList));
  644. CheckEquals('PubPropRO', PropList[0].Name);
  645. CheckEquals('PubPropRW', PropList[1].Name);
  646. CheckEquals('PubPropSetRO', PropList[2].Name);
  647. CheckEquals('PubPropSetRW', PropList[3].Name);
  648. LContext.Free;
  649. end;
  650. procedure TTestCase1.GetClassPropertiesValue;
  651. var
  652. AGetClassProperties: TGetClassProperties;
  653. LContext: TRttiContext;
  654. LType: TRttiType;
  655. AValue: TValue;
  656. begin
  657. LContext := TRttiContext.Create;
  658. LType := LContext.GetType(TGetClassProperties);
  659. AGetClassProperties := TGetClassProperties.Create;
  660. try
  661. AGetClassProperties.PubPropRW:=12345;
  662. AValue := LType.GetProperty('PubPropRW').GetValue(AGetClassProperties);
  663. CheckEquals(12345, AValue.AsInteger);
  664. finally
  665. AGetClassProperties.Free;
  666. end;
  667. LContext.Free;
  668. end;
  669. procedure TTestCase1.TestIsManaged;
  670. begin
  671. CheckEquals(true, IsManaged(TypeInfo(ansistring)), 'IsManaged for tkAString');
  672. CheckEquals(true, IsManaged(TypeInfo(widestring)), 'IsManaged for tkWString');
  673. CheckEquals(true, IsManaged(TypeInfo(Variant)), 'IsManaged for tkVariant');
  674. CheckEquals(true, IsManaged(TypeInfo(TArrayOfManagedRec)),
  675. 'IsManaged for tkArray (with managed ElType)');
  676. CheckEquals(true, IsManaged(TypeInfo(TArrayOfString)),
  677. 'IsManaged for tkArray (with managed ElType)');
  678. CheckEquals(true, IsManaged(TypeInfo(TManagedRec)), 'IsManaged for tkRecord');
  679. {$ifdef fpc}
  680. CheckEquals(true, IsManaged(TypeInfo(TManagedRecOp)), 'IsManaged for tkRecord');
  681. {$endif}
  682. CheckEquals(true, IsManaged(TypeInfo(IInterface)), 'IsManaged for tkInterface');
  683. CheckEquals(true, IsManaged(TypeInfo(TManagedObj)), 'IsManaged for tkObject');
  684. {$ifdef fpc}
  685. CheckEquals(true, IsManaged(TypeInfo(specialize TArray<byte>)), 'IsManaged for tkDynArray');
  686. {$else}
  687. CheckEquals(true, IsManaged(TypeInfo(TArray<byte>)), 'IsManaged for tkDynArray');
  688. {$endif}
  689. CheckEquals(true, IsManaged(TypeInfo(unicodestring)), 'IsManaged for tkUString');
  690. CheckEquals(false, IsManaged(TypeInfo(shortstring)), 'IsManaged for tkSString');
  691. CheckEquals(false, IsManaged(TypeInfo(Byte)), 'IsManaged for tkInteger');
  692. CheckEquals(false, IsManaged(TypeInfo(Char)), 'IsManaged for tkChar');
  693. CheckEquals(false, IsManaged(TypeInfo(TTestEnum)), 'IsManaged for tkEnumeration');
  694. CheckEquals(false, IsManaged(TypeInfo(Single)), 'IsManaged for tkFloat');
  695. CheckEquals(false, IsManaged(TypeInfo(TTestSet)), 'IsManaged for tkSet');
  696. CheckEquals(false, IsManaged(TypeInfo(TTestMethod)), 'IsManaged for tkMethod');
  697. CheckEquals(false, IsManaged(TypeInfo(TArrayOfByte)),
  698. 'IsManaged for tkArray (with non managed ElType)');
  699. CheckEquals(false, IsManaged(TypeInfo(TArrayOfNonManagedRec)),
  700. 'IsManaged for tkArray (with non managed ElType)');
  701. CheckEquals(false, IsManaged(TypeInfo(TNonManagedRec)), 'IsManaged for tkRecord');
  702. CheckEquals(false, IsManaged(TypeInfo(TObject)), 'IsManaged for tkClass');
  703. CheckEquals(false, IsManaged(TypeInfo(TNonManagedObj)), 'IsManaged for tkObject');
  704. CheckEquals(false, IsManaged(TypeInfo(WideChar)), 'IsManaged for tkWChar');
  705. CheckEquals(false, IsManaged(TypeInfo(Boolean)), 'IsManaged for tkBool');
  706. CheckEquals(false, IsManaged(TypeInfo(Int64)), 'IsManaged for tkInt64');
  707. CheckEquals(false, IsManaged(TypeInfo(UInt64)), 'IsManaged for tkQWord');
  708. {$ifdef fpc}
  709. CheckEquals(false, IsManaged(TypeInfo(ICORBATest)), 'IsManaged for tkInterfaceRaw');
  710. {$endif}
  711. CheckEquals(false, IsManaged(TypeInfo(TTestProc)), 'IsManaged for tkProcVar');
  712. CheckEquals(false, IsManaged(TypeInfo(TTestHelper)), 'IsManaged for tkHelper');
  713. CheckEquals(false, IsManaged(TypeInfo(file)), 'IsManaged for tkFile');
  714. CheckEquals(false, IsManaged(TypeInfo(TClass)), 'IsManaged for tkClassRef');
  715. CheckEquals(false, IsManaged(TypeInfo(Pointer)), 'IsManaged for tkPointer');
  716. CheckEquals(false, IsManaged(nil), 'IsManaged for nil');
  717. end;
  718. initialization
  719. {$ifdef fpc}
  720. RegisterTest(TTestCase1);
  721. {$else fpc}
  722. RegisterTest(TTestCase1.Suite);
  723. {$endif fpc}
  724. end.