tests.rtti.pas 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573
  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 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 TestPropSetValueString;
  40. procedure TestPropSetValueInteger;
  41. procedure TestPropSetValueBoolean;
  42. procedure TestPropSetValueShortString;
  43. procedure TestPropSetValueObject;
  44. procedure TestPropSetValueInterface;
  45. procedure TestPropSetValueFloat;
  46. procedure TestPropSetValueDynArray;
  47. procedure TestPropSetValueEnumeration;
  48. procedure TestPropSetValueChars;
  49. procedure TestGetValueStringCastError;
  50. procedure TestGetIsReadable;
  51. procedure TestIsWritable;
  52. procedure TestIsType;
  53. procedure TestMakeNil;
  54. procedure TestMakeObject;
  55. procedure TestMakeArrayDynamic;
  56. procedure TestMakeArrayStatic;
  57. {$ifdef fpc}
  58. procedure TestMakeArrayOpen;
  59. {$endif}
  60. procedure TestMakeSingle;
  61. procedure TestMakeDouble;
  62. procedure TestMakeExtended;
  63. procedure TestMakeCurrency;
  64. procedure TestMakeComp;
  65. procedure TestMakeEnum;
  66. procedure TestMakeAnsiChar;
  67. procedure TestMakeWideChar;
  68. procedure TestFromOrdinal;
  69. procedure TestDataSize;
  70. procedure TestDataSizeEmpty;
  71. procedure TestReferenceRawData;
  72. procedure TestReferenceRawDataEmpty;
  73. procedure TestIsManaged;
  74. {$ifdef fpc}
  75. procedure TestOpenArrayToDyn;
  76. {$endif}
  77. procedure TestInterface;
  78. {$ifdef fpc}
  79. procedure TestInterfaceRaw;
  80. {$endif}
  81. procedure TestProcVar;
  82. procedure TestMethod;
  83. procedure TestRawThunk;
  84. private
  85. procedure MakeFromOrdinalTObject;
  86. procedure MakeFromOrdinalSet;
  87. procedure MakeFromOrdinalString;
  88. procedure MakeFromOrdinalNil;
  89. end;
  90. implementation
  91. uses
  92. Tests.Rtti.Util;
  93. type
  94. {$M+}
  95. TGetClassProperties = class
  96. private
  97. FPubPropRO: integer;
  98. FPubPropRW: integer;
  99. published
  100. property PubPropRO: integer read FPubPropRO;
  101. property PubPropRW: integer read FPubPropRW write FPubPropRW;
  102. property PubPropSetRO: integer read FPubPropRO;
  103. property PubPropSetRW: integer read FPubPropRW write FPubPropRW;
  104. end;
  105. TGetClassPropertiesSub = class(TGetClassProperties)
  106. end;
  107. TTestDynArray = array of Integer;
  108. TTestEnumeration = (en1, en2, en3, en4);
  109. {$M-}
  110. { TTestValueClass }
  111. {$M+}
  112. TTestValueClass = class
  113. private
  114. FAArray: TTestDynArray;
  115. FAChar: AnsiChar;
  116. FAComp: Comp;
  117. FACurrency: Currency;
  118. FADouble: Double;
  119. FAEnumeration: TTestEnumeration;
  120. FAExtended: Extended;
  121. FAInteger: integer;
  122. FAObject: TObject;
  123. FASingle: Single;
  124. FAString: string;
  125. FABoolean: boolean;
  126. FAShortString: ShortString;
  127. FAUnknown: IUnknown;
  128. FAWideChar: WideChar;
  129. function GetAInteger: integer;
  130. function GetAString: string;
  131. function GetABoolean: boolean;
  132. function GetAShortString: ShortString;
  133. procedure SetWriteOnly(AValue: integer);
  134. published
  135. property AArray: TTestDynArray read FAArray write FAArray;
  136. property AEnumeration: TTestEnumeration read FAEnumeration write FAEnumeration;
  137. property AInteger: Integer read FAInteger write FAInteger;
  138. property AString: string read FAString write FAString;
  139. property ASingle: Single read FASingle write FASingle;
  140. property ADouble: Double read FADouble write FADouble;
  141. property AExtended: Extended read FAExtended write FAExtended;
  142. property ACurrency: Currency read FACurrency write FACurrency;
  143. property AObject: TObject read FAObject write FAObject;
  144. property AUnknown: IUnknown read FAUnknown write FAUnknown;
  145. property AComp: Comp read FAComp write FAComp;
  146. property ABoolean: boolean read FABoolean write FABoolean;
  147. property AShortString: ShortString read FAShortString write FAShortString;
  148. property AGetInteger: Integer read GetAInteger;
  149. property AGetString: string read GetAString;
  150. property AGetBoolean: boolean read GetABoolean;
  151. property AGetShortString: ShortString read GetAShortString;
  152. property AWriteOnly: integer write SetWriteOnly;
  153. property AChar: AnsiChar read FAChar write FAChar;
  154. property AWideChar: WideChar read FAWideChar write FAWideChar;
  155. end;
  156. {$M-}
  157. {$M+}
  158. ITestInterface = interface
  159. procedure Test;
  160. function Test2: LongInt;
  161. procedure Test3(aArg1: LongInt; const aArg2: AnsiString; var aArg3: Boolean; out aArg4: Word);
  162. function Test4(aArg1: array of LongInt; aArg2: array of const): AnsiString;
  163. end;
  164. {$M-}
  165. TManagedRec = record
  166. s: string;
  167. end;
  168. {$ifdef fpc}
  169. TManagedRecOp = record
  170. class operator AddRef(var a: TManagedRecOp);
  171. end;
  172. {$endif}
  173. TNonManagedRec = record
  174. i: Integer;
  175. end;
  176. TManagedObj = object
  177. i: IInterface;
  178. end;
  179. TNonManagedObj = object
  180. d: double;
  181. end;
  182. TTestEnum = (te1, te2, te3, te4, te5);
  183. TTestSet = set of TTestEnum;
  184. TTestProc = procedure;
  185. TTestFunc1 = function: LongInt;
  186. TTestFunc2 = function(aArg1: LongInt; aArg2: array of LongInt): String;
  187. TTestMethod = procedure of object;
  188. TTestMethod1 = function: LongInt of object;
  189. TTestMethod2 = function(aArg1: LongInt; aArg2: array of LongInt): String of object;
  190. TTestHelper = class helper for TObject
  191. end;
  192. TArrayOfString = array[0..0] of string;
  193. TArrayOfManagedRec = array[0..0] of TManagedRec;
  194. TArrayOfNonManagedRec = array[0..0] of TNonManagedRec;
  195. TArrayOfByte = array[0..0] of byte;
  196. TArrayOfLongintDyn = array of LongInt;
  197. TArrayOfLongintStatic = array[0..3] of LongInt;
  198. TTestRecord = record
  199. Value1: LongInt;
  200. Value2: String;
  201. end;
  202. PTestRecord = ^TTestRecord;
  203. {$ifdef fpc}
  204. {$PUSH}
  205. {$INTERFACES CORBA}
  206. ICORBATest = interface
  207. end;
  208. {$POP}
  209. {$endif}
  210. {$ifdef fpc}
  211. class operator TManagedRecOp.AddRef(var a: TManagedRecOp);
  212. begin
  213. end;
  214. {$endif}
  215. { TTestValueClass }
  216. function TTestValueClass.GetAInteger: integer;
  217. begin
  218. result := FAInteger;
  219. end;
  220. function TTestValueClass.GetAString: string;
  221. begin
  222. result := FAString;
  223. end;
  224. function TTestValueClass.GetABoolean: boolean;
  225. begin
  226. result := FABoolean;
  227. end;
  228. function TTestValueClass.GetAShortString: ShortString;
  229. begin
  230. Result := FAShortString;
  231. end;
  232. procedure TTestValueClass.SetWriteOnly(AValue: integer);
  233. begin
  234. // Do nothing
  235. end;
  236. { Note: GetTypes currently only returns those types that had been acquired using
  237. GetType, so GetTypes itself can't be really tested currently }
  238. (*procedure TTestCase1.GetTypes;
  239. var
  240. LContext: TRttiContext;
  241. LType: TRttiType;
  242. IsTestCaseClassFound: boolean;
  243. begin
  244. LContext := TRttiContext.Create;
  245. { Enumerate all types declared in the application }
  246. for LType in LContext.GetTypes() do
  247. begin
  248. if LType.Name='TTestCase1' then
  249. IsTestCaseClassFound:=true;
  250. end;
  251. LContext.Free;
  252. CheckTrue(IsTestCaseClassFound, 'RTTI information does not contain class of testcase.');
  253. end;*)
  254. procedure TTestCase1.TestGetValueStringCastError;
  255. var
  256. ATestClass : TTestValueClass;
  257. c: TRttiContext;
  258. ARttiType: TRttiType;
  259. AValue: TValue;
  260. i: integer;
  261. HadException: boolean;
  262. begin
  263. c := TRttiContext.Create;
  264. try
  265. ATestClass := TTestValueClass.Create;
  266. ATestClass.AString := '12';
  267. try
  268. ARttiType := c.GetType(ATestClass.ClassInfo);
  269. AValue := ARttiType.GetProperty('astring').GetValue(ATestClass);
  270. HadException := false;
  271. try
  272. i := AValue.AsInteger;
  273. except
  274. on E: Exception do
  275. if E.ClassType=EInvalidCast then
  276. HadException := true;
  277. end;
  278. Check(HadException, 'No or invalid exception on invalid cast');
  279. finally
  280. AtestClass.Free;
  281. end;
  282. finally
  283. c.Free;
  284. end;
  285. end;
  286. procedure TTestCase1.TestMakeNil;
  287. var
  288. value: TValue;
  289. begin
  290. TValue.Make(Nil, Nil, value);
  291. CheckTrue(value.Kind = tkUnknown);
  292. CheckTrue(value.IsEmpty);
  293. CheckTrue(value.IsObject);
  294. CheckTrue(value.IsClass);
  295. CheckTrue(value.IsOrdinal);
  296. CheckFalse(value.IsArray);
  297. CheckTrue(value.AsObject = Nil);
  298. CheckTrue(value.AsClass = Nil);
  299. CheckTrue(value.AsInterface = Nil);
  300. CheckEquals(0, value.AsOrdinal);
  301. TValue.Make(Nil, TypeInfo(TObject), value);
  302. CheckTrue(value.IsEmpty);
  303. CheckTrue(value.IsObject);
  304. CheckTrue(value.IsClass);
  305. CheckTrue(value.IsOrdinal);
  306. CheckFalse(value.IsArray);
  307. CheckTrue(value.AsObject=Nil);
  308. CheckTrue(value.AsClass=Nil);
  309. CheckTrue(value.AsInterface=Nil);
  310. CheckEquals(0, value.AsOrdinal);
  311. TValue.Make(Nil, TypeInfo(TClass), value);
  312. CheckTrue(value.IsEmpty);
  313. CheckTrue(value.IsClass);
  314. CheckTrue(value.IsOrdinal);
  315. CheckFalse(value.IsArray);
  316. CheckTrue(value.AsObject=Nil);
  317. CheckTrue(value.AsClass=Nil);
  318. CheckTrue(value.AsInterface=Nil);
  319. CheckEquals(0, value.AsOrdinal);
  320. TValue.Make(Nil, TypeInfo(LongInt), value);
  321. CheckTrue(value.IsOrdinal);
  322. CheckFalse(value.IsEmpty);
  323. CheckFalse(value.IsClass);
  324. CheckFalse(value.IsObject);
  325. CheckFalse(value.IsArray);
  326. CheckEquals(0, value.AsOrdinal);
  327. CheckEquals(0, value.AsInteger);
  328. CheckEquals(0, value.AsInt64);
  329. CheckEquals(0, value.AsUInt64);
  330. TValue.Make(Nil, TypeInfo(String), value);
  331. CheckFalse(value.IsEmpty);
  332. CheckFalse(value.IsObject);
  333. CheckFalse(value.IsClass);
  334. CheckFalse(value.IsArray);
  335. CheckEquals('', value.AsString);
  336. end;
  337. procedure TTestCase1.TestMakeObject;
  338. var
  339. AValue: TValue;
  340. ATestClass: TTestValueClass;
  341. begin
  342. ATestClass := TTestValueClass.Create;
  343. ATestClass.AInteger := 54329;
  344. TValue.Make(@ATestClass, TypeInfo(TTestValueClass),AValue);
  345. CheckEquals(AValue.IsClass, False);
  346. CheckEquals(AValue.IsObject, True);
  347. Check(AValue.AsObject=ATestClass);
  348. Check(PPointer(AValue.GetReferenceToRawData)^ = Pointer(ATestClass));
  349. CheckEquals(TTestValueClass(AValue.AsObject).AInteger, 54329);
  350. ATestClass.Free;
  351. end;
  352. procedure TTestCase1.TestMakeArrayDynamic;
  353. var
  354. arr: TArrayOfLongintDyn;
  355. value: TValue;
  356. begin
  357. SetLength(arr, 2);
  358. arr[0] := 42;
  359. arr[1] := 21;
  360. TValue.Make(@arr, TypeInfo(TArrayOfLongintDyn), value);
  361. CheckEquals(value.IsArray, True);
  362. CheckEquals(value.IsObject, False);
  363. CheckEquals(value.IsOrdinal, False);
  364. CheckEquals(value.IsClass, False);
  365. CheckEquals(value.GetArrayLength, 2);
  366. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  367. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  368. Check(PPointer(value.GetReferenceToRawData)^ = Pointer(arr));
  369. value.SetArrayElement(0, 84);
  370. CheckEquals(arr[0], 84);
  371. end;
  372. procedure TTestCase1.TestMakeArrayStatic;
  373. type
  374. TArrStat = array[0..1] of LongInt;
  375. TArrStat2D = array[0..1, 0..1] of LongInt;
  376. var
  377. arr: TArrStat;
  378. arr2D: TArrStat2D;
  379. value: TValue;
  380. begin
  381. arr[0] := 42;
  382. arr[1] := 21;
  383. TValue.Make(@arr, TypeInfo(TArrStat), value);
  384. CheckEquals(value.IsArray, True);
  385. CheckEquals(value.IsObject, False);
  386. CheckEquals(value.IsOrdinal, False);
  387. CheckEquals(value.IsClass, False);
  388. CheckEquals(value.GetArrayLength, 2);
  389. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  390. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  391. value.SetArrayElement(0, 84);
  392. { since this is a static array the original array isn't touched! }
  393. CheckEquals(arr[0], 42);
  394. arr2D[0, 0] := 42;
  395. arr2D[0, 1] := 21;
  396. arr2D[1, 0] := 84;
  397. arr2D[1, 1] := 63;
  398. TValue.Make(@arr2D, TypeInfo(TArrStat2D), value);
  399. CheckEquals(value.IsArray, True);
  400. CheckEquals(value.GetArrayLength, 4);
  401. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  402. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  403. CheckEquals(value.GetArrayElement(2).AsInteger, 84);
  404. CheckEquals(value.GetArrayElement(3).AsInteger, 63);
  405. end;
  406. {$ifdef fpc}
  407. procedure TTestCase1.TestMakeArrayOpen;
  408. procedure TestOpenArrayValueCopy(aArr: array of LongInt);
  409. var
  410. value: TValue;
  411. begin
  412. TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
  413. CheckEquals(value.IsArray, True);
  414. CheckEquals(value.IsOpenArray, True);
  415. CheckEquals(value.IsObject, False);
  416. CheckEquals(value.IsOrdinal, False);
  417. CheckEquals(value.IsClass, False);
  418. CheckEquals(value.GetArrayLength, 2);
  419. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  420. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  421. value.SetArrayElement(0, 84);
  422. { since this is an open array the original array is modified! }
  423. CheckEquals(aArr[0], 84);
  424. end;
  425. procedure TestOpenArrayValueVar(var aArr: array of LongInt);
  426. var
  427. value: TValue;
  428. begin
  429. TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
  430. CheckEquals(value.IsArray, True);
  431. CheckEquals(value.IsOpenArray, True);
  432. CheckEquals(value.IsObject, False);
  433. CheckEquals(value.IsOrdinal, False);
  434. CheckEquals(value.IsClass, False);
  435. CheckEquals(value.GetArrayLength, 2);
  436. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  437. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  438. value.SetArrayElement(0, 84);
  439. { since this is an open array the original array is modified! }
  440. CheckEquals(aArr[0], 84);
  441. end;
  442. procedure TestOpenArrayValueOut(var aArr: array of LongInt);
  443. var
  444. value: TValue;
  445. begin
  446. TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
  447. CheckEquals(value.IsArray, True);
  448. CheckEquals(value.IsOpenArray, True);
  449. CheckEquals(value.IsObject, False);
  450. CheckEquals(value.IsOrdinal, False);
  451. CheckEquals(value.IsClass, False);
  452. CheckEquals(value.GetArrayLength, 2);
  453. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  454. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  455. value.SetArrayElement(0, 84);
  456. value.SetArrayElement(1, 128);
  457. { since this is an open array the original array is modified! }
  458. CheckEquals(aArr[0], 84);
  459. CheckEquals(aArr[1], 128);
  460. CheckEquals(value.GetArrayElement(0).AsInteger, 84);
  461. CheckEquals(value.GetArrayElement(1).AsInteger, 128);
  462. end;
  463. var
  464. arr: array of LongInt;
  465. begin
  466. TestOpenArrayValueCopy([42, 21]);
  467. arr := [42, 21];
  468. TestOpenArrayValueVar(arr);
  469. CheckEquals(arr[0], 84);
  470. CheckEquals(arr[1], 21);
  471. arr := [42, 21];
  472. TestOpenArrayValueOut(arr);
  473. CheckEquals(arr[0], 84);
  474. CheckEquals(arr[1], 128);
  475. end;
  476. {$endif}
  477. procedure TTestCase1.TestMakeSingle;
  478. var
  479. fs: Single;
  480. v: TValue;
  481. hadexcept: Boolean;
  482. begin
  483. fs := 3.14;
  484. TValue.Make(@fs, TypeInfo(fs), v);
  485. CheckEquals(v.IsClass, False);
  486. CheckEquals(v.IsObject, False);
  487. CheckEquals(v.IsOrdinal, False);
  488. Check(v.AsExtended=fs);
  489. Check(v.GetReferenceToRawData <> @fs);
  490. try
  491. hadexcept := False;
  492. v.AsInt64;
  493. except
  494. hadexcept := True;
  495. end;
  496. CheckTrue(hadexcept, 'No signed type conversion exception');
  497. try
  498. hadexcept := False;
  499. v.AsUInt64;
  500. except
  501. hadexcept := True;
  502. end;
  503. CheckTrue(hadexcept, 'No unsigned type conversion exception');
  504. end;
  505. procedure TTestCase1.TestMakeDouble;
  506. var
  507. fd: Double;
  508. v: TValue;
  509. hadexcept: Boolean;
  510. begin
  511. fd := 3.14;
  512. TValue.Make(@fd, TypeInfo(fd), v);
  513. CheckEquals(v.IsClass, False);
  514. CheckEquals(v.IsObject, False);
  515. CheckEquals(v.IsOrdinal, False);
  516. Check(v.AsExtended=fd);
  517. Check(v.GetReferenceToRawData <> @fd);
  518. try
  519. hadexcept := False;
  520. v.AsInt64;
  521. except
  522. hadexcept := True;
  523. end;
  524. CheckTrue(hadexcept, 'No signed type conversion exception');
  525. try
  526. hadexcept := False;
  527. v.AsUInt64;
  528. except
  529. hadexcept := True;
  530. end;
  531. CheckTrue(hadexcept, 'No unsigned type conversion exception');
  532. end;
  533. procedure TTestCase1.TestMakeExtended;
  534. var
  535. fe: Extended;
  536. v: TValue;
  537. hadexcept: Boolean;
  538. begin
  539. fe := 3.14;
  540. TValue.Make(@fe, TypeInfo(fe), v);
  541. CheckEquals(v.IsClass, False);
  542. CheckEquals(v.IsObject, False);
  543. CheckEquals(v.IsOrdinal, False);
  544. Check(v.AsExtended=fe);
  545. Check(v.GetReferenceToRawData <> @fe);
  546. try
  547. hadexcept := False;
  548. v.AsInt64;
  549. except
  550. hadexcept := True;
  551. end;
  552. CheckTrue(hadexcept, 'No signed type conversion exception');
  553. try
  554. hadexcept := False;
  555. v.AsUInt64;
  556. except
  557. hadexcept := True;
  558. end;
  559. CheckTrue(hadexcept, 'No unsigned type conversion exception');
  560. end;
  561. procedure TTestCase1.TestMakeCurrency;
  562. var
  563. fcu: Currency;
  564. v: TValue;
  565. hadexcept: Boolean;
  566. begin
  567. fcu := 3.14;
  568. TValue.Make(@fcu, TypeInfo(fcu), v);
  569. CheckEquals(v.IsClass, False);
  570. CheckEquals(v.IsObject, False);
  571. CheckEquals(v.IsOrdinal, False);
  572. Check(v.AsExtended=Extended(fcu));
  573. Check(v.AsCurrency=fcu);
  574. Check(v.GetReferenceToRawData <> @fcu);
  575. try
  576. hadexcept := False;
  577. v.AsInt64;
  578. except
  579. hadexcept := True;
  580. end;
  581. CheckTrue(hadexcept, 'No signed type conversion exception');
  582. try
  583. hadexcept := False;
  584. v.AsUInt64;
  585. except
  586. hadexcept := True;
  587. end;
  588. CheckTrue(hadexcept, 'No unsigned type conversion exception');
  589. end;
  590. procedure TTestCase1.TestMakeComp;
  591. var
  592. fco: Comp;
  593. v: TValue;
  594. hadexcept: Boolean;
  595. begin
  596. fco := 314;
  597. TValue.Make(@fco, TypeInfo(fco), v);
  598. if v.Kind <> tkFloat then
  599. Exit;
  600. CheckEquals(v.IsClass, False);
  601. CheckEquals(v.IsObject, False);
  602. CheckEquals(v.IsOrdinal, False);
  603. Check(v.AsExtended=Extended(fco));
  604. Check(v.GetReferenceToRawData <> @fco);
  605. try
  606. hadexcept := False;
  607. CheckEquals(v.AsInt64, 314);
  608. except
  609. hadexcept := True;
  610. end;
  611. CheckFalse(hadexcept, 'Had signed type conversion exception');
  612. try
  613. hadexcept := False;
  614. CheckEquals(v.AsUInt64, 314);
  615. except
  616. hadexcept := True;
  617. end;
  618. CheckFalse(hadexcept, 'Had unsigned type conversion exception');
  619. end;
  620. procedure TTestCase1.TestMakeEnum;
  621. var
  622. e: TTestEnum;
  623. v: TValue;
  624. begin
  625. e := te1;
  626. TValue.Make(@e, TypeInfo(e), v);
  627. Check(not v.IsClass);
  628. Check(not v.IsArray);
  629. Check(not v.IsEmpty);
  630. Check(not v.IsOpenArray);
  631. Check(not v.IsObject);
  632. Check(v.IsOrdinal);
  633. Check(v.GetReferenceToRawData <> @e);
  634. Check(TTestEnum(v.AsOrdinal) = te1);
  635. end;
  636. procedure TTestCase1.TestMakeAnsiChar;
  637. var
  638. c: AnsiChar;
  639. v: TValue;
  640. begin
  641. c := #20;
  642. TValue.Make(@c, TypeInfo(c), v);
  643. Check(not v.IsClass);
  644. Check(not v.IsArray);
  645. Check(not v.IsEmpty);
  646. Check(not v.IsOpenArray);
  647. Check(not v.IsObject);
  648. Check(v.IsOrdinal);
  649. Check(v.GetReferenceToRawData <> @c);
  650. Check(AnsiChar(v.AsOrdinal) = #20);
  651. Check(v.AsAnsiChar = #20);
  652. end;
  653. procedure TTestCase1.TestMakeWideChar;
  654. var
  655. c: WideChar;
  656. v: TValue;
  657. begin
  658. c := #$1234;
  659. TValue.Make(@c, TypeInfo(c), v);
  660. Check(not v.IsClass);
  661. Check(not v.IsArray);
  662. Check(not v.IsEmpty);
  663. Check(not v.IsOpenArray);
  664. Check(not v.IsObject);
  665. Check(v.IsOrdinal);
  666. Check(v.GetReferenceToRawData <> @c);
  667. Check(WideChar(v.AsOrdinal) = #$1234);
  668. Check(v.AsWideChar = #$1234);
  669. end;
  670. procedure TTestCase1.MakeFromOrdinalTObject;
  671. begin
  672. TValue.FromOrdinal(TypeInfo(TObject), 42);
  673. end;
  674. procedure TTestCase1.MakeFromOrdinalSet;
  675. begin
  676. TValue.FromOrdinal(TypeInfo(TTestSet), 42);
  677. end;
  678. procedure TTestCase1.MakeFromOrdinalString;
  679. begin
  680. TValue.FromOrdinal(TypeInfo(AnsiString), 42);
  681. end;
  682. procedure TTestCase1.MakeFromOrdinalNil;
  683. begin
  684. TValue.FromOrdinal(Nil, 42);
  685. end;
  686. procedure TTestCase1.TestFromOrdinal;
  687. var
  688. v: TValue;
  689. begin
  690. v := TValue.FromOrdinal(TypeInfo(LongInt), 42);
  691. Check(v.IsOrdinal);
  692. CheckEquals(v.AsOrdinal, 42);
  693. v := TValue.FromOrdinal(TypeInfo(Boolean), Ord(True));
  694. Check(v.IsOrdinal);
  695. CheckEquals(v.AsOrdinal, Ord(True));
  696. v := TValue.FromOrdinal(TypeInfo(Int64), $1234123412341234);
  697. Check(v.IsOrdinal);
  698. CheckEquals(v.AsOrdinal, $1234123412341234);
  699. v := TValue.FromOrdinal(TypeInfo(QWord), $1234123412341234);
  700. Check(v.IsOrdinal);
  701. CheckEquals(v.AsOrdinal, $1234123412341234);
  702. v := TValue.FromOrdinal(TypeInfo(LongBool), Ord(True));
  703. Check(v.IsOrdinal);
  704. CheckEquals(v.AsOrdinal, Ord(True));
  705. v := TValue.FromOrdinal(TypeInfo(TTestEnum), Ord(te1));
  706. Check(v.IsOrdinal);
  707. CheckEquals(v.AsOrdinal, Ord(te1));
  708. v := TValue.FromOrdinal(TypeInfo(AnsiChar), Ord(#20));
  709. Check(v.IsOrdinal);
  710. CheckEquals(v.AsOrdinal, Ord(#20));
  711. v := TValue.FromOrdinal(TypeInfo(WideChar), Ord(#$1234));
  712. Check(v.IsOrdinal);
  713. CheckEquals(v.AsOrdinal, Ord(#$1234));
  714. CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalNil, EInvalidCast);
  715. CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalTObject, EInvalidCast);
  716. CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalSet, EInvalidCast);
  717. CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalString, EInvalidCast);
  718. end;
  719. procedure TTestCase1.TestGetIsReadable;
  720. var
  721. c: TRttiContext;
  722. ARttiType: TRttiType;
  723. AProperty: TRttiProperty;
  724. begin
  725. c := TRttiContext.Create;
  726. try
  727. ARttiType := c.GetType(TTestValueClass);
  728. AProperty := ARttiType.GetProperty('aBoolean');
  729. CheckEquals(AProperty.IsReadable, true);
  730. AProperty := ARttiType.GetProperty('aGetBoolean');
  731. CheckEquals(AProperty.IsReadable, true);
  732. AProperty := ARttiType.GetProperty('aWriteOnly');
  733. CheckEquals(AProperty.IsReadable, False);
  734. finally
  735. c.Free;
  736. end;
  737. end;
  738. procedure TTestCase1.TestIsWritable;
  739. var
  740. c: TRttiContext;
  741. ARttiType: TRttiType;
  742. AProperty: TRttiProperty;
  743. begin
  744. c := TRttiContext.Create;
  745. try
  746. ARttiType := c.GetType(TTestValueClass);
  747. AProperty := ARttiType.GetProperty('aBoolean');
  748. CheckEquals(AProperty.IsWritable, true);
  749. AProperty := ARttiType.GetProperty('aGetBoolean');
  750. CheckEquals(AProperty.IsWritable, false);
  751. AProperty := ARttiType.GetProperty('aWriteOnly');
  752. CheckEquals(AProperty.IsWritable, True);
  753. finally
  754. c.Free;
  755. end;
  756. end;
  757. procedure TTestCase1.TestIsType;
  758. type
  759. TMyLongInt = type LongInt;
  760. var
  761. v: TValue;
  762. l: LongInt;
  763. ml: TMyLongInt;
  764. begin
  765. l := 42;
  766. ml := 42;
  767. TValue.Make(@l, TypeInfo(l), v);
  768. Check(v.IsType(TypeInfo(l)));
  769. Check(not v.IsType(TypeInfo(ml)));
  770. Check(not v.IsType(TypeInfo(String)));
  771. Check(v.specialize IsType<LongInt>);
  772. Check(not v.specialize IsType<TMyLongInt>);
  773. Check(not v.specialize IsType<String>);
  774. TValue.Make(@ml, TypeInfo(ml), v);
  775. Check(v.IsType(TypeInfo(ml)));
  776. Check(not v.IsType(TypeInfo(l)));
  777. Check(not v.IsType(TypeInfo(String)));
  778. Check(v.specialize IsType<TMyLongInt>);
  779. Check(not v.specialize IsType<LongInt>);
  780. Check(not v.specialize IsType<String>);
  781. end;
  782. procedure TTestCase1.TestPropGetValueBoolean;
  783. var
  784. ATestClass : TTestValueClass;
  785. c: TRttiContext;
  786. ARttiType: TRttiType;
  787. AProperty: TRttiProperty;
  788. AValue: TValue;
  789. begin
  790. c := TRttiContext.Create;
  791. try
  792. ATestClass := TTestValueClass.Create;
  793. ATestClass.ABoolean := true;
  794. try
  795. ARttiType := c.GetType(ATestClass.ClassInfo);
  796. Check(assigned(ARttiType));
  797. AProperty := ARttiType.GetProperty('aBoolean');
  798. AValue := AProperty.GetValue(ATestClass);
  799. CheckEquals(true,AValue.AsBoolean);
  800. ATestClass.ABoolean := false;
  801. CheckEquals(true, AValue.AsBoolean);
  802. CheckEquals('True', AValue.ToString);
  803. CheckEquals(True, AValue.IsOrdinal);
  804. CheckEquals(1, AValue.AsOrdinal);
  805. finally
  806. AtestClass.Free;
  807. end;
  808. CheckEquals(True,AValue.AsBoolean);
  809. finally
  810. c.Free;
  811. end;
  812. end;
  813. procedure TTestCase1.TestPropGetValueShortString;
  814. var
  815. ATestClass : TTestValueClass;
  816. c: TRttiContext;
  817. ARttiType: TRttiType;
  818. AProperty: TRttiProperty;
  819. AValue: TValue;
  820. begin
  821. c := TRttiContext.Create;
  822. try
  823. ATestClass := TTestValueClass.Create;
  824. ATestClass.AShortString := 'Hello World';
  825. try
  826. ARttiType := c.GetType(ATestClass.ClassInfo);
  827. Check(assigned(ARttiType));
  828. AProperty := ARttiType.GetProperty('aShortString');
  829. AValue := AProperty.GetValue(ATestClass);
  830. CheckEquals('Hello World',AValue.AsString);
  831. ATestClass.AShortString := 'Foobar';
  832. CheckEquals('Hello World', AValue.AsString);
  833. CheckEquals(False, AValue.IsOrdinal);
  834. CheckEquals(False, AValue.IsObject);
  835. CheckEquals(False, AValue.IsArray);
  836. CheckEquals(False, AValue.IsClass);
  837. finally
  838. AtestClass.Free;
  839. end;
  840. CheckEquals('Hello World',AValue.AsString);
  841. finally
  842. c.Free;
  843. end;
  844. end;
  845. procedure TTestCase1.TestPropGetValueInteger;
  846. var
  847. ATestClass : TTestValueClass;
  848. c: TRttiContext;
  849. ARttiType: TRttiType;
  850. AProperty: TRttiProperty;
  851. AValue: TValue;
  852. begin
  853. c := TRttiContext.Create;
  854. try
  855. ATestClass := TTestValueClass.Create;
  856. ATestClass.AInteger := 472349;
  857. try
  858. ARttiType := c.GetType(ATestClass.ClassInfo);
  859. Check(assigned(ARttiType));
  860. AProperty := ARttiType.GetProperty('ainteger');
  861. AValue := AProperty.GetValue(ATestClass);
  862. CheckEquals(472349,AValue.AsInteger);
  863. ATestClass.AInteger := 12;
  864. CheckEquals(472349, AValue.AsInteger);
  865. CheckEquals('472349', AValue.ToString);
  866. CheckEquals(True, AValue.IsOrdinal);
  867. finally
  868. AtestClass.Free;
  869. end;
  870. CheckEquals(472349,AValue.AsInteger);
  871. finally
  872. c.Free;
  873. end;
  874. end;
  875. procedure TTestCase1.TestPropGetValueString;
  876. var
  877. ATestClass : TTestValueClass;
  878. c: TRttiContext;
  879. ARttiType: TRttiType;
  880. AProperty: TRttiProperty;
  881. AValue: TValue;
  882. i: int64;
  883. begin
  884. c := TRttiContext.Create;
  885. try
  886. ATestClass := TTestValueClass.Create;
  887. ATestClass.AString := 'Hello World';
  888. try
  889. ARttiType := c.GetType(ATestClass.ClassInfo);
  890. Check(assigned(ARttiType));
  891. AProperty := ARttiType.GetProperty('astring');
  892. AValue := AProperty.GetValue(ATestClass);
  893. CheckEquals('Hello World',AValue.AsString);
  894. ATestClass.AString := 'Goodbye World';
  895. CheckEquals('Hello World',AValue.AsString);
  896. CheckEquals('Hello World',AValue.ToString);
  897. Check(TypeInfo(string)=AValue.TypeInfo);
  898. Check(AValue.TypeData=GetTypeData(AValue.TypeInfo));
  899. Check(AValue.IsEmpty=false);
  900. Check(AValue.IsObject=false);
  901. Check(AValue.IsClass=false);
  902. CheckEquals(AValue.IsOrdinal, false);
  903. CheckEquals(AValue.TryAsOrdinal(i), false);
  904. CheckEquals(AValue.IsType(TypeInfo(string)), true);
  905. CheckEquals(AValue.IsType(TypeInfo(integer)), false);
  906. CheckEquals(AValue.IsArray, false);
  907. finally
  908. AtestClass.Free;
  909. end;
  910. CheckEquals('Hello World',AValue.AsString);
  911. finally
  912. c.Free;
  913. end;
  914. end;
  915. procedure TTestCase1.TestPropGetValueProcBoolean;
  916. var
  917. ATestClass : TTestValueClass;
  918. c: TRttiContext;
  919. ARttiType: TRttiType;
  920. AProperty: TRttiProperty;
  921. AValue: TValue;
  922. begin
  923. c := TRttiContext.Create;
  924. try
  925. ATestClass := TTestValueClass.Create;
  926. ATestClass.ABoolean := true;
  927. try
  928. ARttiType := c.GetType(ATestClass.ClassInfo);
  929. Check(assigned(ARttiType));
  930. AProperty := ARttiType.GetProperty('aGetBoolean');
  931. AValue := AProperty.GetValue(ATestClass);
  932. CheckEquals(true,AValue.AsBoolean);
  933. finally
  934. AtestClass.Free;
  935. end;
  936. CheckEquals(True,AValue.AsBoolean);
  937. finally
  938. c.Free;
  939. end;
  940. end;
  941. procedure TTestCase1.TestPropGetValueProcShortString;
  942. var
  943. ATestClass : TTestValueClass;
  944. c: TRttiContext;
  945. ARttiType: TRttiType;
  946. AProperty: TRttiProperty;
  947. AValue: TValue;
  948. begin
  949. c := TRttiContext.Create;
  950. try
  951. ATestClass := TTestValueClass.Create;
  952. ATestClass.AShortString := 'Hello World';
  953. try
  954. ARttiType := c.GetType(ATestClass.ClassInfo);
  955. Check(assigned(ARttiType));
  956. AProperty := ARttiType.GetProperty('aGetShortString');
  957. AValue := AProperty.GetValue(ATestClass);
  958. CheckEquals('Hello World',AValue.AsString);
  959. finally
  960. AtestClass.Free;
  961. end;
  962. CheckEquals('Hello World',AValue.AsString);
  963. finally
  964. c.Free;
  965. end;
  966. end;
  967. procedure TTestCase1.TestPropGetValueObject;
  968. var
  969. ATestClass : TTestValueClass;
  970. c: TRttiContext;
  971. ARttiType: TRttiType;
  972. AProperty: TRttiProperty;
  973. AValue: TValue;
  974. O: TObject;
  975. begin
  976. c := TRttiContext.Create;
  977. O := TObject.Create;
  978. try
  979. ATestClass := TTestValueClass.Create;
  980. ATestClass.AObject := O;
  981. try
  982. ARttiType := c.GetType(ATestClass.ClassInfo);
  983. Check(assigned(ARttiType));
  984. AProperty := ARttiType.GetProperty('AObject');
  985. AValue := AProperty.GetValue(ATestClass);
  986. CheckEquals(O.GetHashCode, AValue.AsObject.GetHashCode);
  987. finally
  988. AtestClass.Free;
  989. end;
  990. CheckEquals(O.GetHashCode, AValue.AsObject.GetHashCode);
  991. finally
  992. c.Free;
  993. O.Free;
  994. end;
  995. end;
  996. procedure TTestCase1.TestPropGetValueInterface;
  997. var
  998. ATestClass : TTestValueClass;
  999. c: TRttiContext;
  1000. ARttiType: TRttiType;
  1001. AProperty: TRttiProperty;
  1002. AValue: TValue;
  1003. i: IInterface;
  1004. begin
  1005. c := TRttiContext.Create;
  1006. i := TInterfacedObject.Create;
  1007. try
  1008. ATestClass := TTestValueClass.Create;
  1009. ATestClass.AUnknown := i;
  1010. try
  1011. ARttiType := c.GetType(ATestClass.ClassInfo);
  1012. Check(assigned(ARttiType));
  1013. AProperty := ARttiType.GetProperty('AUnknown');
  1014. AValue := AProperty.GetValue(ATestClass);
  1015. Check(i = AValue.AsInterface);
  1016. finally
  1017. AtestClass.Free;
  1018. end;
  1019. Check(i = AValue.AsInterface);
  1020. finally
  1021. c.Free;
  1022. end;
  1023. end;
  1024. procedure TTestCase1.TestPropGetValueFloat;
  1025. var
  1026. ATestClass : TTestValueClass;
  1027. c: TRttiContext;
  1028. ARttiType: TRttiType;
  1029. AProperty: TRttiProperty;
  1030. AValueS, AValueD, AValueE, AValueC, AValueCm: TValue;
  1031. begin
  1032. c := TRttiContext.Create;
  1033. try
  1034. ATestClass := TTestValueClass.Create;
  1035. ATestClass.ASingle := 1.1;
  1036. ATestClass.ADouble := 2.2;
  1037. ATestClass.AExtended := 3.3;
  1038. ATestClass.ACurrency := 4;
  1039. ATestClass.AComp := 5;
  1040. try
  1041. ARttiType := c.GetType(ATestClass.ClassInfo);
  1042. Check(assigned(ARttiType));
  1043. AProperty := ARttiType.GetProperty('ASingle');
  1044. AValueS := AProperty.GetValue(ATestClass);
  1045. CheckEquals(1.1, AValueS.AsExtended, 0.001);
  1046. AProperty := ARttiType.GetProperty('ADouble');
  1047. AValueD := AProperty.GetValue(ATestClass);
  1048. CheckEquals(2.2, AValueD.AsExtended, 0.001);
  1049. AProperty := ARttiType.GetProperty('AExtended');
  1050. AValueE := AProperty.GetValue(ATestClass);
  1051. CheckEquals(3.3, AValueE.AsExtended, 0.001);
  1052. AProperty := ARttiType.GetProperty('ACurrency');
  1053. AValueC := AProperty.GetValue(ATestClass);
  1054. CheckEquals(4.0, AValueC.AsExtended, 0.001);
  1055. AProperty := ARttiType.GetProperty('AComp');
  1056. AValueCm := AProperty.GetValue(ATestClass);
  1057. CheckEquals(5.0, AValueCm.AsExtended, 0.001);
  1058. finally
  1059. AtestClass.Free;
  1060. end;
  1061. CheckEquals(1.1, AValueS.AsExtended, 0.001);
  1062. CheckEquals(2.2, AValueD.AsExtended, 0.001);
  1063. CheckEquals(3.3, AValueE.AsExtended, 0.001);
  1064. CheckEquals(4.0, AValueC.AsExtended, 0.001);
  1065. CheckEquals(5.0, AValueCm.AsExtended, 0.001);
  1066. finally
  1067. c.Free;
  1068. end;
  1069. end;
  1070. procedure TTestCase1.TestPropGetValueDynArray;
  1071. var
  1072. ATestClass : TTestValueClass;
  1073. c: TRttiContext;
  1074. ARttiType: TRttiType;
  1075. AProperty: TRttiProperty;
  1076. AValue: TValue;
  1077. A: TTestDynArray;
  1078. begin
  1079. c := TRttiContext.Create;
  1080. A := [1, 2, 3, 4];
  1081. try
  1082. ATestClass := TTestValueClass.Create;
  1083. ATestClass.AArray := A;
  1084. try
  1085. ARttiType := c.GetType(ATestClass.ClassInfo);
  1086. Check(assigned(ARttiType));
  1087. AProperty := ARttiType.GetProperty('AArray');
  1088. AValue := AProperty.GetValue(ATestClass);
  1089. CheckEquals(A[0], AValue.GetArrayElement(0).AsInteger);
  1090. CheckEquals(A[1], AValue.GetArrayElement(1).AsInteger);
  1091. CheckEquals(A[2], AValue.GetArrayElement(2).AsInteger);
  1092. CheckEquals(A[3], AValue.GetArrayElement(3).AsInteger);
  1093. finally
  1094. AtestClass.Free;
  1095. end;
  1096. finally
  1097. c.Free;
  1098. end;
  1099. end;
  1100. procedure TTestCase1.TestPropGetValueEnumeration;
  1101. var
  1102. ATestClass : TTestValueClass;
  1103. c: TRttiContext;
  1104. ARttiType: TRttiType;
  1105. AProperty: TRttiProperty;
  1106. AValue: TValue;
  1107. begin
  1108. c := TRttiContext.Create;
  1109. try
  1110. ATestClass := TTestValueClass.Create;
  1111. ATestClass.AEnumeration := en3;
  1112. try
  1113. ARttiType := c.GetType(ATestClass.ClassInfo);
  1114. Check(assigned(ARttiType));
  1115. AProperty := ARttiType.GetProperty('AEnumeration');
  1116. AValue := AProperty.GetValue(ATestClass);
  1117. CheckEquals(Ord(en3),AValue.AsOrdinal);
  1118. ATestClass.AEnumeration := en1;
  1119. CheckEquals(Ord(en3), AValue.AsOrdinal);
  1120. CheckEquals('en3', AValue.ToString);
  1121. CheckEquals(True, AValue.IsOrdinal);
  1122. finally
  1123. AtestClass.Free;
  1124. end;
  1125. CheckEquals(Ord(en3),AValue.AsOrdinal);
  1126. finally
  1127. c.Free;
  1128. end;
  1129. end;
  1130. procedure TTestCase1.TestPropGetValueChars;
  1131. var
  1132. ATestClass : TTestValueClass;
  1133. c: TRttiContext;
  1134. ARttiType: TRttiType;
  1135. AProperty: TRttiProperty;
  1136. AValueC, AValueW: TValue;
  1137. begin
  1138. c := TRttiContext.Create;
  1139. try
  1140. ATestClass := TTestValueClass.Create;
  1141. ATestClass.AChar := 'C';
  1142. ATestClass.AWideChar := 'W';
  1143. try
  1144. ARttiType := c.GetType(ATestClass.ClassInfo);
  1145. Check(assigned(ARttiType));
  1146. AProperty := ARttiType.GetProperty('AChar');
  1147. AValueC := AProperty.GetValue(ATestClass);
  1148. CheckEquals('C',AValueC.AsAnsiChar);
  1149. ATestClass.AChar := 'N';
  1150. CheckEquals('C', AValueC.AsAnsiChar);
  1151. CheckEquals('C', AValueC.ToString);
  1152. CheckEquals(True, AValueC.IsOrdinal);
  1153. AProperty := ARttiType.GetProperty('AWideChar');
  1154. AValueW := AProperty.GetValue(ATestClass);
  1155. CheckEquals('W',AValueW.AsWideChar);
  1156. ATestClass.AWideChar := 'Z';
  1157. CheckEquals('W', AValueW.AsWideChar);
  1158. CheckEquals('W', AValueW.ToString);
  1159. CheckEquals(True, AValueW.IsOrdinal);
  1160. finally
  1161. AtestClass.Free;
  1162. end;
  1163. CheckEquals('C',AValueC.AsAnsiChar);
  1164. CheckEquals('W',AValueW.AsWideChar);
  1165. finally
  1166. c.Free;
  1167. end;
  1168. end;
  1169. procedure TTestCase1.TestPropSetValueString;
  1170. var
  1171. ATestClass : TTestValueClass;
  1172. c: TRttiContext;
  1173. ARttiType: TRttiType;
  1174. AProperty: TRttiProperty;
  1175. AValue: TValue;
  1176. s: string;
  1177. begin
  1178. c := TRttiContext.Create;
  1179. try
  1180. ATestClass := TTestValueClass.Create;
  1181. try
  1182. ARttiType := c.GetType(ATestClass.ClassInfo);
  1183. AProperty := ARttiType.GetProperty('astring');
  1184. s := 'ipse lorem or something like that';
  1185. TValue.Make(@s, TypeInfo(string), AValue);
  1186. AProperty.SetValue(ATestClass, AValue);
  1187. CheckEquals(ATestClass.AString, s);
  1188. s := 'Another string';
  1189. CheckEquals(ATestClass.AString, 'ipse lorem or something like that');
  1190. finally
  1191. AtestClass.Free;
  1192. end;
  1193. finally
  1194. c.Free;
  1195. end;
  1196. end;
  1197. procedure TTestCase1.TestPropSetValueInteger;
  1198. var
  1199. ATestClass : TTestValueClass;
  1200. c: TRttiContext;
  1201. ARttiType: TRttiType;
  1202. AProperty: TRttiProperty;
  1203. AValue: TValue;
  1204. i: integer;
  1205. begin
  1206. c := TRttiContext.Create;
  1207. try
  1208. ATestClass := TTestValueClass.Create;
  1209. try
  1210. ARttiType := c.GetType(ATestClass.ClassInfo);
  1211. AProperty := ARttiType.GetProperty('aInteger');
  1212. i := -43573;
  1213. TValue.Make(@i, TypeInfo(Integer), AValue);
  1214. AProperty.SetValue(ATestClass, AValue);
  1215. CheckEquals(ATestClass.AInteger, i);
  1216. i := 1;
  1217. CheckEquals(ATestClass.AInteger, -43573);
  1218. finally
  1219. AtestClass.Free;
  1220. end;
  1221. finally
  1222. c.Free;
  1223. end;
  1224. end;
  1225. procedure TTestCase1.TestPropSetValueBoolean;
  1226. var
  1227. ATestClass : TTestValueClass;
  1228. c: TRttiContext;
  1229. ARttiType: TRttiType;
  1230. AProperty: TRttiProperty;
  1231. AValue: TValue;
  1232. b: boolean;
  1233. begin
  1234. c := TRttiContext.Create;
  1235. try
  1236. ATestClass := TTestValueClass.Create;
  1237. try
  1238. ARttiType := c.GetType(ATestClass.ClassInfo);
  1239. AProperty := ARttiType.GetProperty('aboolean');
  1240. b := true;
  1241. TValue.Make(@b, TypeInfo(Boolean), AValue);
  1242. AProperty.SetValue(ATestClass, AValue);
  1243. CheckEquals(ATestClass.ABoolean, b);
  1244. b := false;
  1245. CheckEquals(ATestClass.ABoolean, true);
  1246. TValue.Make(@b, TypeInfo(Boolean), AValue);
  1247. AProperty.SetValue(ATestClass, AValue);
  1248. CheckEquals(ATestClass.ABoolean, false);
  1249. finally
  1250. AtestClass.Free;
  1251. end;
  1252. finally
  1253. c.Free;
  1254. end;
  1255. end;
  1256. procedure TTestCase1.TestPropSetValueShortString;
  1257. var
  1258. ATestClass : TTestValueClass;
  1259. c: TRttiContext;
  1260. ARttiType: TRttiType;
  1261. AProperty: TRttiProperty;
  1262. AValue: TValue;
  1263. s: string;
  1264. ss: ShortString;
  1265. begin
  1266. c := TRttiContext.Create;
  1267. try
  1268. ATestClass := TTestValueClass.Create;
  1269. try
  1270. ARttiType := c.GetType(ATestClass.ClassInfo);
  1271. AProperty := ARttiType.GetProperty('aShortString');
  1272. s := 'ipse lorem or something like that';
  1273. TValue.Make(@s, TypeInfo(String), AValue);
  1274. AProperty.SetValue(ATestClass, AValue);
  1275. CheckEquals(ATestClass.AShortString, s);
  1276. s := 'Another string';
  1277. CheckEquals(ATestClass.AShortString, 'ipse lorem or something like that');
  1278. ss := 'Hello World';
  1279. TValue.Make(@ss, TypeInfo(ShortString), AValue);
  1280. AProperty.SetValue(ATestClass, AValue);
  1281. CheckEquals(ATestClass.AShortString, ss);
  1282. ss := 'Foobar';
  1283. CheckEquals(ATestClass.AShortString, 'Hello World');
  1284. AProperty.SetValue(ATestClass, 'Another string');
  1285. CheckEquals(ATestClass.AShortString, 'Another string');
  1286. finally
  1287. AtestClass.Free;
  1288. end;
  1289. finally
  1290. c.Free;
  1291. end;
  1292. end;
  1293. procedure TTestCase1.TestPropSetValueObject;
  1294. var
  1295. ATestClass : TTestValueClass;
  1296. c: TRttiContext;
  1297. ARttiType: TRttiType;
  1298. AProperty: TRttiProperty;
  1299. AValue: TValue;
  1300. O: TObject;
  1301. TypeInfo: PTypeInfo;
  1302. begin
  1303. c := TRttiContext.Create;
  1304. try
  1305. ATestClass := TTestValueClass.Create;
  1306. try
  1307. ARttiType := c.GetType(ATestClass.ClassInfo);
  1308. AProperty := ARttiType.GetProperty('AObject');
  1309. TypeInfo := GetPropInfo(ATestClass, 'AObject')^.PropType;
  1310. O := TPersistent.Create;
  1311. TValue.Make(@O, TypeInfo, AValue);
  1312. AProperty.SetValue(ATestClass, AValue);
  1313. CheckEquals(ATestClass.AObject.GetHashCode, O.GetHashCode);
  1314. O.Free;
  1315. O := TPersistent.Create;
  1316. AProperty.SetValue(ATestClass, O);
  1317. CheckEquals(ATestClass.AObject.GetHashCode, O.GetHashCode);
  1318. O.Free;
  1319. finally
  1320. AtestClass.Free;
  1321. end;
  1322. finally
  1323. c.Free;
  1324. end;
  1325. end;
  1326. procedure TTestCase1.TestPropSetValueInterface;
  1327. var
  1328. ATestClass : TTestValueClass;
  1329. c: TRttiContext;
  1330. ARttiType: TRttiType;
  1331. AProperty: TRttiProperty;
  1332. AValue: TValue;
  1333. TypeInfo: PTypeInfo;
  1334. i: IInterface;
  1335. begin
  1336. c := TRttiContext.Create;
  1337. try
  1338. ATestClass := TTestValueClass.Create;
  1339. try
  1340. ARttiType := c.GetType(ATestClass.ClassInfo);
  1341. AProperty := ARttiType.GetProperty('AUnknown');
  1342. TypeInfo := GetPropInfo(ATestClass, 'AUnknown')^.PropType;
  1343. i := TInterfacedObject.Create;
  1344. TValue.Make(@i, TypeInfo, AValue);
  1345. AProperty.SetValue(ATestClass, AValue);
  1346. Check(ATestClass.AUnknown = i);
  1347. i := TInterfacedObject.Create;
  1348. AProperty.SetValue(ATestClass, i);
  1349. Check(ATestClass.AUnknown = i);
  1350. finally
  1351. AtestClass.Free;
  1352. end;
  1353. finally
  1354. c.Free;
  1355. end;
  1356. end;
  1357. procedure TTestCase1.TestPropSetValueFloat;
  1358. var
  1359. ATestClass : TTestValueClass;
  1360. c: TRttiContext;
  1361. ARttiType: TRttiType;
  1362. AProperty: TRttiProperty;
  1363. AValue: TValue;
  1364. TypeInfo: PTypeInfo;
  1365. S: Single;
  1366. D: Double;
  1367. E: Extended;
  1368. Cur: Currency;
  1369. Cmp: Comp;
  1370. begin
  1371. c := TRttiContext.Create;
  1372. try
  1373. ATestClass := TTestValueClass.Create;
  1374. try
  1375. ARttiType := c.GetType(ATestClass.ClassInfo);
  1376. AProperty := ARttiType.GetProperty('ASingle');
  1377. TypeInfo := GetPropInfo(ATestClass, 'ASingle')^.PropType;
  1378. S := 1.1;
  1379. TValue.Make(@S, TypeInfo, AValue);
  1380. AProperty.SetValue(ATestClass, AValue);
  1381. CheckEquals(S, ATestClass.ASingle, 0.001);
  1382. S := 1.2;
  1383. AProperty.SetValue(ATestClass, S);
  1384. CheckEquals(S, ATestClass.ASingle, 0.001);
  1385. AProperty := ARttiType.GetProperty('ADouble');
  1386. TypeInfo := GetPropInfo(ATestClass, 'ADouble')^.PropType;
  1387. D := 2.1;
  1388. TValue.Make(@D, TypeInfo, AValue);
  1389. AProperty.SetValue(ATestClass, AValue);
  1390. CheckEquals(D, ATestClass.ADouble, 0.001);
  1391. D := 2.2;
  1392. AProperty.SetValue(ATestClass, D);
  1393. CheckEquals(D, ATestClass.ADouble, 0.001);
  1394. AProperty := ARttiType.GetProperty('AExtended');
  1395. TypeInfo := GetPropInfo(ATestClass, 'AExtended')^.PropType;
  1396. E := 3.1;
  1397. TValue.Make(@E, TypeInfo, AValue);
  1398. AProperty.SetValue(ATestClass, AValue);
  1399. CheckEquals(E, ATestClass.AExtended, 0.001);
  1400. E := 3.2;
  1401. AProperty.SetValue(ATestClass, E);
  1402. CheckEquals(E, ATestClass.AExtended, 0.001);
  1403. AProperty := ARttiType.GetProperty('ACurrency');
  1404. TypeInfo := GetPropInfo(ATestClass, 'ACurrency')^.PropType;
  1405. Cur := 40;
  1406. TValue.Make(@Cur, TypeInfo, AValue);
  1407. AProperty.SetValue(ATestClass, AValue);
  1408. CheckEquals(Cur, ATestClass.ACurrency, 0.001);
  1409. Cur := 41;
  1410. AProperty.SetValue(ATestClass, Cur);
  1411. CheckEquals(Cur, ATestClass.ACurrency, 0.001);
  1412. AProperty := ARttiType.GetProperty('AComp');
  1413. TypeInfo := GetPropInfo(ATestClass, 'AComp')^.PropType;
  1414. Cmp := 50;
  1415. TValue.Make(@Cmp, TypeInfo, AValue);
  1416. AProperty.SetValue(ATestClass, AValue);
  1417. CheckEquals(Cmp, ATestClass.AComp, 0.001);
  1418. Cmp := 51;
  1419. AProperty.SetValue(ATestClass, Cmp);
  1420. CheckEquals(Cmp, ATestClass.AComp, 0.001);
  1421. finally
  1422. AtestClass.Free;
  1423. end;
  1424. finally
  1425. c.Free;
  1426. end;
  1427. end;
  1428. procedure TTestCase1.TestPropSetValueDynArray;
  1429. var
  1430. ATestClass : TTestValueClass;
  1431. c: TRttiContext;
  1432. ARttiType: TRttiType;
  1433. AProperty: TRttiProperty;
  1434. AValue: TValue;
  1435. A: TTestDynArray;
  1436. TypeInfo: PTypeInfo;
  1437. i: Integer;
  1438. begin
  1439. c := TRttiContext.Create;
  1440. try
  1441. ATestClass := TTestValueClass.Create;
  1442. try
  1443. ARttiType := c.GetType(ATestClass.ClassInfo);
  1444. AProperty := ARttiType.GetProperty('AArray');
  1445. TypeInfo := GetPropInfo(ATestClass, 'AArray')^.PropType;
  1446. A := [1, 2, 3, 4, 5];
  1447. TValue.Make(@A, TypeInfo, AValue);
  1448. AProperty.SetValue(ATestClass, AValue);
  1449. for i := 0 to High(A) do
  1450. CheckEquals(A[i], ATestClass.AArray[i]);
  1451. finally
  1452. AtestClass.Free;
  1453. end;
  1454. finally
  1455. c.Free;
  1456. end;
  1457. end;
  1458. procedure TTestCase1.TestPropSetValueEnumeration;
  1459. var
  1460. ATestClass : TTestValueClass;
  1461. c: TRttiContext;
  1462. ARttiType: TRttiType;
  1463. AProperty: TRttiProperty;
  1464. AValue: TValue;
  1465. E: TTestEnumeration;
  1466. begin
  1467. c := TRttiContext.Create;
  1468. try
  1469. ATestClass := TTestValueClass.Create;
  1470. try
  1471. ARttiType := c.GetType(ATestClass.ClassInfo);
  1472. AProperty := ARttiType.GetProperty('AEnumeration');
  1473. E := en2;
  1474. TValue.Make(@E, TypeInfo(TTestEnumeration), AValue);
  1475. AProperty.SetValue(ATestClass, AValue);
  1476. CheckEquals(Ord(E), Ord(ATestClass.AEnumeration));
  1477. finally
  1478. AtestClass.Free;
  1479. end;
  1480. finally
  1481. c.Free;
  1482. end;
  1483. end;
  1484. procedure TTestCase1.TestPropSetValueChars;
  1485. var
  1486. ATestClass : TTestValueClass;
  1487. c: TRttiContext;
  1488. ARttiType: TRttiType;
  1489. AProperty: TRttiProperty;
  1490. AValueC, AValueW: TValue;
  1491. begin
  1492. c := TRttiContext.Create;
  1493. try
  1494. ATestClass := TTestValueClass.Create;
  1495. ATestClass.AChar := 'C';
  1496. ATestClass.AWideChar := 'W';
  1497. try
  1498. ARttiType := c.GetType(ATestClass.ClassInfo);
  1499. Check(assigned(ARttiType));
  1500. AProperty := ARttiType.GetProperty('AChar');
  1501. AValueC := AProperty.GetValue(ATestClass);
  1502. CheckEquals('C', AValueC.AsAnsiChar);
  1503. AProperty := ARttiType.GetProperty('AWideChar');
  1504. AValueW := AProperty.GetValue(ATestClass);
  1505. CheckEquals('W', AValueW.AsWideChar);
  1506. finally
  1507. AtestClass.Free;
  1508. end;
  1509. CheckEquals('C', AValueC.AsAnsiChar);
  1510. CheckEquals('W', AValueW.AsWideChar);
  1511. finally
  1512. c.Free;
  1513. end;
  1514. end;
  1515. procedure TTestCase1.TestPropGetValueProcInteger;
  1516. var
  1517. ATestClass : TTestValueClass;
  1518. c: TRttiContext;
  1519. ARttiType: TRttiType;
  1520. AProperty: TRttiProperty;
  1521. AValue: TValue;
  1522. begin
  1523. c := TRttiContext.Create;
  1524. try
  1525. ATestClass := TTestValueClass.Create;
  1526. ATestClass.AInteger := 472349;
  1527. try
  1528. ARttiType := c.GetType(ATestClass.ClassInfo);
  1529. Check(assigned(ARttiType));
  1530. AProperty := ARttiType.GetProperty('agetinteger');
  1531. AValue := AProperty.GetValue(ATestClass);
  1532. CheckEquals(472349,AValue.AsInteger);
  1533. finally
  1534. AtestClass.Free;
  1535. end;
  1536. CheckEquals(472349,AValue.AsInteger);
  1537. finally
  1538. c.Free;
  1539. end;
  1540. end;
  1541. procedure TTestCase1.TestPropGetValueProcString;
  1542. var
  1543. ATestClass : TTestValueClass;
  1544. c: TRttiContext;
  1545. ARttiType: TRttiType;
  1546. AProperty: TRttiProperty;
  1547. AValue: TValue;
  1548. begin
  1549. c := TRttiContext.Create;
  1550. try
  1551. ATestClass := TTestValueClass.Create;
  1552. ATestClass.AString := 'Hello World';
  1553. try
  1554. ARttiType := c.GetType(ATestClass.ClassInfo);
  1555. Check(assigned(ARttiType));
  1556. AProperty := ARttiType.GetProperty('agetstring');
  1557. AValue := AProperty.GetValue(ATestClass);
  1558. CheckEquals('Hello World',AValue.AsString);
  1559. finally
  1560. AtestClass.Free;
  1561. end;
  1562. CheckEquals('Hello World',AValue.AsString);
  1563. finally
  1564. c.Free;
  1565. end;
  1566. end;
  1567. procedure TTestCase1.TestTRttiTypeProperties;
  1568. var
  1569. c: TRttiContext;
  1570. ARttiType: TRttiType;
  1571. begin
  1572. c := TRttiContext.Create;
  1573. try
  1574. ARttiType := c.GetType(TTestValueClass);
  1575. Check(assigned(ARttiType));
  1576. CheckEquals(ARttiType.Name,'TTestValueClass');
  1577. Check(ARttiType.TypeKind=tkClass);
  1578. // CheckEquals(ARttiType.IsPublicType,false);
  1579. CheckEquals(ARttiType.TypeSize,SizeOf(TObject));
  1580. CheckEquals(ARttiType.IsManaged,false);
  1581. CheckEquals(ARttiType.BaseType.classname,'TRttiInstanceType');
  1582. CheckEquals(ARttiType.IsInstance,True);
  1583. CheckEquals(ARttiType.AsInstance.DeclaringUnitName,'tests.rtti');
  1584. Check(ARttiType.BaseType.Name='TObject');
  1585. Check(ARttiType.AsInstance.BaseType.Name='TObject');
  1586. CheckEquals(ARttiType.IsOrdinal,False);
  1587. CheckEquals(ARttiType.IsRecord,False);
  1588. CheckEquals(ARttiType.IsSet,False);
  1589. finally
  1590. c.Free;
  1591. end;
  1592. end;
  1593. procedure TTestCase1.GetTypeInteger;
  1594. var
  1595. LContext: TRttiContext;
  1596. LType: TRttiType;
  1597. begin
  1598. LContext := TRttiContext.Create;
  1599. LType := LContext.GetType(TypeInfo(integer));
  1600. {$ifdef fpc}
  1601. CheckEquals(LType.Name, 'LongInt');
  1602. {$else}
  1603. CheckEquals(LType.Name, 'Integer');
  1604. {$endif}
  1605. LContext.Free;
  1606. end;
  1607. procedure TTestCase1.GetTypePointer;
  1608. var
  1609. context: TRttiContext;
  1610. t: TRttiType;
  1611. p: TRttiPointerType absolute t;
  1612. begin
  1613. context := TRttiContext.Create;
  1614. try
  1615. t := context.GetType(TypeInfo(Pointer));
  1616. Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
  1617. Assert(not Assigned(p.ReferredType), 'ReferredType of Pointer is not Nil');
  1618. t := context.GetType(TypeInfo(PLongInt));
  1619. Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
  1620. Assert(Assigned(p.ReferredType), 'ReferredType of PLongInt is Nil');
  1621. Assert(p.ReferredType = context.GetType(TypeInfo(LongInt)), 'ReferredType of PLongInt is not a LongInt');
  1622. t := context.GetType(TypeInfo(PWideChar));
  1623. Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
  1624. Assert(Assigned(p.ReferredType), 'ReferredType of PWideChar is Nil');
  1625. Assert(p.ReferredType = context.GetType(TypeInfo(WideChar)), 'ReferredType of PWideChar is not a WideChar');
  1626. finally
  1627. context.Free;
  1628. end;
  1629. end;
  1630. procedure TTestCase1.GetClassProperties;
  1631. var
  1632. LContext: TRttiContext;
  1633. LType: TRttiType;
  1634. PropList, PropList2: {$ifdef fpc}specialize{$endif} TArray<TRttiProperty>;
  1635. i: LongInt;
  1636. begin
  1637. LContext := TRttiContext.Create;
  1638. LType := LContext.GetType(TypeInfo(TGetClassProperties));
  1639. PropList := LType.GetProperties;
  1640. CheckEquals(4, length(PropList));
  1641. CheckEquals('PubPropRO', PropList[0].Name);
  1642. CheckEquals('PubPropRW', PropList[1].Name);
  1643. CheckEquals('PubPropSetRO', PropList[2].Name);
  1644. CheckEquals('PubPropSetRW', PropList[3].Name);
  1645. LType := LContext.GetType(TypeInfo(TGetClassPropertiesSub));
  1646. PropList2 := LType.GetProperties;
  1647. CheckEquals(Length(PropList), Length(PropList2));
  1648. for i := 0 to High(PropList) do
  1649. Check(PropList[i] = PropList2[i], 'Property instances are not equal');
  1650. LContext.Free;
  1651. end;
  1652. procedure TTestCase1.GetClassPropertiesValue;
  1653. var
  1654. AGetClassProperties: TGetClassProperties;
  1655. LContext: TRttiContext;
  1656. LType: TRttiType;
  1657. AValue: TValue;
  1658. begin
  1659. LContext := TRttiContext.Create;
  1660. LType := LContext.GetType(TGetClassProperties);
  1661. AGetClassProperties := TGetClassProperties.Create;
  1662. try
  1663. AGetClassProperties.PubPropRW:=12345;
  1664. AValue := LType.GetProperty('PubPropRW').GetValue(AGetClassProperties);
  1665. CheckEquals(12345, AValue.AsInteger);
  1666. finally
  1667. AGetClassProperties.Free;
  1668. end;
  1669. LContext.Free;
  1670. end;
  1671. procedure TTestCase1.TestReferenceRawData;
  1672. var
  1673. value: TValue;
  1674. str: String;
  1675. intf: IInterface;
  1676. i: LongInt;
  1677. test: TTestRecord;
  1678. arrdyn: TArrayOfLongintDyn;
  1679. arrstat: TArrayOfLongintStatic;
  1680. begin
  1681. str := 'Hello World';
  1682. UniqueString(str);
  1683. TValue.Make(@str, TypeInfo(String), value);
  1684. Check(PPointer(value.GetReferenceToRawData)^ = Pointer(str), 'Reference to string data differs');
  1685. intf := TInterfacedObject.Create;
  1686. TValue.Make(@intf, TypeInfo(IInterface), value);
  1687. Check(PPointer(value.GetReferenceToRawData)^ = Pointer(intf), 'Reference to interface data differs');
  1688. i := 42;
  1689. TValue.Make(@i, TypeInfo(LongInt), value);
  1690. Check(value.GetReferenceToRawData <> @i, 'Reference to longint is equal');
  1691. Check(PLongInt(value.GetReferenceToRawData)^ = PLongInt(@i)^, 'Reference to longint data differs');
  1692. test.value1 := 42;
  1693. test.value2 := 'Hello World';
  1694. TValue.Make(@test, TypeInfo(TTestRecord), value);
  1695. Check(value.GetReferenceToRawData <> @test, 'Reference to record is equal');
  1696. Check(PTestRecord(value.GetReferenceToRawData)^.value1 = PTestRecord(@test)^.value1, 'Reference to record data value1 differs');
  1697. Check(PTestRecord(value.GetReferenceToRawData)^.value2 = PTestRecord(@test)^.value2, 'Reference to record data value2 differs');
  1698. SetLength(arrdyn, 3);
  1699. arrdyn[0] := 42;
  1700. arrdyn[1] := 23;
  1701. arrdyn[2] := 49;
  1702. TValue.Make(@arrdyn, TypeInfo(TArrayOfLongintDyn), value);
  1703. Check(PPointer(value.GetReferenceToRawData)^ = Pointer(arrdyn), 'Reference to dynamic array data differs');
  1704. arrstat[0] := 42;
  1705. arrstat[1] := 23;
  1706. arrstat[2] := 49;
  1707. arrstat[3] := 59;
  1708. TValue.Make(@arrstat, TypeInfo(TArrayOfLongintStatic), value);
  1709. Check(value.GetReferenceToRawData <> @arrstat, 'Reference to static array is equal');
  1710. Check(PLongInt(value.GetReferenceToRawData)^ = PLongInt(@arrstat)^, 'Reference to static array data differs');
  1711. end;
  1712. procedure TTestCase1.TestReferenceRawDataEmpty;
  1713. var
  1714. value: TValue;
  1715. begin
  1716. TValue.Make(Nil, TypeInfo(String), value);
  1717. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty String is not assigned');
  1718. Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty String data is assigned');
  1719. TValue.Make(Nil, TypeInfo(IInterface), value);
  1720. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty interface is not assigned');
  1721. Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty interface data is assigned');
  1722. TValue.Make(Nil, TypeInfo(LongInt), value);
  1723. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty LongInt is not assigned');
  1724. Check(PLongInt(value.GetReferenceToRawData)^ = 0, 'Empty longint data is not 0');
  1725. TValue.Make(Nil, TypeInfo(TTestRecord), value);
  1726. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty record is not assigned');
  1727. Check(PTestRecord(value.GetReferenceToRawData)^.value1 = 0, 'Empty record data value1 is not 0');
  1728. Check(PTestRecord(value.GetReferenceToRawData)^.value2 = '', 'Empty record data value2 is not empty');
  1729. TValue.Make(Nil, TypeInfo(TArrayOfLongintDyn), value);
  1730. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty dynamic array is not assigned');
  1731. Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty dynamic array data is assigned');
  1732. TValue.Make(Nil, TypeInfo(TArrayOfLongintStatic), value);
  1733. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty static array is not assigned');
  1734. Check(PLongInt(value.GetReferenceToRawData)^ = 0, 'Empty static array data is not 0');
  1735. end;
  1736. procedure TTestCase1.TestDataSize;
  1737. var
  1738. u8: UInt8;
  1739. u16: UInt16;
  1740. u32: UInt32;
  1741. u64: UInt64;
  1742. s8: Int8;
  1743. s16: Int16;
  1744. s32: Int32;
  1745. s64: Int64;
  1746. f32: Single;
  1747. f64: Double;
  1748. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1749. f80: Extended;
  1750. {$endif}
  1751. fco: Comp;
  1752. fcu: Currency;
  1753. ss: ShortString;
  1754. sa: AnsiString;
  1755. su: UnicodeString;
  1756. sw: WideString;
  1757. o: TObject;
  1758. c: TClass;
  1759. i: IInterface;
  1760. ad: TArrayOfLongintDyn;
  1761. _as: TArrayOfLongintStatic;
  1762. b8: Boolean;
  1763. {$ifdef fpc}
  1764. b16: Boolean16;
  1765. b32: Boolean32;
  1766. b64: Boolean64;
  1767. {$endif}
  1768. bl8: ByteBool;
  1769. bl16: WordBool;
  1770. bl32: LongBool;
  1771. {$ifdef fpc}
  1772. bl64: QWordBool;
  1773. {$endif}
  1774. e: TTestEnum;
  1775. s: TTestSet;
  1776. t: TTestRecord;
  1777. p: Pointer;
  1778. proc: TTestProc;
  1779. method: TTestMethod;
  1780. value: TValue;
  1781. begin
  1782. u8:=245;
  1783. TValue.Make(@u8, TypeInfo(UInt8), value);
  1784. CheckEquals(1, value.DataSize, 'Size of UInt8 differs');
  1785. u16:=789;
  1786. TValue.Make(@u16, TypeInfo(UInt16), value);
  1787. CheckEquals(2, value.DataSize, 'Size of UInt16 differs');
  1788. u32:=568789;
  1789. TValue.Make(@u32, TypeInfo(UInt32), value);
  1790. CheckEquals(4, value.DataSize, 'Size of UInt32 differs');
  1791. u64:=$abdcefadbcef;
  1792. TValue.Make(@u64, TypeInfo(UInt64), value);
  1793. CheckEquals(8, value.DataSize, 'Size of UInt64 differs');
  1794. s8:=-32;
  1795. TValue.Make(@s8, TypeInfo(Int8), value);
  1796. CheckEquals(1, value.DataSize, 'Size of Int8 differs');
  1797. s16:=-5345;
  1798. TValue.Make(@s16, TypeInfo(Int16), value);
  1799. CheckEquals(2, value.DataSize, 'Size of Int16 differs');
  1800. s32:=-234567;
  1801. TValue.Make(@s32, TypeInfo(Int32), value);
  1802. CheckEquals(4, value.DataSize, 'Size of Int32 differs');
  1803. s64:=23456789012;
  1804. TValue.Make(@s64, TypeInfo(Int64), value);
  1805. CheckEquals(8, value.DataSize, 'Size of Int64 differs');
  1806. b8:=false;
  1807. TValue.Make(@b8, TypeInfo(Boolean), value);
  1808. CheckEquals(1, value.DataSize, 'Size of Boolean differs');
  1809. {$ifdef fpc}
  1810. b16:=true;
  1811. TValue.Make(@b16, TypeInfo(Boolean16), value);
  1812. CheckEquals(2, value.DataSize, 'Size of Boolean16 differs');
  1813. b32:=false;
  1814. TValue.Make(@b32, TypeInfo(Boolean32), value);
  1815. CheckEquals(4, value.DataSize, 'Size of Boolean32 differs');
  1816. b64:=true;
  1817. TValue.Make(@b64, TypeInfo(Boolean64), value);
  1818. CheckEquals(8, value.DataSize, 'Size of Boolean64 differs');
  1819. {$endif}
  1820. bl8:=true;
  1821. TValue.Make(@bl8, TypeInfo(ByteBool), value);
  1822. CheckEquals(1, value.DataSize, 'Size of ByteBool differs');
  1823. bl16:=false;
  1824. TValue.Make(@bl16, TypeInfo(WordBool), value);
  1825. CheckEquals(2, value.DataSize, 'Size of WordBool differs');
  1826. bl32:=false;
  1827. TValue.Make(@bl32, TypeInfo(LongBool), value);
  1828. CheckEquals(4, value.DataSize, 'Size of LongBool differs');
  1829. {$ifdef fpc}
  1830. bl64:=true;
  1831. TValue.Make(@bl64, TypeInfo(QWordBool), value);
  1832. CheckEquals(8, value.DataSize, 'Size of QWordBool differs');
  1833. {$endif}
  1834. f32:=4.567;
  1835. TValue.Make(@f32, TypeInfo(Single), value);
  1836. CheckEquals(4, value.DataSize, 'Size of Single differs');
  1837. f64:=-3456.678;
  1838. TValue.Make(@f64, TypeInfo(Double), value);
  1839. CheckEquals(8, value.DataSize, 'Size of Double differs');
  1840. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1841. f80:=-2345.678;
  1842. TValue.Make(@f80, TypeInfo(Extended), value);
  1843. CheckEquals(10, value.DataSize, 'Size of Extended differs');
  1844. {$endif}
  1845. fcu:=56.78;
  1846. TValue.Make(@fcu, TypeInfo(Currency), value);
  1847. CheckEquals(SizeOf(Currency), value.DataSize, 'Size of Currency differs');
  1848. fco:=456;
  1849. TValue.Make(@fco, TypeInfo(Comp), value);
  1850. CheckEquals(SizeOf(Comp), value.DataSize, 'Size of Comp differs');
  1851. ss := '';
  1852. TValue.Make(@ss, TypeInfo(ShortString), value);
  1853. CheckEquals(254, value.DataSize, 'Size ofShortString differs');
  1854. sa:= '';
  1855. TValue.Make(@sa, TypeInfo(AnsiString), value);
  1856. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of AnsiString differs');
  1857. sw := '';
  1858. TValue.Make(@sw, TypeInfo(WideString), value);
  1859. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of WideString differs');
  1860. su:='';
  1861. TValue.Make(@su, TypeInfo(UnicodeString), value);
  1862. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of UnicodeString differs');
  1863. o := TTestValueClass.Create;
  1864. TValue.Make(@o, TypeInfo(TObject), value);
  1865. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TObject differs');
  1866. o.Free;
  1867. c := TObject;
  1868. TValue.Make(@c, TypeInfo(TClass), value);
  1869. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TClass differs');
  1870. i := Nil;
  1871. TValue.Make(@i, TypeInfo(IInterface), value);
  1872. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of IInterface differs');
  1873. TValue.Make(@t, TypeInfo(TTestRecord), value);
  1874. CheckEquals(SizeOf(TTestRecord), value.DataSize, 'Size of TTestRecord differs');
  1875. proc := Nil;
  1876. TValue.Make(@proc, TypeInfo(TTestProc), value);
  1877. CheckEquals(SizeOf(TTestProc), value.DataSize, 'Size of TTestProc differs');
  1878. method := Nil;
  1879. TValue.Make(@method, TypeInfo(TTestMethod), value);
  1880. CheckEquals(SizeOf(TTestMethod), value.DataSize, 'Size of TTestMethod differs');
  1881. TValue.Make(@_as, TypeInfo(TArrayOfLongintStatic), value);
  1882. CheckEquals(SizeOf(TArrayOfLongintStatic), value.DataSize, 'Size of TArrayOfLongintStatic differs');
  1883. TValue.Make(@ad, TypeInfo(TArrayOfLongintDyn), value);
  1884. CheckEquals(SizeOf(TArrayOfLongintDyn), value.DataSize, 'Size of TArrayOfLongintDyn differs');
  1885. e:=low(TTestEnum);
  1886. TValue.Make(@e, TypeInfo(TTestEnum), value);
  1887. CheckEquals(SizeOf(TTestEnum), value.DataSize, 'Size of TTestEnum differs');
  1888. s:=[low(TTestEnum),high(TTestEnum)];
  1889. TValue.Make(@s, TypeInfo(TTestSet), value);
  1890. CheckEquals(SizeOf(TTestSet), value.DataSize, 'Size of TTestSet differs');
  1891. p := Nil;
  1892. TValue.Make(@p, TypeInfo(Pointer), value);
  1893. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
  1894. end;
  1895. procedure TTestCase1.TestDataSizeEmpty;
  1896. var
  1897. value: TValue;
  1898. begin
  1899. TValue.Make(Nil, TypeInfo(UInt8), value);
  1900. CheckEquals(1, value.DataSize, 'Size of UInt8 differs');
  1901. TValue.Make(Nil, TypeInfo(UInt16), value);
  1902. CheckEquals(2, value.DataSize, 'Size of UInt16 differs');
  1903. TValue.Make(Nil, TypeInfo(UInt32), value);
  1904. CheckEquals(4, value.DataSize, 'Size of UInt32 differs');
  1905. TValue.Make(Nil, TypeInfo(UInt64), value);
  1906. CheckEquals(8, value.DataSize, 'Size of UInt64 differs');
  1907. TValue.Make(Nil, TypeInfo(Int8), value);
  1908. CheckEquals(1, value.DataSize, 'Size of Int8 differs');
  1909. TValue.Make(Nil, TypeInfo(Int16), value);
  1910. CheckEquals(2, value.DataSize, 'Size of Int16 differs');
  1911. TValue.Make(Nil, TypeInfo(Int32), value);
  1912. CheckEquals(4, value.DataSize, 'Size of Int32 differs');
  1913. TValue.Make(Nil, TypeInfo(Int64), value);
  1914. CheckEquals(8, value.DataSize, 'Size of Int64 differs');
  1915. TValue.Make(Nil, TypeInfo(Boolean), value);
  1916. CheckEquals(1, value.DataSize, 'Size of Boolean differs');
  1917. {$ifdef fpc}
  1918. TValue.Make(Nil, TypeInfo(Boolean16), value);
  1919. CheckEquals(2, value.DataSize, 'Size of Boolean16 differs');
  1920. TValue.Make(Nil, TypeInfo(Boolean32), value);
  1921. CheckEquals(4, value.DataSize, 'Size of Boolean32 differs');
  1922. TValue.Make(Nil, TypeInfo(Boolean64), value);
  1923. CheckEquals(8, value.DataSize, 'Size of Boolean64 differs');
  1924. {$endif}
  1925. TValue.Make(Nil, TypeInfo(ByteBool), value);
  1926. CheckEquals(1, value.DataSize, 'Size of ByteBool differs');
  1927. TValue.Make(Nil, TypeInfo(WordBool), value);
  1928. CheckEquals(2, value.DataSize, 'Size of WordBool differs');
  1929. TValue.Make(Nil, TypeInfo(LongBool), value);
  1930. CheckEquals(4, value.DataSize, 'Size of LongBool differs');
  1931. {$ifdef fpc}
  1932. TValue.Make(Nil, TypeInfo(QWordBool), value);
  1933. CheckEquals(8, value.DataSize, 'Size of QWordBool differs');
  1934. {$endif}
  1935. TValue.Make(Nil, TypeInfo(Single), value);
  1936. CheckEquals(4, value.DataSize, 'Size of Single differs');
  1937. TValue.Make(Nil, TypeInfo(Double), value);
  1938. CheckEquals(8, value.DataSize, 'Size of Double differs');
  1939. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1940. TValue.Make(Nil, TypeInfo(Extended), value);
  1941. CheckEquals(10, value.DataSize, 'Size of Extended differs');
  1942. {$endif}
  1943. TValue.Make(Nil, TypeInfo(Currency), value);
  1944. CheckEquals(SizeOf(Currency), value.DataSize, 'Size of Currency differs');
  1945. TValue.Make(Nil, TypeInfo(Comp), value);
  1946. CheckEquals(SizeOf(Comp), value.DataSize, 'Size of Comp differs');
  1947. TValue.Make(Nil, TypeInfo(ShortString), value);
  1948. CheckEquals(254, value.DataSize, 'Size of ShortString differs');
  1949. TValue.Make(Nil, TypeInfo(AnsiString), value);
  1950. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
  1951. TValue.Make(Nil, TypeInfo(WideString), value);
  1952. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of WideString differs');
  1953. TValue.Make(Nil, TypeInfo(UnicodeString), value);
  1954. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of UnicodeString differs');
  1955. TValue.Make(Nil, TypeInfo(TObject), value);
  1956. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TObject differs');
  1957. TValue.Make(Nil, TypeInfo(TClass), value);
  1958. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TClass differs');
  1959. TValue.Make(Nil, TypeInfo(IInterface), value);
  1960. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of IInterface differs');
  1961. TValue.Make(Nil, TypeInfo(TTestRecord), value);
  1962. CheckEquals(SizeOf(TTestRecord), value.DataSize, 'Size of TTestRecord differs');
  1963. TValue.Make(Nil, TypeInfo(TTestProc), value);
  1964. CheckEquals(SizeOf(TTestProc), value.DataSize, 'Size of TTestProc differs');
  1965. TValue.Make(Nil, TypeInfo(TTestMethod), value);
  1966. CheckEquals(SizeOf(TTestMethod), value.DataSize, 'Size of TTestMethod differs');
  1967. TValue.Make(Nil, TypeInfo(TArrayOfLongintStatic), value);
  1968. CheckEquals(SizeOf(TArrayOfLongintStatic), value.DataSize, 'Size of TArrayOfLongintStatic differs');
  1969. TValue.Make(Nil, TypeInfo(TArrayOfLongintDyn), value);
  1970. CheckEquals(SizeOf(TArrayOfLongintDyn), value.DataSize, 'Size of TArrayOfLongintDyn differs');
  1971. TValue.Make(Nil, TypeInfo(TTestEnum), value);
  1972. CheckEquals(SizeOf(TTestEnum), value.DataSize, 'Size of TTestEnum differs');
  1973. TValue.Make(Nil, TypeInfo(TTestSet), value);
  1974. CheckEquals(SizeOf(TTestSet), value.DataSize, 'Size of TTestSet differs');
  1975. TValue.Make(Nil, TypeInfo(Pointer), value);
  1976. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
  1977. end;
  1978. procedure TTestCase1.TestIsManaged;
  1979. begin
  1980. CheckEquals(true, IsManaged(TypeInfo(ansistring)), 'IsManaged for tkAString');
  1981. CheckEquals(true, IsManaged(TypeInfo(widestring)), 'IsManaged for tkWString');
  1982. CheckEquals(true, IsManaged(TypeInfo(Variant)), 'IsManaged for tkVariant');
  1983. CheckEquals(true, IsManaged(TypeInfo(TArrayOfManagedRec)),
  1984. 'IsManaged for tkArray (with managed ElType)');
  1985. CheckEquals(true, IsManaged(TypeInfo(TArrayOfString)),
  1986. 'IsManaged for tkArray (with managed ElType)');
  1987. CheckEquals(true, IsManaged(TypeInfo(TManagedRec)), 'IsManaged for tkRecord');
  1988. {$ifdef fpc}
  1989. CheckEquals(true, IsManaged(TypeInfo(TManagedRecOp)), 'IsManaged for tkRecord');
  1990. {$endif}
  1991. CheckEquals(true, IsManaged(TypeInfo(IInterface)), 'IsManaged for tkInterface');
  1992. CheckEquals(true, IsManaged(TypeInfo(TManagedObj)), 'IsManaged for tkObject');
  1993. {$ifdef fpc}
  1994. CheckEquals(true, IsManaged(TypeInfo(specialize TArray<byte>)), 'IsManaged for tkDynArray');
  1995. {$else}
  1996. CheckEquals(true, IsManaged(TypeInfo(TArray<byte>)), 'IsManaged for tkDynArray');
  1997. {$endif}
  1998. CheckEquals(true, IsManaged(TypeInfo(unicodestring)), 'IsManaged for tkUString');
  1999. CheckEquals(false, IsManaged(TypeInfo(shortstring)), 'IsManaged for tkSString');
  2000. CheckEquals(false, IsManaged(TypeInfo(Byte)), 'IsManaged for tkInteger');
  2001. CheckEquals(false, IsManaged(TypeInfo(Char)), 'IsManaged for tkChar');
  2002. CheckEquals(false, IsManaged(TypeInfo(TTestEnum)), 'IsManaged for tkEnumeration');
  2003. CheckEquals(false, IsManaged(TypeInfo(Single)), 'IsManaged for tkFloat');
  2004. CheckEquals(false, IsManaged(TypeInfo(TTestSet)), 'IsManaged for tkSet');
  2005. {$ifdef fpc}
  2006. CheckEquals(false, IsManaged(TypeInfo(TTestMethod)), 'IsManaged for tkMethod');
  2007. {$else}
  2008. { Delphi bug (or sabotage). For some reason Delphi considers method pointers to be managed (only in newer versions, probably since XE7) :/ }
  2009. CheckEquals({$if RTLVersion>=28}true{$else}false{$endif}, IsManaged(TypeInfo(TTestMethod)), 'IsManaged for tkMethod');
  2010. {$endif}
  2011. CheckEquals(false, IsManaged(TypeInfo(TArrayOfByte)),
  2012. 'IsManaged for tkArray (with non managed ElType)');
  2013. CheckEquals(false, IsManaged(TypeInfo(TArrayOfNonManagedRec)),
  2014. 'IsManaged for tkArray (with non managed ElType)');
  2015. CheckEquals(false, IsManaged(TypeInfo(TNonManagedRec)), 'IsManaged for tkRecord');
  2016. CheckEquals(false, IsManaged(TypeInfo(TObject)), 'IsManaged for tkClass');
  2017. CheckEquals(false, IsManaged(TypeInfo(TNonManagedObj)), 'IsManaged for tkObject');
  2018. CheckEquals(false, IsManaged(TypeInfo(WideChar)), 'IsManaged for tkWChar');
  2019. CheckEquals(false, IsManaged(TypeInfo(Boolean)), 'IsManaged for tkBool');
  2020. CheckEquals(false, IsManaged(TypeInfo(Int64)), 'IsManaged for tkInt64');
  2021. CheckEquals(false, IsManaged(TypeInfo(UInt64)), 'IsManaged for tkQWord');
  2022. {$ifdef fpc}
  2023. CheckEquals(false, IsManaged(TypeInfo(ICORBATest)), 'IsManaged for tkInterfaceRaw');
  2024. {$endif}
  2025. CheckEquals(false, IsManaged(TypeInfo(TTestProc)), 'IsManaged for tkProcVar');
  2026. CheckEquals(false, IsManaged(TypeInfo(TTestHelper)), 'IsManaged for tkHelper');
  2027. {$ifdef fpc}
  2028. CheckEquals(false, IsManaged(TypeInfo(file)), 'IsManaged for tkFile');
  2029. {$endif}
  2030. CheckEquals(false, IsManaged(TypeInfo(TClass)), 'IsManaged for tkClassRef');
  2031. CheckEquals(false, IsManaged(TypeInfo(Pointer)), 'IsManaged for tkPointer');
  2032. CheckEquals(false, IsManaged(nil), 'IsManaged for nil');
  2033. end;
  2034. {$ifdef fpc}
  2035. procedure TTestCase1.TestOpenArrayToDyn;
  2036. procedure OpenArrayProc(aArr: array of LongInt);
  2037. var
  2038. value: TValue;
  2039. begin
  2040. {$ifndef InLazIDE}
  2041. value := specialize OpenArrayToDynArrayValue<LongInt>(aArr);
  2042. {$endif}
  2043. CheckEquals(value.IsArray, True);
  2044. CheckEquals(value.IsOpenArray, False);
  2045. CheckEquals(value.IsObject, False);
  2046. CheckEquals(value.IsOrdinal, False);
  2047. CheckEquals(value.IsClass, False);
  2048. CheckEquals(value.GetArrayLength, 2);
  2049. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  2050. CheckEquals(value.GetArrayElement(1).AsInteger, 84);
  2051. value.SetArrayElement(0, 21);
  2052. { since this is a copy the original array is not modified! }
  2053. CheckEquals(aArr[0], 42);
  2054. end;
  2055. begin
  2056. OpenArrayProc([42, 84]);
  2057. end;
  2058. {$endif}
  2059. procedure TTestCase1.TestInterface;
  2060. var
  2061. context: TRttiContext;
  2062. t: TRttiType;
  2063. ti1, ti2: TRttiInterfaceType;
  2064. methods: {$ifdef fpc}specialize{$endif} TArray<TRttiMethod>;
  2065. params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
  2066. method: TRttiMethod;
  2067. param: TRttiParameter;
  2068. flag: TParamFlag;
  2069. begin
  2070. context := TRttiContext.Create;
  2071. try
  2072. t := context.GetType(TypeInfo(IInterface));
  2073. Check(t is TRttiInterfaceType, 'Type is not an interface type');
  2074. Check(not Assigned(t.BaseType), 'Base type is assigned');
  2075. ti1 := TRttiInterfaceType(t);
  2076. Check(not Assigned(ti1.BaseType), 'Base type is assigned');
  2077. methods := t.GetMethods;
  2078. CheckEquals(0, Length(methods), 'Overall method count does not match');
  2079. methods := t.GetDeclaredMethods;
  2080. CheckEquals(0, Length(methods), 'Declared method conut does not match');
  2081. t := context.GetType(TypeInfo(ITestInterface));
  2082. Check(t is TRttiInterfaceType, 'Type is not an interface type');
  2083. Check(Assigned(t.BaseType), 'Base type is not assigned');
  2084. Check(t.BaseType = TRttiType(ti1), 'Base type does not match');
  2085. ti2 := TRttiInterfaceType(t);
  2086. Check(Assigned(ti2.BaseType), 'Base type is not assigned');
  2087. Check(ti2.BaseType = ti1, 'Base type does not match');
  2088. methods := t.GetMethods;
  2089. CheckEquals(4, Length(methods), 'Overall method count does not match');
  2090. methods := t.GetDeclaredMethods;
  2091. CheckEquals(4, Length(methods), 'Declared method count does not match');
  2092. method := methods[0];
  2093. CheckEquals(method.Name, 'Test', 'Method name of Test does not match');
  2094. Check(method.CallingConvention = DefaultCC, 'Calling convention of Test does not match');
  2095. Check(method.MethodKind = mkProcedure, 'Method kind of Test does not match');
  2096. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test does not match');
  2097. Check(not Assigned(method.CodeAddress), 'Code address of Test is not Nil');
  2098. CheckEquals(method.VirtualIndex, 3, 'Virtual index of Test does not match');
  2099. Check(not Assigned(method.ReturnType), 'Return type of Test is not Nil');
  2100. params := method.GetParameters;
  2101. CheckEquals(0, Length(params), 'Parameter count of Test does not match');
  2102. method := methods[1];
  2103. CheckEquals(method.Name, 'Test2', 'Method name of Test2 does not match');
  2104. Check(method.CallingConvention = DefaultCC, 'Calling convention of Test2 does not match');
  2105. Check(method.MethodKind = mkFunction, 'Method kind of Test2 does not match');
  2106. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test2 does not match');
  2107. Check(not Assigned(method.CodeAddress), 'Code address of Test2 is not Nil');
  2108. CheckEquals(method.VirtualIndex, 4, 'Virtual index of Test2 does not match');
  2109. Check(Assigned(method.ReturnType), 'Return type of Test2 is Nil');
  2110. Check(method.ReturnType.TypeKind = tkInteger, 'Return type of Test2 is not an ordinal');
  2111. params := method.GetParameters;
  2112. CheckEquals(0, Length(params), 'Parameter count of Test2 does not match');
  2113. method := methods[2];
  2114. CheckEquals(method.Name, 'Test3', 'Method name of Test3 does not match');
  2115. Check(method.CallingConvention = DefaultCC, 'Calling convention of Test3 does not match');
  2116. Check(method.MethodKind = mkProcedure, 'Method kind of Test3 does not match');
  2117. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test3 does not match');
  2118. Check(not Assigned(method.CodeAddress), 'Code address of Test3 is not Nil');
  2119. CheckEquals(method.VirtualIndex, 5, 'Virtual index of Test3 does not match');
  2120. Check(not Assigned(method.ReturnType), 'Return type of Test3 is not Nil');
  2121. params := method.GetParameters;
  2122. CheckEquals(4, Length(params), 'Parameter count of Test3 does not match');
  2123. param := params[0];
  2124. CheckEquals(param.Name, 'aArg1', 'Parameter name of Test3.aArg1 does not match');
  2125. Check(param.Flags = [], 'Parameter flags of Test3.aArg1 do not match');
  2126. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg1 is Nil');
  2127. Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test3.aArg1 is not an ordinal');
  2128. param := params[1];
  2129. CheckEquals(param.Name, 'aArg2', 'Parameter name of Test3.aArg2 does not match');
  2130. Check(param.Flags = [pfConst], 'Parameter flags of Test3.aArg2 do not match');
  2131. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg2 is Nil');
  2132. Check(param.ParamType.TypeKind = tkAnsiString, 'Parameter type of Test3.aArg2 is not a string');
  2133. param := params[2];
  2134. CheckEquals(param.Name, 'aArg3', 'Parameter name of Test3.aArg3 does not match');
  2135. Check(param.Flags = [pfVar], 'Parameter flags of Test3.aArg3 do not match');
  2136. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg3 is Nil');
  2137. Check(param.ParamType.TypeKind = {$ifdef fpc}tkBool{$else}tkEnumeration{$endif}, 'Parameter type of Test3.aArg3 is not a boolean');
  2138. param := params[3];
  2139. CheckEquals(param.Name, 'aArg4', 'Parameter name of Test3.aArg4 does not match');
  2140. Check(param.Flags = [pfOut], 'Parameter flags of Test3.aArg4 do not match');
  2141. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg4 is Nil');
  2142. Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test3.aArg4 is not a string');
  2143. method := methods[3];
  2144. CheckEquals(method.Name, 'Test4', 'Method name of Test4 does not match');
  2145. Check(method.CallingConvention = DefaultCC, 'Calling convention of Test4 does not match');
  2146. Check(method.MethodKind = mkFunction, 'Method kind of Test4 does not match');
  2147. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test4 does not match');
  2148. Check(not Assigned(method.CodeAddress), 'Code address of Test4 is not Nil');
  2149. CheckEquals(method.VirtualIndex, 6, 'Virtual index of Test4 does not match');
  2150. Check(Assigned(method.ReturnType), 'Return type of Test4 is not Nil');
  2151. Check(method.ReturnType.TypeKind = tkAnsiString, 'Return type of Test4 is not a string');
  2152. params := method.GetParameters;
  2153. CheckEquals(2, Length(params), 'Parameter count of Test4 does not match');
  2154. param := params[0];
  2155. CheckEquals(param.Name, 'aArg1', 'Parameter name of Test4.aArg1 does not match');
  2156. Check(param.Flags = [pfArray, pfReference], 'Parameter flags of Test4.aArg1 do not match');
  2157. Check(Assigned(param.ParamType), 'Parameter type of Test4.aArg1 is Nil');
  2158. Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test4.aArg1 is not an ordinal');
  2159. param := params[1];
  2160. CheckEquals(param.Name, 'aArg2', 'Parameter name of Test4.aArg2 does not match');
  2161. Check(param.Flags = [pfArray, pfReference], 'Parameter flags of Test4.aArg2 do not match');
  2162. Check(Assigned(param.ParamType), 'Parameter type of Test4.aArg2 is Nil');
  2163. Check(param.ParamType.TypeKind = tkRecord, 'Parameter type of Test4.aArg2 is not a record');
  2164. finally
  2165. context.Free;
  2166. end;
  2167. end;
  2168. procedure TTestCase1.TestRawThunk;
  2169. var
  2170. intf: IInterface;
  2171. begin
  2172. { we test the raw thunking by instantiating a TVirtualInterface of IInterface }
  2173. { this does not require a function call manager as the thunking is implemented
  2174. directly inside the RTTI unit }
  2175. try
  2176. intf := TVirtualInterface.Create(PTypeInfo(TypeInfo(IInterface))) as IInterface;
  2177. except
  2178. on e: ENotImplemented do
  2179. Ignore('RawThunk not implemented');
  2180. end;
  2181. { if all went well QueryInterface and _AddRef were called and now we call
  2182. _Release as well }
  2183. intf := Nil;
  2184. end;
  2185. {$ifdef fpc}
  2186. procedure TTestCase1.TestInterfaceRaw;
  2187. var
  2188. context: TRttiContext;
  2189. t: TRttiType;
  2190. ti: TRttiInterfaceType;
  2191. begin
  2192. context := TRttiContext.Create;
  2193. try
  2194. t := context.GetType(TypeInfo(ICORBATest));
  2195. Check(t is TRttiInterfaceType, 'Type is not a raw interface type');
  2196. Check(not Assigned(t.BaseType), 'Base type is assigned');
  2197. ti := TRttiInterfaceType(t);
  2198. Check(not Assigned(ti.BaseType), 'Base type is assigned');
  2199. finally
  2200. context.Free;
  2201. end;
  2202. end;
  2203. {$endif}
  2204. procedure TTestCase1.TestProcVar;
  2205. var
  2206. context: TRttiContext;
  2207. t: TRttiType;
  2208. p: TRttiProcedureType;
  2209. params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
  2210. begin
  2211. context := TRttiContext.Create;
  2212. try
  2213. t := context.GetType(PTypeInfo(TypeInfo(TTestProc)));
  2214. Check(Assigned(t), 'Rtti Type is Nil');
  2215. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  2216. Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
  2217. p := t as TRttiProcedureType;
  2218. Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
  2219. Check(not Assigned(p.ReturnType), 'Return type is assigned');
  2220. CheckEquals(0, Length(p.GetParameters), 'Procedure variable has parameters');
  2221. t := context.GetType(PTypeInfo(TypeInfo(TTestFunc1)));
  2222. Check(Assigned(t), 'Rtti Type is Nil');
  2223. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  2224. Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
  2225. p := t as TRttiProcedureType;
  2226. Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
  2227. Check(Assigned(p.ReturnType), 'Return type is not assigned');
  2228. //Check(p.ReturnType is TRttiOrdinalType, 'Return type is not an ordinal type');
  2229. CheckEquals(0, Length(p.GetParameters), 'Procedure variable has parameters');
  2230. t := context.GetType(PTypeInfo(TypeInfo(TTestFunc2)));
  2231. Check(Assigned(t), 'Rtti Type is Nil');
  2232. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  2233. Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
  2234. p := t as TRttiProcedureType;
  2235. Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
  2236. Check(Assigned(p.ReturnType), 'Return type is not assigned');
  2237. Check(p.ReturnType is TRttiStringType, 'Return type is not a string type');
  2238. params := p.GetParameters;
  2239. CheckEquals(2, Length(params), 'Procedure variable has incorrect amount of parameters');
  2240. Check(params[0].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 1 is not an ordinal type');
  2241. //Check(params[0].ParamType is TRttiOrdinalType, 'Parameter 1 is not an ordinal type');
  2242. Check(pfArray in params[1].Flags, 'Parameter 2 is not an array');
  2243. Check(params[1].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 2 is not an ordinal array');
  2244. finally
  2245. context.Free;
  2246. end;
  2247. end;
  2248. procedure TTestCase1.TestMethod;
  2249. var
  2250. context: TRttiContext;
  2251. t: TRttiType;
  2252. m: TRttiMethodType;
  2253. params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
  2254. begin
  2255. context := TRttiContext.Create;
  2256. try
  2257. t := context.GetType(PTypeInfo(TypeInfo(TTestMethod)));
  2258. Check(Assigned(t), 'Rtti Type is Nil');
  2259. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  2260. Check(t is TRttiMethodType, 'Rtti Type is not a method type');
  2261. m := t as TRttiMethodType;
  2262. Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
  2263. Check(not Assigned(m.ReturnType), 'Return type is assigned');
  2264. CheckEquals(0, Length(m.GetParameters), 'Method variable has parameters');
  2265. t := context.GetType(PTypeInfo(TypeInfo(TTestMethod1)));
  2266. Check(Assigned(t), 'Rtti Type is Nil');
  2267. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  2268. Check(t is TRttiMethodType, 'Rtti Type is not a method type');
  2269. m := t as TRttiMethodType;
  2270. Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
  2271. Check(Assigned(m.ReturnType), 'Return type is not assigned');
  2272. //Check(p.ReturnType is TRttiOrdinalType, 'Return type is not an ordinal type');
  2273. CheckEquals(0, Length(m.GetParameters), 'Method variable has parameters');
  2274. t := context.GetType(PTypeInfo(TypeInfo(TTestMethod2)));
  2275. Check(Assigned(t), 'Rtti Type is Nil');
  2276. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  2277. Check(t is TRttiMethodType, 'Rtti Type is not a method type');
  2278. m := t as TRttiMethodType;
  2279. Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
  2280. Check(Assigned(m.ReturnType), 'Return type is not assigned');
  2281. Check(m.ReturnType is TRttiStringType, 'Return type is not a string type');
  2282. params := m.GetParameters;
  2283. CheckEquals(2, Length(params), 'Method variable has incorrect amount of parameters');
  2284. Check(params[0].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 1 is not an ordinal type');
  2285. //Check(params[0].ParamType is TRttiOrdinalType, 'Parameter 1 is not an ordinal type');
  2286. Check(pfArray in params[1].Flags, 'Parameter 2 is not an array');
  2287. Check(params[1].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 2 is not an ordinal array');
  2288. finally
  2289. context.Free;
  2290. end;
  2291. end;
  2292. initialization
  2293. {$ifdef fpc}
  2294. RegisterTest(TTestCase1);
  2295. {$else fpc}
  2296. RegisterTest(TTestCase1.Suite);
  2297. {$endif fpc}
  2298. end.