tests.rtti.pas 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  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 TestPropSetValueString;
  34. procedure TestPropSetValueInteger;
  35. procedure TestPropSetValueBoolean;
  36. procedure TestPropSetValueShortString;
  37. procedure TestGetValueStringCastError;
  38. procedure TestGetIsReadable;
  39. procedure TestIsWritable;
  40. procedure TestMakeNil;
  41. procedure TestMakeObject;
  42. procedure TestMakeArrayDynamic;
  43. procedure TestMakeArrayStatic;
  44. {$ifdef fpc}
  45. procedure TestMakeArrayOpen;
  46. {$endif}
  47. procedure TestMakeSingle;
  48. procedure TestMakeDouble;
  49. procedure TestMakeExtended;
  50. procedure TestMakeCurrency;
  51. procedure TestMakeComp;
  52. procedure TestDataSize;
  53. procedure TestDataSizeEmpty;
  54. procedure TestReferenceRawData;
  55. procedure TestReferenceRawDataEmpty;
  56. procedure TestIsManaged;
  57. {$ifdef fpc}
  58. procedure TestOpenArrayToDyn;
  59. {$endif}
  60. procedure TestInterface;
  61. {$ifdef fpc}
  62. procedure TestInterfaceRaw;
  63. {$endif}
  64. procedure TestProcVar;
  65. procedure TestMethod;
  66. end;
  67. implementation
  68. type
  69. {$M+}
  70. TGetClassProperties = class
  71. private
  72. FPubPropRO: integer;
  73. FPubPropRW: integer;
  74. published
  75. property PubPropRO: integer read FPubPropRO;
  76. property PubPropRW: integer read FPubPropRW write FPubPropRW;
  77. property PubPropSetRO: integer read FPubPropRO;
  78. property PubPropSetRW: integer read FPubPropRW write FPubPropRW;
  79. end;
  80. TGetClassPropertiesSub = class(TGetClassProperties)
  81. end;
  82. {$M-}
  83. { TTestValueClass }
  84. {$M+}
  85. TTestValueClass = class
  86. private
  87. FAInteger: integer;
  88. FAString: string;
  89. FABoolean: boolean;
  90. FAShortString: ShortString;
  91. function GetAInteger: integer;
  92. function GetAString: string;
  93. function GetABoolean: boolean;
  94. function GetAShortString: ShortString;
  95. procedure SetWriteOnly(AValue: integer);
  96. published
  97. property AInteger: Integer read FAInteger write FAInteger;
  98. property AString: string read FAString write FAString;
  99. property ABoolean: boolean read FABoolean write FABoolean;
  100. property AShortString: ShortString read FAShortString write FAShortString;
  101. property AGetInteger: Integer read GetAInteger;
  102. property AGetString: string read GetAString;
  103. property AGetBoolean: boolean read GetABoolean;
  104. property AGetShortString: ShortString read GetAShortString;
  105. property AWriteOnly: integer write SetWriteOnly;
  106. end;
  107. {$M-}
  108. {$M+}
  109. ITestInterface = interface
  110. procedure Test;
  111. function Test2: LongInt;
  112. procedure Test3(aArg1: LongInt; const aArg2: AnsiString; var aArg3: Boolean; out aArg4: Word);
  113. function Test4(aArg1: array of LongInt; aArg2: array of const): AnsiString;
  114. end;
  115. {$M-}
  116. TManagedRec = record
  117. s: string;
  118. end;
  119. {$ifdef fpc}
  120. TManagedRecOp = record
  121. class operator AddRef(var a: TManagedRecOp);
  122. end;
  123. {$endif}
  124. TNonManagedRec = record
  125. i: Integer;
  126. end;
  127. TManagedObj = object
  128. i: IInterface;
  129. end;
  130. TNonManagedObj = object
  131. d: double;
  132. end;
  133. TTestEnum = (te1, te2, te3, te4, te5);
  134. TTestSet = set of TTestEnum;
  135. TTestProc = procedure;
  136. TTestFunc1 = function: LongInt;
  137. TTestFunc2 = function(aArg1: LongInt; aArg2: array of LongInt): String;
  138. TTestMethod = procedure of object;
  139. TTestMethod1 = function: LongInt of object;
  140. TTestMethod2 = function(aArg1: LongInt; aArg2: array of LongInt): String of object;
  141. TTestHelper = class helper for TObject
  142. end;
  143. TArrayOfString = array[0..0] of string;
  144. TArrayOfManagedRec = array[0..0] of TManagedRec;
  145. TArrayOfNonManagedRec = array[0..0] of TNonManagedRec;
  146. TArrayOfByte = array[0..0] of byte;
  147. TArrayOfLongintDyn = array of LongInt;
  148. TArrayOfLongintStatic = array[0..3] of LongInt;
  149. TTestRecord = record
  150. Value1: LongInt;
  151. Value2: String;
  152. end;
  153. PTestRecord = ^TTestRecord;
  154. {$ifdef fpc}
  155. {$PUSH}
  156. {$INTERFACES CORBA}
  157. ICORBATest = interface
  158. end;
  159. {$POP}
  160. {$endif}
  161. {$ifdef fpc}
  162. class operator TManagedRecOp.AddRef(var a: TManagedRecOp);
  163. begin
  164. end;
  165. {$endif}
  166. { TTestValueClass }
  167. function TTestValueClass.GetAInteger: integer;
  168. begin
  169. result := FAInteger;
  170. end;
  171. function TTestValueClass.GetAString: string;
  172. begin
  173. result := FAString;
  174. end;
  175. function TTestValueClass.GetABoolean: boolean;
  176. begin
  177. result := FABoolean;
  178. end;
  179. function TTestValueClass.GetAShortString: ShortString;
  180. begin
  181. Result := FAShortString;
  182. end;
  183. procedure TTestValueClass.SetWriteOnly(AValue: integer);
  184. begin
  185. // Do nothing
  186. end;
  187. { Note: GetTypes currently only returns those types that had been acquired using
  188. GetType, so GetTypes itself can't be really tested currently }
  189. (*procedure TTestCase1.GetTypes;
  190. var
  191. LContext: TRttiContext;
  192. LType: TRttiType;
  193. IsTestCaseClassFound: boolean;
  194. begin
  195. LContext := TRttiContext.Create;
  196. { Enumerate all types declared in the application }
  197. for LType in LContext.GetTypes() do
  198. begin
  199. if LType.Name='TTestCase1' then
  200. IsTestCaseClassFound:=true;
  201. end;
  202. LContext.Free;
  203. CheckTrue(IsTestCaseClassFound, 'RTTI information does not contain class of testcase.');
  204. end;*)
  205. procedure TTestCase1.TestGetValueStringCastError;
  206. var
  207. ATestClass : TTestValueClass;
  208. c: TRttiContext;
  209. ARttiType: TRttiType;
  210. AValue: TValue;
  211. i: integer;
  212. HadException: boolean;
  213. begin
  214. c := TRttiContext.Create;
  215. try
  216. ATestClass := TTestValueClass.Create;
  217. ATestClass.AString := '12';
  218. try
  219. ARttiType := c.GetType(ATestClass.ClassInfo);
  220. AValue := ARttiType.GetProperty('astring').GetValue(ATestClass);
  221. HadException := false;
  222. try
  223. i := AValue.AsInteger;
  224. except
  225. on E: Exception do
  226. if E.ClassType=EInvalidCast then
  227. HadException := true;
  228. end;
  229. Check(HadException, 'No or invalid exception on invalid cast');
  230. finally
  231. AtestClass.Free;
  232. end;
  233. finally
  234. c.Free;
  235. end;
  236. end;
  237. procedure TTestCase1.TestMakeNil;
  238. var
  239. value: TValue;
  240. begin
  241. TValue.Make(Nil, Nil, value);
  242. CheckTrue(value.Kind = tkUnknown);
  243. CheckTrue(value.IsEmpty);
  244. CheckTrue(value.IsObject);
  245. CheckTrue(value.IsClass);
  246. CheckTrue(value.IsOrdinal);
  247. CheckFalse(value.IsArray);
  248. CheckTrue(value.AsObject = Nil);
  249. CheckTrue(value.AsClass = Nil);
  250. CheckTrue(value.AsInterface = Nil);
  251. CheckEquals(0, value.AsOrdinal);
  252. TValue.Make(Nil, TypeInfo(TObject), value);
  253. CheckTrue(value.IsEmpty);
  254. CheckTrue(value.IsObject);
  255. CheckTrue(value.IsClass);
  256. CheckTrue(value.IsOrdinal);
  257. CheckFalse(value.IsArray);
  258. CheckTrue(value.AsObject=Nil);
  259. CheckTrue(value.AsClass=Nil);
  260. CheckTrue(value.AsInterface=Nil);
  261. CheckEquals(0, value.AsOrdinal);
  262. TValue.Make(Nil, TypeInfo(TClass), value);
  263. CheckTrue(value.IsEmpty);
  264. CheckTrue(value.IsClass);
  265. CheckTrue(value.IsOrdinal);
  266. CheckFalse(value.IsArray);
  267. CheckTrue(value.AsObject=Nil);
  268. CheckTrue(value.AsClass=Nil);
  269. CheckTrue(value.AsInterface=Nil);
  270. CheckEquals(0, value.AsOrdinal);
  271. TValue.Make(Nil, TypeInfo(LongInt), value);
  272. CheckTrue(value.IsOrdinal);
  273. CheckFalse(value.IsEmpty);
  274. CheckFalse(value.IsClass);
  275. CheckFalse(value.IsObject);
  276. CheckFalse(value.IsArray);
  277. CheckEquals(0, value.AsOrdinal);
  278. CheckEquals(0, value.AsInteger);
  279. CheckEquals(0, value.AsInt64);
  280. CheckEquals(0, value.AsUInt64);
  281. TValue.Make(Nil, TypeInfo(String), value);
  282. CheckFalse(value.IsEmpty);
  283. CheckFalse(value.IsObject);
  284. CheckFalse(value.IsClass);
  285. CheckFalse(value.IsArray);
  286. CheckEquals('', value.AsString);
  287. end;
  288. procedure TTestCase1.TestMakeObject;
  289. var
  290. AValue: TValue;
  291. ATestClass: TTestValueClass;
  292. begin
  293. ATestClass := TTestValueClass.Create;
  294. ATestClass.AInteger := 54329;
  295. TValue.Make(@ATestClass, TypeInfo(TTestValueClass),AValue);
  296. CheckEquals(AValue.IsClass, False);
  297. CheckEquals(AValue.IsObject, True);
  298. Check(AValue.AsObject=ATestClass);
  299. Check(PPointer(AValue.GetReferenceToRawData)^ = Pointer(ATestClass));
  300. CheckEquals(TTestValueClass(AValue.AsObject).AInteger, 54329);
  301. ATestClass.Free;
  302. end;
  303. procedure TTestCase1.TestMakeArrayDynamic;
  304. var
  305. arr: TArrayOfLongintDyn;
  306. value: TValue;
  307. begin
  308. SetLength(arr, 2);
  309. arr[0] := 42;
  310. arr[1] := 21;
  311. TValue.Make(@arr, TypeInfo(TArrayOfLongintDyn), value);
  312. CheckEquals(value.IsArray, True);
  313. CheckEquals(value.IsObject, False);
  314. CheckEquals(value.IsOrdinal, False);
  315. CheckEquals(value.IsClass, False);
  316. CheckEquals(value.GetArrayLength, 2);
  317. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  318. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  319. Check(PPointer(value.GetReferenceToRawData)^ = Pointer(arr));
  320. value.SetArrayElement(0, 84);
  321. CheckEquals(arr[0], 84);
  322. end;
  323. procedure TTestCase1.TestMakeArrayStatic;
  324. type
  325. TArrStat = array[0..1] of LongInt;
  326. TArrStat2D = array[0..1, 0..1] of LongInt;
  327. var
  328. arr: TArrStat;
  329. arr2D: TArrStat2D;
  330. value: TValue;
  331. begin
  332. arr[0] := 42;
  333. arr[1] := 21;
  334. TValue.Make(@arr, TypeInfo(TArrStat), value);
  335. CheckEquals(value.IsArray, True);
  336. CheckEquals(value.IsObject, False);
  337. CheckEquals(value.IsOrdinal, False);
  338. CheckEquals(value.IsClass, False);
  339. CheckEquals(value.GetArrayLength, 2);
  340. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  341. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  342. value.SetArrayElement(0, 84);
  343. { since this is a static array the original array isn't touched! }
  344. CheckEquals(arr[0], 42);
  345. arr2D[0, 0] := 42;
  346. arr2D[0, 1] := 21;
  347. arr2D[1, 0] := 84;
  348. arr2D[1, 1] := 63;
  349. TValue.Make(@arr2D, TypeInfo(TArrStat2D), value);
  350. CheckEquals(value.IsArray, True);
  351. CheckEquals(value.GetArrayLength, 4);
  352. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  353. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  354. CheckEquals(value.GetArrayElement(2).AsInteger, 84);
  355. CheckEquals(value.GetArrayElement(3).AsInteger, 63);
  356. end;
  357. {$ifdef fpc}
  358. procedure TTestCase1.TestMakeArrayOpen;
  359. procedure TestOpenArrayValueCopy(aArr: array of LongInt);
  360. var
  361. value: TValue;
  362. begin
  363. TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
  364. CheckEquals(value.IsArray, True);
  365. CheckEquals(value.IsOpenArray, True);
  366. CheckEquals(value.IsObject, False);
  367. CheckEquals(value.IsOrdinal, False);
  368. CheckEquals(value.IsClass, False);
  369. CheckEquals(value.GetArrayLength, 2);
  370. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  371. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  372. value.SetArrayElement(0, 84);
  373. { since this is an open array the original array is modified! }
  374. CheckEquals(aArr[0], 84);
  375. end;
  376. procedure TestOpenArrayValueVar(var aArr: array of LongInt);
  377. var
  378. value: TValue;
  379. begin
  380. TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
  381. CheckEquals(value.IsArray, True);
  382. CheckEquals(value.IsOpenArray, True);
  383. CheckEquals(value.IsObject, False);
  384. CheckEquals(value.IsOrdinal, False);
  385. CheckEquals(value.IsClass, False);
  386. CheckEquals(value.GetArrayLength, 2);
  387. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  388. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  389. value.SetArrayElement(0, 84);
  390. { since this is an open array the original array is modified! }
  391. CheckEquals(aArr[0], 84);
  392. end;
  393. procedure TestOpenArrayValueOut(var aArr: array of LongInt);
  394. var
  395. value: TValue;
  396. begin
  397. TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
  398. CheckEquals(value.IsArray, True);
  399. CheckEquals(value.IsOpenArray, True);
  400. CheckEquals(value.IsObject, False);
  401. CheckEquals(value.IsOrdinal, False);
  402. CheckEquals(value.IsClass, False);
  403. CheckEquals(value.GetArrayLength, 2);
  404. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  405. CheckEquals(value.GetArrayElement(1).AsInteger, 21);
  406. value.SetArrayElement(0, 84);
  407. value.SetArrayElement(1, 128);
  408. { since this is an open array the original array is modified! }
  409. CheckEquals(aArr[0], 84);
  410. CheckEquals(aArr[1], 128);
  411. CheckEquals(value.GetArrayElement(0).AsInteger, 84);
  412. CheckEquals(value.GetArrayElement(1).AsInteger, 128);
  413. end;
  414. var
  415. arr: array of LongInt;
  416. begin
  417. TestOpenArrayValueCopy([42, 21]);
  418. arr := [42, 21];
  419. TestOpenArrayValueVar(arr);
  420. CheckEquals(arr[0], 84);
  421. CheckEquals(arr[1], 21);
  422. arr := [42, 21];
  423. TestOpenArrayValueOut(arr);
  424. CheckEquals(arr[0], 84);
  425. CheckEquals(arr[1], 128);
  426. end;
  427. {$endif}
  428. procedure TTestCase1.TestMakeSingle;
  429. var
  430. fs: Single;
  431. v: TValue;
  432. hadexcept: Boolean;
  433. begin
  434. fs := 3.14;
  435. TValue.Make(@fs, TypeInfo(fs), v);
  436. CheckEquals(v.IsClass, False);
  437. CheckEquals(v.IsObject, False);
  438. CheckEquals(v.IsOrdinal, False);
  439. Check(v.AsExtended=fs);
  440. Check(v.GetReferenceToRawData <> @fs);
  441. try
  442. hadexcept := False;
  443. v.AsInt64;
  444. except
  445. hadexcept := True;
  446. end;
  447. CheckTrue(hadexcept, 'No signed type conversion exception');
  448. try
  449. hadexcept := False;
  450. v.AsUInt64;
  451. except
  452. hadexcept := True;
  453. end;
  454. CheckTrue(hadexcept, 'No unsigned type conversion exception');
  455. end;
  456. procedure TTestCase1.TestMakeDouble;
  457. var
  458. fd: Double;
  459. v: TValue;
  460. hadexcept: Boolean;
  461. begin
  462. fd := 3.14;
  463. TValue.Make(@fd, TypeInfo(fd), v);
  464. CheckEquals(v.IsClass, False);
  465. CheckEquals(v.IsObject, False);
  466. CheckEquals(v.IsOrdinal, False);
  467. Check(v.AsExtended=fd);
  468. Check(v.GetReferenceToRawData <> @fd);
  469. try
  470. hadexcept := False;
  471. v.AsInt64;
  472. except
  473. hadexcept := True;
  474. end;
  475. CheckTrue(hadexcept, 'No signed type conversion exception');
  476. try
  477. hadexcept := False;
  478. v.AsUInt64;
  479. except
  480. hadexcept := True;
  481. end;
  482. CheckTrue(hadexcept, 'No unsigned type conversion exception');
  483. end;
  484. procedure TTestCase1.TestMakeExtended;
  485. var
  486. fe: Extended;
  487. v: TValue;
  488. hadexcept: Boolean;
  489. begin
  490. fe := 3.14;
  491. TValue.Make(@fe, TypeInfo(fe), v);
  492. CheckEquals(v.IsClass, False);
  493. CheckEquals(v.IsObject, False);
  494. CheckEquals(v.IsOrdinal, False);
  495. Check(v.AsExtended=fe);
  496. Check(v.GetReferenceToRawData <> @fe);
  497. try
  498. hadexcept := False;
  499. v.AsInt64;
  500. except
  501. hadexcept := True;
  502. end;
  503. CheckTrue(hadexcept, 'No signed type conversion exception');
  504. try
  505. hadexcept := False;
  506. v.AsUInt64;
  507. except
  508. hadexcept := True;
  509. end;
  510. CheckTrue(hadexcept, 'No unsigned type conversion exception');
  511. end;
  512. procedure TTestCase1.TestMakeCurrency;
  513. var
  514. fcu: Currency;
  515. v: TValue;
  516. hadexcept: Boolean;
  517. begin
  518. fcu := 3.14;
  519. TValue.Make(@fcu, TypeInfo(fcu), v);
  520. CheckEquals(v.IsClass, False);
  521. CheckEquals(v.IsObject, False);
  522. CheckEquals(v.IsOrdinal, False);
  523. Check(v.AsExtended=Extended(fcu));
  524. Check(v.AsCurrency=fcu);
  525. Check(v.GetReferenceToRawData <> @fcu);
  526. try
  527. hadexcept := False;
  528. v.AsInt64;
  529. except
  530. hadexcept := True;
  531. end;
  532. CheckTrue(hadexcept, 'No signed type conversion exception');
  533. try
  534. hadexcept := False;
  535. v.AsUInt64;
  536. except
  537. hadexcept := True;
  538. end;
  539. CheckTrue(hadexcept, 'No unsigned type conversion exception');
  540. end;
  541. procedure TTestCase1.TestMakeComp;
  542. var
  543. fco: Comp;
  544. v: TValue;
  545. hadexcept: Boolean;
  546. begin
  547. fco := 314;
  548. TValue.Make(@fco, TypeInfo(fco), v);
  549. if v.Kind <> tkFloat then
  550. Exit;
  551. CheckEquals(v.IsClass, False);
  552. CheckEquals(v.IsObject, False);
  553. CheckEquals(v.IsOrdinal, False);
  554. Check(v.AsExtended=Extended(fco));
  555. Check(v.GetReferenceToRawData <> @fco);
  556. try
  557. hadexcept := False;
  558. CheckEquals(v.AsInt64, 314);
  559. except
  560. hadexcept := True;
  561. end;
  562. CheckFalse(hadexcept, 'Had signed type conversion exception');
  563. try
  564. hadexcept := False;
  565. CheckEquals(v.AsUInt64, 314);
  566. except
  567. hadexcept := True;
  568. end;
  569. CheckFalse(hadexcept, 'Had unsigned type conversion exception');
  570. end;
  571. procedure TTestCase1.TestGetIsReadable;
  572. var
  573. c: TRttiContext;
  574. ARttiType: TRttiType;
  575. AProperty: TRttiProperty;
  576. begin
  577. c := TRttiContext.Create;
  578. try
  579. ARttiType := c.GetType(TTestValueClass);
  580. AProperty := ARttiType.GetProperty('aBoolean');
  581. CheckEquals(AProperty.IsReadable, true);
  582. AProperty := ARttiType.GetProperty('aGetBoolean');
  583. CheckEquals(AProperty.IsReadable, true);
  584. AProperty := ARttiType.GetProperty('aWriteOnly');
  585. CheckEquals(AProperty.IsReadable, False);
  586. finally
  587. c.Free;
  588. end;
  589. end;
  590. procedure TTestCase1.TestIsWritable;
  591. var
  592. c: TRttiContext;
  593. ARttiType: TRttiType;
  594. AProperty: TRttiProperty;
  595. begin
  596. c := TRttiContext.Create;
  597. try
  598. ARttiType := c.GetType(TTestValueClass);
  599. AProperty := ARttiType.GetProperty('aBoolean');
  600. CheckEquals(AProperty.IsWritable, true);
  601. AProperty := ARttiType.GetProperty('aGetBoolean');
  602. CheckEquals(AProperty.IsWritable, false);
  603. AProperty := ARttiType.GetProperty('aWriteOnly');
  604. CheckEquals(AProperty.IsWritable, True);
  605. finally
  606. c.Free;
  607. end;
  608. end;
  609. procedure TTestCase1.TestPropGetValueBoolean;
  610. var
  611. ATestClass : TTestValueClass;
  612. c: TRttiContext;
  613. ARttiType: TRttiType;
  614. AProperty: TRttiProperty;
  615. AValue: TValue;
  616. begin
  617. c := TRttiContext.Create;
  618. try
  619. ATestClass := TTestValueClass.Create;
  620. ATestClass.ABoolean := true;
  621. try
  622. ARttiType := c.GetType(ATestClass.ClassInfo);
  623. Check(assigned(ARttiType));
  624. AProperty := ARttiType.GetProperty('aBoolean');
  625. AValue := AProperty.GetValue(ATestClass);
  626. CheckEquals(true,AValue.AsBoolean);
  627. ATestClass.ABoolean := false;
  628. CheckEquals(true, AValue.AsBoolean);
  629. CheckEquals('True', AValue.ToString);
  630. CheckEquals(True, AValue.IsOrdinal);
  631. CheckEquals(1, AValue.AsOrdinal);
  632. finally
  633. AtestClass.Free;
  634. end;
  635. CheckEquals(True,AValue.AsBoolean);
  636. finally
  637. c.Free;
  638. end;
  639. end;
  640. procedure TTestCase1.TestPropGetValueShortString;
  641. var
  642. ATestClass : TTestValueClass;
  643. c: TRttiContext;
  644. ARttiType: TRttiType;
  645. AProperty: TRttiProperty;
  646. AValue: TValue;
  647. begin
  648. c := TRttiContext.Create;
  649. try
  650. ATestClass := TTestValueClass.Create;
  651. ATestClass.AShortString := 'Hello World';
  652. try
  653. ARttiType := c.GetType(ATestClass.ClassInfo);
  654. Check(assigned(ARttiType));
  655. AProperty := ARttiType.GetProperty('aShortString');
  656. AValue := AProperty.GetValue(ATestClass);
  657. CheckEquals('Hello World',AValue.AsString);
  658. ATestClass.AShortString := 'Foobar';
  659. CheckEquals('Hello World', AValue.AsString);
  660. CheckEquals(False, AValue.IsOrdinal);
  661. CheckEquals(False, AValue.IsObject);
  662. CheckEquals(False, AValue.IsArray);
  663. CheckEquals(False, AValue.IsClass);
  664. finally
  665. AtestClass.Free;
  666. end;
  667. CheckEquals('Hello World',AValue.AsString);
  668. finally
  669. c.Free;
  670. end;
  671. end;
  672. procedure TTestCase1.TestPropGetValueInteger;
  673. var
  674. ATestClass : TTestValueClass;
  675. c: TRttiContext;
  676. ARttiType: TRttiType;
  677. AProperty: TRttiProperty;
  678. AValue: TValue;
  679. begin
  680. c := TRttiContext.Create;
  681. try
  682. ATestClass := TTestValueClass.Create;
  683. ATestClass.AInteger := 472349;
  684. try
  685. ARttiType := c.GetType(ATestClass.ClassInfo);
  686. Check(assigned(ARttiType));
  687. AProperty := ARttiType.GetProperty('ainteger');
  688. AValue := AProperty.GetValue(ATestClass);
  689. CheckEquals(472349,AValue.AsInteger);
  690. ATestClass.AInteger := 12;
  691. CheckEquals(472349, AValue.AsInteger);
  692. CheckEquals('472349', AValue.ToString);
  693. CheckEquals(True, AValue.IsOrdinal);
  694. finally
  695. AtestClass.Free;
  696. end;
  697. CheckEquals(472349,AValue.AsInteger);
  698. finally
  699. c.Free;
  700. end;
  701. end;
  702. procedure TTestCase1.TestPropGetValueString;
  703. var
  704. ATestClass : TTestValueClass;
  705. c: TRttiContext;
  706. ARttiType: TRttiType;
  707. AProperty: TRttiProperty;
  708. AValue: TValue;
  709. i: int64;
  710. begin
  711. c := TRttiContext.Create;
  712. try
  713. ATestClass := TTestValueClass.Create;
  714. ATestClass.AString := 'Hello World';
  715. try
  716. ARttiType := c.GetType(ATestClass.ClassInfo);
  717. Check(assigned(ARttiType));
  718. AProperty := ARttiType.GetProperty('astring');
  719. AValue := AProperty.GetValue(ATestClass);
  720. CheckEquals('Hello World',AValue.AsString);
  721. ATestClass.AString := 'Goodbye World';
  722. CheckEquals('Hello World',AValue.AsString);
  723. CheckEquals('Hello World',AValue.ToString);
  724. Check(TypeInfo(string)=AValue.TypeInfo);
  725. Check(AValue.TypeData=GetTypeData(AValue.TypeInfo));
  726. Check(AValue.IsEmpty=false);
  727. Check(AValue.IsObject=false);
  728. Check(AValue.IsClass=false);
  729. CheckEquals(AValue.IsOrdinal, false);
  730. CheckEquals(AValue.TryAsOrdinal(i), false);
  731. CheckEquals(AValue.IsType(TypeInfo(string)), true);
  732. CheckEquals(AValue.IsType(TypeInfo(integer)), false);
  733. CheckEquals(AValue.IsArray, false);
  734. finally
  735. AtestClass.Free;
  736. end;
  737. CheckEquals('Hello World',AValue.AsString);
  738. finally
  739. c.Free;
  740. end;
  741. end;
  742. procedure TTestCase1.TestPropGetValueProcBoolean;
  743. var
  744. ATestClass : TTestValueClass;
  745. c: TRttiContext;
  746. ARttiType: TRttiType;
  747. AProperty: TRttiProperty;
  748. AValue: TValue;
  749. begin
  750. c := TRttiContext.Create;
  751. try
  752. ATestClass := TTestValueClass.Create;
  753. ATestClass.ABoolean := true;
  754. try
  755. ARttiType := c.GetType(ATestClass.ClassInfo);
  756. Check(assigned(ARttiType));
  757. AProperty := ARttiType.GetProperty('aGetBoolean');
  758. AValue := AProperty.GetValue(ATestClass);
  759. CheckEquals(true,AValue.AsBoolean);
  760. finally
  761. AtestClass.Free;
  762. end;
  763. CheckEquals(True,AValue.AsBoolean);
  764. finally
  765. c.Free;
  766. end;
  767. end;
  768. procedure TTestCase1.TestPropGetValueProcShortString;
  769. var
  770. ATestClass : TTestValueClass;
  771. c: TRttiContext;
  772. ARttiType: TRttiType;
  773. AProperty: TRttiProperty;
  774. AValue: TValue;
  775. begin
  776. c := TRttiContext.Create;
  777. try
  778. ATestClass := TTestValueClass.Create;
  779. ATestClass.AShortString := 'Hello World';
  780. try
  781. ARttiType := c.GetType(ATestClass.ClassInfo);
  782. Check(assigned(ARttiType));
  783. AProperty := ARttiType.GetProperty('aGetShortString');
  784. AValue := AProperty.GetValue(ATestClass);
  785. CheckEquals('Hello World',AValue.AsString);
  786. finally
  787. AtestClass.Free;
  788. end;
  789. CheckEquals('Hello World',AValue.AsString);
  790. finally
  791. c.Free;
  792. end;
  793. end;
  794. procedure TTestCase1.TestPropSetValueString;
  795. var
  796. ATestClass : TTestValueClass;
  797. c: TRttiContext;
  798. ARttiType: TRttiType;
  799. AProperty: TRttiProperty;
  800. AValue: TValue;
  801. s: string;
  802. begin
  803. c := TRttiContext.Create;
  804. try
  805. ATestClass := TTestValueClass.Create;
  806. try
  807. ARttiType := c.GetType(ATestClass.ClassInfo);
  808. AProperty := ARttiType.GetProperty('astring');
  809. s := 'ipse lorem or something like that';
  810. TValue.Make(@s, TypeInfo(string), AValue);
  811. AProperty.SetValue(ATestClass, AValue);
  812. CheckEquals(ATestClass.AString, s);
  813. s := 'Another string';
  814. CheckEquals(ATestClass.AString, 'ipse lorem or something like that');
  815. finally
  816. AtestClass.Free;
  817. end;
  818. finally
  819. c.Free;
  820. end;
  821. end;
  822. procedure TTestCase1.TestPropSetValueInteger;
  823. var
  824. ATestClass : TTestValueClass;
  825. c: TRttiContext;
  826. ARttiType: TRttiType;
  827. AProperty: TRttiProperty;
  828. AValue: TValue;
  829. i: integer;
  830. begin
  831. c := TRttiContext.Create;
  832. try
  833. ATestClass := TTestValueClass.Create;
  834. try
  835. ARttiType := c.GetType(ATestClass.ClassInfo);
  836. AProperty := ARttiType.GetProperty('aInteger');
  837. i := -43573;
  838. TValue.Make(@i, TypeInfo(Integer), AValue);
  839. AProperty.SetValue(ATestClass, AValue);
  840. CheckEquals(ATestClass.AInteger, i);
  841. i := 1;
  842. CheckEquals(ATestClass.AInteger, -43573);
  843. finally
  844. AtestClass.Free;
  845. end;
  846. finally
  847. c.Free;
  848. end;
  849. end;
  850. procedure TTestCase1.TestPropSetValueBoolean;
  851. var
  852. ATestClass : TTestValueClass;
  853. c: TRttiContext;
  854. ARttiType: TRttiType;
  855. AProperty: TRttiProperty;
  856. AValue: TValue;
  857. b: boolean;
  858. begin
  859. c := TRttiContext.Create;
  860. try
  861. ATestClass := TTestValueClass.Create;
  862. try
  863. ARttiType := c.GetType(ATestClass.ClassInfo);
  864. AProperty := ARttiType.GetProperty('aboolean');
  865. b := true;
  866. TValue.Make(@b, TypeInfo(Boolean), AValue);
  867. AProperty.SetValue(ATestClass, AValue);
  868. CheckEquals(ATestClass.ABoolean, b);
  869. b := false;
  870. CheckEquals(ATestClass.ABoolean, true);
  871. TValue.Make(@b, TypeInfo(Boolean), AValue);
  872. AProperty.SetValue(ATestClass, AValue);
  873. CheckEquals(ATestClass.ABoolean, false);
  874. finally
  875. AtestClass.Free;
  876. end;
  877. finally
  878. c.Free;
  879. end;
  880. end;
  881. procedure TTestCase1.TestPropSetValueShortString;
  882. var
  883. ATestClass : TTestValueClass;
  884. c: TRttiContext;
  885. ARttiType: TRttiType;
  886. AProperty: TRttiProperty;
  887. AValue: TValue;
  888. s: string;
  889. ss: ShortString;
  890. begin
  891. c := TRttiContext.Create;
  892. try
  893. ATestClass := TTestValueClass.Create;
  894. try
  895. ARttiType := c.GetType(ATestClass.ClassInfo);
  896. AProperty := ARttiType.GetProperty('aShortString');
  897. s := 'ipse lorem or something like that';
  898. TValue.Make(@s, TypeInfo(String), AValue);
  899. AProperty.SetValue(ATestClass, AValue);
  900. CheckEquals(ATestClass.AShortString, s);
  901. s := 'Another string';
  902. CheckEquals(ATestClass.AShortString, 'ipse lorem or something like that');
  903. ss := 'Hello World';
  904. TValue.Make(@ss, TypeInfo(ShortString), AValue);
  905. AProperty.SetValue(ATestClass, AValue);
  906. CheckEquals(ATestClass.AShortString, ss);
  907. ss := 'Foobar';
  908. CheckEquals(ATestClass.AShortString, 'Hello World');
  909. finally
  910. AtestClass.Free;
  911. end;
  912. finally
  913. c.Free;
  914. end;
  915. end;
  916. procedure TTestCase1.TestPropGetValueProcInteger;
  917. var
  918. ATestClass : TTestValueClass;
  919. c: TRttiContext;
  920. ARttiType: TRttiType;
  921. AProperty: TRttiProperty;
  922. AValue: TValue;
  923. begin
  924. c := TRttiContext.Create;
  925. try
  926. ATestClass := TTestValueClass.Create;
  927. ATestClass.AInteger := 472349;
  928. try
  929. ARttiType := c.GetType(ATestClass.ClassInfo);
  930. Check(assigned(ARttiType));
  931. AProperty := ARttiType.GetProperty('agetinteger');
  932. AValue := AProperty.GetValue(ATestClass);
  933. CheckEquals(472349,AValue.AsInteger);
  934. finally
  935. AtestClass.Free;
  936. end;
  937. CheckEquals(472349,AValue.AsInteger);
  938. finally
  939. c.Free;
  940. end;
  941. end;
  942. procedure TTestCase1.TestPropGetValueProcString;
  943. var
  944. ATestClass : TTestValueClass;
  945. c: TRttiContext;
  946. ARttiType: TRttiType;
  947. AProperty: TRttiProperty;
  948. AValue: TValue;
  949. begin
  950. c := TRttiContext.Create;
  951. try
  952. ATestClass := TTestValueClass.Create;
  953. ATestClass.AString := 'Hello World';
  954. try
  955. ARttiType := c.GetType(ATestClass.ClassInfo);
  956. Check(assigned(ARttiType));
  957. AProperty := ARttiType.GetProperty('agetstring');
  958. AValue := AProperty.GetValue(ATestClass);
  959. CheckEquals('Hello World',AValue.AsString);
  960. finally
  961. AtestClass.Free;
  962. end;
  963. CheckEquals('Hello World',AValue.AsString);
  964. finally
  965. c.Free;
  966. end;
  967. end;
  968. procedure TTestCase1.TestTRttiTypeProperties;
  969. var
  970. c: TRttiContext;
  971. ARttiType: TRttiType;
  972. begin
  973. c := TRttiContext.Create;
  974. try
  975. ARttiType := c.GetType(TTestValueClass);
  976. Check(assigned(ARttiType));
  977. CheckEquals(ARttiType.Name,'TTestValueClass');
  978. Check(ARttiType.TypeKind=tkClass);
  979. // CheckEquals(ARttiType.IsPublicType,false);
  980. CheckEquals(ARttiType.TypeSize,SizeOf(TObject));
  981. CheckEquals(ARttiType.IsManaged,false);
  982. CheckEquals(ARttiType.BaseType.classname,'TRttiInstanceType');
  983. CheckEquals(ARttiType.IsInstance,True);
  984. CheckEquals(ARttiType.AsInstance.DeclaringUnitName,'tests.rtti');
  985. Check(ARttiType.BaseType.Name='TObject');
  986. Check(ARttiType.AsInstance.BaseType.Name='TObject');
  987. CheckEquals(ARttiType.IsOrdinal,False);
  988. CheckEquals(ARttiType.IsRecord,False);
  989. CheckEquals(ARttiType.IsSet,False);
  990. finally
  991. c.Free;
  992. end;
  993. end;
  994. procedure TTestCase1.GetTypeInteger;
  995. var
  996. LContext: TRttiContext;
  997. LType: TRttiType;
  998. begin
  999. LContext := TRttiContext.Create;
  1000. LType := LContext.GetType(TypeInfo(integer));
  1001. {$ifdef fpc}
  1002. CheckEquals(LType.Name, 'LongInt');
  1003. {$else}
  1004. CheckEquals(LType.Name, 'Integer');
  1005. {$endif}
  1006. LContext.Free;
  1007. end;
  1008. procedure TTestCase1.GetTypePointer;
  1009. var
  1010. context: TRttiContext;
  1011. t: TRttiType;
  1012. p: TRttiPointerType absolute t;
  1013. begin
  1014. context := TRttiContext.Create;
  1015. try
  1016. t := context.GetType(TypeInfo(Pointer));
  1017. Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
  1018. Assert(not Assigned(p.ReferredType), 'ReferredType of Pointer is not Nil');
  1019. t := context.GetType(TypeInfo(PLongInt));
  1020. Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
  1021. Assert(Assigned(p.ReferredType), 'ReferredType of PLongInt is Nil');
  1022. Assert(p.ReferredType = context.GetType(TypeInfo(LongInt)), 'ReferredType of PLongInt is not a LongInt');
  1023. t := context.GetType(TypeInfo(PWideChar));
  1024. Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
  1025. Assert(Assigned(p.ReferredType), 'ReferredType of PWideChar is Nil');
  1026. Assert(p.ReferredType = context.GetType(TypeInfo(WideChar)), 'ReferredType of PWideChar is not a WideChar');
  1027. finally
  1028. context.Free;
  1029. end;
  1030. end;
  1031. procedure TTestCase1.GetClassProperties;
  1032. var
  1033. LContext: TRttiContext;
  1034. LType: TRttiType;
  1035. PropList, PropList2: {$ifdef fpc}specialize{$endif} TArray<TRttiProperty>;
  1036. i: LongInt;
  1037. begin
  1038. LContext := TRttiContext.Create;
  1039. LType := LContext.GetType(TypeInfo(TGetClassProperties));
  1040. PropList := LType.GetProperties;
  1041. CheckEquals(4, length(PropList));
  1042. CheckEquals('PubPropRO', PropList[0].Name);
  1043. CheckEquals('PubPropRW', PropList[1].Name);
  1044. CheckEquals('PubPropSetRO', PropList[2].Name);
  1045. CheckEquals('PubPropSetRW', PropList[3].Name);
  1046. LType := LContext.GetType(TypeInfo(TGetClassPropertiesSub));
  1047. PropList2 := LType.GetProperties;
  1048. CheckEquals(Length(PropList), Length(PropList2));
  1049. for i := 0 to High(PropList) do
  1050. Check(PropList[i] = PropList2[i], 'Property instances are not equal');
  1051. LContext.Free;
  1052. end;
  1053. procedure TTestCase1.GetClassPropertiesValue;
  1054. var
  1055. AGetClassProperties: TGetClassProperties;
  1056. LContext: TRttiContext;
  1057. LType: TRttiType;
  1058. AValue: TValue;
  1059. begin
  1060. LContext := TRttiContext.Create;
  1061. LType := LContext.GetType(TGetClassProperties);
  1062. AGetClassProperties := TGetClassProperties.Create;
  1063. try
  1064. AGetClassProperties.PubPropRW:=12345;
  1065. AValue := LType.GetProperty('PubPropRW').GetValue(AGetClassProperties);
  1066. CheckEquals(12345, AValue.AsInteger);
  1067. finally
  1068. AGetClassProperties.Free;
  1069. end;
  1070. LContext.Free;
  1071. end;
  1072. procedure TTestCase1.TestReferenceRawData;
  1073. var
  1074. value: TValue;
  1075. str: String;
  1076. intf: IInterface;
  1077. i: LongInt;
  1078. test: TTestRecord;
  1079. arrdyn: TArrayOfLongintDyn;
  1080. arrstat: TArrayOfLongintStatic;
  1081. begin
  1082. str := 'Hello World';
  1083. UniqueString(str);
  1084. TValue.Make(@str, TypeInfo(String), value);
  1085. Check(PPointer(value.GetReferenceToRawData)^ = Pointer(str), 'Reference to string data differs');
  1086. intf := TInterfacedObject.Create;
  1087. TValue.Make(@intf, TypeInfo(IInterface), value);
  1088. Check(PPointer(value.GetReferenceToRawData)^ = Pointer(intf), 'Reference to interface data differs');
  1089. i := 42;
  1090. TValue.Make(@i, TypeInfo(LongInt), value);
  1091. Check(value.GetReferenceToRawData <> @i, 'Reference to longint is equal');
  1092. Check(PLongInt(value.GetReferenceToRawData)^ = PLongInt(@i)^, 'Reference to longint data differs');
  1093. test.value1 := 42;
  1094. test.value2 := 'Hello World';
  1095. TValue.Make(@test, TypeInfo(TTestRecord), value);
  1096. Check(value.GetReferenceToRawData <> @test, 'Reference to record is equal');
  1097. Check(PTestRecord(value.GetReferenceToRawData)^.value1 = PTestRecord(@test)^.value1, 'Reference to record data value1 differs');
  1098. Check(PTestRecord(value.GetReferenceToRawData)^.value2 = PTestRecord(@test)^.value2, 'Reference to record data value2 differs');
  1099. SetLength(arrdyn, 3);
  1100. arrdyn[0] := 42;
  1101. arrdyn[1] := 23;
  1102. arrdyn[2] := 49;
  1103. TValue.Make(@arrdyn, TypeInfo(TArrayOfLongintDyn), value);
  1104. Check(PPointer(value.GetReferenceToRawData)^ = Pointer(arrdyn), 'Reference to dynamic array data differs');
  1105. arrstat[0] := 42;
  1106. arrstat[1] := 23;
  1107. arrstat[2] := 49;
  1108. arrstat[3] := 59;
  1109. TValue.Make(@arrstat, TypeInfo(TArrayOfLongintStatic), value);
  1110. Check(value.GetReferenceToRawData <> @arrstat, 'Reference to static array is equal');
  1111. Check(PLongInt(value.GetReferenceToRawData)^ = PLongInt(@arrstat)^, 'Reference to static array data differs');
  1112. end;
  1113. procedure TTestCase1.TestReferenceRawDataEmpty;
  1114. var
  1115. value: TValue;
  1116. begin
  1117. TValue.Make(Nil, TypeInfo(String), value);
  1118. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty String is not assigned');
  1119. Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty String data is assigned');
  1120. TValue.Make(Nil, TypeInfo(IInterface), value);
  1121. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty interface is not assigned');
  1122. Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty interface data is assigned');
  1123. TValue.Make(Nil, TypeInfo(LongInt), value);
  1124. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty LongInt is not assigned');
  1125. Check(PLongInt(value.GetReferenceToRawData)^ = 0, 'Empty longint data is not 0');
  1126. TValue.Make(Nil, TypeInfo(TTestRecord), value);
  1127. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty record is not assigned');
  1128. Check(PTestRecord(value.GetReferenceToRawData)^.value1 = 0, 'Empty record data value1 is not 0');
  1129. Check(PTestRecord(value.GetReferenceToRawData)^.value2 = '', 'Empty record data value2 is not empty');
  1130. TValue.Make(Nil, TypeInfo(TArrayOfLongintDyn), value);
  1131. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty dynamic array is not assigned');
  1132. Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty dynamic array data is assigned');
  1133. TValue.Make(Nil, TypeInfo(TArrayOfLongintStatic), value);
  1134. Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty static array is not assigned');
  1135. Check(PLongInt(value.GetReferenceToRawData)^ = 0, 'Empty static array data is not 0');
  1136. end;
  1137. procedure TTestCase1.TestDataSize;
  1138. var
  1139. u8: UInt8;
  1140. u16: UInt16;
  1141. u32: UInt32;
  1142. u64: UInt64;
  1143. s8: Int8;
  1144. s16: Int16;
  1145. s32: Int32;
  1146. s64: Int64;
  1147. f32: Single;
  1148. f64: Double;
  1149. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1150. f80: Extended;
  1151. {$endif}
  1152. fco: Comp;
  1153. fcu: Currency;
  1154. ss: ShortString;
  1155. sa: AnsiString;
  1156. su: UnicodeString;
  1157. sw: WideString;
  1158. o: TObject;
  1159. c: TClass;
  1160. i: IInterface;
  1161. ad: TArrayOfLongintDyn;
  1162. _as: TArrayOfLongintStatic;
  1163. b8: Boolean;
  1164. {$ifdef fpc}
  1165. b16: Boolean16;
  1166. b32: Boolean32;
  1167. b64: Boolean64;
  1168. {$endif}
  1169. bl8: ByteBool;
  1170. bl16: WordBool;
  1171. bl32: LongBool;
  1172. {$ifdef fpc}
  1173. bl64: QWordBool;
  1174. {$endif}
  1175. e: TTestEnum;
  1176. s: TTestSet;
  1177. t: TTestRecord;
  1178. p: Pointer;
  1179. proc: TTestProc;
  1180. method: TTestMethod;
  1181. value: TValue;
  1182. begin
  1183. TValue.Make(@u8, TypeInfo(UInt8), value);
  1184. CheckEquals(1, value.DataSize, 'Size of UInt8 differs');
  1185. TValue.Make(@u16, TypeInfo(UInt16), value);
  1186. CheckEquals(2, value.DataSize, 'Size of UInt16 differs');
  1187. TValue.Make(@u32, TypeInfo(UInt32), value);
  1188. CheckEquals(4, value.DataSize, 'Size of UInt32 differs');
  1189. TValue.Make(@u64, TypeInfo(UInt64), value);
  1190. CheckEquals(8, value.DataSize, 'Size of UInt64 differs');
  1191. TValue.Make(@s8, TypeInfo(Int8), value);
  1192. CheckEquals(1, value.DataSize, 'Size of Int8 differs');
  1193. TValue.Make(@s16, TypeInfo(Int16), value);
  1194. CheckEquals(2, value.DataSize, 'Size of Int16 differs');
  1195. TValue.Make(@s32, TypeInfo(Int32), value);
  1196. CheckEquals(4, value.DataSize, 'Size of Int32 differs');
  1197. TValue.Make(@s64, TypeInfo(Int64), value);
  1198. CheckEquals(8, value.DataSize, 'Size of Int64 differs');
  1199. TValue.Make(@b8, TypeInfo(Boolean), value);
  1200. CheckEquals(1, value.DataSize, 'Size of Boolean differs');
  1201. {$ifdef fpc}
  1202. TValue.Make(@b16, TypeInfo(Boolean16), value);
  1203. CheckEquals(2, value.DataSize, 'Size of Boolean16 differs');
  1204. TValue.Make(@b32, TypeInfo(Boolean32), value);
  1205. CheckEquals(4, value.DataSize, 'Size of Boolean32 differs');
  1206. TValue.Make(@b64, TypeInfo(Boolean64), value);
  1207. CheckEquals(8, value.DataSize, 'Size of Boolean64 differs');
  1208. {$endif}
  1209. TValue.Make(@bl8, TypeInfo(ByteBool), value);
  1210. CheckEquals(1, value.DataSize, 'Size of ByteBool differs');
  1211. TValue.Make(@bl16, TypeInfo(WordBool), value);
  1212. CheckEquals(2, value.DataSize, 'Size of WordBool differs');
  1213. TValue.Make(@bl32, TypeInfo(LongBool), value);
  1214. CheckEquals(4, value.DataSize, 'Size of LongBool differs');
  1215. {$ifdef fpc}
  1216. TValue.Make(@bl64, TypeInfo(QWordBool), value);
  1217. CheckEquals(8, value.DataSize, 'Size of QWordBool differs');
  1218. {$endif}
  1219. TValue.Make(@f32, TypeInfo(Single), value);
  1220. CheckEquals(4, value.DataSize, 'Size of Single differs');
  1221. TValue.Make(@f64, TypeInfo(Double), value);
  1222. CheckEquals(8, value.DataSize, 'Size of Double differs');
  1223. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1224. TValue.Make(@f80, TypeInfo(Extended), value);
  1225. CheckEquals(10, value.DataSize, 'Size of Extended differs');
  1226. {$endif}
  1227. TValue.Make(@fcu, TypeInfo(Currency), value);
  1228. CheckEquals(SizeOf(Currency), value.DataSize, 'Size of Currency differs');
  1229. TValue.Make(@fco, TypeInfo(Comp), value);
  1230. CheckEquals(SizeOf(Comp), value.DataSize, 'Size of Comp differs');
  1231. ss := '';
  1232. TValue.Make(@ss, TypeInfo(ShortString), value);
  1233. CheckEquals(254, value.DataSize, 'Size ofShortString differs');
  1234. TValue.Make(@sa, TypeInfo(AnsiString), value);
  1235. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of AnsiString differs');
  1236. TValue.Make(@sw, TypeInfo(WideString), value);
  1237. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of WideString differs');
  1238. TValue.Make(@su, TypeInfo(UnicodeString), value);
  1239. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of UnicodeString differs');
  1240. o := TTestValueClass.Create;
  1241. TValue.Make(@o, TypeInfo(TObject), value);
  1242. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TObject differs');
  1243. o.Free;
  1244. c := TObject;
  1245. TValue.Make(@c, TypeInfo(TClass), value);
  1246. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TClass differs');
  1247. TValue.Make(@i, TypeInfo(IInterface), value);
  1248. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of IInterface differs');
  1249. TValue.Make(@t, TypeInfo(TTestRecord), value);
  1250. CheckEquals(SizeOf(TTestRecord), value.DataSize, 'Size of TTestRecord differs');
  1251. proc := Nil;
  1252. TValue.Make(@proc, TypeInfo(TTestProc), value);
  1253. CheckEquals(SizeOf(TTestProc), value.DataSize, 'Size of TTestProc differs');
  1254. method := Nil;
  1255. TValue.Make(@method, TypeInfo(TTestMethod), value);
  1256. CheckEquals(SizeOf(TTestMethod), value.DataSize, 'Size of TTestMethod differs');
  1257. TValue.Make(@_as, TypeInfo(TArrayOfLongintStatic), value);
  1258. CheckEquals(SizeOf(TArrayOfLongintStatic), value.DataSize, 'Size of TArrayOfLongintStatic differs');
  1259. TValue.Make(@ad, TypeInfo(TArrayOfLongintDyn), value);
  1260. CheckEquals(SizeOf(TArrayOfLongintDyn), value.DataSize, 'Size of TArrayOfLongintDyn differs');
  1261. TValue.Make(@e, TypeInfo(TTestEnum), value);
  1262. CheckEquals(SizeOf(TTestEnum), value.DataSize, 'Size of TTestEnum differs');
  1263. TValue.Make(@s, TypeInfo(TTestSet), value);
  1264. CheckEquals(SizeOf(TTestSet), value.DataSize, 'Size of TTestSet differs');
  1265. p := Nil;
  1266. TValue.Make(@p, TypeInfo(Pointer), value);
  1267. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
  1268. end;
  1269. procedure TTestCase1.TestDataSizeEmpty;
  1270. var
  1271. value: TValue;
  1272. begin
  1273. TValue.Make(Nil, TypeInfo(UInt8), value);
  1274. CheckEquals(1, value.DataSize, 'Size of UInt8 differs');
  1275. TValue.Make(Nil, TypeInfo(UInt16), value);
  1276. CheckEquals(2, value.DataSize, 'Size of UInt16 differs');
  1277. TValue.Make(Nil, TypeInfo(UInt32), value);
  1278. CheckEquals(4, value.DataSize, 'Size of UInt32 differs');
  1279. TValue.Make(Nil, TypeInfo(UInt64), value);
  1280. CheckEquals(8, value.DataSize, 'Size of UInt64 differs');
  1281. TValue.Make(Nil, TypeInfo(Int8), value);
  1282. CheckEquals(1, value.DataSize, 'Size of Int8 differs');
  1283. TValue.Make(Nil, TypeInfo(Int16), value);
  1284. CheckEquals(2, value.DataSize, 'Size of Int16 differs');
  1285. TValue.Make(Nil, TypeInfo(Int32), value);
  1286. CheckEquals(4, value.DataSize, 'Size of Int32 differs');
  1287. TValue.Make(Nil, TypeInfo(Int64), value);
  1288. CheckEquals(8, value.DataSize, 'Size of Int64 differs');
  1289. TValue.Make(Nil, TypeInfo(Boolean), value);
  1290. CheckEquals(1, value.DataSize, 'Size of Boolean differs');
  1291. {$ifdef fpc}
  1292. TValue.Make(Nil, TypeInfo(Boolean16), value);
  1293. CheckEquals(2, value.DataSize, 'Size of Boolean16 differs');
  1294. TValue.Make(Nil, TypeInfo(Boolean32), value);
  1295. CheckEquals(4, value.DataSize, 'Size of Boolean32 differs');
  1296. TValue.Make(Nil, TypeInfo(Boolean64), value);
  1297. CheckEquals(8, value.DataSize, 'Size of Boolean64 differs');
  1298. {$endif}
  1299. TValue.Make(Nil, TypeInfo(ByteBool), value);
  1300. CheckEquals(1, value.DataSize, 'Size of ByteBool differs');
  1301. TValue.Make(Nil, TypeInfo(WordBool), value);
  1302. CheckEquals(2, value.DataSize, 'Size of WordBool differs');
  1303. TValue.Make(Nil, TypeInfo(LongBool), value);
  1304. CheckEquals(4, value.DataSize, 'Size of LongBool differs');
  1305. {$ifdef fpc}
  1306. TValue.Make(Nil, TypeInfo(QWordBool), value);
  1307. CheckEquals(8, value.DataSize, 'Size of QWordBool differs');
  1308. {$endif}
  1309. TValue.Make(Nil, TypeInfo(Single), value);
  1310. CheckEquals(4, value.DataSize, 'Size of Single differs');
  1311. TValue.Make(Nil, TypeInfo(Double), value);
  1312. CheckEquals(8, value.DataSize, 'Size of Double differs');
  1313. {$ifdef FPC_HAS_TYPE_EXTENDED}
  1314. TValue.Make(Nil, TypeInfo(Extended), value);
  1315. CheckEquals(10, value.DataSize, 'Size of Extended differs');
  1316. {$endif}
  1317. TValue.Make(Nil, TypeInfo(Currency), value);
  1318. CheckEquals(SizeOf(Currency), value.DataSize, 'Size of Currency differs');
  1319. TValue.Make(Nil, TypeInfo(Comp), value);
  1320. CheckEquals(SizeOf(Comp), value.DataSize, 'Size of Comp differs');
  1321. TValue.Make(Nil, TypeInfo(ShortString), value);
  1322. CheckEquals(254, value.DataSize, 'Size of ShortString differs');
  1323. TValue.Make(Nil, TypeInfo(AnsiString), value);
  1324. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
  1325. TValue.Make(Nil, TypeInfo(WideString), value);
  1326. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of WideString differs');
  1327. TValue.Make(Nil, TypeInfo(UnicodeString), value);
  1328. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of UnicodeString differs');
  1329. TValue.Make(Nil, TypeInfo(TObject), value);
  1330. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TObject differs');
  1331. TValue.Make(Nil, TypeInfo(TClass), value);
  1332. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TClass differs');
  1333. TValue.Make(Nil, TypeInfo(IInterface), value);
  1334. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of IInterface differs');
  1335. TValue.Make(Nil, TypeInfo(TTestRecord), value);
  1336. CheckEquals(SizeOf(TTestRecord), value.DataSize, 'Size of TTestRecord differs');
  1337. TValue.Make(Nil, TypeInfo(TTestProc), value);
  1338. CheckEquals(SizeOf(TTestProc), value.DataSize, 'Size of TTestProc differs');
  1339. TValue.Make(Nil, TypeInfo(TTestMethod), value);
  1340. CheckEquals(SizeOf(TTestMethod), value.DataSize, 'Size of TTestMethod differs');
  1341. TValue.Make(Nil, TypeInfo(TArrayOfLongintStatic), value);
  1342. CheckEquals(SizeOf(TArrayOfLongintStatic), value.DataSize, 'Size of TArrayOfLongintStatic differs');
  1343. TValue.Make(Nil, TypeInfo(TArrayOfLongintDyn), value);
  1344. CheckEquals(SizeOf(TArrayOfLongintDyn), value.DataSize, 'Size of TArrayOfLongintDyn differs');
  1345. TValue.Make(Nil, TypeInfo(TTestEnum), value);
  1346. CheckEquals(SizeOf(TTestEnum), value.DataSize, 'Size of TTestEnum differs');
  1347. TValue.Make(Nil, TypeInfo(TTestSet), value);
  1348. CheckEquals(SizeOf(TTestSet), value.DataSize, 'Size of TTestSet differs');
  1349. TValue.Make(Nil, TypeInfo(Pointer), value);
  1350. CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
  1351. end;
  1352. procedure TTestCase1.TestIsManaged;
  1353. begin
  1354. CheckEquals(true, IsManaged(TypeInfo(ansistring)), 'IsManaged for tkAString');
  1355. CheckEquals(true, IsManaged(TypeInfo(widestring)), 'IsManaged for tkWString');
  1356. CheckEquals(true, IsManaged(TypeInfo(Variant)), 'IsManaged for tkVariant');
  1357. CheckEquals(true, IsManaged(TypeInfo(TArrayOfManagedRec)),
  1358. 'IsManaged for tkArray (with managed ElType)');
  1359. CheckEquals(true, IsManaged(TypeInfo(TArrayOfString)),
  1360. 'IsManaged for tkArray (with managed ElType)');
  1361. CheckEquals(true, IsManaged(TypeInfo(TManagedRec)), 'IsManaged for tkRecord');
  1362. {$ifdef fpc}
  1363. CheckEquals(true, IsManaged(TypeInfo(TManagedRecOp)), 'IsManaged for tkRecord');
  1364. {$endif}
  1365. CheckEquals(true, IsManaged(TypeInfo(IInterface)), 'IsManaged for tkInterface');
  1366. CheckEquals(true, IsManaged(TypeInfo(TManagedObj)), 'IsManaged for tkObject');
  1367. {$ifdef fpc}
  1368. CheckEquals(true, IsManaged(TypeInfo(specialize TArray<byte>)), 'IsManaged for tkDynArray');
  1369. {$else}
  1370. CheckEquals(true, IsManaged(TypeInfo(TArray<byte>)), 'IsManaged for tkDynArray');
  1371. {$endif}
  1372. CheckEquals(true, IsManaged(TypeInfo(unicodestring)), 'IsManaged for tkUString');
  1373. CheckEquals(false, IsManaged(TypeInfo(shortstring)), 'IsManaged for tkSString');
  1374. CheckEquals(false, IsManaged(TypeInfo(Byte)), 'IsManaged for tkInteger');
  1375. CheckEquals(false, IsManaged(TypeInfo(Char)), 'IsManaged for tkChar');
  1376. CheckEquals(false, IsManaged(TypeInfo(TTestEnum)), 'IsManaged for tkEnumeration');
  1377. CheckEquals(false, IsManaged(TypeInfo(Single)), 'IsManaged for tkFloat');
  1378. CheckEquals(false, IsManaged(TypeInfo(TTestSet)), 'IsManaged for tkSet');
  1379. {$ifdef fpc}
  1380. CheckEquals(false, IsManaged(TypeInfo(TTestMethod)), 'IsManaged for tkMethod');
  1381. {$else}
  1382. { Delphi bug (or sabotage). For some reason Delphi considers method pointers to be managed (only in newer versions, probably since XE7) :/ }
  1383. CheckEquals({$if RTLVersion>=28}true{$else}false{$endif}, IsManaged(TypeInfo(TTestMethod)), 'IsManaged for tkMethod');
  1384. {$endif}
  1385. CheckEquals(false, IsManaged(TypeInfo(TArrayOfByte)),
  1386. 'IsManaged for tkArray (with non managed ElType)');
  1387. CheckEquals(false, IsManaged(TypeInfo(TArrayOfNonManagedRec)),
  1388. 'IsManaged for tkArray (with non managed ElType)');
  1389. CheckEquals(false, IsManaged(TypeInfo(TNonManagedRec)), 'IsManaged for tkRecord');
  1390. CheckEquals(false, IsManaged(TypeInfo(TObject)), 'IsManaged for tkClass');
  1391. CheckEquals(false, IsManaged(TypeInfo(TNonManagedObj)), 'IsManaged for tkObject');
  1392. CheckEquals(false, IsManaged(TypeInfo(WideChar)), 'IsManaged for tkWChar');
  1393. CheckEquals(false, IsManaged(TypeInfo(Boolean)), 'IsManaged for tkBool');
  1394. CheckEquals(false, IsManaged(TypeInfo(Int64)), 'IsManaged for tkInt64');
  1395. CheckEquals(false, IsManaged(TypeInfo(UInt64)), 'IsManaged for tkQWord');
  1396. {$ifdef fpc}
  1397. CheckEquals(false, IsManaged(TypeInfo(ICORBATest)), 'IsManaged for tkInterfaceRaw');
  1398. {$endif}
  1399. CheckEquals(false, IsManaged(TypeInfo(TTestProc)), 'IsManaged for tkProcVar');
  1400. CheckEquals(false, IsManaged(TypeInfo(TTestHelper)), 'IsManaged for tkHelper');
  1401. {$ifdef fpc}
  1402. CheckEquals(false, IsManaged(TypeInfo(file)), 'IsManaged for tkFile');
  1403. {$endif}
  1404. CheckEquals(false, IsManaged(TypeInfo(TClass)), 'IsManaged for tkClassRef');
  1405. CheckEquals(false, IsManaged(TypeInfo(Pointer)), 'IsManaged for tkPointer');
  1406. CheckEquals(false, IsManaged(nil), 'IsManaged for nil');
  1407. end;
  1408. {$ifdef fpc}
  1409. procedure TTestCase1.TestOpenArrayToDyn;
  1410. procedure OpenArrayProc(aArr: array of LongInt);
  1411. var
  1412. value: TValue;
  1413. begin
  1414. {$ifndef InLazIDE}
  1415. value := specialize OpenArrayToDynArrayValue<LongInt>(aArr);
  1416. {$endif}
  1417. CheckEquals(value.IsArray, True);
  1418. CheckEquals(value.IsOpenArray, False);
  1419. CheckEquals(value.IsObject, False);
  1420. CheckEquals(value.IsOrdinal, False);
  1421. CheckEquals(value.IsClass, False);
  1422. CheckEquals(value.GetArrayLength, 2);
  1423. CheckEquals(value.GetArrayElement(0).AsInteger, 42);
  1424. CheckEquals(value.GetArrayElement(1).AsInteger, 84);
  1425. value.SetArrayElement(0, 21);
  1426. { since this is a copy the original array is not modified! }
  1427. CheckEquals(aArr[0], 42);
  1428. end;
  1429. begin
  1430. OpenArrayProc([42, 84]);
  1431. end;
  1432. {$endif}
  1433. procedure TTestCase1.TestInterface;
  1434. var
  1435. context: TRttiContext;
  1436. t: TRttiType;
  1437. ti1, ti2: TRttiInterfaceType;
  1438. methods: {$ifdef fpc}specialize{$endif} TArray<TRttiMethod>;
  1439. params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
  1440. method: TRttiMethod;
  1441. param: TRttiParameter;
  1442. flag: TParamFlag;
  1443. begin
  1444. context := TRttiContext.Create;
  1445. try
  1446. t := context.GetType(TypeInfo(IInterface));
  1447. Check(t is TRttiInterfaceType, 'Type is not an interface type');
  1448. Check(not Assigned(t.BaseType), 'Base type is assigned');
  1449. ti1 := TRttiInterfaceType(t);
  1450. Check(not Assigned(ti1.BaseType), 'Base type is assigned');
  1451. methods := t.GetMethods;
  1452. CheckEquals(0, Length(methods), 'Overall method count does not match');
  1453. methods := t.GetDeclaredMethods;
  1454. CheckEquals(0, Length(methods), 'Declared method conut does not match');
  1455. t := context.GetType(TypeInfo(ITestInterface));
  1456. Check(t is TRttiInterfaceType, 'Type is not an interface type');
  1457. Check(Assigned(t.BaseType), 'Base type is not assigned');
  1458. Check(t.BaseType = TRttiType(ti1), 'Base type does not match');
  1459. ti2 := TRttiInterfaceType(t);
  1460. Check(Assigned(ti2.BaseType), 'Base type is not assigned');
  1461. Check(ti2.BaseType = ti1, 'Base type does not match');
  1462. methods := t.GetMethods;
  1463. CheckEquals(4, Length(methods), 'Overall method count does not match');
  1464. methods := t.GetDeclaredMethods;
  1465. CheckEquals(4, Length(methods), 'Declared method count does not match');
  1466. method := methods[0];
  1467. CheckEquals(method.Name, 'Test', 'Method name of Test does not match');
  1468. Check(method.CallingConvention = ccReg, 'Calling convention of Test does not match');
  1469. Check(method.MethodKind = mkProcedure, 'Method kind of Test does not match');
  1470. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test does not match');
  1471. Check(not Assigned(method.CodeAddress), 'Code address of Test is not Nil');
  1472. CheckEquals(method.VirtualIndex, 3, 'Virtual index of Test does not match');
  1473. Check(not Assigned(method.ReturnType), 'Return type of Test is not Nil');
  1474. params := method.GetParameters;
  1475. CheckEquals(0, Length(params), 'Parameter count of Test does not match');
  1476. method := methods[1];
  1477. CheckEquals(method.Name, 'Test2', 'Method name of Test2 does not match');
  1478. Check(method.CallingConvention = ccReg, 'Calling convention of Test2 does not match');
  1479. Check(method.MethodKind = mkFunction, 'Method kind of Test2 does not match');
  1480. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test2 does not match');
  1481. Check(not Assigned(method.CodeAddress), 'Code address of Test2 is not Nil');
  1482. CheckEquals(method.VirtualIndex, 4, 'Virtual index of Test2 does not match');
  1483. Check(Assigned(method.ReturnType), 'Return type of Test2 is Nil');
  1484. Check(method.ReturnType.TypeKind = tkInteger, 'Return type of Test2 is not an ordinal');
  1485. params := method.GetParameters;
  1486. CheckEquals(0, Length(params), 'Parameter count of Test2 does not match');
  1487. method := methods[2];
  1488. CheckEquals(method.Name, 'Test3', 'Method name of Test3 does not match');
  1489. Check(method.CallingConvention = ccReg, 'Calling convention of Test3 does not match');
  1490. Check(method.MethodKind = mkProcedure, 'Method kind of Test3 does not match');
  1491. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test3 does not match');
  1492. Check(not Assigned(method.CodeAddress), 'Code address of Test3 is not Nil');
  1493. CheckEquals(method.VirtualIndex, 5, 'Virtual index of Test3 does not match');
  1494. Check(not Assigned(method.ReturnType), 'Return type of Test3 is not Nil');
  1495. params := method.GetParameters;
  1496. CheckEquals(4, Length(params), 'Parameter count of Test3 does not match');
  1497. param := params[0];
  1498. CheckEquals(param.Name, 'aArg1', 'Parameter name of Test3.aArg1 does not match');
  1499. Check(param.Flags = [], 'Parameter flags of Test3.aArg1 do not match');
  1500. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg1 is Nil');
  1501. Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test3.aArg1 is not an ordinal');
  1502. param := params[1];
  1503. CheckEquals(param.Name, 'aArg2', 'Parameter name of Test3.aArg2 does not match');
  1504. Check(param.Flags = [pfConst], 'Parameter flags of Test3.aArg2 do not match');
  1505. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg2 is Nil');
  1506. Check(param.ParamType.TypeKind = tkAnsiString, 'Parameter type of Test3.aArg2 is not a string');
  1507. param := params[2];
  1508. CheckEquals(param.Name, 'aArg3', 'Parameter name of Test3.aArg3 does not match');
  1509. Check(param.Flags = [pfVar], 'Parameter flags of Test3.aArg3 do not match');
  1510. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg3 is Nil');
  1511. Check(param.ParamType.TypeKind = {$ifdef fpc}tkBool{$else}tkEnumeration{$endif}, 'Parameter type of Test3.aArg3 is not a boolean');
  1512. param := params[3];
  1513. CheckEquals(param.Name, 'aArg4', 'Parameter name of Test3.aArg4 does not match');
  1514. Check(param.Flags = [pfOut], 'Parameter flags of Test3.aArg4 do not match');
  1515. Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg4 is Nil');
  1516. Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test3.aArg4 is not a string');
  1517. method := methods[3];
  1518. CheckEquals(method.Name, 'Test4', 'Method name of Test4 does not match');
  1519. Check(method.CallingConvention = ccReg, 'Calling convention of Test4 does not match');
  1520. Check(method.MethodKind = mkFunction, 'Method kind of Test4 does not match');
  1521. Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test4 does not match');
  1522. Check(not Assigned(method.CodeAddress), 'Code address of Test4 is not Nil');
  1523. CheckEquals(method.VirtualIndex, 6, 'Virtual index of Test4 does not match');
  1524. Check(Assigned(method.ReturnType), 'Return type of Test4 is not Nil');
  1525. Check(method.ReturnType.TypeKind = tkAnsiString, 'Return type of Test4 is not a string');
  1526. params := method.GetParameters;
  1527. CheckEquals(2, Length(params), 'Parameter count of Test4 does not match');
  1528. param := params[0];
  1529. CheckEquals(param.Name, 'aArg1', 'Parameter name of Test4.aArg1 does not match');
  1530. Check(param.Flags = [pfArray, pfReference], 'Parameter flags of Test4.aArg1 do not match');
  1531. Check(Assigned(param.ParamType), 'Parameter type of Test4.aArg1 is Nil');
  1532. Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test4.aArg1 is not an ordinal');
  1533. param := params[1];
  1534. CheckEquals(param.Name, 'aArg2', 'Parameter name of Test4.aArg2 does not match');
  1535. Check(param.Flags = [pfArray, pfReference], 'Parameter flags of Test4.aArg2 do not match');
  1536. Check(Assigned(param.ParamType), 'Parameter type of Test4.aArg2 is Nil');
  1537. Check(param.ParamType.TypeKind = tkRecord, 'Parameter type of Test4.aArg2 is not a record');
  1538. finally
  1539. context.Free;
  1540. end;
  1541. end;
  1542. {$ifdef fpc}
  1543. procedure TTestCase1.TestInterfaceRaw;
  1544. var
  1545. context: TRttiContext;
  1546. t: TRttiType;
  1547. ti: TRttiInterfaceType;
  1548. begin
  1549. context := TRttiContext.Create;
  1550. try
  1551. t := context.GetType(TypeInfo(ICORBATest));
  1552. Check(t is TRttiInterfaceType, 'Type is not a raw interface type');
  1553. Check(not Assigned(t.BaseType), 'Base type is assigned');
  1554. ti := TRttiInterfaceType(t);
  1555. Check(not Assigned(ti.BaseType), 'Base type is assigned');
  1556. finally
  1557. context.Free;
  1558. end;
  1559. end;
  1560. procedure TTestCase1.TestProcVar;
  1561. var
  1562. context: TRttiContext;
  1563. t: TRttiType;
  1564. p: TRttiProcedureType;
  1565. params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
  1566. begin
  1567. context := TRttiContext.Create;
  1568. try
  1569. t := context.GetType(PTypeInfo(TypeInfo(TTestProc)));
  1570. Check(Assigned(t), 'Rtti Type is Nil');
  1571. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1572. Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
  1573. p := t as TRttiProcedureType;
  1574. Check(p.CallingConvention = ccReg, 'Calling convention does not match');
  1575. Check(not Assigned(p.ReturnType), 'Return type is assigned');
  1576. CheckEquals(0, Length(p.GetParameters), 'Procedure variable has parameters');
  1577. t := context.GetType(PTypeInfo(TypeInfo(TTestFunc1)));
  1578. Check(Assigned(t), 'Rtti Type is Nil');
  1579. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1580. Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
  1581. p := t as TRttiProcedureType;
  1582. Check(p.CallingConvention = ccReg, 'Calling convention does not match');
  1583. Check(Assigned(p.ReturnType), 'Return type is not assigned');
  1584. //Check(p.ReturnType is TRttiOrdinalType, 'Return type is not an ordinal type');
  1585. CheckEquals(0, Length(p.GetParameters), 'Procedure variable has parameters');
  1586. t := context.GetType(PTypeInfo(TypeInfo(TTestFunc2)));
  1587. Check(Assigned(t), 'Rtti Type is Nil');
  1588. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1589. Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
  1590. p := t as TRttiProcedureType;
  1591. Check(p.CallingConvention = ccReg, 'Calling convention does not match');
  1592. Check(Assigned(p.ReturnType), 'Return type is not assigned');
  1593. Check(p.ReturnType is TRttiStringType, 'Return type is not a string type');
  1594. params := p.GetParameters;
  1595. CheckEquals(2, Length(params), 'Procedure variable has incorrect amount of parameters');
  1596. Check(params[0].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 1 is not an ordinal type');
  1597. //Check(params[0].ParamType is TRttiOrdinalType, 'Parameter 1 is not an ordinal type');
  1598. Check(pfArray in params[1].Flags, 'Parameter 2 is not an array');
  1599. Check(params[1].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 2 is not an ordinal array');
  1600. finally
  1601. context.Free;
  1602. end;
  1603. end;
  1604. procedure TTestCase1.TestMethod;
  1605. var
  1606. context: TRttiContext;
  1607. t: TRttiType;
  1608. m: TRttiMethodType;
  1609. params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
  1610. begin
  1611. context := TRttiContext.Create;
  1612. try
  1613. t := context.GetType(PTypeInfo(TypeInfo(TTestMethod)));
  1614. Check(Assigned(t), 'Rtti Type is Nil');
  1615. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1616. Check(t is TRttiMethodType, 'Rtti Type is not a method type');
  1617. m := t as TRttiMethodType;
  1618. Check(m.CallingConvention = ccReg, 'Calling convention does not match');
  1619. Check(not Assigned(m.ReturnType), 'Return type is assigned');
  1620. CheckEquals(0, Length(m.GetParameters), 'Method variable has parameters');
  1621. t := context.GetType(PTypeInfo(TypeInfo(TTestMethod1)));
  1622. Check(Assigned(t), 'Rtti Type is Nil');
  1623. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1624. Check(t is TRttiMethodType, 'Rtti Type is not a method type');
  1625. m := t as TRttiMethodType;
  1626. Check(m.CallingConvention = ccReg, 'Calling convention does not match');
  1627. Check(Assigned(m.ReturnType), 'Return type is not assigned');
  1628. //Check(p.ReturnType is TRttiOrdinalType, 'Return type is not an ordinal type');
  1629. CheckEquals(0, Length(m.GetParameters), 'Method variable has parameters');
  1630. t := context.GetType(PTypeInfo(TypeInfo(TTestMethod2)));
  1631. Check(Assigned(t), 'Rtti Type is Nil');
  1632. Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
  1633. Check(t is TRttiMethodType, 'Rtti Type is not a method type');
  1634. m := t as TRttiMethodType;
  1635. Check(m.CallingConvention = ccReg, 'Calling convention does not match');
  1636. Check(Assigned(m.ReturnType), 'Return type is not assigned');
  1637. Check(m.ReturnType is TRttiStringType, 'Return type is not a string type');
  1638. params := m.GetParameters;
  1639. CheckEquals(2, Length(params), 'Method variable has incorrect amount of parameters');
  1640. Check(params[0].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 1 is not an ordinal type');
  1641. //Check(params[0].ParamType is TRttiOrdinalType, 'Parameter 1 is not an ordinal type');
  1642. Check(pfArray in params[1].Flags, 'Parameter 2 is not an array');
  1643. Check(params[1].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 2 is not an ordinal array');
  1644. finally
  1645. context.Free;
  1646. end;
  1647. end;
  1648. {$endif}
  1649. initialization
  1650. {$ifdef fpc}
  1651. RegisterTest(TTestCase1);
  1652. {$else fpc}
  1653. RegisterTest(TTestCase1.Suite);
  1654. {$endif fpc}
  1655. end.