tests.rtti.pas 78 KB

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