tests.rtti.pas 83 KB

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