tests.rtti.pas 63 KB

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