1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941 |
- unit tests.rtti;
- {$ifdef fpc}
- {$mode objfpc}{$H+}
- {$modeswitch advancedrecords}
- {$endif}
- interface
- uses
- {$IFDEF FPC}
- fpcunit,testregistry, testutils,
- {$ELSE FPC}
- TestFramework,
- {$ENDIF FPC}
- Classes, SysUtils, typinfo,
- Rtti;
- type
- { TTestCase1 }
- TTestCase1= class(TTestCase)
- published
- //procedure GetTypes;
- procedure GetTypeInteger;
- procedure GetTypePointer;
- procedure GetClassProperties;
- procedure GetClassPropertiesValue;
- procedure TestTRttiTypeProperties;
- procedure TestPropGetValueString;
- procedure TestPropGetValueInteger;
- procedure TestPropGetValueBoolean;
- procedure TestPropGetValueShortString;
- procedure TestPropGetValueProcString;
- procedure TestPropGetValueProcInteger;
- procedure TestPropGetValueProcBoolean;
- procedure TestPropGetValueProcShortString;
- procedure TestPropGetValueObject;
- procedure TestPropGetValueInterface;
- procedure TestPropGetValueFloat;
- procedure TestPropGetValueDynArray;
- procedure TestPropGetValueEnumeration;
- procedure TestPropGetValueChars;
- procedure TestPropSetValueString;
- procedure TestPropSetValueInteger;
- procedure TestPropSetValueBoolean;
- procedure TestPropSetValueShortString;
- procedure TestPropSetValueObject;
- procedure TestPropSetValueInterface;
- procedure TestPropSetValueFloat;
- procedure TestPropSetValueDynArray;
- procedure TestPropSetValueEnumeration;
- procedure TestPropSetValueChars;
- procedure TestGetValueStringCastError;
- procedure TestGetIsReadable;
- procedure TestIsWritable;
- procedure TestIsType;
- procedure TestMakeNil;
- procedure TestMakeObject;
- procedure TestMakeArrayDynamic;
- procedure TestMakeArrayStatic;
- {$ifdef fpc}
- procedure TestMakeArrayOpen;
- {$endif}
- procedure TestMakeSingle;
- procedure TestMakeDouble;
- procedure TestMakeExtended;
- procedure TestMakeCurrency;
- procedure TestMakeComp;
- procedure TestMakeEnum;
- procedure TestMakeAnsiChar;
- procedure TestMakeWideChar;
- procedure TestMakeNativeInt;
- procedure TestMakeFromArray;
- procedure TestMakeGenericNil;
- procedure TestMakeGenericLongInt;
- procedure TestMakeGenericString;
- procedure TestMakeGenericObject;
- procedure TestMakeGenericDouble;
- procedure TestMakeGenericAnsiChar;
- procedure TestMakeGenericWideChar;
- procedure TestFromOrdinal;
- procedure TestDataSize;
- procedure TestDataSizeEmpty;
- procedure TestReferenceRawData;
- procedure TestReferenceRawDataEmpty;
- procedure TestIsManaged;
- {$ifdef fpc}
- procedure TestOpenArrayToDyn;
- {$endif}
- procedure TestInterface;
- {$ifdef fpc}
- procedure TestInterfaceRaw;
- {$endif}
- procedure TestArray;
- procedure TestDynArray;
- procedure TestProcVar;
- procedure TestMethod;
- procedure TestRawThunk;
- private
- procedure MakeFromOrdinalTObject;
- procedure MakeFromOrdinalSet;
- procedure MakeFromOrdinalString;
- procedure MakeFromOrdinalNil;
- {$ifndef fpc}
- procedure Ignore(const aMsg: String);
- {$endif}
- end;
- implementation
- uses
- Tests.Rtti.Util;
- type
- {$M+}
- TGetClassProperties = class
- private
- FPubPropRO: integer;
- FPubPropRW: integer;
- published
- property PubPropRO: integer read FPubPropRO;
- property PubPropRW: integer read FPubPropRW write FPubPropRW;
- property PubPropSetRO: integer read FPubPropRO;
- property PubPropSetRW: integer read FPubPropRW write FPubPropRW;
- end;
- TGetClassPropertiesSub = class(TGetClassProperties)
- end;
- TTestDynArray = array of Integer;
- TTestEnumeration = (en1, en2, en3, en4);
- {$M-}
- { TTestValueClass }
- {$M+}
- TTestValueClass = class
- private
- FAArray: TTestDynArray;
- FAChar: AnsiChar;
- FAComp: Comp;
- FACurrency: Currency;
- FADouble: Double;
- FAEnumeration: TTestEnumeration;
- FAExtended: Extended;
- FAInteger: integer;
- FAObject: TObject;
- FASingle: Single;
- FAString: string;
- FABoolean: boolean;
- FAShortString: ShortString;
- FAUnknown: IUnknown;
- FAWideChar: WideChar;
- function GetAInteger: integer;
- function GetAString: string;
- function GetABoolean: boolean;
- function GetAShortString: ShortString;
- procedure SetWriteOnly(AValue: integer);
- published
- property AArray: TTestDynArray read FAArray write FAArray;
- property AEnumeration: TTestEnumeration read FAEnumeration write FAEnumeration;
- property AInteger: Integer read FAInteger write FAInteger;
- property AString: string read FAString write FAString;
- property ASingle: Single read FASingle write FASingle;
- property ADouble: Double read FADouble write FADouble;
- property AExtended: Extended read FAExtended write FAExtended;
- property ACurrency: Currency read FACurrency write FACurrency;
- property AObject: TObject read FAObject write FAObject;
- property AUnknown: IUnknown read FAUnknown write FAUnknown;
- property AComp: Comp read FAComp write FAComp;
- property ABoolean: boolean read FABoolean write FABoolean;
- property AShortString: ShortString read FAShortString write FAShortString;
- property AGetInteger: Integer read GetAInteger;
- property AGetString: string read GetAString;
- property AGetBoolean: boolean read GetABoolean;
- property AGetShortString: ShortString read GetAShortString;
- property AWriteOnly: integer write SetWriteOnly;
- property AChar: AnsiChar read FAChar write FAChar;
- property AWideChar: WideChar read FAWideChar write FAWideChar;
- end;
- {$M-}
- {$M+}
- ITestInterface = interface
- procedure Test;
- function Test2: LongInt;
- procedure Test3(aArg1: LongInt; const aArg2: AnsiString; var aArg3: Boolean; out aArg4: Word);
- function Test4(aArg1: array of LongInt; aArg2: array of const): AnsiString;
- end;
- {$M-}
- TManagedRec = record
- s: string;
- end;
- {$ifdef fpc}
- TManagedRecOp = record
- class operator AddRef(var a: TManagedRecOp);
- end;
- {$endif}
- TNonManagedRec = record
- i: Integer;
- end;
- TManagedObj = object
- i: IInterface;
- end;
- TNonManagedObj = object
- d: double;
- end;
- TTestEnum = (te1, te2, te3, te4, te5);
- TTestSet = set of TTestEnum;
- TTestProc = procedure;
- TTestFunc1 = function: LongInt;
- TTestFunc2 = function(aArg1: LongInt; aArg2: array of LongInt): String;
- TTestMethod = procedure of object;
- TTestMethod1 = function: LongInt of object;
- TTestMethod2 = function(aArg1: LongInt; aArg2: array of LongInt): String of object;
- TTestHelper = class helper for TObject
- end;
- TArrayOfString = array[0..0] of string;
- TArrayOfManagedRec = array[0..0] of TManagedRec;
- TArrayOfNonManagedRec = array[0..0] of TNonManagedRec;
- TArrayOfByte = array[0..0] of byte;
- TArrayOfLongintDyn = array of LongInt;
- TArrayOfLongintStatic = array[0..3] of LongInt;
- TArrayOfLongint2DStatic = array[0..3, 2..4] of LongInt;
- TTestRecord = record
- Value1: LongInt;
- Value2: String;
- end;
- PTestRecord = ^TTestRecord;
- {$ifdef fpc}
- {$PUSH}
- {$INTERFACES CORBA}
- ICORBATest = interface
- end;
- {$POP}
- {$endif}
- {$ifdef fpc}
- class operator TManagedRecOp.AddRef(var a: TManagedRecOp);
- begin
- end;
- {$endif}
- { TTestValueClass }
- function TTestValueClass.GetAInteger: integer;
- begin
- result := FAInteger;
- end;
- function TTestValueClass.GetAString: string;
- begin
- result := FAString;
- end;
- function TTestValueClass.GetABoolean: boolean;
- begin
- result := FABoolean;
- end;
- function TTestValueClass.GetAShortString: ShortString;
- begin
- Result := FAShortString;
- end;
- procedure TTestValueClass.SetWriteOnly(AValue: integer);
- begin
- // Do nothing
- end;
- { Note: GetTypes currently only returns those types that had been acquired using
- GetType, so GetTypes itself can't be really tested currently }
- (*procedure TTestCase1.GetTypes;
- var
- LContext: TRttiContext;
- LType: TRttiType;
- IsTestCaseClassFound: boolean;
- begin
- LContext := TRttiContext.Create;
- { Enumerate all types declared in the application }
- for LType in LContext.GetTypes() do
- begin
- if LType.Name='TTestCase1' then
- IsTestCaseClassFound:=true;
- end;
- LContext.Free;
- CheckTrue(IsTestCaseClassFound, 'RTTI information does not contain class of testcase.');
- end;*)
- {$ifndef fpc}
- procedure TTestCase1.Ignore(const aMsg: string);
- begin
- { empty }
- end;
- {$endif}
- procedure TTestCase1.TestGetValueStringCastError;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AValue: TValue;
- i: integer;
- HadException: boolean;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AString := '12';
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- AValue := ARttiType.GetProperty('astring').GetValue(ATestClass);
- HadException := false;
- try
- i := AValue.AsInteger;
- except
- on E: Exception do
- if E.ClassType=EInvalidCast then
- HadException := true;
- end;
- Check(HadException, 'No or invalid exception on invalid cast');
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestMakeNil;
- var
- value: TValue;
- begin
- TValue.Make(Nil, Nil, value);
- CheckTrue(value.Kind = tkUnknown);
- CheckTrue(value.IsEmpty);
- CheckTrue(value.IsObject);
- CheckTrue(value.IsClass);
- CheckTrue(value.IsOrdinal);
- CheckFalse(value.IsArray);
- CheckTrue(value.AsObject = Nil);
- CheckTrue(value.AsClass = Nil);
- CheckTrue(value.AsInterface = Nil);
- CheckEquals(0, value.AsOrdinal);
- TValue.Make(Nil, TypeInfo(TObject), value);
- CheckTrue(value.IsEmpty);
- CheckTrue(value.IsObject);
- CheckTrue(value.IsClass);
- CheckTrue(value.IsOrdinal);
- CheckFalse(value.IsArray);
- CheckTrue(value.AsObject=Nil);
- CheckTrue(value.AsClass=Nil);
- CheckTrue(value.AsInterface=Nil);
- CheckEquals(0, value.AsOrdinal);
- TValue.Make(Nil, TypeInfo(TClass), value);
- CheckTrue(value.IsEmpty);
- CheckTrue(value.IsClass);
- CheckTrue(value.IsOrdinal);
- CheckFalse(value.IsArray);
- CheckTrue(value.AsObject=Nil);
- CheckTrue(value.AsClass=Nil);
- CheckTrue(value.AsInterface=Nil);
- CheckEquals(0, value.AsOrdinal);
- TValue.Make(Nil, TypeInfo(LongInt), value);
- CheckTrue(value.IsOrdinal);
- CheckFalse(value.IsEmpty);
- CheckFalse(value.IsClass);
- CheckFalse(value.IsObject);
- CheckFalse(value.IsArray);
- CheckEquals(0, value.AsOrdinal);
- CheckEquals(0, value.AsInteger);
- CheckEquals(0, value.AsInt64);
- CheckEquals(0, value.AsUInt64);
- TValue.Make(Nil, TypeInfo(String), value);
- CheckFalse(value.IsEmpty);
- CheckFalse(value.IsObject);
- CheckFalse(value.IsClass);
- CheckFalse(value.IsArray);
- CheckEquals('', value.AsString);
- end;
- procedure TTestCase1.TestMakeObject;
- var
- AValue: TValue;
- ATestClass: TTestValueClass;
- begin
- ATestClass := TTestValueClass.Create;
- ATestClass.AInteger := 54329;
- TValue.Make(@ATestClass, TypeInfo(TTestValueClass),AValue);
- CheckEquals(AValue.IsClass, False);
- CheckEquals(AValue.IsObject, True);
- Check(AValue.AsObject=ATestClass);
- Check(PPointer(AValue.GetReferenceToRawData)^ = Pointer(ATestClass));
- CheckEquals(TTestValueClass(AValue.AsObject).AInteger, 54329);
- ATestClass.Free;
- end;
- procedure TTestCase1.TestMakeArrayDynamic;
- var
- arr: TArrayOfLongintDyn;
- value: TValue;
- begin
- SetLength(arr, 2);
- arr[0] := 42;
- arr[1] := 21;
- TValue.Make(@arr, TypeInfo(TArrayOfLongintDyn), value);
- CheckEquals(value.IsArray, True);
- CheckEquals(value.IsObject, False);
- CheckEquals(value.IsOrdinal, False);
- CheckEquals(value.IsClass, False);
- CheckEquals(value.GetArrayLength, 2);
- CheckEquals(value.GetArrayElement(0).AsInteger, 42);
- CheckEquals(value.GetArrayElement(1).AsInteger, 21);
- Check(PPointer(value.GetReferenceToRawData)^ = Pointer(arr));
- value.SetArrayElement(0, 84);
- CheckEquals(arr[0], 84);
- end;
- procedure TTestCase1.TestMakeArrayStatic;
- type
- TArrStat = array[0..1] of LongInt;
- TArrStat2D = array[0..1, 0..1] of LongInt;
- var
- arr: TArrStat;
- arr2D: TArrStat2D;
- value: TValue;
- begin
- arr[0] := 42;
- arr[1] := 21;
- TValue.Make(@arr, TypeInfo(TArrStat), value);
- CheckEquals(value.IsArray, True);
- CheckEquals(value.IsObject, False);
- CheckEquals(value.IsOrdinal, False);
- CheckEquals(value.IsClass, False);
- CheckEquals(value.GetArrayLength, 2);
- CheckEquals(value.GetArrayElement(0).AsInteger, 42);
- CheckEquals(value.GetArrayElement(1).AsInteger, 21);
- value.SetArrayElement(0, 84);
- { since this is a static array the original array isn't touched! }
- CheckEquals(arr[0], 42);
- arr2D[0, 0] := 42;
- arr2D[0, 1] := 21;
- arr2D[1, 0] := 84;
- arr2D[1, 1] := 63;
- TValue.Make(@arr2D, TypeInfo(TArrStat2D), value);
- CheckEquals(value.IsArray, True);
- CheckEquals(value.GetArrayLength, 4);
- CheckEquals(value.GetArrayElement(0).AsInteger, 42);
- CheckEquals(value.GetArrayElement(1).AsInteger, 21);
- CheckEquals(value.GetArrayElement(2).AsInteger, 84);
- CheckEquals(value.GetArrayElement(3).AsInteger, 63);
- end;
- {$ifdef fpc}
- procedure TTestCase1.TestMakeArrayOpen;
- procedure TestOpenArrayValueCopy(aArr: array of LongInt);
- var
- value: TValue;
- begin
- TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
- CheckEquals(value.IsArray, True);
- CheckEquals(value.IsOpenArray, True);
- CheckEquals(value.IsObject, False);
- CheckEquals(value.IsOrdinal, False);
- CheckEquals(value.IsClass, False);
- CheckEquals(value.GetArrayLength, 2);
- CheckEquals(value.GetArrayElement(0).AsInteger, 42);
- CheckEquals(value.GetArrayElement(1).AsInteger, 21);
- value.SetArrayElement(0, 84);
- { since this is an open array the original array is modified! }
- CheckEquals(aArr[0], 84);
- end;
- procedure TestOpenArrayValueVar(var aArr: array of LongInt);
- var
- value: TValue;
- begin
- TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
- CheckEquals(value.IsArray, True);
- CheckEquals(value.IsOpenArray, True);
- CheckEquals(value.IsObject, False);
- CheckEquals(value.IsOrdinal, False);
- CheckEquals(value.IsClass, False);
- CheckEquals(value.GetArrayLength, 2);
- CheckEquals(value.GetArrayElement(0).AsInteger, 42);
- CheckEquals(value.GetArrayElement(1).AsInteger, 21);
- value.SetArrayElement(0, 84);
- { since this is an open array the original array is modified! }
- CheckEquals(aArr[0], 84);
- end;
- procedure TestOpenArrayValueOut(var aArr: array of LongInt);
- var
- value: TValue;
- begin
- TValue.MakeOpenArray(@aArr[0], Length(aArr), PTypeInfo(TypeInfo(aArr)), value);
- CheckEquals(value.IsArray, True);
- CheckEquals(value.IsOpenArray, True);
- CheckEquals(value.IsObject, False);
- CheckEquals(value.IsOrdinal, False);
- CheckEquals(value.IsClass, False);
- CheckEquals(value.GetArrayLength, 2);
- CheckEquals(value.GetArrayElement(0).AsInteger, 42);
- CheckEquals(value.GetArrayElement(1).AsInteger, 21);
- value.SetArrayElement(0, 84);
- value.SetArrayElement(1, 128);
- { since this is an open array the original array is modified! }
- CheckEquals(aArr[0], 84);
- CheckEquals(aArr[1], 128);
- CheckEquals(value.GetArrayElement(0).AsInteger, 84);
- CheckEquals(value.GetArrayElement(1).AsInteger, 128);
- end;
- var
- arr: array of LongInt;
- begin
- TestOpenArrayValueCopy([42, 21]);
- arr := [42, 21];
- TestOpenArrayValueVar(arr);
- CheckEquals(arr[0], 84);
- CheckEquals(arr[1], 21);
- arr := [42, 21];
- TestOpenArrayValueOut(arr);
- CheckEquals(arr[0], 84);
- CheckEquals(arr[1], 128);
- end;
- {$endif}
- procedure TTestCase1.TestMakeSingle;
- var
- fs: Single;
- v: TValue;
- hadexcept: Boolean;
- begin
- fs := 3.14;
- TValue.Make(@fs, TypeInfo(Single), v);
- CheckEquals(v.IsClass, False);
- CheckEquals(v.IsObject, False);
- CheckEquals(v.IsOrdinal, False);
- Check(v.AsExtended=fs);
- Check(v.GetReferenceToRawData <> @fs);
- try
- hadexcept := False;
- v.AsInt64;
- except
- hadexcept := True;
- end;
- CheckTrue(hadexcept, 'No signed type conversion exception');
- try
- hadexcept := False;
- v.AsUInt64;
- except
- hadexcept := True;
- end;
- CheckTrue(hadexcept, 'No unsigned type conversion exception');
- end;
- procedure TTestCase1.TestMakeDouble;
- var
- fd: Double;
- v: TValue;
- hadexcept: Boolean;
- begin
- fd := 3.14;
- TValue.Make(@fd, TypeInfo(Double), v);
- CheckEquals(v.IsClass, False);
- CheckEquals(v.IsObject, False);
- CheckEquals(v.IsOrdinal, False);
- Check(v.AsExtended=fd);
- Check(v.GetReferenceToRawData <> @fd);
- try
- hadexcept := False;
- v.AsInt64;
- except
- hadexcept := True;
- end;
- CheckTrue(hadexcept, 'No signed type conversion exception');
- try
- hadexcept := False;
- v.AsUInt64;
- except
- hadexcept := True;
- end;
- CheckTrue(hadexcept, 'No unsigned type conversion exception');
- end;
- procedure TTestCase1.TestMakeExtended;
- var
- fe: Extended;
- v: TValue;
- hadexcept: Boolean;
- begin
- fe := 3.14;
- TValue.Make(@fe, TypeInfo(Extended), v);
- CheckEquals(v.IsClass, False);
- CheckEquals(v.IsObject, False);
- CheckEquals(v.IsOrdinal, False);
- Check(v.AsExtended=fe);
- Check(v.GetReferenceToRawData <> @fe);
- try
- hadexcept := False;
- v.AsInt64;
- except
- hadexcept := True;
- end;
- CheckTrue(hadexcept, 'No signed type conversion exception');
- try
- hadexcept := False;
- v.AsUInt64;
- except
- hadexcept := True;
- end;
- CheckTrue(hadexcept, 'No unsigned type conversion exception');
- end;
- procedure TTestCase1.TestMakeCurrency;
- var
- fcu: Currency;
- v: TValue;
- hadexcept: Boolean;
- begin
- fcu := 3.14;
- TValue.Make(@fcu, TypeInfo(Currency), v);
- CheckEquals(v.IsClass, False);
- CheckEquals(v.IsObject, False);
- CheckEquals(v.IsOrdinal, False);
- Check(v.AsExtended=Extended(fcu));
- Check(v.AsCurrency=fcu);
- Check(v.GetReferenceToRawData <> @fcu);
- try
- hadexcept := False;
- v.AsInt64;
- except
- hadexcept := True;
- end;
- CheckTrue(hadexcept, 'No signed type conversion exception');
- try
- hadexcept := False;
- v.AsUInt64;
- except
- hadexcept := True;
- end;
- CheckTrue(hadexcept, 'No unsigned type conversion exception');
- end;
- procedure TTestCase1.TestMakeComp;
- var
- fco: Comp;
- v: TValue;
- hadexcept: Boolean;
- begin
- fco := 314;
- TValue.Make(@fco, TypeInfo(Comp), v);
- if v.Kind <> tkFloat then
- Exit;
- CheckEquals(v.IsClass, False);
- CheckEquals(v.IsObject, False);
- CheckEquals(v.IsOrdinal, False);
- Check(v.AsExtended=Extended(fco));
- Check(v.GetReferenceToRawData <> @fco);
- try
- hadexcept := False;
- CheckEquals(v.AsInt64, 314);
- except
- hadexcept := True;
- end;
- CheckFalse(hadexcept, 'Had signed type conversion exception');
- try
- hadexcept := False;
- CheckEquals(v.AsUInt64, 314);
- except
- hadexcept := True;
- end;
- CheckFalse(hadexcept, 'Had unsigned type conversion exception');
- end;
- procedure TTestCase1.TestMakeEnum;
- var
- e: TTestEnum;
- v: TValue;
- begin
- e := te1;
- TValue.Make(@e, TypeInfo(TTestEnum), v);
- Check(not v.IsClass);
- Check(not v.IsArray);
- Check(not v.IsEmpty);
- {$ifdef fpc}
- Check(not v.IsOpenArray);
- {$endif}
- Check(not v.IsObject);
- Check(v.IsOrdinal);
- Check(v.GetReferenceToRawData <> @e);
- Check(TTestEnum(v.AsOrdinal) = te1);
- end;
- procedure TTestCase1.TestMakeAnsiChar;
- var
- c: AnsiChar;
- v: TValue;
- begin
- c := #20;
- TValue.Make(@c, TypeInfo(AnsiChar), v);
- Check(not v.IsClass);
- Check(not v.IsArray);
- Check(not v.IsEmpty);
- {$ifdef fpc}
- Check(not v.IsOpenArray);
- {$endif}
- Check(not v.IsObject);
- Check(v.IsOrdinal);
- Check(v.GetReferenceToRawData <> @c);
- Check(AnsiChar(v.AsOrdinal) = #20);
- Check(v.AsAnsiChar = #20);
- end;
- procedure TTestCase1.TestMakeWideChar;
- var
- c: WideChar;
- v: TValue;
- begin
- c := #$1234;
- TValue.Make(@c, TypeInfo(WideChar), v);
- Check(not v.IsClass);
- Check(not v.IsArray);
- Check(not v.IsEmpty);
- {$ifdef fpc}
- Check(not v.IsOpenArray);
- {$endif}
- Check(not v.IsObject);
- Check(v.IsOrdinal);
- Check(v.GetReferenceToRawData <> @c);
- Check(WideChar(v.AsOrdinal) = #$1234);
- Check(v.AsWideChar = #$1234);
- end;
- procedure TTestCase1.TestMakeNativeInt;
- var
- fni: NativeInt;
- s: AnsiString;
- v: TValue;
- o: TObject;
- begin
- fni := 2021;
- TValue.Make(fni, TypeInfo(LongInt), v);
- CheckEquals(v.IsClass, False);
- CheckEquals(v.IsObject, False);
- CheckEquals(v.IsOrdinal, True);
- Check(NativeInt(v.GetReferenceToRawData) <> fni);
- CheckEquals(v.AsOrdinal, 2021);
- s := 'Hello World';
- TValue.Make(NativeInt(s), TypeInfo(AnsiString), v);
- CheckEquals(v.IsClass, False);
- CheckEquals(v.IsObject, False);
- CheckEquals(v.IsOrdinal, False);
- CheckEquals(v.AsString, s);
- o := TObject.Create;
- TValue.Make(NativeInt(o), TypeInfo(TObject), v);
- CheckEquals(v.IsClass, False);
- CheckEquals(v.IsObject, True);
- CheckEquals(v.IsOrdinal, False);
- Check(PPointer(v.GetReferenceToRawData)^ = Pointer(o));
- Check(v.AsObject = o);
- o.Free;
- end;
- procedure TTestCase1.TestMakeFromArray;
- var
- arr, subarr: array of TValue;
- v, varr: TValue;
- ti: PTypeInfo;
- i: LongInt;
- begin
- SetLength(arr, 3 * 4);
- for i := 0 to High(arr) do
- TValue.{$ifdef fpc}specialize{$endif} Make<LongInt>(i + 1, arr[i]);
- ti := PTypeInfo(TypeInfo(LongInt));
- v := TValue.FromArray(TypeInfo(TArrayOfLongintDyn), arr);
- Check(not v.IsEmpty, 'Array is empty');
- Check(v.IsArray, 'Value is not an array');
- CheckEquals(Length(arr), v.GetArrayLength, 'Array length does not match');
- for i := 0 to High(arr) do begin
- varr := v.GetArrayElement(i);
- Check(varr.TypeInfo = ti, 'Type info of array element does not match');
- Check(varr.IsOrdinal, 'Array element is not an ordinal');
- Check(varr.AsInteger = arr[i].AsInteger, 'Value of array element does not match');
- end;
- subarr := Copy(arr, 0, 4);
- v := TValue.FromArray(TypeInfo(TArrayOfLongintStatic), subarr);
- Check(not v.IsEmpty, 'Array is empty');
- Check(v.IsArray, 'Value is not an array');
- CheckEquals(Length(subarr), v.GetArrayLength, 'Array length does not match');
- for i := 0 to High(subarr) do begin
- varr := v.GetArrayElement(i);
- Check(varr.TypeInfo = ti, 'Type info of array element does not match');
- Check(varr.IsOrdinal, 'Array element is not an ordinal');
- Check(varr.AsInteger = subarr[i].AsInteger, 'Value of array element does not match');
- end;
- v := TValue.FromArray(TypeInfo(TArrayOfLongint2DStatic), arr);
- Check(not v.IsEmpty, 'Array is empty');
- Check(v.IsArray, 'Value is not an array');
- CheckEquals(Length(arr), v.GetArrayLength, 'Array length does not match');
- for i := 0 to High(arr) do begin
- varr := v.GetArrayElement(i);
- Check(varr.TypeInfo = ti, 'Type info of array element does not match');
- Check(varr.IsOrdinal, 'Array element is not an ordinal');
- Check(varr.AsInteger = arr[i].AsInteger, 'Value of array element does not match');
- end;
- end;
- procedure TTestCase1.TestMakeGenericNil;
- var
- value: TValue;
- begin
- TValue.{$ifdef fpc}specialize{$endif} Make<TObject>(Nil, value);
- CheckTrue(value.IsEmpty);
- CheckTrue(value.IsObject);
- CheckTrue(value.IsClass);
- CheckTrue(value.IsOrdinal);
- CheckFalse(value.IsArray);
- CheckTrue(value.AsObject=Nil);
- CheckTrue(value.AsClass=Nil);
- CheckTrue(value.AsInterface=Nil);
- CheckEquals(0, value.AsOrdinal);
- TValue.{$ifdef fpc}specialize{$endif} Make<TClass>(Nil, value);
- CheckTrue(value.IsEmpty);
- CheckTrue(value.IsClass);
- CheckTrue(value.IsOrdinal);
- CheckFalse(value.IsArray);
- CheckTrue(value.AsObject=Nil);
- CheckTrue(value.AsClass=Nil);
- CheckTrue(value.AsInterface=Nil);
- CheckEquals(0, value.AsOrdinal);
- end;
- procedure TTestCase1.TestMakeGenericLongInt;
- var
- value: TValue;
- begin
- TValue.{$ifdef fpc}specialize{$endif} Make<LongInt>(0, value);
- CheckTrue(value.IsOrdinal);
- CheckFalse(value.IsEmpty);
- CheckFalse(value.IsClass);
- CheckFalse(value.IsObject);
- CheckFalse(value.IsArray);
- CheckEquals(0, value.AsOrdinal);
- CheckEquals(0, value.AsInteger);
- CheckEquals(0, value.AsInt64);
- CheckEquals(0, value.AsUInt64);
- end;
- procedure TTestCase1.TestMakeGenericString;
- var
- value: TValue;
- begin
- TValue.{$ifdef fpc}specialize{$endif} Make<String>('test', value);
- CheckFalse(value.IsEmpty);
- CheckFalse(value.IsObject);
- CheckFalse(value.IsClass);
- CheckFalse(value.IsArray);
- CheckEquals('test', value.AsString);
- end;
- procedure TTestCase1.TestMakeGenericObject;
- var
- value: TValue;
- TestClass: TTestValueClass;
- begin
- TestClass := TTestValueClass.Create;
- TestClass.AInteger := 54329;
- TValue.{$ifdef fpc}specialize{$endif} Make<TTestValueClass>(TestClass, value);
- CheckEquals(value.IsClass, False);
- CheckEquals(value.IsObject, True);
- Check(value.AsObject=TestClass);
- Check(PPointer(value.GetReferenceToRawData)^ = Pointer(TestClass));
- CheckEquals(TTestValueClass(value.AsObject).AInteger, 54329);
- TestClass.Free;
- end;
- procedure TTestCase1.TestMakeGenericDouble;
- var
- fd: Double;
- v: TValue;
- hadexcept: Boolean;
- begin
- fd := 3.14;
- TValue.{$ifdef fpc}specialize{$endif} Make<Double>(fd, v);
- CheckEquals(v.IsClass, False);
- CheckEquals(v.IsObject, False);
- CheckEquals(v.IsOrdinal, False);
- Check(v.AsExtended=fd);
- Check(v.GetReferenceToRawData <> @fd);
- try
- hadexcept := False;
- v.AsInt64;
- except
- hadexcept := True;
- end;
- CheckTrue(hadexcept, 'No signed type conversion exception');
- try
- hadexcept := False;
- v.AsUInt64;
- except
- hadexcept := True;
- end;
- CheckTrue(hadexcept, 'No unsigned type conversion exception');
- end;
- procedure TTestCase1.TestMakeGenericAnsiChar;
- var
- c: AnsiChar;
- v: TValue;
- begin
- c := #20;
- TValue.{$ifdef fpc}specialize{$endif} Make<AnsiChar>(c, v);
- Check(not v.IsClass);
- Check(not v.IsArray);
- Check(not v.IsEmpty);
- {$ifdef fpc}
- Check(not v.IsOpenArray);
- {$endif}
- Check(not v.IsObject);
- Check(v.IsOrdinal);
- Check(v.GetReferenceToRawData <> @c);
- Check(AnsiChar(v.AsOrdinal) = #20);
- Check(v.AsAnsiChar = #20);
- end;
- procedure TTestCase1.TestMakeGenericWideChar;
- var
- c: WideChar;
- v: TValue;
- begin
- c := #$1234;
- TValue.{$ifdef fpc}specialize{$endif} Make<WideChar>(c, v);
- Check(not v.IsClass);
- Check(not v.IsArray);
- Check(not v.IsEmpty);
- {$ifdef fpc}
- Check(not v.IsOpenArray);
- {$endif}
- Check(not v.IsObject);
- Check(v.IsOrdinal);
- Check(v.GetReferenceToRawData <> @c);
- Check(WideChar(v.AsOrdinal) = #$1234);
- Check(v.AsWideChar = #$1234);
- end;
- procedure TTestCase1.MakeFromOrdinalTObject;
- begin
- TValue.FromOrdinal(TypeInfo(TObject), 42);
- end;
- procedure TTestCase1.MakeFromOrdinalSet;
- begin
- TValue.FromOrdinal(TypeInfo(TTestSet), 42);
- end;
- procedure TTestCase1.MakeFromOrdinalString;
- begin
- TValue.FromOrdinal(TypeInfo(AnsiString), 42);
- end;
- procedure TTestCase1.MakeFromOrdinalNil;
- begin
- TValue.FromOrdinal(Nil, 42);
- end;
- procedure TTestCase1.TestFromOrdinal;
- var
- v: TValue;
- begin
- v := TValue.FromOrdinal(TypeInfo(LongInt), 42);
- Check(v.IsOrdinal);
- CheckEquals(v.AsOrdinal, 42);
- v := TValue.FromOrdinal(TypeInfo(Boolean), Ord(True));
- Check(v.IsOrdinal);
- CheckEquals(v.AsOrdinal, Ord(True));
- v := TValue.FromOrdinal(TypeInfo(Int64), $1234123412341234);
- Check(v.IsOrdinal);
- CheckEquals(v.AsOrdinal, $1234123412341234);
- v := TValue.FromOrdinal(TypeInfo(QWord), $1234123412341234);
- Check(v.IsOrdinal);
- CheckEquals(v.AsOrdinal, $1234123412341234);
- v := TValue.FromOrdinal(TypeInfo(LongBool), Ord(True));
- Check(v.IsOrdinal);
- CheckEquals(v.AsOrdinal, Ord(True));
- v := TValue.FromOrdinal(TypeInfo(TTestEnum), Ord(te1));
- Check(v.IsOrdinal);
- CheckEquals(v.AsOrdinal, Ord(te1));
- v := TValue.FromOrdinal(TypeInfo(AnsiChar), Ord(#20));
- Check(v.IsOrdinal);
- CheckEquals(v.AsOrdinal, Ord(#20));
- v := TValue.FromOrdinal(TypeInfo(WideChar), Ord(#$1234));
- Check(v.IsOrdinal);
- CheckEquals(v.AsOrdinal, Ord(#$1234));
- CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalNil, EInvalidCast);
- CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalTObject, EInvalidCast);
- CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalSet, EInvalidCast);
- CheckException({$ifdef fpc}@{$endif}MakeFromOrdinalString, EInvalidCast);
- end;
- procedure TTestCase1.TestGetIsReadable;
- var
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- begin
- c := TRttiContext.Create;
- try
- ARttiType := c.GetType(TTestValueClass);
- AProperty := ARttiType.GetProperty('aBoolean');
- CheckEquals(AProperty.IsReadable, true);
- AProperty := ARttiType.GetProperty('aGetBoolean');
- CheckEquals(AProperty.IsReadable, true);
- AProperty := ARttiType.GetProperty('aWriteOnly');
- CheckEquals(AProperty.IsReadable, False);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestIsWritable;
- var
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- begin
- c := TRttiContext.Create;
- try
- ARttiType := c.GetType(TTestValueClass);
- AProperty := ARttiType.GetProperty('aBoolean');
- CheckEquals(AProperty.IsWritable, true);
- AProperty := ARttiType.GetProperty('aGetBoolean');
- CheckEquals(AProperty.IsWritable, false);
- AProperty := ARttiType.GetProperty('aWriteOnly');
- CheckEquals(AProperty.IsWritable, True);
- finally
- c.Free;
- end;
- end;
- type
- TMyLongInt = type LongInt;
- procedure TTestCase1.TestIsType;
- { Delphi does not provide type information for local types :/ }
- {type
- TMyLongInt = type LongInt;}
- var
- v: TValue;
- l: LongInt;
- ml: TMyLongInt;
- begin
- l := 42;
- ml := 42;
- TValue.Make(@l, TypeInfo(LongInt), v);
- Check(v.IsType(TypeInfo(LongInt)));
- Check(not v.IsType(TypeInfo(TMyLongInt)));
- Check(not v.IsType(TypeInfo(String)));
- Check(v.{$ifdef fpc}specialize{$endif} IsType<LongInt>);
- Check(not v.{$ifdef fpc}specialize{$endif} IsType<TMyLongInt>);
- Check(not v.{$ifdef fpc}specialize{$endif} IsType<String>);
- TValue.Make(@ml, TypeInfo(TMyLongInt), v);
- Check(v.IsType(TypeInfo(TMyLongInt)));
- Check(not v.IsType(TypeInfo(LongInt)));
- Check(not v.IsType(TypeInfo(String)));
- Check(v.{$ifdef fpc}specialize{$endif} IsType<TMyLongInt>);
- Check(not v.{$ifdef fpc}specialize{$endif} IsType<LongInt>);
- Check(not v.{$ifdef fpc}specialize{$endif} IsType<String>);
- end;
- procedure TTestCase1.TestPropGetValueBoolean;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.ABoolean := true;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('aBoolean');
- AValue := AProperty.GetValue(ATestClass);
- CheckEquals(true,AValue.AsBoolean);
- ATestClass.ABoolean := false;
- CheckEquals(true, AValue.AsBoolean);
- CheckEquals('True', AValue.ToString);
- CheckEquals(True, AValue.IsOrdinal);
- CheckEquals(1, AValue.AsOrdinal);
- finally
- AtestClass.Free;
- end;
- CheckEquals(True,AValue.AsBoolean);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueShortString;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AShortString := 'Hello World';
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('aShortString');
- AValue := AProperty.GetValue(ATestClass);
- CheckEquals('Hello World',AValue.AsString);
- ATestClass.AShortString := 'Foobar';
- CheckEquals('Hello World', AValue.AsString);
- CheckEquals(False, AValue.IsOrdinal);
- CheckEquals(False, AValue.IsObject);
- CheckEquals(False, AValue.IsArray);
- CheckEquals(False, AValue.IsClass);
- finally
- AtestClass.Free;
- end;
- CheckEquals('Hello World',AValue.AsString);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueInteger;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AInteger := 472349;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('ainteger');
- AValue := AProperty.GetValue(ATestClass);
- CheckEquals(472349,AValue.AsInteger);
- ATestClass.AInteger := 12;
- CheckEquals(472349, AValue.AsInteger);
- CheckEquals('472349', AValue.ToString);
- CheckEquals(True, AValue.IsOrdinal);
- finally
- AtestClass.Free;
- end;
- CheckEquals(472349,AValue.AsInteger);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueString;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- i: int64;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AString := 'Hello World';
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('astring');
- AValue := AProperty.GetValue(ATestClass);
- CheckEquals('Hello World',AValue.AsString);
- ATestClass.AString := 'Goodbye World';
- CheckEquals('Hello World',AValue.AsString);
- CheckEquals('Hello World',AValue.ToString);
- Check(TypeInfo(string)=AValue.TypeInfo);
- Check(AValue.TypeData=GetTypeData(AValue.TypeInfo));
- Check(AValue.IsEmpty=false);
- Check(AValue.IsObject=false);
- Check(AValue.IsClass=false);
- CheckEquals(AValue.IsOrdinal, false);
- CheckEquals(AValue.TryAsOrdinal(i), false);
- CheckEquals(AValue.IsType(TypeInfo(string)), true);
- CheckEquals(AValue.IsType(TypeInfo(integer)), false);
- CheckEquals(AValue.IsArray, false);
- finally
- AtestClass.Free;
- end;
- CheckEquals('Hello World',AValue.AsString);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueProcBoolean;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.ABoolean := true;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('aGetBoolean');
- AValue := AProperty.GetValue(ATestClass);
- CheckEquals(true,AValue.AsBoolean);
- finally
- AtestClass.Free;
- end;
- CheckEquals(True,AValue.AsBoolean);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueProcShortString;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AShortString := 'Hello World';
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('aGetShortString');
- AValue := AProperty.GetValue(ATestClass);
- CheckEquals('Hello World',AValue.AsString);
- finally
- AtestClass.Free;
- end;
- CheckEquals('Hello World',AValue.AsString);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueObject;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- O: TObject;
- begin
- c := TRttiContext.Create;
- O := TObject.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AObject := O;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('AObject');
- AValue := AProperty.GetValue(ATestClass);
- CheckEquals(O.GetHashCode, AValue.AsObject.GetHashCode);
- finally
- AtestClass.Free;
- end;
- CheckEquals(O.GetHashCode, AValue.AsObject.GetHashCode);
- finally
- c.Free;
- O.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueInterface;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- i: IInterface;
- begin
- c := TRttiContext.Create;
- i := TInterfacedObject.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AUnknown := i;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('AUnknown');
- AValue := AProperty.GetValue(ATestClass);
- Check(i = AValue.AsInterface);
- finally
- AtestClass.Free;
- end;
- Check(i = AValue.AsInterface);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueFloat;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValueS, AValueD, AValueE, AValueC, AValueCm: TValue;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.ASingle := 1.1;
- ATestClass.ADouble := 2.2;
- ATestClass.AExtended := 3.3;
- ATestClass.ACurrency := 4;
- ATestClass.AComp := 5;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('ASingle');
- AValueS := AProperty.GetValue(ATestClass);
- CheckEquals(1.1, AValueS.AsExtended, 0.001);
- AProperty := ARttiType.GetProperty('ADouble');
- AValueD := AProperty.GetValue(ATestClass);
- CheckEquals(2.2, AValueD.AsExtended, 0.001);
- AProperty := ARttiType.GetProperty('AExtended');
- AValueE := AProperty.GetValue(ATestClass);
- CheckEquals(3.3, AValueE.AsExtended, 0.001);
- AProperty := ARttiType.GetProperty('ACurrency');
- AValueC := AProperty.GetValue(ATestClass);
- CheckEquals(4.0, AValueC.AsExtended, 0.001);
- AProperty := ARttiType.GetProperty('AComp');
- AValueCm := AProperty.GetValue(ATestClass);
- CheckEquals(5.0, AValueCm.AsExtended, 0.001);
- finally
- AtestClass.Free;
- end;
- CheckEquals(1.1, AValueS.AsExtended, 0.001);
- CheckEquals(2.2, AValueD.AsExtended, 0.001);
- CheckEquals(3.3, AValueE.AsExtended, 0.001);
- CheckEquals(4.0, AValueC.AsExtended, 0.001);
- CheckEquals(5.0, AValueCm.AsExtended, 0.001);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueDynArray;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- A: TTestDynArray;
- begin
- c := TRttiContext.Create;
- A := [1, 2, 3, 4];
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AArray := A;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('AArray');
- AValue := AProperty.GetValue(ATestClass);
- CheckEquals(A[0], AValue.GetArrayElement(0).AsInteger);
- CheckEquals(A[1], AValue.GetArrayElement(1).AsInteger);
- CheckEquals(A[2], AValue.GetArrayElement(2).AsInteger);
- CheckEquals(A[3], AValue.GetArrayElement(3).AsInteger);
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueEnumeration;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AEnumeration := en3;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('AEnumeration');
- AValue := AProperty.GetValue(ATestClass);
- CheckEquals(Ord(en3),AValue.AsOrdinal);
- ATestClass.AEnumeration := en1;
- CheckEquals(Ord(en3), AValue.AsOrdinal);
- CheckEquals('en3', AValue.ToString);
- CheckEquals(True, AValue.IsOrdinal);
- finally
- AtestClass.Free;
- end;
- CheckEquals(Ord(en3),AValue.AsOrdinal);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueChars;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValueC, AValueW: TValue;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AChar := 'C';
- ATestClass.AWideChar := 'W';
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('AChar');
- AValueC := AProperty.GetValue(ATestClass);
- CheckEquals('C',AValueC.AsAnsiChar);
- ATestClass.AChar := 'N';
- CheckEquals('C', AValueC.AsAnsiChar);
- CheckEquals('C', AValueC.ToString);
- CheckEquals(True, AValueC.IsOrdinal);
- AProperty := ARttiType.GetProperty('AWideChar');
- AValueW := AProperty.GetValue(ATestClass);
- CheckEquals('W',AValueW.AsWideChar);
- ATestClass.AWideChar := 'Z';
- CheckEquals('W', AValueW.AsWideChar);
- CheckEquals('W', AValueW.ToString);
- CheckEquals(True, AValueW.IsOrdinal);
- finally
- AtestClass.Free;
- end;
- CheckEquals('C',AValueC.AsAnsiChar);
- CheckEquals('W',AValueW.AsWideChar);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropSetValueString;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- s: string;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- AProperty := ARttiType.GetProperty('astring');
- s := 'ipse lorem or something like that';
- TValue.Make(@s, TypeInfo(string), AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(ATestClass.AString, s);
- s := 'Another string';
- CheckEquals(ATestClass.AString, 'ipse lorem or something like that');
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropSetValueInteger;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- i: integer;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- AProperty := ARttiType.GetProperty('aInteger');
- i := -43573;
- TValue.Make(@i, TypeInfo(Integer), AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(ATestClass.AInteger, i);
- i := 1;
- CheckEquals(ATestClass.AInteger, -43573);
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropSetValueBoolean;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- b: boolean;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- AProperty := ARttiType.GetProperty('aboolean');
- b := true;
- TValue.Make(@b, TypeInfo(Boolean), AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(ATestClass.ABoolean, b);
- b := false;
- CheckEquals(ATestClass.ABoolean, true);
- TValue.Make(@b, TypeInfo(Boolean), AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(ATestClass.ABoolean, false);
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropSetValueShortString;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- s: string;
- ss: ShortString;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- AProperty := ARttiType.GetProperty('aShortString');
- s := 'ipse lorem or something like that';
- TValue.Make(@s, TypeInfo(String), AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(ATestClass.AShortString, s);
- s := 'Another string';
- CheckEquals(ATestClass.AShortString, 'ipse lorem or something like that');
- ss := 'Hello World';
- TValue.Make(@ss, TypeInfo(ShortString), AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(ATestClass.AShortString, ss);
- ss := 'Foobar';
- CheckEquals(ATestClass.AShortString, 'Hello World');
- AProperty.SetValue(ATestClass, 'Another string');
- CheckEquals(ATestClass.AShortString, 'Another string');
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropSetValueObject;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- O: TObject;
- TypeInfo: PTypeInfo;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- AProperty := ARttiType.GetProperty('AObject');
- TypeInfo := GetPropInfo(ATestClass, 'AObject')^.PropType{$ifndef fpc}^{$endif};
- O := TPersistent.Create;
- TValue.Make(@O, TypeInfo, AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(ATestClass.AObject.GetHashCode, O.GetHashCode);
- O.Free;
- O := TPersistent.Create;
- AProperty.SetValue(ATestClass, O);
- CheckEquals(ATestClass.AObject.GetHashCode, O.GetHashCode);
- O.Free;
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropSetValueInterface;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- TypeInfo: PTypeInfo;
- i: IInterface;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- AProperty := ARttiType.GetProperty('AUnknown');
- TypeInfo := GetPropInfo(ATestClass, 'AUnknown')^.PropType{$ifndef fpc}^{$endif};
- i := TInterfacedObject.Create;
- TValue.Make(@i, TypeInfo, AValue);
- AProperty.SetValue(ATestClass, AValue);
- Check(ATestClass.AUnknown = i);
- {$ifdef fpc}
- { Delphi does not provide an implicit assignment overload for IUnknown }
- i := TInterfacedObject.Create;
- AProperty.SetValue(ATestClass, i);
- Check(ATestClass.AUnknown = i);
- {$endif}
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropSetValueFloat;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- TypeInfo: PTypeInfo;
- S: Single;
- D: Double;
- E: Extended;
- Cur: Currency;
- Cmp: Comp;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- AProperty := ARttiType.GetProperty('ASingle');
- TypeInfo := GetPropInfo(ATestClass, 'ASingle')^.PropType{$ifndef fpc}^{$endif};
- S := 1.1;
- TValue.Make(@S, TypeInfo, AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(S, ATestClass.ASingle, 0.001);
- S := 1.2;
- AProperty.SetValue(ATestClass, S);
- CheckEquals(S, ATestClass.ASingle, 0.001);
- AProperty := ARttiType.GetProperty('ADouble');
- TypeInfo := GetPropInfo(ATestClass, 'ADouble')^.PropType{$ifndef fpc}^{$endif};
- D := 2.1;
- TValue.Make(@D, TypeInfo, AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(D, ATestClass.ADouble, 0.001);
- D := 2.2;
- AProperty.SetValue(ATestClass, D);
- CheckEquals(D, ATestClass.ADouble, 0.001);
- AProperty := ARttiType.GetProperty('AExtended');
- TypeInfo := GetPropInfo(ATestClass, 'AExtended')^.PropType{$ifndef fpc}^{$endif};
- E := 3.1;
- TValue.Make(@E, TypeInfo, AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(E, ATestClass.AExtended, 0.001);
- E := 3.2;
- AProperty.SetValue(ATestClass, E);
- CheckEquals(E, ATestClass.AExtended, 0.001);
- AProperty := ARttiType.GetProperty('ACurrency');
- TypeInfo := GetPropInfo(ATestClass, 'ACurrency')^.PropType{$ifndef fpc}^{$endif};
- Cur := 40;
- TValue.Make(@Cur, TypeInfo, AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(Cur, ATestClass.ACurrency, 0.001);
- Cur := 41;
- AProperty.SetValue(ATestClass, Cur);
- CheckEquals(Cur, ATestClass.ACurrency, 0.001);
- AProperty := ARttiType.GetProperty('AComp');
- TypeInfo := GetPropInfo(ATestClass, 'AComp')^.PropType{$ifndef fpc}^{$endif};
- Cmp := 50;
- TValue.Make(@Cmp, TypeInfo, AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(Cmp, ATestClass.AComp, 0.001);
- Cmp := 51;
- AProperty.SetValue(ATestClass, Cmp);
- CheckEquals(Cmp, ATestClass.AComp, 0.001);
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropSetValueDynArray;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- A: TTestDynArray;
- TypeInfo: PTypeInfo;
- i: Integer;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- AProperty := ARttiType.GetProperty('AArray');
- TypeInfo := GetPropInfo(ATestClass, 'AArray')^.PropType{$ifndef fpc}^{$endif};
- A := [1, 2, 3, 4, 5];
- TValue.Make(@A, TypeInfo, AValue);
- AProperty.SetValue(ATestClass, AValue);
- for i := 0 to High(A) do
- CheckEquals(A[i], ATestClass.AArray[i]);
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropSetValueEnumeration;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- E: TTestEnumeration;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- AProperty := ARttiType.GetProperty('AEnumeration');
- E := en2;
- TValue.Make(@E, TypeInfo(TTestEnumeration), AValue);
- AProperty.SetValue(ATestClass, AValue);
- CheckEquals(Ord(E), Ord(ATestClass.AEnumeration));
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropSetValueChars;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValueC, AValueW: TValue;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AChar := 'C';
- ATestClass.AWideChar := 'W';
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('AChar');
- AValueC := AProperty.GetValue(ATestClass);
- CheckEquals('C', AValueC.AsAnsiChar);
- AProperty := ARttiType.GetProperty('AWideChar');
- AValueW := AProperty.GetValue(ATestClass);
- CheckEquals('W', AValueW.AsWideChar);
- finally
- AtestClass.Free;
- end;
- CheckEquals('C', AValueC.AsAnsiChar);
- CheckEquals('W', AValueW.AsWideChar);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueProcInteger;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AInteger := 472349;
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('agetinteger');
- AValue := AProperty.GetValue(ATestClass);
- CheckEquals(472349,AValue.AsInteger);
- finally
- AtestClass.Free;
- end;
- CheckEquals(472349,AValue.AsInteger);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestPropGetValueProcString;
- var
- ATestClass : TTestValueClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- begin
- c := TRttiContext.Create;
- try
- ATestClass := TTestValueClass.Create;
- ATestClass.AString := 'Hello World';
- try
- ARttiType := c.GetType(ATestClass.ClassInfo);
- Check(assigned(ARttiType));
- AProperty := ARttiType.GetProperty('agetstring');
- AValue := AProperty.GetValue(ATestClass);
- CheckEquals('Hello World',AValue.AsString);
- finally
- AtestClass.Free;
- end;
- CheckEquals('Hello World',AValue.AsString);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.TestTRttiTypeProperties;
- var
- c: TRttiContext;
- ARttiType: TRttiType;
- begin
- c := TRttiContext.Create;
- try
- ARttiType := c.GetType(TTestValueClass);
- Check(assigned(ARttiType));
- CheckEquals(ARttiType.Name,'TTestValueClass');
- Check(ARttiType.TypeKind=tkClass);
- // CheckEquals(ARttiType.IsPublicType,false);
- CheckEquals(ARttiType.TypeSize,SizeOf(TObject));
- CheckEquals(ARttiType.IsManaged,false);
- CheckEquals(ARttiType.BaseType.classname,'TRttiInstanceType');
- CheckEquals(ARttiType.IsInstance,True);
- CheckEquals(ARttiType.AsInstance.DeclaringUnitName,'tests.rtti');
- Check(ARttiType.BaseType.Name='TObject');
- Check(ARttiType.AsInstance.BaseType.Name='TObject');
- CheckEquals(ARttiType.IsOrdinal,False);
- CheckEquals(ARttiType.IsRecord,False);
- CheckEquals(ARttiType.IsSet,False);
- finally
- c.Free;
- end;
- end;
- procedure TTestCase1.GetTypeInteger;
- var
- LContext: TRttiContext;
- LType: TRttiType;
- begin
- LContext := TRttiContext.Create;
- LType := LContext.GetType(TypeInfo(integer));
- {$ifdef fpc}
- CheckEquals(LType.Name, 'LongInt');
- {$else}
- CheckEquals(LType.Name, 'Integer');
- {$endif}
- LContext.Free;
- end;
- procedure TTestCase1.GetTypePointer;
- var
- context: TRttiContext;
- t: TRttiType;
- p: TRttiPointerType absolute t;
- begin
- context := TRttiContext.Create;
- try
- t := context.GetType(TypeInfo(Pointer));
- Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
- Assert(not Assigned(p.ReferredType), 'ReferredType of Pointer is not Nil');
- t := context.GetType(TypeInfo(PLongInt));
- Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
- Assert(Assigned(p.ReferredType), 'ReferredType of PLongInt is Nil');
- Assert(p.ReferredType = context.GetType(TypeInfo(LongInt)), 'ReferredType of PLongInt is not a LongInt');
- t := context.GetType(TypeInfo(PWideChar));
- Assert(t is TRttiPointerType, 'Type of Pointer is not a TRttiPointerType');
- Assert(Assigned(p.ReferredType), 'ReferredType of PWideChar is Nil');
- Assert(p.ReferredType = context.GetType(TypeInfo(WideChar)), 'ReferredType of PWideChar is not a WideChar');
- finally
- context.Free;
- end;
- end;
- procedure TTestCase1.GetClassProperties;
- var
- LContext: TRttiContext;
- LType: TRttiType;
- PropList, PropList2: {$ifdef fpc}specialize{$endif} TArray<TRttiProperty>;
- i: LongInt;
- begin
- LContext := TRttiContext.Create;
- LType := LContext.GetType(TypeInfo(TGetClassProperties));
- PropList := LType.GetProperties;
- CheckEquals(4, length(PropList));
- CheckEquals('PubPropRO', PropList[0].Name);
- CheckEquals('PubPropRW', PropList[1].Name);
- CheckEquals('PubPropSetRO', PropList[2].Name);
- CheckEquals('PubPropSetRW', PropList[3].Name);
- LType := LContext.GetType(TypeInfo(TGetClassPropertiesSub));
- PropList2 := LType.GetProperties;
- CheckEquals(Length(PropList), Length(PropList2));
- for i := 0 to High(PropList) do
- Check(PropList[i] = PropList2[i], 'Property instances are not equal');
- LContext.Free;
- end;
- procedure TTestCase1.GetClassPropertiesValue;
- var
- AGetClassProperties: TGetClassProperties;
- LContext: TRttiContext;
- LType: TRttiType;
- AValue: TValue;
- begin
- LContext := TRttiContext.Create;
- LType := LContext.GetType(TGetClassProperties);
- AGetClassProperties := TGetClassProperties.Create;
- try
- AGetClassProperties.PubPropRW:=12345;
- AValue := LType.GetProperty('PubPropRW').GetValue(AGetClassProperties);
- CheckEquals(12345, AValue.AsInteger);
- finally
- AGetClassProperties.Free;
- end;
- LContext.Free;
- end;
- procedure TTestCase1.TestReferenceRawData;
- var
- value: TValue;
- str: String;
- intf: IInterface;
- i: LongInt;
- test: TTestRecord;
- arrdyn: TArrayOfLongintDyn;
- arrstat: TArrayOfLongintStatic;
- begin
- str := 'Hello World';
- UniqueString(str);
- TValue.Make(@str, TypeInfo(String), value);
- Check(PPointer(value.GetReferenceToRawData)^ = Pointer(str), 'Reference to string data differs');
- intf := TInterfacedObject.Create;
- TValue.Make(@intf, TypeInfo(IInterface), value);
- Check(PPointer(value.GetReferenceToRawData)^ = Pointer(intf), 'Reference to interface data differs');
- i := 42;
- TValue.Make(@i, TypeInfo(LongInt), value);
- Check(value.GetReferenceToRawData <> @i, 'Reference to longint is equal');
- Check(PLongInt(value.GetReferenceToRawData)^ = PLongInt(@i)^, 'Reference to longint data differs');
- test.value1 := 42;
- test.value2 := 'Hello World';
- TValue.Make(@test, TypeInfo(TTestRecord), value);
- Check(value.GetReferenceToRawData <> @test, 'Reference to record is equal');
- Check(PTestRecord(value.GetReferenceToRawData)^.value1 = PTestRecord(@test)^.value1, 'Reference to record data value1 differs');
- Check(PTestRecord(value.GetReferenceToRawData)^.value2 = PTestRecord(@test)^.value2, 'Reference to record data value2 differs');
- SetLength(arrdyn, 3);
- arrdyn[0] := 42;
- arrdyn[1] := 23;
- arrdyn[2] := 49;
- TValue.Make(@arrdyn, TypeInfo(TArrayOfLongintDyn), value);
- Check(PPointer(value.GetReferenceToRawData)^ = Pointer(arrdyn), 'Reference to dynamic array data differs');
- arrstat[0] := 42;
- arrstat[1] := 23;
- arrstat[2] := 49;
- arrstat[3] := 59;
- TValue.Make(@arrstat, TypeInfo(TArrayOfLongintStatic), value);
- Check(value.GetReferenceToRawData <> @arrstat, 'Reference to static array is equal');
- Check(PLongInt(value.GetReferenceToRawData)^ = PLongInt(@arrstat)^, 'Reference to static array data differs');
- end;
- procedure TTestCase1.TestReferenceRawDataEmpty;
- var
- value: TValue;
- begin
- TValue.Make(Nil, TypeInfo(String), value);
- Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty String is not assigned');
- Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty String data is assigned');
- TValue.Make(Nil, TypeInfo(IInterface), value);
- Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty interface is not assigned');
- Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty interface data is assigned');
- TValue.Make(Nil, TypeInfo(LongInt), value);
- Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty LongInt is not assigned');
- Check(PLongInt(value.GetReferenceToRawData)^ = 0, 'Empty longint data is not 0');
- TValue.Make(Nil, TypeInfo(TTestRecord), value);
- Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty record is not assigned');
- Check(PTestRecord(value.GetReferenceToRawData)^.value1 = 0, 'Empty record data value1 is not 0');
- Check(PTestRecord(value.GetReferenceToRawData)^.value2 = '', 'Empty record data value2 is not empty');
- TValue.Make(Nil, TypeInfo(TArrayOfLongintDyn), value);
- Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty dynamic array is not assigned');
- Check(not Assigned(PPointer(value.GetReferenceToRawData)^), 'Empty dynamic array data is assigned');
- TValue.Make(Nil, TypeInfo(TArrayOfLongintStatic), value);
- Check(Assigned(value.GetReferenceToRawData()), 'Reference to empty static array is not assigned');
- Check(PLongInt(value.GetReferenceToRawData)^ = 0, 'Empty static array data is not 0');
- end;
- procedure TTestCase1.TestDataSize;
- var
- u8: UInt8;
- u16: UInt16;
- u32: UInt32;
- u64: UInt64;
- s8: Int8;
- s16: Int16;
- s32: Int32;
- s64: Int64;
- f32: Single;
- f64: Double;
- {$ifdef FPC_HAS_TYPE_EXTENDED}
- f80: Extended;
- {$endif}
- fco: Comp;
- fcu: Currency;
- ss: ShortString;
- sa: AnsiString;
- su: UnicodeString;
- sw: WideString;
- o: TObject;
- c: TClass;
- i: IInterface;
- ad: TArrayOfLongintDyn;
- _as: TArrayOfLongintStatic;
- b8: Boolean;
- {$ifdef fpc}
- b16: Boolean16;
- b32: Boolean32;
- b64: Boolean64;
- {$endif}
- bl8: ByteBool;
- bl16: WordBool;
- bl32: LongBool;
- {$ifdef fpc}
- bl64: QWordBool;
- {$endif}
- e: TTestEnum;
- s: TTestSet;
- t: TTestRecord;
- p: Pointer;
- proc: TTestProc;
- method: TTestMethod;
- value: TValue;
- begin
- u8:=245;
- TValue.Make(@u8, TypeInfo(UInt8), value);
- CheckEquals(1, value.DataSize, 'Size of UInt8 differs');
- u16:=789;
- TValue.Make(@u16, TypeInfo(UInt16), value);
- CheckEquals(2, value.DataSize, 'Size of UInt16 differs');
- u32:=568789;
- TValue.Make(@u32, TypeInfo(UInt32), value);
- CheckEquals(4, value.DataSize, 'Size of UInt32 differs');
- u64:=$abdcefadbcef;
- TValue.Make(@u64, TypeInfo(UInt64), value);
- CheckEquals(8, value.DataSize, 'Size of UInt64 differs');
- s8:=-32;
- TValue.Make(@s8, TypeInfo(Int8), value);
- CheckEquals(1, value.DataSize, 'Size of Int8 differs');
- s16:=-5345;
- TValue.Make(@s16, TypeInfo(Int16), value);
- CheckEquals(2, value.DataSize, 'Size of Int16 differs');
- s32:=-234567;
- TValue.Make(@s32, TypeInfo(Int32), value);
- CheckEquals(4, value.DataSize, 'Size of Int32 differs');
- s64:=23456789012;
- TValue.Make(@s64, TypeInfo(Int64), value);
- CheckEquals(8, value.DataSize, 'Size of Int64 differs');
- b8:=false;
- TValue.Make(@b8, TypeInfo(Boolean), value);
- CheckEquals(1, value.DataSize, 'Size of Boolean differs');
- {$ifdef fpc}
- b16:=true;
- TValue.Make(@b16, TypeInfo(Boolean16), value);
- CheckEquals(2, value.DataSize, 'Size of Boolean16 differs');
- b32:=false;
- TValue.Make(@b32, TypeInfo(Boolean32), value);
- CheckEquals(4, value.DataSize, 'Size of Boolean32 differs');
- b64:=true;
- TValue.Make(@b64, TypeInfo(Boolean64), value);
- CheckEquals(8, value.DataSize, 'Size of Boolean64 differs');
- {$endif}
- bl8:=true;
- TValue.Make(@bl8, TypeInfo(ByteBool), value);
- CheckEquals(1, value.DataSize, 'Size of ByteBool differs');
- bl16:=false;
- TValue.Make(@bl16, TypeInfo(WordBool), value);
- CheckEquals(2, value.DataSize, 'Size of WordBool differs');
- bl32:=false;
- TValue.Make(@bl32, TypeInfo(LongBool), value);
- CheckEquals(4, value.DataSize, 'Size of LongBool differs');
- {$ifdef fpc}
- bl64:=true;
- TValue.Make(@bl64, TypeInfo(QWordBool), value);
- CheckEquals(8, value.DataSize, 'Size of QWordBool differs');
- {$endif}
- f32:=4.567;
- TValue.Make(@f32, TypeInfo(Single), value);
- CheckEquals(4, value.DataSize, 'Size of Single differs');
- f64:=-3456.678;
- TValue.Make(@f64, TypeInfo(Double), value);
- CheckEquals(8, value.DataSize, 'Size of Double differs');
- {$ifdef FPC_HAS_TYPE_EXTENDED}
- f80:=-2345.678;
- TValue.Make(@f80, TypeInfo(Extended), value);
- CheckEquals(10, value.DataSize, 'Size of Extended differs');
- {$endif}
- fcu:=56.78;
- TValue.Make(@fcu, TypeInfo(Currency), value);
- CheckEquals(SizeOf(Currency), value.DataSize, 'Size of Currency differs');
- fco:=456;
- TValue.Make(@fco, TypeInfo(Comp), value);
- CheckEquals(SizeOf(Comp), value.DataSize, 'Size of Comp differs');
- ss := '';
- TValue.Make(@ss, TypeInfo(ShortString), value);
- CheckEquals(254, value.DataSize, 'Size ofShortString differs');
- sa:= '';
- TValue.Make(@sa, TypeInfo(AnsiString), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of AnsiString differs');
- sw := '';
- TValue.Make(@sw, TypeInfo(WideString), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of WideString differs');
- su:='';
- TValue.Make(@su, TypeInfo(UnicodeString), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of UnicodeString differs');
- o := TTestValueClass.Create;
- TValue.Make(@o, TypeInfo(TObject), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TObject differs');
- o.Free;
- c := TObject;
- TValue.Make(@c, TypeInfo(TClass), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TClass differs');
- i := Nil;
- TValue.Make(@i, TypeInfo(IInterface), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of IInterface differs');
- TValue.Make(@t, TypeInfo(TTestRecord), value);
- CheckEquals(SizeOf(TTestRecord), value.DataSize, 'Size of TTestRecord differs');
- proc := Nil;
- TValue.Make(@proc, TypeInfo(TTestProc), value);
- CheckEquals(SizeOf(TTestProc), value.DataSize, 'Size of TTestProc differs');
- method := Nil;
- TValue.Make(@method, TypeInfo(TTestMethod), value);
- CheckEquals(SizeOf(TTestMethod), value.DataSize, 'Size of TTestMethod differs');
- TValue.Make(@_as, TypeInfo(TArrayOfLongintStatic), value);
- CheckEquals(SizeOf(TArrayOfLongintStatic), value.DataSize, 'Size of TArrayOfLongintStatic differs');
- TValue.Make(@ad, TypeInfo(TArrayOfLongintDyn), value);
- CheckEquals(SizeOf(TArrayOfLongintDyn), value.DataSize, 'Size of TArrayOfLongintDyn differs');
- e:=low(TTestEnum);
- TValue.Make(@e, TypeInfo(TTestEnum), value);
- CheckEquals(SizeOf(TTestEnum), value.DataSize, 'Size of TTestEnum differs');
- s:=[low(TTestEnum),high(TTestEnum)];
- TValue.Make(@s, TypeInfo(TTestSet), value);
- CheckEquals(SizeOf(TTestSet), value.DataSize, 'Size of TTestSet differs');
- p := Nil;
- TValue.Make(@p, TypeInfo(Pointer), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
- end;
- procedure TTestCase1.TestDataSizeEmpty;
- var
- value: TValue;
- begin
- TValue.Make(Nil, TypeInfo(UInt8), value);
- CheckEquals(1, value.DataSize, 'Size of UInt8 differs');
- TValue.Make(Nil, TypeInfo(UInt16), value);
- CheckEquals(2, value.DataSize, 'Size of UInt16 differs');
- TValue.Make(Nil, TypeInfo(UInt32), value);
- CheckEquals(4, value.DataSize, 'Size of UInt32 differs');
- TValue.Make(Nil, TypeInfo(UInt64), value);
- CheckEquals(8, value.DataSize, 'Size of UInt64 differs');
- TValue.Make(Nil, TypeInfo(Int8), value);
- CheckEquals(1, value.DataSize, 'Size of Int8 differs');
- TValue.Make(Nil, TypeInfo(Int16), value);
- CheckEquals(2, value.DataSize, 'Size of Int16 differs');
- TValue.Make(Nil, TypeInfo(Int32), value);
- CheckEquals(4, value.DataSize, 'Size of Int32 differs');
- TValue.Make(Nil, TypeInfo(Int64), value);
- CheckEquals(8, value.DataSize, 'Size of Int64 differs');
- TValue.Make(Nil, TypeInfo(Boolean), value);
- CheckEquals(1, value.DataSize, 'Size of Boolean differs');
- {$ifdef fpc}
- TValue.Make(Nil, TypeInfo(Boolean16), value);
- CheckEquals(2, value.DataSize, 'Size of Boolean16 differs');
- TValue.Make(Nil, TypeInfo(Boolean32), value);
- CheckEquals(4, value.DataSize, 'Size of Boolean32 differs');
- TValue.Make(Nil, TypeInfo(Boolean64), value);
- CheckEquals(8, value.DataSize, 'Size of Boolean64 differs');
- {$endif}
- TValue.Make(Nil, TypeInfo(ByteBool), value);
- CheckEquals(1, value.DataSize, 'Size of ByteBool differs');
- TValue.Make(Nil, TypeInfo(WordBool), value);
- CheckEquals(2, value.DataSize, 'Size of WordBool differs');
- TValue.Make(Nil, TypeInfo(LongBool), value);
- CheckEquals(4, value.DataSize, 'Size of LongBool differs');
- {$ifdef fpc}
- TValue.Make(Nil, TypeInfo(QWordBool), value);
- CheckEquals(8, value.DataSize, 'Size of QWordBool differs');
- {$endif}
- TValue.Make(Nil, TypeInfo(Single), value);
- CheckEquals(4, value.DataSize, 'Size of Single differs');
- TValue.Make(Nil, TypeInfo(Double), value);
- CheckEquals(8, value.DataSize, 'Size of Double differs');
- {$ifdef FPC_HAS_TYPE_EXTENDED}
- TValue.Make(Nil, TypeInfo(Extended), value);
- CheckEquals(10, value.DataSize, 'Size of Extended differs');
- {$endif}
- TValue.Make(Nil, TypeInfo(Currency), value);
- CheckEquals(SizeOf(Currency), value.DataSize, 'Size of Currency differs');
- TValue.Make(Nil, TypeInfo(Comp), value);
- CheckEquals(SizeOf(Comp), value.DataSize, 'Size of Comp differs');
- TValue.Make(Nil, TypeInfo(ShortString), value);
- CheckEquals(254, value.DataSize, 'Size of ShortString differs');
- TValue.Make(Nil, TypeInfo(AnsiString), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
- TValue.Make(Nil, TypeInfo(WideString), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of WideString differs');
- TValue.Make(Nil, TypeInfo(UnicodeString), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of UnicodeString differs');
- TValue.Make(Nil, TypeInfo(TObject), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TObject differs');
- TValue.Make(Nil, TypeInfo(TClass), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of TClass differs');
- TValue.Make(Nil, TypeInfo(IInterface), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of IInterface differs');
- TValue.Make(Nil, TypeInfo(TTestRecord), value);
- CheckEquals(SizeOf(TTestRecord), value.DataSize, 'Size of TTestRecord differs');
- TValue.Make(Nil, TypeInfo(TTestProc), value);
- CheckEquals(SizeOf(TTestProc), value.DataSize, 'Size of TTestProc differs');
- TValue.Make(Nil, TypeInfo(TTestMethod), value);
- CheckEquals(SizeOf(TTestMethod), value.DataSize, 'Size of TTestMethod differs');
- TValue.Make(Nil, TypeInfo(TArrayOfLongintStatic), value);
- CheckEquals(SizeOf(TArrayOfLongintStatic), value.DataSize, 'Size of TArrayOfLongintStatic differs');
- TValue.Make(Nil, TypeInfo(TArrayOfLongintDyn), value);
- CheckEquals(SizeOf(TArrayOfLongintDyn), value.DataSize, 'Size of TArrayOfLongintDyn differs');
- TValue.Make(Nil, TypeInfo(TTestEnum), value);
- CheckEquals(SizeOf(TTestEnum), value.DataSize, 'Size of TTestEnum differs');
- TValue.Make(Nil, TypeInfo(TTestSet), value);
- CheckEquals(SizeOf(TTestSet), value.DataSize, 'Size of TTestSet differs');
- TValue.Make(Nil, TypeInfo(Pointer), value);
- CheckEquals(SizeOf(Pointer), value.DataSize, 'Size of Pointer differs');
- end;
- procedure TTestCase1.TestIsManaged;
- begin
- CheckEquals(true, IsManaged(TypeInfo(ansistring)), 'IsManaged for tkAString');
- CheckEquals(true, IsManaged(TypeInfo(widestring)), 'IsManaged for tkWString');
- CheckEquals(true, IsManaged(TypeInfo(Variant)), 'IsManaged for tkVariant');
- CheckEquals(true, IsManaged(TypeInfo(TArrayOfManagedRec)),
- 'IsManaged for tkArray (with managed ElType)');
- CheckEquals(true, IsManaged(TypeInfo(TArrayOfString)),
- 'IsManaged for tkArray (with managed ElType)');
- CheckEquals(true, IsManaged(TypeInfo(TManagedRec)), 'IsManaged for tkRecord');
- {$ifdef fpc}
- CheckEquals(true, IsManaged(TypeInfo(TManagedRecOp)), 'IsManaged for tkRecord');
- {$endif}
- CheckEquals(true, IsManaged(TypeInfo(IInterface)), 'IsManaged for tkInterface');
- CheckEquals(true, IsManaged(TypeInfo(TManagedObj)), 'IsManaged for tkObject');
- {$ifdef fpc}
- CheckEquals(true, IsManaged(TypeInfo(specialize TArray<byte>)), 'IsManaged for tkDynArray');
- {$else}
- CheckEquals(true, IsManaged(TypeInfo(TArray<byte>)), 'IsManaged for tkDynArray');
- {$endif}
- CheckEquals(true, IsManaged(TypeInfo(unicodestring)), 'IsManaged for tkUString');
- CheckEquals(false, IsManaged(TypeInfo(shortstring)), 'IsManaged for tkSString');
- CheckEquals(false, IsManaged(TypeInfo(Byte)), 'IsManaged for tkInteger');
- CheckEquals(false, IsManaged(TypeInfo(Char)), 'IsManaged for tkChar');
- CheckEquals(false, IsManaged(TypeInfo(TTestEnum)), 'IsManaged for tkEnumeration');
- CheckEquals(false, IsManaged(TypeInfo(Single)), 'IsManaged for tkFloat');
- CheckEquals(false, IsManaged(TypeInfo(TTestSet)), 'IsManaged for tkSet');
- {$ifdef fpc}
- CheckEquals(false, IsManaged(TypeInfo(TTestMethod)), 'IsManaged for tkMethod');
- {$else}
- { Delphi bug (or sabotage). For some reason Delphi considers method pointers to be managed (only in newer versions, probably since XE7) :/ }
- CheckEquals({$if RTLVersion>=28}true{$else}false{$endif}, IsManaged(TypeInfo(TTestMethod)), 'IsManaged for tkMethod');
- {$endif}
- CheckEquals(false, IsManaged(TypeInfo(TArrayOfByte)),
- 'IsManaged for tkArray (with non managed ElType)');
- CheckEquals(false, IsManaged(TypeInfo(TArrayOfNonManagedRec)),
- 'IsManaged for tkArray (with non managed ElType)');
- CheckEquals(false, IsManaged(TypeInfo(TNonManagedRec)), 'IsManaged for tkRecord');
- CheckEquals(false, IsManaged(TypeInfo(TObject)), 'IsManaged for tkClass');
- CheckEquals(false, IsManaged(TypeInfo(TNonManagedObj)), 'IsManaged for tkObject');
- CheckEquals(false, IsManaged(TypeInfo(WideChar)), 'IsManaged for tkWChar');
- CheckEquals(false, IsManaged(TypeInfo(Boolean)), 'IsManaged for tkBool');
- CheckEquals(false, IsManaged(TypeInfo(Int64)), 'IsManaged for tkInt64');
- CheckEquals(false, IsManaged(TypeInfo(UInt64)), 'IsManaged for tkQWord');
- {$ifdef fpc}
- CheckEquals(false, IsManaged(TypeInfo(ICORBATest)), 'IsManaged for tkInterfaceRaw');
- {$endif}
- CheckEquals(false, IsManaged(TypeInfo(TTestProc)), 'IsManaged for tkProcVar');
- CheckEquals(false, IsManaged(TypeInfo(TTestHelper)), 'IsManaged for tkHelper');
- {$ifdef fpc}
- CheckEquals(false, IsManaged(TypeInfo(file)), 'IsManaged for tkFile');
- {$endif}
- CheckEquals(false, IsManaged(TypeInfo(TClass)), 'IsManaged for tkClassRef');
- CheckEquals(false, IsManaged(TypeInfo(Pointer)), 'IsManaged for tkPointer');
- CheckEquals(false, IsManaged(nil), 'IsManaged for nil');
- end;
- {$ifdef fpc}
- procedure TTestCase1.TestOpenArrayToDyn;
- procedure OpenArrayProc(aArr: array of LongInt);
- var
- value: TValue;
- begin
- {$ifndef InLazIDE}
- value := specialize OpenArrayToDynArrayValue<LongInt>(aArr);
- {$endif}
- CheckEquals(value.IsArray, True);
- CheckEquals(value.IsOpenArray, False);
- CheckEquals(value.IsObject, False);
- CheckEquals(value.IsOrdinal, False);
- CheckEquals(value.IsClass, False);
- CheckEquals(value.GetArrayLength, 2);
- CheckEquals(value.GetArrayElement(0).AsInteger, 42);
- CheckEquals(value.GetArrayElement(1).AsInteger, 84);
- value.SetArrayElement(0, 21);
- { since this is a copy the original array is not modified! }
- CheckEquals(aArr[0], 42);
- end;
- begin
- OpenArrayProc([42, 84]);
- end;
- {$endif}
- procedure TTestCase1.TestInterface;
- var
- context: TRttiContext;
- t: TRttiType;
- ti1, ti2: TRttiInterfaceType;
- methods: {$ifdef fpc}specialize{$endif} TArray<TRttiMethod>;
- params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
- method: TRttiMethod;
- param: TRttiParameter;
- flag: TParamFlag;
- begin
- context := TRttiContext.Create;
- try
- t := context.GetType(TypeInfo(IInterface));
- Check(t is TRttiInterfaceType, 'Type is not an interface type');
- Check(not Assigned(t.BaseType), 'Base type is assigned');
- ti1 := TRttiInterfaceType(t);
- Check(not Assigned(ti1.BaseType), 'Base type is assigned');
- methods := t.GetMethods;
- CheckEquals(0, Length(methods), 'Overall method count does not match');
- methods := t.GetDeclaredMethods;
- CheckEquals(0, Length(methods), 'Declared method conut does not match');
- t := context.GetType(TypeInfo(ITestInterface));
- Check(t is TRttiInterfaceType, 'Type is not an interface type');
- Check(Assigned(t.BaseType), 'Base type is not assigned');
- Check(t.BaseType = TRttiType(ti1), 'Base type does not match');
- ti2 := TRttiInterfaceType(t);
- Check(Assigned(ti2.BaseType), 'Base type is not assigned');
- Check(ti2.BaseType = ti1, 'Base type does not match');
- methods := t.GetMethods;
- CheckEquals(4, Length(methods), 'Overall method count does not match');
- methods := t.GetDeclaredMethods;
- CheckEquals(4, Length(methods), 'Declared method count does not match');
- method := methods[0];
- CheckEquals(method.Name, 'Test', 'Method name of Test does not match');
- Check(method.CallingConvention = DefaultCC, 'Calling convention of Test does not match');
- Check(method.MethodKind = mkProcedure, 'Method kind of Test does not match');
- Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test does not match');
- Check(not Assigned(method.CodeAddress), 'Code address of Test is not Nil');
- CheckEquals(method.VirtualIndex, 3, 'Virtual index of Test does not match');
- Check(not Assigned(method.ReturnType), 'Return type of Test is not Nil');
- params := method.GetParameters;
- CheckEquals(0, Length(params), 'Parameter count of Test does not match');
- method := methods[1];
- CheckEquals(method.Name, 'Test2', 'Method name of Test2 does not match');
- Check(method.CallingConvention = DefaultCC, 'Calling convention of Test2 does not match');
- Check(method.MethodKind = mkFunction, 'Method kind of Test2 does not match');
- Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test2 does not match');
- Check(not Assigned(method.CodeAddress), 'Code address of Test2 is not Nil');
- CheckEquals(method.VirtualIndex, 4, 'Virtual index of Test2 does not match');
- Check(Assigned(method.ReturnType), 'Return type of Test2 is Nil');
- Check(method.ReturnType.TypeKind = tkInteger, 'Return type of Test2 is not an ordinal');
- params := method.GetParameters;
- CheckEquals(0, Length(params), 'Parameter count of Test2 does not match');
- method := methods[2];
- CheckEquals(method.Name, 'Test3', 'Method name of Test3 does not match');
- Check(method.CallingConvention = DefaultCC, 'Calling convention of Test3 does not match');
- Check(method.MethodKind = mkProcedure, 'Method kind of Test3 does not match');
- Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test3 does not match');
- Check(not Assigned(method.CodeAddress), 'Code address of Test3 is not Nil');
- CheckEquals(method.VirtualIndex, 5, 'Virtual index of Test3 does not match');
- Check(not Assigned(method.ReturnType), 'Return type of Test3 is not Nil');
- params := method.GetParameters;
- CheckEquals(4, Length(params), 'Parameter count of Test3 does not match');
- param := params[0];
- CheckEquals(param.Name, 'aArg1', 'Parameter name of Test3.aArg1 does not match');
- Check(param.Flags = [], 'Parameter flags of Test3.aArg1 do not match');
- Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg1 is Nil');
- Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test3.aArg1 is not an ordinal');
- param := params[1];
- CheckEquals(param.Name, 'aArg2', 'Parameter name of Test3.aArg2 does not match');
- Check(param.Flags = [pfConst], 'Parameter flags of Test3.aArg2 do not match');
- Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg2 is Nil');
- Check(param.ParamType.TypeKind = tkAnsiString, 'Parameter type of Test3.aArg2 is not a string');
- param := params[2];
- CheckEquals(param.Name, 'aArg3', 'Parameter name of Test3.aArg3 does not match');
- Check(param.Flags = [pfVar], 'Parameter flags of Test3.aArg3 do not match');
- Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg3 is Nil');
- Check(param.ParamType.TypeKind = {$ifdef fpc}tkBool{$else}tkEnumeration{$endif}, 'Parameter type of Test3.aArg3 is not a boolean');
- param := params[3];
- CheckEquals(param.Name, 'aArg4', 'Parameter name of Test3.aArg4 does not match');
- Check(param.Flags = [pfOut], 'Parameter flags of Test3.aArg4 do not match');
- Check(Assigned(param.ParamType), 'Parameter type of Test3.aArg4 is Nil');
- Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test3.aArg4 is not a string');
- method := methods[3];
- CheckEquals(method.Name, 'Test4', 'Method name of Test4 does not match');
- Check(method.CallingConvention = DefaultCC, 'Calling convention of Test4 does not match');
- Check(method.MethodKind = mkFunction, 'Method kind of Test4 does not match');
- Check(method.DispatchKind = dkInterface, 'Dispatch kind of Test4 does not match');
- Check(not Assigned(method.CodeAddress), 'Code address of Test4 is not Nil');
- CheckEquals(method.VirtualIndex, 6, 'Virtual index of Test4 does not match');
- Check(Assigned(method.ReturnType), 'Return type of Test4 is not Nil');
- Check(method.ReturnType.TypeKind = tkAnsiString, 'Return type of Test4 is not a string');
- params := method.GetParameters;
- CheckEquals(2, Length(params), 'Parameter count of Test4 does not match');
- param := params[0];
- CheckEquals(param.Name, 'aArg1', 'Parameter name of Test4.aArg1 does not match');
- Check(param.Flags = [pfArray, pfReference], 'Parameter flags of Test4.aArg1 do not match');
- Check(Assigned(param.ParamType), 'Parameter type of Test4.aArg1 is Nil');
- Check(param.ParamType.TypeKind = tkInteger, 'Parameter type of Test4.aArg1 is not an ordinal');
- param := params[1];
- CheckEquals(param.Name, 'aArg2', 'Parameter name of Test4.aArg2 does not match');
- Check(param.Flags = [pfArray, pfReference], 'Parameter flags of Test4.aArg2 do not match');
- Check(Assigned(param.ParamType), 'Parameter type of Test4.aArg2 is Nil');
- Check(param.ParamType.TypeKind = tkRecord, 'Parameter type of Test4.aArg2 is not a record');
- finally
- context.Free;
- end;
- end;
- procedure TTestCase1.TestRawThunk;
- var
- intf: IInterface;
- begin
- { we test the raw thunking by instantiating a TVirtualInterface of IInterface }
- { this does not require a function call manager as the thunking is implemented
- directly inside the RTTI unit }
- try
- intf := TVirtualInterface.Create(PTypeInfo(TypeInfo(IInterface))) as IInterface;
- except
- on e: ENotImplemented do
- Ignore('RawThunk not implemented');
- end;
- { if all went well QueryInterface and _AddRef were called and now we call
- _Release as well }
- intf := Nil;
- end;
- {$ifdef fpc}
- procedure TTestCase1.TestInterfaceRaw;
- var
- context: TRttiContext;
- t: TRttiType;
- ti: TRttiInterfaceType;
- begin
- context := TRttiContext.Create;
- try
- t := context.GetType(TypeInfo(ICORBATest));
- Check(t is TRttiInterfaceType, 'Type is not a raw interface type');
- Check(not Assigned(t.BaseType), 'Base type is assigned');
- ti := TRttiInterfaceType(t);
- Check(not Assigned(ti.BaseType), 'Base type is assigned');
- finally
- context.Free;
- end;
- end;
- {$endif}
- procedure TTestCase1.TestArray;
- var
- context: TRttiContext;
- t, el: TRttiType;
- a: TRttiArrayType;
- o: TRttiOrdinalType;
- begin
- context := TRttiContext.Create;
- try
- t := context.GetType(PTypeInfo(TypeInfo(TArrayOfLongintStatic)));
- Check(t is TRttiArrayType, 'Type is not a TRttiArrayType');
- a := TRttiArrayType(t);
- CheckEquals(1, a.DimensionCount, 'Dimension count does not match');
- CheckEquals(4, a.TotalElementCount, 'Total element count does not match');
- el := a.ElementType;
- Check(el is TRttiOrdinalType, 'Element type is not a TRttiOrdinalType');
- Check(el = context.GetType(PTypeInfo(TypeInfo(LongInt))), 'Element type is not a LongInt');
- t := a.Dimensions[0];
- {$ifdef fpc}
- Check(t is TRttiOrdinalType, 'Index type is not a TRttiOrdinalType');
- o := TRttiOrdinalType(t);
- { Currently this is a full type :/ }
- {CheckEquals(0, o.MinValue, 'Minimum value of 1st dimension does not match');
- CheckEquals(3, o.MaxValue, 'Maximum value of 1st dimension does not match');}
- {$else}
- Check(t = Nil, 'Index type is not Nil');
- {$endif}
- t := context.GetType(PTypeInfo(TypeInfo(TArrayOfLongint2DStatic)));
- Check(t is TRttiArrayType, 'Type is not a TRttiArrayType');
- a := TRttiArrayType(t);
- CheckEquals(2, a.DimensionCount, 'Dimension count does not match');
- CheckEquals(4 * 3, a.TotalElementCount, 'Total element count does not match');
- el := a.ElementType;
- Check(el is TRttiOrdinalType, 'Element type is not a TRttiOrdinalType');
- Check(el = context.GetType(PTypeInfo(TypeInfo(LongInt))), 'Element type is not a LongInt');
- t := a.Dimensions[0];
- {$ifdef fpc}
- Check(t is TRttiOrdinalType, 'Index type is not a TRttiOrdinalType');
- o := TRttiOrdinalType(t);
- { Currently this is a full type :/ }
- {CheckEquals(0, o.MinValue, 'Minimum value of 1st dimension does not match');
- CheckEquals(3, o.MaxValue, 'Maximum value of 1st dimension does not match');}
- {$else}
- Check(t = Nil, 'Index type is not Nil');
- {$endif}
- t := a.Dimensions[1];
- {$ifdef fpc}
- Check(t is TRttiOrdinalType, 'Index type is not a TRttiOrdinalType');
- o := TRttiOrdinalType(t);
- { Currently this is a full type :/ }
- {CheckEquals(2, o.MinValue, 'Minimum value of 1st dimension does not match');
- CheckEquals(4, o.MaxValue, 'Maximum value of 1st dimension does not match');}
- {$else}
- Check(t = Nil, 'Index type is not Nil');
- {$endif}
- finally
- context.Free;
- end;
- end;
- procedure TTestCase1.TestDynArray;
- var
- context: TRttiContext;
- t, el: TRttiType;
- a: TRttiDynamicArrayType;
- begin
- context := TRttiContext.Create;
- try
- t := context.GetType(PTypeInfo(TypeInfo(TArrayOfLongintDyn)));
- Check(t is TRttiDynamicArrayType, 'Type is not a TRttiDynamicArrayType');
- a := TRttiDynamicArrayType(t);
- CheckEquals('tests.rtti', LowerCase(a.DeclaringUnitName), 'Unit type does not match for dynamic array');
- CheckEquals(a.ElementSize, SizeUInt(SizeOf(LongInt)), 'Element size does not match for dynamic array');
- el := a.ElementType;
- Check(el is TRttiOrdinalType, 'Element type is not a TRttiOrdinalType');
- Check(el = context.GetType(PTypeInfo(TypeInfo(LongInt))), 'Element type is not a LongInt');
- { ToDo: check OLE type }
- finally
- context.Free;
- end;
- end;
- procedure TTestCase1.TestProcVar;
- var
- context: TRttiContext;
- t: TRttiType;
- p: TRttiProcedureType;
- params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
- begin
- context := TRttiContext.Create;
- try
- t := context.GetType(PTypeInfo(TypeInfo(TTestProc)));
- Check(Assigned(t), 'Rtti Type is Nil');
- Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
- Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
- p := t as TRttiProcedureType;
- Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
- Check(not Assigned(p.ReturnType), 'Return type is assigned');
- CheckEquals(0, Length(p.GetParameters), 'Procedure variable has parameters');
- t := context.GetType(PTypeInfo(TypeInfo(TTestFunc1)));
- Check(Assigned(t), 'Rtti Type is Nil');
- Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
- Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
- p := t as TRttiProcedureType;
- Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
- Check(Assigned(p.ReturnType), 'Return type is not assigned');
- //Check(p.ReturnType is TRttiOrdinalType, 'Return type is not an ordinal type');
- CheckEquals(0, Length(p.GetParameters), 'Procedure variable has parameters');
- t := context.GetType(PTypeInfo(TypeInfo(TTestFunc2)));
- Check(Assigned(t), 'Rtti Type is Nil');
- Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
- Check(t is TRttiProcedureType, 'Rtti Type is not a procedure type');
- p := t as TRttiProcedureType;
- Check(p.CallingConvention = DefaultCC, 'Calling convention does not match');
- Check(Assigned(p.ReturnType), 'Return type is not assigned');
- Check(p.ReturnType is TRttiStringType, 'Return type is not a string type');
- params := p.GetParameters;
- CheckEquals(2, Length(params), 'Procedure variable has incorrect amount of parameters');
- Check(params[0].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 1 is not an ordinal type');
- //Check(params[0].ParamType is TRttiOrdinalType, 'Parameter 1 is not an ordinal type');
- Check(pfArray in params[1].Flags, 'Parameter 2 is not an array');
- Check(params[1].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 2 is not an ordinal array');
- finally
- context.Free;
- end;
- end;
- procedure TTestCase1.TestMethod;
- var
- context: TRttiContext;
- t: TRttiType;
- m: TRttiMethodType;
- params: {$ifdef fpc}specialize{$endif} TArray<TRttiParameter>;
- begin
- context := TRttiContext.Create;
- try
- t := context.GetType(PTypeInfo(TypeInfo(TTestMethod)));
- Check(Assigned(t), 'Rtti Type is Nil');
- Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
- Check(t is TRttiMethodType, 'Rtti Type is not a method type');
- m := t as TRttiMethodType;
- Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
- Check(not Assigned(m.ReturnType), 'Return type is assigned');
- CheckEquals(0, Length(m.GetParameters), 'Method variable has parameters');
- t := context.GetType(PTypeInfo(TypeInfo(TTestMethod1)));
- Check(Assigned(t), 'Rtti Type is Nil');
- Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
- Check(t is TRttiMethodType, 'Rtti Type is not a method type');
- m := t as TRttiMethodType;
- Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
- Check(Assigned(m.ReturnType), 'Return type is not assigned');
- //Check(p.ReturnType is TRttiOrdinalType, 'Return type is not an ordinal type');
- CheckEquals(0, Length(m.GetParameters), 'Method variable has parameters');
- t := context.GetType(PTypeInfo(TypeInfo(TTestMethod2)));
- Check(Assigned(t), 'Rtti Type is Nil');
- Check(t is TRttiInvokableType, 'Rtti Type is not an invokeable');
- Check(t is TRttiMethodType, 'Rtti Type is not a method type');
- m := t as TRttiMethodType;
- Check(m.CallingConvention = DefaultCC, 'Calling convention does not match');
- Check(Assigned(m.ReturnType), 'Return type is not assigned');
- Check(m.ReturnType is TRttiStringType, 'Return type is not a string type');
- params := m.GetParameters;
- CheckEquals(2, Length(params), 'Method variable has incorrect amount of parameters');
- Check(params[0].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 1 is not an ordinal type');
- //Check(params[0].ParamType is TRttiOrdinalType, 'Parameter 1 is not an ordinal type');
- Check(pfArray in params[1].Flags, 'Parameter 2 is not an array');
- Check(params[1].ParamType.TypeKind in [tkInteger, tkInt64], 'Parameter 2 is not an ordinal array');
- finally
- context.Free;
- end;
- end;
- initialization
- {$ifdef fpc}
- RegisterTest(TTestCase1);
- {$else fpc}
- RegisterTest(TTestCase1.Suite);
- {$endif fpc}
- end.
|