2
0

tests.rtti.pas 88 KB

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