tests.rtti.pas 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375
  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. { TTestRTTI }
  17. TTestRTTI= class(TTestCase)
  18. published
  19. //procedure GetTypes;
  20. procedure GetTypeInteger;
  21. procedure GetTypePointer;
  22. procedure GetClassProperties;
  23. procedure GetClassPropertiesValue;
  24. procedure TestTRttiTypeProperties;
  25. procedure TestPropGetValueString;
  26. procedure TestPropGetValueInteger;
  27. procedure TestPropGetValueBoolean;
  28. procedure TestPropGetValueShortString;
  29. procedure TestPropGetValueProcString;
  30. procedure TestPropGetValueProcInteger;
  31. procedure TestPropGetValueProcBoolean;
  32. procedure TestPropGetValueProcShortString;
  33. procedure TestPropGetValueObject;
  34. procedure TestPropGetValueInterface;
  35. procedure TestPropGetValueFloat;
  36. procedure TestPropGetValueDynArray;
  37. procedure TestPropGetValueEnumeration;
  38. procedure TestPropGetValueChars;
  39. procedure TestPropGetValueRecord;
  40. procedure TestPropSetValueString;
  41. procedure TestPropSetValueInteger;
  42. procedure TestPropSetValueBoolean;
  43. procedure TestPropSetValueShortString;
  44. procedure TestPropSetValueObject;
  45. procedure TestPropSetValueInterface;
  46. procedure TestPropSetValueFloat;
  47. procedure TestPropSetValueDynArray;
  48. procedure TestPropSetValueEnumeration;
  49. procedure TestPropSetValueChars;
  50. procedure TestPropSetValueRecord;
  51. procedure TestGetValueStringCastError;
  52. procedure TestGetIsReadable;
  53. procedure TestIsWritable;
  54. procedure TestGetAttribute;
  55. procedure TestInterface;
  56. {$ifdef fpc}
  57. procedure TestInterfaceRaw;
  58. {$endif}
  59. procedure TestArray;
  60. procedure TestDynArray;
  61. procedure TestProcVar;
  62. procedure TestMethod;
  63. procedure TestRawThunk;
  64. private
  65. {$ifndef fpc}
  66. procedure Ignore(const aMsg: String);
  67. {$endif}
  68. end;
  69. { TTestExtendedRTTI }
  70. // Note: the tests assume that TObject has no RTTI associated with it.
  71. // The tests need to be adapted so they will work in both cases.
  72. TTestExtendedRTTI = class(TTestCase)
  73. Private
  74. FCtx: TRttiContext;
  75. Procedure AssertEquals(Msg : String; aExpected,aActual : TMemberVisibility); overload;
  76. Procedure AssertEquals(Msg : String; aExpected,aActual : TTypeKind);overload;
  77. procedure CheckField(aIdx: Integer; aData: TRttiField; aName: String; aKind: TTypeKind; aVisibility: TMemberVisibility;
  78. aStrict: Boolean=False);
  79. procedure CheckMethod(aPrefix: string; aIdx: Integer; aData: TRttiMethod; aName: String; aVisibility: TMemberVisibility;
  80. aStrict: Boolean=False);
  81. procedure CheckProperty(aIdx: Integer; aData: TRttiProperty; aName: String; aKind: TTypeKind; aVisibility: TMemberVisibility;
  82. isStrict: Boolean=False);
  83. public
  84. Procedure Setup; override;
  85. Procedure TearDown; override;
  86. end;
  87. { TTestClassExtendedRTTI }
  88. TTestClassExtendedRTTI = class(TTestExtendedRtti)
  89. published
  90. Procedure TestFields;
  91. Procedure TestDeclaredFields;
  92. Procedure TestProperties;
  93. Procedure TestDeclaredMethods;
  94. Procedure TestMethods;
  95. Procedure TestMethodByAddress;
  96. Procedure TestMethodsInherited;
  97. Procedure TestPrivateFieldAttributes;
  98. Procedure TestProtectedFieldAttributes;
  99. Procedure TestPublicFieldAttributes;
  100. Procedure TestPrivatePropertyAttributes;
  101. Procedure TestProtectedPropertyAttributes;
  102. Procedure TestPublicPropertyAttributes;
  103. Procedure TestPublishedPropertyAttributes;
  104. procedure TestGetStaticProperty;
  105. procedure TestSetStaticProperty;
  106. end;
  107. { TTestRecordExtendedRTTI }
  108. TTestRecordExtendedRTTI = class(TTestExtendedRtti)
  109. published
  110. Procedure TestFields;
  111. Procedure TestProperties;
  112. Procedure TestDeclaredMethods;
  113. Procedure TestMethods;
  114. Procedure TestPrivateFieldAttributes;
  115. Procedure TestPublicFieldAttributes;
  116. end;
  117. implementation
  118. uses
  119. Tests.Rtti.Util,
  120. {tests.rtti.exttypes, }
  121. tests.rtti.attrtypes2,
  122. tests.rtti.attrtypes,
  123. tests.rtti.types;
  124. { Note: GetTypes currently only returns those types that had been acquired using
  125. GetType, so GetTypes itself can't be really tested currently }
  126. (*procedure TTestRTTI.GetTypes;
  127. var
  128. LContext: TRttiContext;
  129. LType: TRttiType;
  130. IsTestCaseClassFound: boolean;
  131. begin
  132. LContext := TRttiContext.Create;
  133. { Enumerate all types declared in the application }
  134. for LType in LContext.GetTypes() do
  135. begin
  136. if LType.Name='TTestRTTI' then
  137. IsTestCaseClassFound:=true;
  138. end;
  139. LContext.Free;
  140. CheckTrue(IsTestCaseClassFound, 'RTTI information does not contain class of testcase.');
  141. end;*)
  142. {$ifndef fpc}
  143. procedure TTestRTTI.Ignore(const aMsg: string);
  144. begin
  145. { empty }
  146. end;
  147. {$endif}
  148. procedure TTestRTTI.TestGetValueStringCastError;
  149. var
  150. ATestClass : TTestValueClass;
  151. c: TRttiContext;
  152. ARttiType: TRttiType;
  153. AValue: TValue;
  154. i: integer;
  155. HadException: boolean;
  156. begin
  157. c := TRttiContext.Create;
  158. try
  159. ATestClass := TTestValueClass.Create;
  160. ATestClass.AString := '12';
  161. try
  162. ARttiType := c.GetType(ATestClass.ClassInfo);
  163. AValue := ARttiType.GetProperty('astring').GetValue(ATestClass);
  164. HadException := false;
  165. try
  166. i := AValue.AsInteger;
  167. except
  168. on E: Exception do
  169. if E.ClassType=EInvalidCast then
  170. HadException := true;
  171. end;
  172. Check(HadException, 'No or invalid exception on invalid cast');
  173. finally
  174. AtestClass.Free;
  175. end;
  176. finally
  177. c.Free;
  178. end;
  179. end;
  180. procedure TTestRTTI.TestGetIsReadable;
  181. var
  182. c: TRttiContext;
  183. ARttiType: TRttiType;
  184. AProperty: TRttiProperty;
  185. begin
  186. c := TRttiContext.Create;
  187. try
  188. ARttiType := c.GetType(TTestValueClass);
  189. AProperty := ARttiType.GetProperty('aBoolean');
  190. CheckEquals(AProperty.IsReadable, true);
  191. AProperty := ARttiType.GetProperty('aGetBoolean');
  192. CheckEquals(AProperty.IsReadable, true);
  193. AProperty := ARttiType.GetProperty('aWriteOnly');
  194. CheckEquals(AProperty.IsReadable, False);
  195. finally
  196. c.Free;
  197. end;
  198. end;
  199. procedure TTestRTTI.TestIsWritable;
  200. var
  201. c: TRttiContext;
  202. ARttiType: TRttiType;
  203. AProperty: TRttiProperty;
  204. begin
  205. c := TRttiContext.Create;
  206. try
  207. ARttiType := c.GetType(TTestValueClass);
  208. AProperty := ARttiType.GetProperty('aBoolean');
  209. CheckEquals(AProperty.IsWritable, true);
  210. AProperty := ARttiType.GetProperty('aGetBoolean');
  211. CheckEquals(AProperty.IsWritable, false);
  212. AProperty := ARttiType.GetProperty('aWriteOnly');
  213. CheckEquals(AProperty.IsWritable, True);
  214. finally
  215. c.Free;
  216. end;
  217. end;
  218. procedure TTestRTTI.TestGetAttribute;
  219. // TMyAnnotatedClass
  220. // TMyAttribute
  221. var
  222. c: TRttiContext;
  223. aType: TRttiType;
  224. aClass : TMyAnnotatedClass;
  225. custAttr : TCustomAttribute;
  226. myAttr : TMyAttribute absolute custattr;
  227. begin
  228. aType:=nil;
  229. custAttr:=Nil;
  230. c := TRttiContext.Create;
  231. try
  232. aClass:=TMyAnnotatedClass.Create;
  233. aType := c.GetType(aClass.ClassInfo);
  234. custAttr:=aType.GetAttribute(TMyAttribute);
  235. CheckEquals(custAttr.ClassType,TMyAttribute,'Correct class');
  236. CheckEquals('something',MyAttr.value,'Correct value');
  237. finally
  238. aClass.Free;
  239. // custAttr.Free;
  240. C.Free;
  241. end;
  242. end;
  243. procedure TTestRTTI.TestPropGetValueBoolean;
  244. var
  245. ATestClass : TTestValueClass;
  246. c: TRttiContext;
  247. ARttiType: TRttiType;
  248. AProperty: TRttiProperty;
  249. AValue: TValue;
  250. begin
  251. c := TRttiContext.Create;
  252. try
  253. ATestClass := TTestValueClass.Create;
  254. ATestClass.ABoolean := true;
  255. try
  256. ARttiType := c.GetType(ATestClass.ClassInfo);
  257. Check(assigned(ARttiType));
  258. AProperty := ARttiType.GetProperty('aBoolean');
  259. AValue := AProperty.GetValue(ATestClass);
  260. CheckEquals(true,AValue.AsBoolean);
  261. ATestClass.ABoolean := false;
  262. CheckEquals(true, AValue.AsBoolean);
  263. CheckEquals('True', AValue.ToString);
  264. CheckEquals(True, AValue.IsOrdinal);
  265. CheckEquals(1, AValue.AsOrdinal);
  266. finally
  267. AtestClass.Free;
  268. end;
  269. CheckEquals(True,AValue.AsBoolean);
  270. finally
  271. c.Free;
  272. end;
  273. end;
  274. procedure TTestRTTI.TestPropGetValueShortString;
  275. var
  276. ATestClass : TTestValueClass;
  277. c: TRttiContext;
  278. ARttiType: TRttiType;
  279. AProperty: TRttiProperty;
  280. AValue: TValue;
  281. begin
  282. c := TRttiContext.Create;
  283. try
  284. ATestClass := TTestValueClass.Create;
  285. ATestClass.AShortString := 'Hello World';
  286. try
  287. ARttiType := c.GetType(ATestClass.ClassInfo);
  288. Check(assigned(ARttiType));
  289. AProperty := ARttiType.GetProperty('aShortString');
  290. AValue := AProperty.GetValue(ATestClass);
  291. CheckEquals('Hello World',AValue.AsString);
  292. ATestClass.AShortString := 'Foobar';
  293. CheckEquals('Hello World', AValue.AsString);
  294. CheckEquals(False, AValue.IsOrdinal);
  295. CheckEquals(False, AValue.IsObject);
  296. CheckEquals(False, AValue.IsArray);
  297. CheckEquals(False, AValue.IsClass);
  298. finally
  299. AtestClass.Free;
  300. end;
  301. CheckEquals('Hello World',AValue.AsString);
  302. finally
  303. c.Free;
  304. end;
  305. end;
  306. procedure TTestRTTI.TestPropGetValueInteger;
  307. var
  308. ATestClass : TTestValueClass;
  309. c: TRttiContext;
  310. ARttiType: TRttiType;
  311. AProperty: TRttiProperty;
  312. AValue: TValue;
  313. begin
  314. c := TRttiContext.Create;
  315. try
  316. ATestClass := TTestValueClass.Create;
  317. ATestClass.AInteger := 472349;
  318. try
  319. ARttiType := c.GetType(ATestClass.ClassInfo);
  320. Check(assigned(ARttiType));
  321. AProperty := ARttiType.GetProperty('ainteger');
  322. AValue := AProperty.GetValue(ATestClass);
  323. CheckEquals(472349,AValue.AsInteger);
  324. ATestClass.AInteger := 12;
  325. CheckEquals(472349, AValue.AsInteger);
  326. CheckEquals('472349', AValue.ToString);
  327. CheckEquals(True, AValue.IsOrdinal);
  328. finally
  329. AtestClass.Free;
  330. end;
  331. CheckEquals(472349,AValue.AsInteger);
  332. finally
  333. c.Free;
  334. end;
  335. end;
  336. procedure TTestRTTI.TestPropGetValueString;
  337. var
  338. ATestClass : TTestValueClass;
  339. c: TRttiContext;
  340. ARttiType: TRttiType;
  341. AProperty: TRttiProperty;
  342. AValue: TValue;
  343. i: int64;
  344. begin
  345. c := TRttiContext.Create;
  346. try
  347. ATestClass := TTestValueClass.Create;
  348. ATestClass.AString := 'Hello World';
  349. try
  350. ARttiType := c.GetType(ATestClass.ClassInfo);
  351. Check(assigned(ARttiType));
  352. AProperty := ARttiType.GetProperty('astring');
  353. AValue := AProperty.GetValue(ATestClass);
  354. CheckEquals('Hello World',AValue.AsString);
  355. ATestClass.AString := 'Goodbye World';
  356. CheckEquals('Hello World',AValue.AsString);
  357. CheckEquals('Hello World',AValue.ToString);
  358. Check(TypeInfo(string)=AValue.TypeInfo);
  359. Check(AValue.TypeData=GetTypeData(AValue.TypeInfo));
  360. Check(AValue.IsEmpty=false);
  361. Check(AValue.IsObject=false);
  362. Check(AValue.IsClass=false);
  363. CheckEquals(AValue.IsOrdinal, false);
  364. CheckEquals(AValue.TryAsOrdinal(i), false);
  365. CheckEquals(AValue.IsType(TypeInfo(string)), true);
  366. CheckEquals(AValue.IsType(TypeInfo(integer)), false);
  367. CheckEquals(AValue.IsArray, false);
  368. finally
  369. AtestClass.Free;
  370. end;
  371. CheckEquals('Hello World',AValue.AsString);
  372. finally
  373. c.Free;
  374. end;
  375. end;
  376. procedure TTestRTTI.TestPropGetValueProcBoolean;
  377. var
  378. ATestClass : TTestValueClass;
  379. c: TRttiContext;
  380. ARttiType: TRttiType;
  381. AProperty: TRttiProperty;
  382. AValue: TValue;
  383. begin
  384. c := TRttiContext.Create;
  385. try
  386. ATestClass := TTestValueClass.Create;
  387. ATestClass.ABoolean := true;
  388. try
  389. ARttiType := c.GetType(ATestClass.ClassInfo);
  390. Check(assigned(ARttiType));
  391. AProperty := ARttiType.GetProperty('aGetBoolean');
  392. AValue := AProperty.GetValue(ATestClass);
  393. CheckEquals(true,AValue.AsBoolean);
  394. finally
  395. AtestClass.Free;
  396. end;
  397. CheckEquals(True,AValue.AsBoolean);
  398. finally
  399. c.Free;
  400. end;
  401. end;
  402. procedure TTestRTTI.TestPropGetValueProcShortString;
  403. var
  404. ATestClass : TTestValueClass;
  405. c: TRttiContext;
  406. ARttiType: TRttiType;
  407. AProperty: TRttiProperty;
  408. AValue: TValue;
  409. begin
  410. c := TRttiContext.Create;
  411. try
  412. ATestClass := TTestValueClass.Create;
  413. ATestClass.AShortString := 'Hello World';
  414. try
  415. ARttiType := c.GetType(ATestClass.ClassInfo);
  416. Check(assigned(ARttiType));
  417. AProperty := ARttiType.GetProperty('aGetShortString');
  418. AValue := AProperty.GetValue(ATestClass);
  419. CheckEquals('Hello World',AValue.AsString);
  420. finally
  421. AtestClass.Free;
  422. end;
  423. CheckEquals('Hello World',AValue.AsString);
  424. finally
  425. c.Free;
  426. end;
  427. end;
  428. procedure TTestRTTI.TestPropGetValueObject;
  429. var
  430. ATestClass : TTestValueClass;
  431. c: TRttiContext;
  432. ARttiType: TRttiType;
  433. AProperty: TRttiProperty;
  434. AValue: TValue;
  435. O: TObject;
  436. begin
  437. c := TRttiContext.Create;
  438. O := TObject.Create;
  439. try
  440. ATestClass := TTestValueClass.Create;
  441. ATestClass.AObject := O;
  442. try
  443. ARttiType := c.GetType(ATestClass.ClassInfo);
  444. Check(assigned(ARttiType));
  445. AProperty := ARttiType.GetProperty('AObject');
  446. AValue := AProperty.GetValue(ATestClass);
  447. CheckEquals(O.GetHashCode, AValue.AsObject.GetHashCode);
  448. finally
  449. AtestClass.Free;
  450. end;
  451. CheckEquals(O.GetHashCode, AValue.AsObject.GetHashCode);
  452. finally
  453. c.Free;
  454. O.Free;
  455. end;
  456. end;
  457. procedure TTestRTTI.TestPropGetValueInterface;
  458. var
  459. ATestClass : TTestValueClass;
  460. c: TRttiContext;
  461. ARttiType: TRttiType;
  462. AProperty: TRttiProperty;
  463. AValue: TValue;
  464. i: IInterface;
  465. begin
  466. c := TRttiContext.Create;
  467. i := TInterfacedObject.Create;
  468. try
  469. ATestClass := TTestValueClass.Create;
  470. ATestClass.AUnknown := i;
  471. try
  472. ARttiType := c.GetType(ATestClass.ClassInfo);
  473. Check(assigned(ARttiType));
  474. AProperty := ARttiType.GetProperty('AUnknown');
  475. AValue := AProperty.GetValue(ATestClass);
  476. Check(i = AValue.AsInterface);
  477. finally
  478. AtestClass.Free;
  479. end;
  480. Check(i = AValue.AsInterface);
  481. finally
  482. c.Free;
  483. end;
  484. end;
  485. procedure TTestRTTI.TestPropGetValueFloat;
  486. var
  487. ATestClass : TTestValueClass;
  488. c: TRttiContext;
  489. ARttiType: TRttiType;
  490. AProperty: TRttiProperty;
  491. AValueS, AValueD, AValueE, AValueC, AValueCm: TValue;
  492. begin
  493. c := TRttiContext.Create;
  494. try
  495. ATestClass := TTestValueClass.Create;
  496. ATestClass.ASingle := 1.1;
  497. ATestClass.ADouble := 2.2;
  498. ATestClass.AExtended := 3.3;
  499. ATestClass.ACurrency := 4;
  500. ATestClass.AComp := 5;
  501. try
  502. ARttiType := c.GetType(ATestClass.ClassInfo);
  503. Check(assigned(ARttiType));
  504. AProperty := ARttiType.GetProperty('ASingle');
  505. AValueS := AProperty.GetValue(ATestClass);
  506. CheckEquals(1.1, AValueS.AsExtended, 0.001);
  507. AProperty := ARttiType.GetProperty('ADouble');
  508. AValueD := AProperty.GetValue(ATestClass);
  509. CheckEquals(2.2, AValueD.AsExtended, 0.001);
  510. AProperty := ARttiType.GetProperty('AExtended');
  511. AValueE := AProperty.GetValue(ATestClass);
  512. CheckEquals(3.3, AValueE.AsExtended, 0.001);
  513. AProperty := ARttiType.GetProperty('ACurrency');
  514. AValueC := AProperty.GetValue(ATestClass);
  515. CheckEquals(4.0, AValueC.AsExtended, 0.001);
  516. AProperty := ARttiType.GetProperty('AComp');
  517. AValueCm := AProperty.GetValue(ATestClass);
  518. CheckEquals(5.0, AValueCm.AsExtended, 0.001);
  519. finally
  520. AtestClass.Free;
  521. end;
  522. CheckEquals(1.1, AValueS.AsExtended, 0.001);
  523. CheckEquals(2.2, AValueD.AsExtended, 0.001);
  524. CheckEquals(3.3, AValueE.AsExtended, 0.001);
  525. CheckEquals(4.0, AValueC.AsExtended, 0.001);
  526. CheckEquals(5.0, AValueCm.AsExtended, 0.001);
  527. finally
  528. c.Free;
  529. end;
  530. end;
  531. procedure TTestRTTI.TestPropGetValueDynArray;
  532. var
  533. ATestClass : TTestValueClass;
  534. c: TRttiContext;
  535. ARttiType: TRttiType;
  536. AProperty: TRttiProperty;
  537. AValue: TValue;
  538. A: TTestDynArray;
  539. begin
  540. c := TRttiContext.Create;
  541. A := [1, 2, 3, 4];
  542. try
  543. ATestClass := TTestValueClass.Create;
  544. ATestClass.AArray := A;
  545. try
  546. ARttiType := c.GetType(ATestClass.ClassInfo);
  547. Check(assigned(ARttiType));
  548. AProperty := ARttiType.GetProperty('AArray');
  549. AValue := AProperty.GetValue(ATestClass);
  550. CheckEquals(A[0], AValue.GetArrayElement(0).AsInteger);
  551. CheckEquals(A[1], AValue.GetArrayElement(1).AsInteger);
  552. CheckEquals(A[2], AValue.GetArrayElement(2).AsInteger);
  553. CheckEquals(A[3], AValue.GetArrayElement(3).AsInteger);
  554. finally
  555. AtestClass.Free;
  556. end;
  557. finally
  558. c.Free;
  559. end;
  560. end;
  561. procedure TTestRTTI.TestPropGetValueEnumeration;
  562. var
  563. ATestClass : TTestValueClass;
  564. c: TRttiContext;
  565. ARttiType: TRttiType;
  566. AProperty: TRttiProperty;
  567. AValue: TValue;
  568. begin
  569. c := TRttiContext.Create;
  570. try
  571. ATestClass := TTestValueClass.Create;
  572. ATestClass.AEnumeration := en3;
  573. try
  574. ARttiType := c.GetType(ATestClass.ClassInfo);
  575. Check(assigned(ARttiType));
  576. AProperty := ARttiType.GetProperty('AEnumeration');
  577. AValue := AProperty.GetValue(ATestClass);
  578. CheckEquals(Ord(en3),AValue.AsOrdinal);
  579. ATestClass.AEnumeration := en1;
  580. CheckEquals(Ord(en3), AValue.AsOrdinal);
  581. CheckEquals('en3', AValue.ToString);
  582. CheckEquals(True, AValue.IsOrdinal);
  583. finally
  584. AtestClass.Free;
  585. end;
  586. CheckEquals(Ord(en3),AValue.AsOrdinal);
  587. finally
  588. c.Free;
  589. end;
  590. end;
  591. procedure TTestRTTI.TestPropGetValueChars;
  592. var
  593. ATestClass : TTestValueClass;
  594. c: TRttiContext;
  595. ARttiType: TRttiType;
  596. AProperty: TRttiProperty;
  597. AValueC, AValueW: TValue;
  598. begin
  599. c := TRttiContext.Create;
  600. try
  601. ATestClass := TTestValueClass.Create;
  602. ATestClass.AChar := 'C';
  603. ATestClass.AWideChar := 'W';
  604. try
  605. ARttiType := c.GetType(ATestClass.ClassInfo);
  606. Check(assigned(ARttiType));
  607. AProperty := ARttiType.GetProperty('AChar');
  608. AValueC := AProperty.GetValue(ATestClass);
  609. CheckEquals('C',AValueC.AsAnsiChar);
  610. ATestClass.AChar := 'N';
  611. CheckEquals('C', AValueC.AsAnsiChar);
  612. CheckEquals('C', AValueC.ToString);
  613. CheckEquals(True, AValueC.IsOrdinal);
  614. AProperty := ARttiType.GetProperty('AWideChar');
  615. AValueW := AProperty.GetValue(ATestClass);
  616. CheckEquals('W',AValueW.AsWideChar);
  617. ATestClass.AWideChar := 'Z';
  618. CheckEquals('W', AValueW.AsWideChar);
  619. CheckEquals('W', AValueW.ToString);
  620. CheckEquals(True, AValueW.IsOrdinal);
  621. finally
  622. AtestClass.Free;
  623. end;
  624. CheckEquals('C',AValueC.AsAnsiChar);
  625. CheckEquals('W',AValueW.AsWideChar);
  626. finally
  627. c.Free;
  628. end;
  629. end;
  630. procedure TTestRTTI.TestPropGetValueRecord;
  631. var
  632. ATestClass : TRecordRttiClass;
  633. c: TRttiContext;
  634. ARttiType: TRttiType;
  635. AProperty: TRttiProperty;
  636. AValue: TValue;
  637. R : TRTTIRecord;
  638. P : PRTTIRecord;
  639. begin
  640. R.a:=23;
  641. r.B:=54;
  642. c := TRttiContext.Create(False);
  643. try
  644. ATestClass := TRecordRttiClass.Create;
  645. ATestClass.RecordProp:=R;
  646. try
  647. ARttiType := c.GetType(TRecordRttiClass);
  648. CheckNotNull(ARttiType,'Type');
  649. AProperty := ARttiType.GetProperty('RecordProp');
  650. CheckNotNull(aProperty,'Prop');
  651. AValue := AProperty.GetValue(ATestClass);
  652. P:=PRTTIRecord(AValue.GetReferenceToRawData);
  653. AssertEquals('a',23,P^.A);
  654. AssertEquals('b',54,P^.B);
  655. finally
  656. AtestClass.Free;
  657. end;
  658. finally
  659. c.Free;
  660. end;
  661. end;
  662. procedure TTestRTTI.TestPropSetValueString;
  663. var
  664. ATestClass : TTestValueClass;
  665. c: TRttiContext;
  666. ARttiType: TRttiType;
  667. AProperty: TRttiProperty;
  668. AValue: TValue;
  669. s: string;
  670. begin
  671. c := TRttiContext.Create;
  672. try
  673. ATestClass := TTestValueClass.Create;
  674. try
  675. ARttiType := c.GetType(ATestClass.ClassInfo);
  676. AProperty := ARttiType.GetProperty('astring');
  677. s := 'ipse lorem or something like that';
  678. TValue.Make(@s, TypeInfo(string), AValue);
  679. AProperty.SetValue(ATestClass, AValue);
  680. CheckEquals(ATestClass.AString, s);
  681. s := 'Another string';
  682. CheckEquals(ATestClass.AString, 'ipse lorem or something like that');
  683. finally
  684. AtestClass.Free;
  685. end;
  686. finally
  687. c.Free;
  688. end;
  689. end;
  690. procedure TTestRTTI.TestPropSetValueInteger;
  691. var
  692. ATestClass : TTestValueClass;
  693. c: TRttiContext;
  694. ARttiType: TRttiType;
  695. AProperty: TRttiProperty;
  696. AValue: TValue;
  697. i: integer;
  698. begin
  699. c := TRttiContext.Create;
  700. try
  701. ATestClass := TTestValueClass.Create;
  702. try
  703. ARttiType := c.GetType(ATestClass.ClassInfo);
  704. AProperty := ARttiType.GetProperty('aInteger');
  705. i := -43573;
  706. TValue.Make(@i, TypeInfo(Integer), AValue);
  707. AProperty.SetValue(ATestClass, AValue);
  708. CheckEquals(ATestClass.AInteger, i);
  709. i := 1;
  710. CheckEquals(ATestClass.AInteger, -43573);
  711. finally
  712. AtestClass.Free;
  713. end;
  714. finally
  715. c.Free;
  716. end;
  717. end;
  718. procedure TTestRTTI.TestPropSetValueBoolean;
  719. var
  720. ATestClass : TTestValueClass;
  721. c: TRttiContext;
  722. ARttiType: TRttiType;
  723. AProperty: TRttiProperty;
  724. AValue: TValue;
  725. b: boolean;
  726. begin
  727. c := TRttiContext.Create;
  728. try
  729. ATestClass := TTestValueClass.Create;
  730. try
  731. ARttiType := c.GetType(ATestClass.ClassInfo);
  732. AProperty := ARttiType.GetProperty('aboolean');
  733. b := true;
  734. TValue.Make(@b, TypeInfo(Boolean), AValue);
  735. AProperty.SetValue(ATestClass, AValue);
  736. CheckEquals(ATestClass.ABoolean, b);
  737. b := false;
  738. CheckEquals(ATestClass.ABoolean, true);
  739. TValue.Make(@b, TypeInfo(Boolean), AValue);
  740. AProperty.SetValue(ATestClass, AValue);
  741. CheckEquals(ATestClass.ABoolean, false);
  742. finally
  743. AtestClass.Free;
  744. end;
  745. finally
  746. c.Free;
  747. end;
  748. end;
  749. procedure TTestRTTI.TestPropSetValueShortString;
  750. var
  751. ATestClass : TTestValueClass;
  752. c: TRttiContext;
  753. ARttiType: TRttiType;
  754. AProperty: TRttiProperty;
  755. AValue: TValue;
  756. s: string;
  757. ss: ShortString;
  758. begin
  759. c := TRttiContext.Create;
  760. try
  761. ATestClass := TTestValueClass.Create;
  762. try
  763. ARttiType := c.GetType(ATestClass.ClassInfo);
  764. AProperty := ARttiType.GetProperty('aShortString');
  765. s := 'ipse lorem or something like that';
  766. TValue.Make(@s, TypeInfo(String), AValue);
  767. AProperty.SetValue(ATestClass, AValue);
  768. CheckEquals(ATestClass.AShortString, s);
  769. s := 'Another string';
  770. CheckEquals(ATestClass.AShortString, 'ipse lorem or something like that');
  771. ss := 'Hello World';
  772. TValue.Make(@ss, TypeInfo(ShortString), AValue);
  773. AProperty.SetValue(ATestClass, AValue);
  774. CheckEquals(ATestClass.AShortString, ss);
  775. ss := 'Foobar';
  776. CheckEquals(ATestClass.AShortString, 'Hello World');
  777. AProperty.SetValue(ATestClass, 'Another string');
  778. CheckEquals(ATestClass.AShortString, 'Another string');
  779. finally
  780. AtestClass.Free;
  781. end;
  782. finally
  783. c.Free;
  784. end;
  785. end;
  786. procedure TTestRTTI.TestPropSetValueObject;
  787. var
  788. ATestClass : TTestValueClass;
  789. c: TRttiContext;
  790. ARttiType: TRttiType;
  791. AProperty: TRttiProperty;
  792. AValue: TValue;
  793. O: TObject;
  794. TypeInfo: PTypeInfo;
  795. begin
  796. c := TRttiContext.Create;
  797. try
  798. ATestClass := TTestValueClass.Create;
  799. try
  800. ARttiType := c.GetType(ATestClass.ClassInfo);
  801. AProperty := ARttiType.GetProperty('AObject');
  802. TypeInfo := GetPropInfo(ATestClass, 'AObject')^.PropType{$ifndef fpc}^{$endif};
  803. O := TPersistent.Create;
  804. TValue.Make(@O, TypeInfo, AValue);
  805. AProperty.SetValue(ATestClass, AValue);
  806. CheckEquals(ATestClass.AObject.GetHashCode, O.GetHashCode);
  807. O.Free;
  808. O := TPersistent.Create;
  809. AProperty.SetValue(ATestClass, O);
  810. CheckEquals(ATestClass.AObject.GetHashCode, O.GetHashCode);
  811. O.Free;
  812. finally
  813. AtestClass.Free;
  814. end;
  815. finally
  816. c.Free;
  817. end;
  818. end;
  819. procedure TTestRTTI.TestPropSetValueInterface;
  820. var
  821. ATestClass : TTestValueClass;
  822. c: TRttiContext;
  823. ARttiType: TRttiType;
  824. AProperty: TRttiProperty;
  825. AValue: TValue;
  826. TypeInfo: PTypeInfo;
  827. i: IInterface;
  828. begin
  829. c := TRttiContext.Create;
  830. try
  831. ATestClass := TTestValueClass.Create;
  832. try
  833. ARttiType := c.GetType(ATestClass.ClassInfo);
  834. AProperty := ARttiType.GetProperty('AUnknown');
  835. TypeInfo := GetPropInfo(ATestClass, 'AUnknown')^.PropType{$ifndef fpc}^{$endif};
  836. i := TInterfacedObject.Create;
  837. TValue.Make(@i, TypeInfo, AValue);
  838. AProperty.SetValue(ATestClass, AValue);
  839. Check(ATestClass.AUnknown = i);
  840. {$ifdef fpc}
  841. { Delphi does not provide an implicit assignment overload for IUnknown }
  842. i := TInterfacedObject.Create;
  843. AProperty.SetValue(ATestClass, i);
  844. Check(ATestClass.AUnknown = i);
  845. {$endif}
  846. finally
  847. AtestClass.Free;
  848. end;
  849. finally
  850. c.Free;
  851. end;
  852. end;
  853. procedure TTestRTTI.TestPropSetValueFloat;
  854. var
  855. ATestClass : TTestValueClass;
  856. c: TRttiContext;
  857. ARttiType: TRttiType;
  858. AProperty: TRttiProperty;
  859. AValue: TValue;
  860. TypeInfo: PTypeInfo;
  861. S: Single;
  862. D: Double;
  863. E: Extended;
  864. Cur: Currency;
  865. Cmp: Comp;
  866. begin
  867. c := TRttiContext.Create;
  868. try
  869. ATestClass := TTestValueClass.Create;
  870. try
  871. ARttiType := c.GetType(ATestClass.ClassInfo);
  872. AProperty := ARttiType.GetProperty('ASingle');
  873. TypeInfo := GetPropInfo(ATestClass, 'ASingle')^.PropType{$ifndef fpc}^{$endif};
  874. S := 1.1;
  875. TValue.Make(@S, TypeInfo, AValue);
  876. AProperty.SetValue(ATestClass, AValue);
  877. CheckEquals(S, ATestClass.ASingle, 0.001);
  878. S := 1.2;
  879. AProperty.SetValue(ATestClass, S);
  880. CheckEquals(S, ATestClass.ASingle, 0.001);
  881. AProperty := ARttiType.GetProperty('ADouble');
  882. TypeInfo := GetPropInfo(ATestClass, 'ADouble')^.PropType{$ifndef fpc}^{$endif};
  883. D := 2.1;
  884. TValue.Make(@D, TypeInfo, AValue);
  885. AProperty.SetValue(ATestClass, AValue);
  886. CheckEquals(D, ATestClass.ADouble, 0.001);
  887. D := 2.2;
  888. AProperty.SetValue(ATestClass, D);
  889. CheckEquals(D, ATestClass.ADouble, 0.001);
  890. AProperty := ARttiType.GetProperty('AExtended');
  891. TypeInfo := GetPropInfo(ATestClass, 'AExtended')^.PropType{$ifndef fpc}^{$endif};
  892. E := 3.1;
  893. TValue.Make(@E, TypeInfo, AValue);
  894. AProperty.SetValue(ATestClass, AValue);
  895. CheckEquals(E, ATestClass.AExtended, 0.001);
  896. E := 3.2;
  897. AProperty.SetValue(ATestClass, E);
  898. CheckEquals(E, ATestClass.AExtended, 0.001);
  899. AProperty := ARttiType.GetProperty('ACurrency');
  900. TypeInfo := GetPropInfo(ATestClass, 'ACurrency')^.PropType{$ifndef fpc}^{$endif};
  901. Cur := 40;
  902. TValue.Make(@Cur, TypeInfo, AValue);
  903. AProperty.SetValue(ATestClass, AValue);
  904. CheckEquals(Cur, ATestClass.ACurrency, 0.001);
  905. Cur := 41;
  906. AProperty.SetValue(ATestClass, Cur);
  907. CheckEquals(Cur, ATestClass.ACurrency, 0.001);
  908. AProperty := ARttiType.GetProperty('AComp');
  909. TypeInfo := GetPropInfo(ATestClass, 'AComp')^.PropType{$ifndef fpc}^{$endif};
  910. Cmp := 50;
  911. TValue.Make(@Cmp, TypeInfo, AValue);
  912. AProperty.SetValue(ATestClass, AValue);
  913. CheckEquals(Cmp, ATestClass.AComp, 0.001);
  914. Cmp := 51;
  915. AProperty.SetValue(ATestClass, Cmp);
  916. CheckEquals(Cmp, ATestClass.AComp, 0.001);
  917. finally
  918. AtestClass.Free;
  919. end;
  920. finally
  921. c.Free;
  922. end;
  923. end;
  924. procedure TTestRTTI.TestPropSetValueDynArray;
  925. var
  926. ATestClass : TTestValueClass;
  927. c: TRttiContext;
  928. ARttiType: TRttiType;
  929. AProperty: TRttiProperty;
  930. AValue: TValue;
  931. A: TTestDynArray;
  932. TypeInfo: PTypeInfo;
  933. i: Integer;
  934. begin
  935. c := TRttiContext.Create;
  936. try
  937. ATestClass := TTestValueClass.Create;
  938. try
  939. ARttiType := c.GetType(ATestClass.ClassInfo);
  940. AProperty := ARttiType.GetProperty('AArray');
  941. TypeInfo := GetPropInfo(ATestClass, 'AArray')^.PropType{$ifndef fpc}^{$endif};
  942. A := [1, 2, 3, 4, 5];
  943. TValue.Make(@A, TypeInfo, AValue);
  944. AProperty.SetValue(ATestClass, AValue);
  945. for i := 0 to High(A) do
  946. CheckEquals(A[i], ATestClass.AArray[i]);
  947. finally
  948. AtestClass.Free;
  949. end;
  950. finally
  951. c.Free;
  952. end;
  953. end;
  954. procedure TTestRTTI.TestPropSetValueEnumeration;
  955. var
  956. ATestClass : TTestValueClass;
  957. c: TRttiContext;
  958. ARttiType: TRttiType;
  959. AProperty: TRttiProperty;
  960. AValue: TValue;
  961. E: TTestEnumeration;
  962. begin
  963. c := TRttiContext.Create;
  964. try
  965. ATestClass := TTestValueClass.Create;
  966. try
  967. ARttiType := c.GetType(ATestClass.ClassInfo);
  968. AProperty := ARttiType.GetProperty('AEnumeration');
  969. E := en2;
  970. TValue.Make(@E, TypeInfo(TTestEnumeration), AValue);
  971. AProperty.SetValue(ATestClass, AValue);
  972. CheckEquals(Ord(E), Ord(ATestClass.AEnumeration));
  973. finally
  974. AtestClass.Free;
  975. end;
  976. finally
  977. c.Free;
  978. end;
  979. end;
  980. procedure TTestRTTI.TestPropSetValueChars;
  981. var
  982. ATestClass : TTestValueClass;
  983. c: TRttiContext;
  984. ARttiType: TRttiType;
  985. AProperty: TRttiProperty;
  986. AValueC, AValueW: TValue;
  987. begin
  988. c := TRttiContext.Create;
  989. try
  990. ATestClass := TTestValueClass.Create;
  991. ATestClass.AChar := 'C';
  992. ATestClass.AWideChar := 'W';
  993. try
  994. ARttiType := c.GetType(ATestClass.ClassInfo);
  995. Check(assigned(ARttiType));
  996. AProperty := ARttiType.GetProperty('AChar');
  997. AValueC := AProperty.GetValue(ATestClass);
  998. CheckEquals('C', AValueC.AsAnsiChar);
  999. AProperty := ARttiType.GetProperty('AWideChar');
  1000. AValueW := AProperty.GetValue(ATestClass);
  1001. CheckEquals('W', AValueW.AsWideChar);
  1002. finally
  1003. AtestClass.Free;
  1004. end;
  1005. CheckEquals('C', AValueC.AsAnsiChar);
  1006. CheckEquals('W', AValueW.AsWideChar);
  1007. finally
  1008. c.Free;
  1009. end;
  1010. end;
  1011. procedure TTestRTTI.TestPropSetValueRecord;
  1012. var
  1013. ATestClass : TRecordRttiClass;
  1014. c: TRttiContext;
  1015. ARttiType: TRttiType;
  1016. AProperty: TRttiProperty;
  1017. AValue: TValue;
  1018. R : TRTTIRecord;
  1019. P : PRTTIRecord;
  1020. begin
  1021. R.a:=23;
  1022. r.B:=54;
  1023. c := TRttiContext.Create(False);
  1024. try
  1025. ATestClass := TRecordRttiClass.Create;
  1026. ATestClass.RecordProp:=R;
  1027. try
  1028. ARttiType := c.GetType(TRecordRttiClass);
  1029. CheckNotNull(ARttiType,'Type');
  1030. AProperty := ARttiType.GetProperty('RecordProp');
  1031. CheckNotNull(aProperty,'Prop');
  1032. AProperty.SetValue(ATestClass,TValue.specialize From<TRttiRecord>(R));
  1033. AssertEquals('a',23,aTestClass.RecordProp.A);
  1034. AssertEquals('b',54,aTestClass.RecordProp.B);
  1035. finally
  1036. AtestClass.Free;
  1037. end;
  1038. finally
  1039. c.Free;
  1040. end;
  1041. end;
  1042. procedure TTestRTTI.TestPropGetValueProcInteger;
  1043. var
  1044. ATestClass : TTestValueClass;
  1045. c: TRttiContext;
  1046. ARttiType: TRttiType;
  1047. AProperty: TRttiProperty;
  1048. AValue: TValue;
  1049. begin
  1050. c := TRttiContext.Create;
  1051. try
  1052. ATestClass := TTestValueClass.Create;
  1053. ATestClass.AInteger := 472349;
  1054. try
  1055. ARttiType := c.GetType(ATestClass.ClassInfo);
  1056. Check(assigned(ARttiType));
  1057. AProperty := ARttiType.GetProperty('agetinteger');
  1058. AValue := AProperty.GetValue(ATestClass);
  1059. CheckEquals(472349,AValue.AsInteger);
  1060. finally
  1061. AtestClass.Free;
  1062. end;
  1063. CheckEquals(472349,AValue.AsInteger);
  1064. finally
  1065. c.Free;
  1066. end;
  1067. end;
  1068. procedure TTestRTTI.TestPropGetValueProcString;
  1069. var
  1070. ATestClass : TTestValueClass;
  1071. c: TRttiContext;
  1072. ARttiType: TRttiType;
  1073. AProperty: TRttiProperty;
  1074. AValue: TValue;
  1075. begin
  1076. c := TRttiContext.Create;
  1077. try
  1078. ATestClass := TTestValueClass.Create;
  1079. ATestClass.AString := 'Hello World';
  1080. try
  1081. ARttiType := c.GetType(ATestClass.ClassInfo);
  1082. Check(assigned(ARttiType));
  1083. AProperty := ARttiType.GetProperty('agetstring');
  1084. AValue := AProperty.GetValue(ATestClass);
  1085. CheckEquals('Hello World',AValue.AsString);
  1086. finally
  1087. AtestClass.Free;
  1088. end;
  1089. CheckEquals('Hello World',AValue.AsString);
  1090. finally
  1091. c.Free;
  1092. end;
  1093. end;
  1094. procedure TTestRTTI.TestTRttiTypeProperties;
  1095. var
  1096. c: TRttiContext;
  1097. ARttiType: TRttiType;
  1098. begin
  1099. c := TRttiContext.Create;
  1100. try
  1101. ARttiType := c.GetType(TTestValueClass);
  1102. Check(assigned(ARttiType));
  1103. CheckEquals(ARttiType.Name,'TTestValueClass');
  1104. Check(ARttiType.TypeKind=tkClass);
  1105. // CheckEquals(ARttiType.IsPublicType,false);
  1106. CheckEquals(ARttiType.TypeSize,SizeOf(TObject));
  1107. CheckEquals(ARttiType.IsManaged,false);
  1108. CheckEquals(ARttiType.BaseType.classname,'TRttiInstanceType');
  1109. CheckEquals(ARttiType.IsInstance,True);
  1110. CheckEquals(ARttiType.AsInstance.DeclaringUnitName,'tests.rtti.types');
  1111. Check(ARttiType.BaseType.Name='TObject');
  1112. Check(ARttiType.AsInstance.BaseType.Name='TObject');
  1113. CheckEquals(ARttiType.IsOrdinal,False);
  1114. CheckEquals(ARttiType.IsRecord,False);
  1115. CheckEquals(ARttiType.IsSet,False);
  1116. finally
  1117. c.Free;
  1118. end;
  1119. end;
  1120. procedure TTestRTTI.GetTypeInteger;
  1121. var
  1122. LContext: TRttiContext;
  1123. LType: TRttiType;
  1124. begin
  1125. LContext := TRttiContext.Create;
  1126. LType := LContext.GetType(TypeInfo(integer));
  1127. {$ifdef fpc}
  1128. CheckEquals(LType.Name, 'LongInt');
  1129. {$else}
  1130. CheckEquals(LType.Name, 'Integer');
  1131. {$endif}
  1132. LContext.Free;
  1133. end;
  1134. procedure TTestRTTI.GetTypePointer;
  1135. var
  1136. context: TRttiContext;
  1137. t: TRttiType;
  1138. p: TRttiPointerType absolute t;
  1139. begin
  1140. context := TRttiContext.Create;
  1141. try
  1142. t := context.GetType(TypeInfo(Pointer));
  1143. Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
  1144. Assert(not Assigned(p.ReferredType), 'ReferredType of Pointer is not Nil');
  1145. t := context.GetType(TypeInfo(PLongInt));
  1146. Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
  1147. Assert(Assigned(p.ReferredType), 'ReferredType of PLongInt is Nil');
  1148. Assert(p.ReferredType = context.GetType(TypeInfo(LongInt)), 'ReferredType of PLongInt is not a LongInt');
  1149. t := context.GetType(TypeInfo(PWideChar));
  1150. Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
  1151. Assert(Assigned(p.ReferredType), 'ReferredType of PWideChar is Nil');
  1152. Assert(p.ReferredType = context.GetType(TypeInfo(WideChar)), 'ReferredType of PWideChar is not a WideChar');
  1153. finally
  1154. context.Free;
  1155. end;
  1156. end;
  1157. procedure TTestRTTI.GetClassProperties;
  1158. var
  1159. LContext: TRttiContext;
  1160. LType: TRttiType;
  1161. PropList, PropList2: {$ifdef fpc}specialize{$endif} TArray<TRttiProperty>;
  1162. i: LongInt;
  1163. begin
  1164. LContext := TRttiContext.Create;
  1165. LType := LContext.GetType(TypeInfo(TGetClassProperties));
  1166. PropList := LType.GetProperties;
  1167. CheckEquals(4, length(PropList));
  1168. CheckEquals('PubPropRO', PropList[0].Name);
  1169. CheckEquals('PubPropRW', PropList[1].Name);
  1170. CheckEquals('PubPropSetRO', PropList[2].Name);
  1171. CheckEquals('PubPropSetRW', PropList[3].Name);
  1172. LType := LContext.GetType(TypeInfo(TGetClassPropertiesSub));
  1173. PropList2 := LType.GetProperties;
  1174. CheckEquals(Length(PropList), Length(PropList2));
  1175. for i := 0 to High(PropList) do
  1176. Check(PropList[i] = PropList2[i], 'Property instances are not equal');
  1177. LContext.Free;
  1178. end;
  1179. procedure TTestRTTI.GetClassPropertiesValue;
  1180. var
  1181. AGetClassProperties: TGetClassProperties;
  1182. LContext: TRttiContext;
  1183. LType: TRttiType;
  1184. AValue: TValue;
  1185. begin
  1186. LContext := TRttiContext.Create;
  1187. LType := LContext.GetType(TGetClassProperties);
  1188. AGetClassProperties := TGetClassProperties.Create;
  1189. try
  1190. AGetClassProperties.PubPropRW:=12345;
  1191. AValue := LType.GetProperty('PubPropRW').GetValue(AGetClassProperties);
  1192. CheckEquals(12345, AValue.AsInteger);
  1193. finally
  1194. AGetClassProperties.Free;
  1195. end;
  1196. LContext.Free;
  1197. end;
  1198. procedure TTestRTTI.TestInterface;
  1199. var
  1200. context: TRttiContext;
  1201. t: TRttiType;
  1202. ti1, ti2: TRttiInterfaceType;
  1203. methods: {$ifdef fpc}specialize{$endif} TArray<TRttiMethod>;
  1204. params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
  1205. method: TRttiMethod;
  1206. param: TRttiParameter;
  1207. flag: TParamFlag;
  1208. begin
  1209. context := TRttiContext.Create;
  1210. try
  1211. t := context.GetType(TypeInfo(IInterface));
  1212. Check(t is TRttiInterfaceType, 'Type is not an interface type');
  1213. Check(not Assigned(t.BaseType), 'Base type is assigned');
  1214. ti1 := TRttiInterfaceType(t);
  1215. Check(not Assigned(ti1.BaseType), 'Base type is assigned');
  1216. methods := t.GetMethods;
  1217. CheckEquals(0, Length(methods), 'Overall method count does not match');
  1218. methods := t.GetDeclaredMethods;
  1219. CheckEquals(0, Length(methods), 'Declared method conut does not match');
  1220. t := context.GetType(TypeInfo(ITestInterface));
  1221. Check(t is TRttiInterfaceType, 'Type is not an interface type');
  1222. Check(Assigned(t.BaseType), 'Base type is not assigned');
  1223. Check(t.BaseType = TRttiType(ti1), 'Base type does not match');
  1224. ti2 := TRttiInterfaceType(t);
  1225. Check(Assigned(ti2.BaseType), 'Base type is not assigned');
  1226. Check(ti2.BaseType = ti1, 'Base type does not match');
  1227. methods := t.GetMethods;
  1228. CheckEquals(4, Length(methods), 'Overall method count does not match');
  1229. methods := t.GetDeclaredMethods;
  1230. CheckEquals(4, Length(methods), 'Declared method count does not match');
  1231. method := methods[0];
  1232. CheckEquals(method.Name, 'Test', 'Method name of Test does not match');
  1233. Check(method.CallingConvention = DefaultCC, 'Calling convention of Test does not match');
  1234. Check(method.MethodKind = mkProcedure, 'Method kind of Test does not match');
  1235. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test does not match');
  1236. Check(not Assigned(method.CodeAddress), 'Code address of Test is not Nil');
  1237. CheckEquals(method.VirtualIndex, 3, 'Virtual index of Test does not match');
  1238. Check(not Assigned(method.ReturnType), 'Return type of Test is not Nil');
  1239. params := method.GetParameters;
  1240. CheckEquals(0, Length(params), 'Parameter count of Test does not match');
  1241. method := methods[1];
  1242. CheckEquals(method.Name, 'Test2', 'Method name of Test2 does not match');
  1243. Check(method.CallingConvention = DefaultCC, 'Calling convention of Test2 does not match');
  1244. Check(method.MethodKind = mkFunction, 'Method kind of Test2 does not match');
  1245. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test2 does not match');
  1246. Check(not Assigned(method.CodeAddress), 'Code address of Test2 is not Nil');
  1247. CheckEquals(method.VirtualIndex, 4, 'Virtual index of Test2 does not match');
  1248. Check(Assigned(method.ReturnType), 'Return type of Test2 is Nil');
  1249. Check(method.ReturnType.TypeKind = tkInteger, 'Return type of Test2 is not an ordinal');
  1250. params := method.GetParameters;
  1251. CheckEquals(0, Length(params), 'Parameter count of Test2 does not match');
  1252. method := methods[2];
  1253. CheckEquals(method.Name, 'Test3', 'Method name of Test3 does not match');
  1254. Check(method.CallingConvention = DefaultCC, 'Calling convention of Test3 does not match');
  1255. Check(method.MethodKind = mkProcedure, 'Method kind of Test3 does not match');
  1256. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test3 does not match');
  1257. Check(not Assigned(method.CodeAddress), 'Code address of Test3 is not Nil');
  1258. CheckEquals(method.VirtualIndex, 5, 'Virtual index of Test3 does not match');
  1259. Check(not Assigned(method.ReturnType), 'Return type of Test3 is not Nil');
  1260. params := method.GetParameters;
  1261. CheckEquals(4, Length(params), 'Parameter count of Test3 does not match');
  1262. param := params[0];
  1263. CheckEquals(param.Name, 'aArg1', 'Parameter name of Test3.aArg1 does not match');
  1264. Check(param.Flags = [], 'Parameter flags of Test3.aArg1 do not match');
  1265. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg1 is Nil');
  1266. Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test3.aArg1 is not an ordinal');
  1267. param := params[1];
  1268. CheckEquals(param.Name, 'aArg2', 'Parameter name of Test3.aArg2 does not match');
  1269. Check(param.Flags = [pfConst], 'Parameter flags of Test3.aArg2 do not match');
  1270. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg2 is Nil');
  1271. Check(param.ParamType.TypeKind = tkAnsiString, 'Parameter type of Test3.aArg2 is not a string');
  1272. param := params[2];
  1273. CheckEquals(param.Name, 'aArg3', 'Parameter name of Test3.aArg3 does not match');
  1274. Check(param.Flags = [pfVar], 'Parameter flags of Test3.aArg3 do not match');
  1275. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg3 is Nil');
  1276. Check(param.ParamType.TypeKind = {$ifdef fpc}tkBool{$else}tkEnumeration{$endif}, 'Parameter type of Test3.aArg3 is not a boolean');
  1277. param := params[3];
  1278. CheckEquals(param.Name, 'aArg4', 'Parameter name of Test3.aArg4 does not match');
  1279. Check(param.Flags = [pfOut], 'Parameter flags of Test3.aArg4 do not match');
  1280. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg4 is Nil');
  1281. Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test3.aArg4 is not a string');
  1282. method := methods[3];
  1283. CheckEquals(method.Name, 'Test4', 'Method name of Test4 does not match');
  1284. Check(method.CallingConvention = DefaultCC, 'Calling convention of Test4 does not match');
  1285. Check(method.MethodKind = mkFunction, 'Method kind of Test4 does not match');
  1286. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test4 does not match');
  1287. Check(not Assigned(method.CodeAddress), 'Code address of Test4 is not Nil');
  1288. CheckEquals(method.VirtualIndex, 6, 'Virtual index of Test4 does not match');
  1289. Check(Assigned(method.ReturnType), 'Return type of Test4 is not Nil');
  1290. Check(method.ReturnType.TypeKind = tkAnsiString, 'Return type of Test4 is not a string');
  1291. params := method.GetParameters;
  1292. CheckEquals(2, Length(params), 'Parameter count of Test4 does not match');
  1293. param := params[0];
  1294. CheckEquals(param.Name, 'aArg1', 'Parameter name of Test4.aArg1 does not match');
  1295. Check(param.Flags = [pfArray, pfReference], 'Parameter flags of Test4.aArg1 do not match');
  1296. Check(Assigned(param.ParamType), 'Parameter type of Test4.aArg1 is Nil');
  1297. Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test4.aArg1 is not an ordinal');
  1298. param := params[1];
  1299. CheckEquals(param.Name, 'aArg2', 'Parameter name of Test4.aArg2 does not match');
  1300. Check(param.Flags = [pfArray, pfReference], 'Parameter flags of Test4.aArg2 do not match');
  1301. Check(Assigned(param.ParamType), 'Parameter type of Test4.aArg2 is Nil');
  1302. Check(param.ParamType.TypeKind = tkRecord, 'Parameter type of Test4.aArg2 is not a record');
  1303. finally
  1304. context.Free;
  1305. end;
  1306. end;
  1307. procedure TTestRTTI.TestRawThunk;
  1308. var
  1309. intf: IInterface;
  1310. begin
  1311. { we test the raw thunking by instantiating a TVirtualInterface of IInterface }
  1312. { this does not require a function call manager as the thunking is implemented
  1313. directly inside the RTTI unit }
  1314. try
  1315. intf := TVirtualInterface.Create(PTypeInfo(TypeInfo(IInterface))) as IInterface;
  1316. except
  1317. on e: ENotImplemented do
  1318. Ignore('RawThunk not implemented');
  1319. end;
  1320. { if all went well QueryInterface and _AddRef were called and now we call
  1321. _Release as well }
  1322. intf := Nil;
  1323. end;
  1324. {$ifdef fpc}
  1325. procedure TTestRTTI.TestInterfaceRaw;
  1326. var
  1327. context: TRttiContext;
  1328. t: TRttiType;
  1329. ti: TRttiInterfaceType;
  1330. begin
  1331. context := TRttiContext.Create;
  1332. try
  1333. t := context.GetType(TypeInfo(ICORBATest));
  1334. Check(t is TRttiInterfaceType, 'Type is not a raw interface type');
  1335. Check(not Assigned(t.BaseType), 'Base type is assigned');
  1336. ti := TRttiInterfaceType(t);
  1337. Check(not Assigned(ti.BaseType), 'Base type is assigned');
  1338. finally
  1339. context.Free;
  1340. end;
  1341. end;
  1342. {$endif}
  1343. procedure TTestRTTI.TestArray;
  1344. var
  1345. context: TRttiContext;
  1346. t, el: TRttiType;
  1347. a: TRttiArrayType;
  1348. o: TRttiOrdinalType;
  1349. begin
  1350. context := TRttiContext.Create;
  1351. try
  1352. t := context.GetType(PTypeInfo(TypeInfo(TArrayOfLongintStatic)));
  1353. Check(t is TRttiArrayType, 'Type is not a TRttiArrayType');
  1354. a := TRttiArrayType(t);
  1355. CheckEquals(1, a.DimensionCount, 'Dimension count does not match');
  1356. CheckEquals(4, a.TotalElementCount, 'Total element count does not match');
  1357. el := a.ElementType;
  1358. Check(el is TRttiOrdinalType, 'Element type is not a TRttiOrdinalType');
  1359. Check(el = context.GetType(PTypeInfo(TypeInfo(LongInt))), 'Element type is not a LongInt');
  1360. t := a.Dimensions[0];
  1361. {$ifdef fpc}
  1362. Check(t is TRttiOrdinalType, 'Index type is not a TRttiOrdinalType');
  1363. o := TRttiOrdinalType(t);
  1364. { Currently this is a full type :/ }
  1365. {CheckEquals(0, o.MinValue, 'Minimum value of 1st dimension does not match');
  1366. CheckEquals(3, o.MaxValue, 'Maximum value of 1st dimension does not match');}
  1367. {$else}
  1368. Check(t = Nil, 'Index type is not Nil');
  1369. {$endif}
  1370. t := context.GetType(PTypeInfo(TypeInfo(TArrayOfLongint2DStatic)));
  1371. Check(t is TRttiArrayType, 'Type is not a TRttiArrayType');
  1372. a := TRttiArrayType(t);
  1373. CheckEquals(2, a.DimensionCount, 'Dimension count does not match');
  1374. CheckEquals(4 * 3, a.TotalElementCount, 'Total element count does not match');
  1375. el := a.ElementType;
  1376. Check(el is TRttiOrdinalType, 'Element type is not a TRttiOrdinalType');
  1377. Check(el = context.GetType(PTypeInfo(TypeInfo(LongInt))), 'Element type is not a LongInt');
  1378. t := a.Dimensions[0];
  1379. {$ifdef fpc}
  1380. Check(t is TRttiOrdinalType, 'Index type is not a TRttiOrdinalType');
  1381. o := TRttiOrdinalType(t);
  1382. { Currently this is a full type :/ }
  1383. {CheckEquals(0, o.MinValue, 'Minimum value of 1st dimension does not match');
  1384. CheckEquals(3, o.MaxValue, 'Maximum value of 1st dimension does not match');}
  1385. {$else}
  1386. Check(t = Nil, 'Index type is not Nil');
  1387. {$endif}
  1388. t := a.Dimensions[1];
  1389. {$ifdef fpc}
  1390. Check(t is TRttiOrdinalType, 'Index type is not a TRttiOrdinalType');
  1391. o := TRttiOrdinalType(t);
  1392. { Currently this is a full type :/ }
  1393. {CheckEquals(2, o.MinValue, 'Minimum value of 1st dimension does not match');
  1394. CheckEquals(4, o.MaxValue, 'Maximum value of 1st dimension does not match');}
  1395. {$else}
  1396. Check(t = Nil, 'Index type is not Nil');
  1397. {$endif}
  1398. finally
  1399. context.Free;
  1400. end;
  1401. end;
  1402. procedure TTestRTTI.TestDynArray;
  1403. var
  1404. context: TRttiContext;
  1405. t, el: TRttiType;
  1406. a: TRttiDynamicArrayType;
  1407. begin
  1408. context := TRttiContext.Create;
  1409. try
  1410. t := context.GetType(PTypeInfo(TypeInfo(TArrayOfLongintDyn)));
  1411. Check(t is TRttiDynamicArrayType, 'Type is not a TRttiDynamicArrayType');
  1412. a := TRttiDynamicArrayType(t);
  1413. CheckEquals('tests.rtti.types', LowerCase(a.DeclaringUnitName), 'Unit type does not match for dynamic array');
  1414. CheckEquals(a.ElementSize, SizeUInt(SizeOf(LongInt)), 'Element size does not match for dynamic array');
  1415. el := a.ElementType;
  1416. Check(el is TRttiOrdinalType, 'Element type is not a TRttiOrdinalType');
  1417. Check(el = context.GetType(PTypeInfo(TypeInfo(LongInt))), 'Element type is not a LongInt');
  1418. { ToDo: check OLE type }
  1419. finally
  1420. context.Free;
  1421. end;
  1422. end;
  1423. procedure TTestRTTI.TestProcVar;
  1424. var
  1425. context: TRttiContext;
  1426. t: TRttiType;
  1427. p: TRttiProcedureType;
  1428. params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
  1429. begin
  1430. context := TRttiContext.Create;
  1431. try
  1432. t := context.GetType(PTypeInfo(TypeInfo(TTestProc)));
  1433. Check(Assigned(t), 'Rtti Type is Nil');
  1434. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1435. Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
  1436. p := t as TRttiProcedureType;
  1437. Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
  1438. Check(not Assigned(p.ReturnType), 'Return type is assigned');
  1439. CheckEquals(0, Length(p.GetParameters), 'Procedure variable has parameters');
  1440. t := context.GetType(PTypeInfo(TypeInfo(TTestFunc1)));
  1441. Check(Assigned(t), 'Rtti Type is Nil');
  1442. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1443. Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
  1444. p := t as TRttiProcedureType;
  1445. Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
  1446. Check(Assigned(p.ReturnType), 'Return type is not assigned');
  1447. //Check(p.ReturnType is TRttiOrdinalType, 'Return type is not an ordinal type');
  1448. CheckEquals(0, Length(p.GetParameters), 'Procedure variable has parameters');
  1449. t := context.GetType(PTypeInfo(TypeInfo(TTestFunc2)));
  1450. Check(Assigned(t), 'Rtti Type is Nil');
  1451. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1452. Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
  1453. p := t as TRttiProcedureType;
  1454. Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
  1455. Check(Assigned(p.ReturnType), 'Return type is not assigned');
  1456. Check(p.ReturnType is TRttiStringType, 'Return type is not a string type');
  1457. params := p.GetParameters;
  1458. CheckEquals(2, Length(params), 'Procedure variable has incorrect amount of parameters');
  1459. Check(params[0].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 1 is not an ordinal type');
  1460. //Check(params[0].ParamType is TRttiOrdinalType, 'Parameter 1 is not an ordinal type');
  1461. Check(pfArray in params[1].Flags, 'Parameter 2 is not an array');
  1462. Check(params[1].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 2 is not an ordinal array');
  1463. finally
  1464. context.Free;
  1465. end;
  1466. end;
  1467. procedure TTestRTTI.TestMethod;
  1468. var
  1469. context: TRttiContext;
  1470. t: TRttiType;
  1471. m: TRttiMethodType;
  1472. params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
  1473. begin
  1474. context := TRttiContext.Create;
  1475. try
  1476. t := context.GetType(PTypeInfo(TypeInfo(TTestMethod)));
  1477. Check(Assigned(t), 'Rtti Type is Nil');
  1478. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1479. Check(t is TRttiMethodType, 'Rtti Type is not a method type');
  1480. m := t as TRttiMethodType;
  1481. Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
  1482. Check(not Assigned(m.ReturnType), 'Return type is assigned');
  1483. CheckEquals(0, Length(m.GetParameters), 'Method variable has parameters');
  1484. t := context.GetType(PTypeInfo(TypeInfo(TTestMethod1)));
  1485. Check(Assigned(t), 'Rtti Type is Nil');
  1486. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1487. Check(t is TRttiMethodType, 'Rtti Type is not a method type');
  1488. m := t as TRttiMethodType;
  1489. Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
  1490. Check(Assigned(m.ReturnType), 'Return type is not assigned');
  1491. //Check(p.ReturnType is TRttiOrdinalType, 'Return type is not an ordinal type');
  1492. CheckEquals(0, Length(m.GetParameters), 'Method variable has parameters');
  1493. t := context.GetType(PTypeInfo(TypeInfo(TTestMethod2)));
  1494. Check(Assigned(t), 'Rtti Type is Nil');
  1495. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1496. Check(t is TRttiMethodType, 'Rtti Type is not a method type');
  1497. m := t as TRttiMethodType;
  1498. Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
  1499. Check(Assigned(m.ReturnType), 'Return type is not assigned');
  1500. Check(m.ReturnType is TRttiStringType, 'Return type is not a string type');
  1501. params := m.GetParameters;
  1502. CheckEquals(2, Length(params), 'Method variable has incorrect amount of parameters');
  1503. Check(params[0].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 1 is not an ordinal type');
  1504. //Check(params[0].ParamType is TRttiOrdinalType, 'Parameter 1 is not an ordinal type');
  1505. Check(pfArray in params[1].Flags, 'Parameter 2 is not an array');
  1506. Check(params[1].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 2 is not an ordinal array');
  1507. finally
  1508. context.Free;
  1509. end;
  1510. end;
  1511. { TTestExtendedRTTI }
  1512. procedure TTestExtendedRTTI.AssertEquals(Msg: String; aExpected, aActual: TMemberVisibility);
  1513. begin
  1514. AssertEquals(Msg,GetEnumName(TypeInfo(TMemberVisibility),Ord(aExpected)),
  1515. GetEnumName(TypeInfo(TMemberVisibility),Ord(aActual)));
  1516. end;
  1517. procedure TTestExtendedRTTI.AssertEquals(Msg: String; aExpected, aActual: TTypeKind);
  1518. begin
  1519. AssertEquals(Msg,GetEnumName(TypeInfo(TTypeKind),Ord(aExpected)),
  1520. GetEnumName(TypeInfo(TTypeKind),Ord(aActual)));
  1521. end;
  1522. procedure TTestExtendedRTTI.Setup;
  1523. begin
  1524. Inherited;
  1525. FCtx:=TRttiContext.Create;
  1526. FCtx.UsePublishedOnly:=False;
  1527. end;
  1528. procedure TTestExtendedRTTI.TearDown;
  1529. begin
  1530. FCtx.Free;
  1531. inherited TearDown;
  1532. end;
  1533. Procedure TTestExtendedRTTI.CheckField(aIdx : Integer; aData: TRttiField; aName : String; aKind : TTypeKind; aVisibility : TMemberVisibility; aStrict : Boolean = False);
  1534. Var
  1535. Msg : String;
  1536. begin
  1537. Msg:='Checking field '+IntToStr(aIdx)+' ('+aName+') ';
  1538. AssertNotNull(Msg+'Have data',AData);
  1539. AssertEquals(Msg+'name',aName,aData.Name);
  1540. AssertEquals(Msg+'kind',aKind,aData.FieldType.TypeKind);
  1541. AssertEquals(Msg+'visibility',aVisibility,aData.Visibility);
  1542. AssertEquals(Msg+'strict',aStrict,aData.StrictVisibility);
  1543. end;
  1544. Procedure TTestExtendedRTTI.CheckProperty(aIdx : Integer; aData: TRttiProperty; aName : String; aKind : TTypeKind; aVisibility : TMemberVisibility; isStrict : Boolean = False);
  1545. Var
  1546. Msg : String;
  1547. begin
  1548. Msg:='Checking prop '+IntToStr(aIdx)+' ('+aName+') ';
  1549. AssertNotNull(Msg+'Have data',AData);
  1550. AssertEquals(Msg+'name',aName, aData.Name);
  1551. AssertEquals(Msg+'kind',aKind, aData.PropertyType.TypeKind);
  1552. AssertEquals(Msg+'visibility',aVisibility,aData.Visibility);
  1553. AssertEquals(Msg+'strict',isStrict,aData.StrictVisibility);
  1554. end;
  1555. Procedure TTestExtendedRTTI.CheckMethod(aPrefix : string; aIdx : Integer; aData: TRttiMethod; aName : String; aVisibility : TMemberVisibility; aStrict : Boolean = False);
  1556. Var
  1557. Msg : String;
  1558. begin
  1559. Msg:=aPrefix+': Checking method '+IntToStr(aIdx)+' ('+aName+') ';
  1560. AssertNotNull(Msg+'Have data',AData);
  1561. AssertEquals(Msg+'name',aData.Name,aName);
  1562. AssertEquals(Msg+'visibility',aVisibility,aData.Visibility);
  1563. AssertEquals(Msg+'strict',aData.StrictVisibility,aStrict);
  1564. end;
  1565. procedure TTestClassExtendedRTTI.TestFields;
  1566. Var
  1567. Obj : TRttiObject;
  1568. RttiData : TRttiInstanceType absolute obj;
  1569. A : TRttiFieldArray;
  1570. t : TFieldRTTI;
  1571. begin
  1572. Obj:=FCtx.GetType(TFieldRTTI.ClassInfo);
  1573. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1574. A:=RttiData.GetFields;
  1575. AssertEquals('Class field Count',10,Length(A));
  1576. CheckField(0, A[0],'FPrivateA',tkInteger,mvPrivate);
  1577. CheckField(1, A[1],'FPrivateB',tkInteger,mvPrivate,True);
  1578. CheckField(2, A[2],'FProtectedA',tkInteger,mvProtected);
  1579. CheckField(3, A[3],'FProtectedB',tkInteger,mvProtected,True);
  1580. CheckField(4, A[4],'FPublicA',tkInteger,mvPublic);
  1581. CheckField(5, A[5],'FPublicB',tkInteger,mvPublic);
  1582. CheckField(6, A[6],'FPublishedA',tkInteger,mvPrivate);
  1583. CheckField(7, A[7],'FPublishedB',tkInteger,mvPrivate);
  1584. CheckField(8, A[8],'FPublishedC',tkClass,mvPublished);
  1585. CheckField(9, A[9],'FPublishedD',tkClass,mvPublished);
  1586. t := TFieldRTTI.Create;
  1587. AssertEquals('Legacy Field 0', A[8].Offset, Integer(PByte(t.FieldAddress('FPublishedC')) - PByte(t)));
  1588. AssertEquals('Legacy Field 1', A[9].Offset, Integer(PByte(t.FieldAddress('FPublishedD')) - PByte(t)));
  1589. T.Free;
  1590. end;
  1591. procedure TTestClassExtendedRTTI.TestDeclaredFields;
  1592. Var
  1593. Obj : TRttiObject;
  1594. RttiData : TRttiInstanceType absolute obj;
  1595. Obj2 : TRttiObject;
  1596. RttiData2 : TRttiInstanceType absolute obj2;
  1597. A : TRttiFieldArray;
  1598. I : Integer;
  1599. begin
  1600. Obj:=FCtx.GetType(TFieldRTTIChild.ClassInfo);
  1601. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1602. Obj2:=FCtx.GetType(TFieldRTTI.ClassInfo);
  1603. AssertEquals('Correct class type',TRttiInstanceType,Obj2.ClassType);
  1604. A:=RttiData.GetFields;
  1605. AssertEquals('Class field Count',11,Length(A));
  1606. For I:=1 to 10 do
  1607. AssertSame('Field parent'+IntToStr(i)+' is parent class', Obj2,A[I].Parent);
  1608. A:=RttiData.GetDeclaredFields;
  1609. AssertEquals('Class declared field Count',1,Length(A));
  1610. AssertSame('Declared Field parent is sels', Obj,A[0].Parent);
  1611. end;
  1612. procedure TTestClassExtendedRTTI.TestProperties;
  1613. Var
  1614. A : TRttiPropertyArray;
  1615. Obj : TRttiObject;
  1616. RttiData : TRttiInstanceType absolute obj;
  1617. aCount : Integer;
  1618. begin
  1619. Obj:=FCtx.GetType(TFieldRTTI.ClassInfo);
  1620. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1621. A:=RttiData.GetProperties;
  1622. aCount:=Length(A);
  1623. AssertEquals('Property Count',8,aCount);
  1624. CheckProperty(0, A[0],'PrivateA',tkInteger,mvPrivate);
  1625. CheckProperty(1, A[1],'PrivateB',tkInteger,mvPrivate,True);
  1626. CheckProperty(2, A[2],'ProtectedA',tkInteger,mvProtected);
  1627. CheckProperty(3, A[3],'ProtectedB',tkInteger,mvProtected,True);
  1628. CheckProperty(4, A[4],'PublicA',tkInteger,mvPublic);
  1629. CheckProperty(5, A[5],'PublicB',tkInteger,mvPublic);
  1630. CheckProperty(6, A[6],'PublishedA',tkInteger,mvPublished);
  1631. CheckProperty(7, A[7],'PublishedB',tkInteger,mvPublished);
  1632. end;
  1633. procedure TTestClassExtendedRTTI.TestDeclaredMethods;
  1634. Var
  1635. A : TRttiMethodArray;
  1636. Obj : TRttiObject;
  1637. RttiData : TRttiInstanceType absolute obj;
  1638. Parms : TRttiParameterArray;
  1639. aCount : Integer;
  1640. begin
  1641. Obj:=FCtx.GetType(TMethodClassRTTI.ClassInfo);
  1642. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1643. A:=RttiData.GetDeclaredMethods;
  1644. aCount:=Length(A);
  1645. AssertEquals('Full Count',12,aCount);
  1646. CheckMethod('Full',0, A[0],'PrivateMethodA',mvPrivate);
  1647. CheckMethod('Full',1, A[1],'PrivateMethodB',mvPrivate,True);
  1648. CheckMethod('Full',2, A[2],'PrivateMethodC',mvPrivate);
  1649. CheckMethod('Full',3, A[3],'ProtectedMethodA',mvProtected);
  1650. CheckMethod('Full',4, A[4],'ProtectedMethodB',mvProtected,True);
  1651. CheckMethod('Full',5, A[5],'ProtectedMethodC',mvProtected);
  1652. CheckMethod('Full',6, A[6],'PublicMethodA',mvPublic);
  1653. CheckMethod('Full',7, A[7],'PublicMethodB',mvPublic);
  1654. CheckMethod('Full',8, A[8],'PublicMethodC',mvPublic);
  1655. CheckMethod('Full',9, A[9],'PublishedMethodA',mvPublished);
  1656. CheckMethod('Full',10, A[10],'PublishedMethodB',mvPublished);
  1657. CheckMethod('Full',11, A[11],'PublishedMethodC',mvPublished);
  1658. Parms:=A[9].GetParameters;
  1659. AssertEquals('Parameter length',1,Length(Parms));
  1660. AssertEquals('Parameter name','a',Parms[0].Name);
  1661. end;
  1662. procedure TTestClassExtendedRTTI.TestMethods;
  1663. Var
  1664. A : TRttiMethodArray;
  1665. Obj : TRttiObject;
  1666. RttiData : TRttiInstanceType absolute obj;
  1667. aCount : Integer;
  1668. begin
  1669. Obj:=FCtx.GetType(TAdditionalMethodClassRTTI.ClassInfo);
  1670. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1671. A:=RttiData.GetMethods;
  1672. aCount:=Length(A);
  1673. AssertEquals('Full Count',13,aCount);
  1674. CheckMethod('Full',0, A[0],'PublicAdditionalMethod',mvPublic);
  1675. A:=RttiData.GetDeclaredMethods;
  1676. aCount:=Length(A);
  1677. AssertEquals('Full declared Count',1,aCount);
  1678. CheckMethod('Full declared',1, A[0],'PublicAdditionalMethod',mvPublic);
  1679. end;
  1680. procedure TTestClassExtendedRTTI.TestMethodByAddress;
  1681. var
  1682. Obj : TRttiObject;
  1683. RttiData : TRttiInstanceType absolute obj;
  1684. M1,M2 : TRttiMethod;
  1685. begin
  1686. Obj:=FCtx.GetType(TAdditionalMethodClassRTTI.ClassInfo);
  1687. M1:=RttiData.GetMethod('PublicAdditionalMethod');
  1688. AssertNotNull('have method',m1);
  1689. M2:=RttiData.GetMethod(@TAdditionalMethodClassRTTI.PublicAdditionalMethod);
  1690. AssertSame('Correct method ',M1,M2);
  1691. end;
  1692. procedure TTestClassExtendedRTTI.TestMethodsInherited;
  1693. Var
  1694. A : TRttiMethodArray;
  1695. Obj,Obj2 : TRttiObject;
  1696. RttiData : TRttiInstanceType absolute obj;
  1697. RttiData2 : TRttiInstanceType absolute obj2;
  1698. i,aCount : Integer;
  1699. begin
  1700. Obj:=FCtx.GetType(TAdditionalMethodClassRTTI.ClassInfo);
  1701. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1702. Obj2:=FCtx.GetType(TMethodClassRTTI.ClassInfo);
  1703. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1704. A:=RttiData.GetMethods;
  1705. aCount:=Length(A);
  1706. AssertEquals('Full Count',13,aCount);
  1707. For I:=1 to 12 do
  1708. AssertSame('Parent is RTTI of parent class',Obj2,A[I].Parent);
  1709. A:=RttiData.GetDeclaredMethods;
  1710. aCount:=Length(A);
  1711. AssertEquals('Full declared Count',1,aCount);
  1712. CheckMethod('Full declared',1, A[0],'PublicAdditionalMethod',mvPublic);
  1713. AssertSame('Parent is RTTI of parent class',Obj,A[0].Parent);
  1714. end;
  1715. procedure TTestClassExtendedRTTI.TestPrivateFieldAttributes;
  1716. var
  1717. Obj : TRttiObject;
  1718. RttiData : TRttiInstanceType absolute obj;
  1719. Attrs : TCustomAttributeArray;
  1720. Fld : TRttiField;
  1721. O : TCustomAttribute;
  1722. M2 : My2Attribute absolute O;
  1723. begin
  1724. Obj:=FCtx.GetType(TypeInfo(TFieldObject));
  1725. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1726. Fld:=RttiData.GetField('PrivateField');
  1727. AssertNotNull('Have field',Fld);
  1728. Attrs:=Fld.GetAttributes;
  1729. AssertNotNull('Have attribute data',Pointer(Attrs));
  1730. AssertEquals('attribute count',3,Length(Attrs));
  1731. AssertEquals('Attribute 1 name','WeakAttribute',Attrs[0].ClassName);
  1732. AssertEquals('Attribute 2 name','MyAttribute',Attrs[1].ClassName);
  1733. AssertEquals('Attribute 2 name','My2Attribute',Attrs[2].ClassName);
  1734. O:=Attrs[2];
  1735. AssertNotNull('Attribute class ',O);
  1736. AssertEquals('Attribute class ',O.ClassType,My2Attribute);
  1737. AssertEquals('Attribute value ',2,M2.Int);
  1738. end;
  1739. procedure TTestClassExtendedRTTI.TestProtectedFieldAttributes;
  1740. var
  1741. Obj : TRttiObject;
  1742. RttiData : TRttiInstanceType absolute obj;
  1743. Attrs : TCustomAttributeArray;
  1744. Fld : TRttiField;
  1745. O : TCustomAttribute;
  1746. M2 : My2Attribute absolute O;
  1747. begin
  1748. Obj:=FCtx.GetType(TypeInfo(TFieldObject));
  1749. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1750. Fld:=RttiData.GetField('ProtectedField');
  1751. AssertNotNull('Have field',Fld);
  1752. Attrs:=Fld.GetAttributes;
  1753. AssertNotNull('Have attribute data',Pointer(Attrs));
  1754. AssertEquals('attribute count',1,Length(Attrs));
  1755. AssertEquals('Attribute 1 name','My2Attribute',Attrs[0].ClassName);
  1756. O:=Attrs[0];
  1757. AssertNotNull('Attribute class ',O);
  1758. AssertEquals('Attribute class ',O.ClassType,My2Attribute);
  1759. AssertEquals('Attribute value ',3,M2.Int);
  1760. end;
  1761. procedure TTestClassExtendedRTTI.TestPublicFieldAttributes;
  1762. var
  1763. Obj : TRttiObject;
  1764. RttiData : TRttiInstanceType absolute obj;
  1765. Attrs : TCustomAttributeArray;
  1766. Fld : TRttiField;
  1767. O : TCustomAttribute;
  1768. M3 : My3Attribute absolute O;
  1769. aCount : Integer;
  1770. begin
  1771. Obj:=FCtx.GetType(TypeInfo(TFieldObject));
  1772. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1773. aCount:=0;
  1774. For Fld in RttiData.GetFields do
  1775. if Fld.Visibility=mvPublic then
  1776. inc(aCount);
  1777. AssertEquals('Field count',3,aCount);
  1778. // PublicField
  1779. Fld:=RttiData.GetField('PublicField');
  1780. AssertNotNull('Have field',Fld);
  1781. Attrs:=Fld.GetAttributes;
  1782. AssertNotNull('Have attribute data',Pointer(Attrs));
  1783. AssertEquals('attribute count',1,Length(Attrs));
  1784. AssertEquals('Attribute 1 name','My3Attribute',Attrs[0].ClassName);
  1785. O:=Attrs[0];
  1786. AssertNotNull('Attribute class ',O);
  1787. AssertEquals('Attribute class ',O.ClassType,My3Attribute);
  1788. AssertEquals('Attribute value ',4,M3.Int);
  1789. // A
  1790. Fld:=RttiData.GetField('A');
  1791. AssertNotNull('A Have field',Fld);
  1792. Attrs:=Fld.GetAttributes;
  1793. AssertNotNull('A Have attribute data',Pointer(Attrs));
  1794. AssertEquals('A Attribute count',1,Length(Attrs));
  1795. AssertEquals('A Attribute 1 name','My3Attribute',Attrs[0].ClassName);
  1796. O:=Attrs[0];
  1797. AssertNotNull('A: Attribute class ',O);
  1798. AssertEquals('A: Attribute class ',O.ClassType,My3Attribute);
  1799. AssertEquals('A: Attribute value ',4,M3.Int);
  1800. // B
  1801. Fld:=RttiData.GetField('B');
  1802. AssertNotNull('B Have field',Fld);
  1803. Attrs:=Fld.GetAttributes;
  1804. AssertNotNull('B Have attribute data',Pointer(Attrs));
  1805. AssertEquals('A Attribute count',1,Length(Attrs));
  1806. AssertEquals('A Attribute 1 name','My3Attribute',Attrs[0].ClassName);
  1807. O:=Attrs[0];
  1808. AssertNotNull('B: Attribute class ',O);
  1809. AssertEquals('B: Attribute class ',O.ClassType,My3Attribute);
  1810. AssertEquals('B: Attribute value ',4,M3.Int);
  1811. end;
  1812. procedure TTestClassExtendedRTTI.TestPrivatePropertyAttributes;
  1813. var
  1814. Obj : TRttiObject;
  1815. RttiData : TRttiInstanceType absolute obj;
  1816. Attrs : TCustomAttributeArray;
  1817. Prop : TRttiProperty;
  1818. O : TCustomAttribute;
  1819. aCount : Integer;
  1820. M2 : My2Attribute absolute O;
  1821. begin
  1822. Obj:=FCtx.GetType(TypeInfo(TPropertyObject));
  1823. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1824. aCount:=0;
  1825. Prop:=RttiData.GetProperty('PrivateProperty');
  1826. AssertNotNull('Have property',Prop);
  1827. Attrs:=Prop.GetAttributes;
  1828. AssertNotNull('Have attribute data',Pointer(Attrs));
  1829. AssertEquals('attribute count',3,Length(Attrs));
  1830. AssertEquals('Attribute 1 name','WeakAttribute',Attrs[0].ClassName);
  1831. AssertEquals('Attribute 2 name','MyAttribute',Attrs[1].ClassName);
  1832. AssertEquals('Attribute 2 name','My2Attribute',Attrs[2].ClassName);
  1833. O:=Attrs[2];
  1834. AssertNotNull('Attribute class ',O);
  1835. AssertEquals('Attribute class ',O.ClassType,My2Attribute);
  1836. AssertEquals('Attribute value ',2,M2.Int);
  1837. end;
  1838. procedure TTestClassExtendedRTTI.TestProtectedPropertyAttributes;
  1839. var
  1840. Obj : TRttiObject;
  1841. RttiData : TRttiInstanceType absolute obj;
  1842. Attrs : TCustomAttributeArray;
  1843. Prop : TRttiProperty;
  1844. O : TCustomAttribute;
  1845. M2 : My2Attribute absolute O;
  1846. begin
  1847. Obj:=FCtx.GetType(TypeInfo(TPropertyObject));
  1848. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1849. Prop:=RttiData.GetProperty('ProtectedProperty');
  1850. AssertNotNull('Have property',Prop);
  1851. Attrs:=Prop.GetAttributes;
  1852. AssertNotNull('Have attribute data',Pointer(Attrs));
  1853. AssertEquals('attribute count',1,Length(Attrs));
  1854. AssertEquals('Attribute 1 name','My2Attribute',Attrs[0].ClassName);
  1855. O:=Attrs[0];
  1856. AssertNotNull('Attribute class ',O);
  1857. AssertEquals('Attribute class ',O.ClassType,My2Attribute);
  1858. AssertEquals('Attribute value ',3,M2.Int);
  1859. end;
  1860. procedure TTestClassExtendedRTTI.TestPublicPropertyAttributes;
  1861. var
  1862. Obj : TRttiObject;
  1863. RttiData : TRttiInstanceType absolute obj;
  1864. Attrs : TCustomAttributeArray;
  1865. Prop : TRttiProperty;
  1866. O : TCustomAttribute;
  1867. M3 : My3Attribute absolute O;
  1868. begin
  1869. Obj:=FCtx.GetType(TypeInfo(TPropertyObject));
  1870. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1871. Prop:=RttiData.GetProperty('PublicProperty');
  1872. AssertNotNull('Have property',Prop);
  1873. Attrs:=Prop.GetAttributes;
  1874. AssertNotNull('Have attribute data',Pointer(Attrs));
  1875. AssertEquals('attribute count',1,Length(Attrs));
  1876. AssertEquals('Attribute 1 name','My3Attribute',Attrs[0].ClassName);
  1877. O:=Attrs[0];
  1878. AssertNotNull('Attribute class ',O);
  1879. AssertEquals('Attribute class ',O.ClassType,My3Attribute);
  1880. AssertEquals('Attribute value ',4,M3.Int);
  1881. end;
  1882. procedure TTestClassExtendedRTTI.TestPublishedPropertyAttributes;
  1883. var
  1884. Obj : TRttiObject;
  1885. RttiData : TRttiInstanceType absolute obj;
  1886. Attrs : TCustomAttributeArray;
  1887. Prop : TRttiProperty;
  1888. O : TCustomAttribute;
  1889. M3 : My3Attribute absolute O;
  1890. begin
  1891. Obj:=FCtx.GetType(TypeInfo(TPropertyObject));
  1892. AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
  1893. Prop:=RttiData.GetProperty('PublishedProperty');
  1894. AssertNotNull('Have property',Prop);
  1895. Attrs:=Prop.GetAttributes;
  1896. AssertNotNull('Have attribute data',Pointer(Attrs));
  1897. AssertEquals('attribute count',1,Length(Attrs));
  1898. AssertEquals('Attribute 1 name','My3Attribute',Attrs[0].ClassName);
  1899. O:=Attrs[0];
  1900. AssertNotNull('Attribute class ',O);
  1901. AssertEquals('Attribute class ',O.ClassType,My3Attribute);
  1902. AssertEquals('Attribute value ',5,M3.Int);
  1903. end;
  1904. procedure TTestClassExtendedRTTI.TestGetStaticProperty;
  1905. var
  1906. Obj : TRttiObject;
  1907. RttiData : TRttiInstanceType absolute obj;
  1908. Prop : TRttiProperty;
  1909. begin
  1910. Obj:=FCtx.GetType(TypeInfo(TTestAttr2Class));
  1911. TTestAttr2Class.StaticProp:=4539;
  1912. Prop:=rttiData.GetProperty('StaticProp');
  1913. AssertEquals('Class property is set or got incorrectly via methods', 4539, Prop.GetValue(nil).AsInteger);
  1914. end;
  1915. procedure TTestClassExtendedRTTI.TestSetStaticProperty;
  1916. var
  1917. Obj : TRttiObject;
  1918. RttiData : TRttiInstanceType absolute obj;
  1919. Prop : TRttiProperty;
  1920. begin
  1921. Obj:=FCtx.GetType(TypeInfo(TTestAttr2Class));
  1922. Prop:=rttiData.GetProperty('StaticProp');
  1923. // Write
  1924. Prop.SetValue(nil, 4539);
  1925. AssertEquals('Property correctly set',4539,TTestAttr2Class.StaticProp);
  1926. end;
  1927. { TTestRecordExtendedRTTI }
  1928. procedure TTestRecordExtendedRTTI.TestFields;
  1929. Var
  1930. A : TRttiFieldArray;
  1931. Obj : TRttiObject;
  1932. RttiData : TRttiRecordType absolute obj;
  1933. aCount : Integer;
  1934. begin
  1935. Obj:=FCtx.GetType(TypeInfo(TRecordFieldRTTI));
  1936. AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
  1937. A:=RttiData.GetFields;
  1938. aCount:=Length(A);
  1939. AssertEquals('Record fields Count',4,aCount);
  1940. CheckField(0, A[0],'FRPrivateA',tkInteger,mvPrivate);
  1941. CheckField(1, A[1],'FRPrivateB',tkInteger,mvPrivate);
  1942. CheckField(4, A[2],'FRPublicA',tkInteger,mvPublic);
  1943. CheckField(5, A[3],'FRPublicB',tkInteger,mvPublic);
  1944. Obj:=FCtx.GetType(TypeInfo(TRecordFieldRTTIMixed));
  1945. AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
  1946. A:=RttiData.GetFields;
  1947. aCount:=Length(A);
  1948. AssertEquals('Mixed record fields Count',4,aCount);
  1949. CheckField(0, A[0],'FRPrivateA',tkInteger,mvPrivate);
  1950. CheckField(1, A[1],'FRPrivateB',tkInteger,mvPrivate);
  1951. CheckField(4, A[2],'FRPublicA',tkInteger,mvPublic);
  1952. CheckField(5, A[3],'FRPublicB',tkInteger,mvPublic);
  1953. end;
  1954. procedure TTestRecordExtendedRTTI.TestProperties;
  1955. Var
  1956. A : TRttiPropertyArray;
  1957. Obj : TRttiObject;
  1958. RttiData : TRttiRecordType absolute obj;
  1959. aCount : Integer;
  1960. begin
  1961. // TRecordFieldRTTI
  1962. Obj:=FCtx.GetType(TypeInfo(TRecordFieldRTTI));
  1963. AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
  1964. A:=RttiData.GetProperties;
  1965. aCount:=Length(A);
  1966. AssertEquals('Record property Count',4,aCount);
  1967. CheckProperty(0, A[0],'RPrivateA',tkInteger,mvPrivate);
  1968. CheckProperty(1, A[1],'RPrivateB',tkInteger,mvPrivate);
  1969. CheckProperty(2, A[2],'RPublicA',tkInteger,mvPublic);
  1970. CheckProperty(3, A[3],'RPublicB',tkInteger,mvPublic);
  1971. // TRecordFieldRTTIMixed
  1972. Obj:=FCtx.GetType(TypeInfo(TRecordFieldRTTIMixed));
  1973. AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
  1974. A:=RttiData.GetProperties;
  1975. aCount:=Length(A);
  1976. AssertEquals('Record mixed property Count',4,aCount);
  1977. CheckProperty(0, A[0],'RPrivateA',tkInteger,mvPrivate);
  1978. CheckProperty(1, A[1],'RPrivateB',tkInteger,mvPrivate);
  1979. CheckProperty(2, A[2],'RPublicA',tkInteger,mvPublic);
  1980. CheckProperty(3, A[3],'RPublicB',tkInteger,mvPublic);
  1981. end;
  1982. procedure TTestRecordExtendedRTTI.TestDeclaredMethods;
  1983. Var
  1984. A : TRttiMethodArray;
  1985. Obj : TRttiObject;
  1986. RttiData : TRttiRecordType absolute obj;
  1987. aCount : Integer;
  1988. Parms : TRttiParameterArray;
  1989. begin
  1990. Obj:=FCtx.GetType(TypeInfo(TRecordMethodRTTI));
  1991. AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
  1992. A:=RttiData.GetDeclaredMethods;
  1993. aCount:=Length(A);
  1994. AssertEquals('Method Full Count',4,aCount);
  1995. CheckMethod('Full',0, A[0],'PrivateMethodA',mvPrivate);
  1996. CheckMethod('Full',1, A[1],'PrivateMethodB',mvPrivate);
  1997. CheckMethod('Full',2, A[2],'PublicMethodA',mvPublic);
  1998. CheckMethod('Full',3, A[3],'PublicMethodB',mvPublic);
  1999. Parms:=A[3].GetParameters;
  2000. AssertEquals('Parameter length',1,Length(Parms));
  2001. AssertNotNull('Have Parameter',Parms[0]);
  2002. AssertEquals('Parameter name','I',Parms[0].Name);
  2003. end;
  2004. procedure TTestRecordExtendedRTTI.TestMethods;
  2005. Var
  2006. A : TRttiMethodArray;
  2007. Obj : TRttiObject;
  2008. RttiData : TRttiRecordType absolute obj;
  2009. aCount : Integer;
  2010. begin
  2011. Obj:=FCtx.GetType(TypeInfo(TRecordMethodRTTI));
  2012. AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
  2013. A:=RttiData.GetDeclaredMethods;
  2014. aCount:=Length(A);
  2015. // Just check that the count is correct
  2016. AssertEquals('Method Full Count',4,aCount);
  2017. end;
  2018. Procedure TTestRecordExtendedRTTI.TestPrivateFieldAttributes;
  2019. var
  2020. Obj : TRttiObject;
  2021. RttiData : TRttiRecordType absolute obj;
  2022. Attrs : TCustomAttributeArray;
  2023. Fld : TRttiField;
  2024. O : TCustomAttribute;
  2025. M2 : My2Attribute absolute O;
  2026. begin
  2027. Obj:=FCtx.GetType(TypeInfo(TFieldRecord));
  2028. AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
  2029. Fld:=RttiData.GetField('PrivateField');
  2030. AssertNotNull('Have field',Fld);
  2031. Attrs:=Fld.GetAttributes;
  2032. AssertNotNull('Have attribute data',Pointer(Attrs));
  2033. AssertEquals('attribute count',3,Length(Attrs));
  2034. AssertEquals('Attribute 1 name','WeakAttribute',Attrs[0].ClassName);
  2035. AssertEquals('Attribute 2 name','MyAttribute',Attrs[1].ClassName);
  2036. AssertEquals('Attribute 2 name','My2Attribute',Attrs[2].ClassName);
  2037. O:=Attrs[2];
  2038. AssertNotNull('Attribute class ',O);
  2039. AssertEquals('Attribute class ',O.ClassType,My2Attribute);
  2040. AssertEquals('Attribute value ',2,M2.Int);
  2041. end;
  2042. Procedure TTestRecordExtendedRTTI.TestPublicFieldAttributes;
  2043. var
  2044. Obj : TRttiObject;
  2045. RttiData : TRttiRecordType absolute obj;
  2046. Attrs : TCustomAttributeArray;
  2047. Fld : TRttiField;
  2048. O : TCustomAttribute;
  2049. M3 : My3Attribute absolute O;
  2050. aCount : Integer;
  2051. begin
  2052. Obj:=FCtx.GetType(TypeInfo(TFieldRecord));
  2053. AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
  2054. aCount:=0;
  2055. For Fld in RttiData.GetFields do
  2056. if Fld.Visibility=mvPublic then
  2057. inc(aCount);
  2058. AssertEquals('Field count',3,aCount);
  2059. // PublicField
  2060. Fld:=RttiData.GetField('PublicField');
  2061. AssertNotNull('Have field',Fld);
  2062. Attrs:=Fld.GetAttributes;
  2063. AssertNotNull('Have attribute data',Pointer(Attrs));
  2064. AssertEquals('attribute count',1,Length(Attrs));
  2065. AssertEquals('Attribute 1 name','My3Attribute',Attrs[0].ClassName);
  2066. O:=Attrs[0];
  2067. AssertNotNull('Attribute class ',O);
  2068. AssertEquals('Attribute class ',O.ClassType,My3Attribute);
  2069. AssertEquals('Attribute value ',3,M3.Int);
  2070. // A
  2071. Fld:=RttiData.GetField('A');
  2072. AssertNotNull('A Have field',Fld);
  2073. Attrs:=Fld.GetAttributes;
  2074. AssertNotNull('A Have attribute data',Pointer(Attrs));
  2075. AssertEquals('A Attribute count',1,Length(Attrs));
  2076. AssertEquals('A Attribute 1 name','My3Attribute',Attrs[0].ClassName);
  2077. O:=Attrs[0];
  2078. AssertNotNull('A: Attribute class ',O);
  2079. AssertEquals('A: Attribute class ',O.ClassType,My3Attribute);
  2080. AssertEquals('A: Attribute value ',4,M3.Int);
  2081. // B
  2082. Fld:=RttiData.GetField('B');
  2083. AssertNotNull('B Have field',Fld);
  2084. Attrs:=Fld.GetAttributes;
  2085. AssertNotNull('B Have attribute data',Pointer(Attrs));
  2086. AssertEquals('A Attribute count',1,Length(Attrs));
  2087. AssertEquals('A Attribute 1 name','My3Attribute',Attrs[0].ClassName);
  2088. O:=Attrs[0];
  2089. AssertNotNull('B: Attribute class ',O);
  2090. AssertEquals('B: Attribute class ',O.ClassType,My3Attribute);
  2091. AssertEquals('B: Attribute value ',4,M3.Int);
  2092. end;
  2093. initialization
  2094. {$ifdef fpc}
  2095. RegisterTest(TTestRTTI);
  2096. RegisterTest(TTestClassExtendedRTTI);
  2097. RegisterTest(TTestRecordExtendedRTTI);
  2098. {$else fpc}
  2099. RegisterTest(TTestRTTI.Suite);
  2100. RegisterTest(TTestClassExtendedRTTI.suite);
  2101. RegisterTest(TTestRecordExtendedRTTI.Suite);
  2102. {$endif fpc}
  2103. end.