12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375 |
- 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
- { TTestRTTI }
- TTestRTTI= 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 TestPropGetValueRecord;
- procedure TestPropSetValueString;
- procedure TestPropSetValueInteger;
- procedure TestPropSetValueBoolean;
- procedure TestPropSetValueShortString;
- procedure TestPropSetValueObject;
- procedure TestPropSetValueInterface;
- procedure TestPropSetValueFloat;
- procedure TestPropSetValueDynArray;
- procedure TestPropSetValueEnumeration;
- procedure TestPropSetValueChars;
- procedure TestPropSetValueRecord;
- procedure TestGetValueStringCastError;
- procedure TestGetIsReadable;
- procedure TestIsWritable;
- procedure TestGetAttribute;
- procedure TestInterface;
- {$ifdef fpc}
- procedure TestInterfaceRaw;
- {$endif}
- procedure TestArray;
- procedure TestDynArray;
- procedure TestProcVar;
- procedure TestMethod;
- procedure TestRawThunk;
- private
- {$ifndef fpc}
- procedure Ignore(const aMsg: String);
- {$endif}
- end;
- { TTestExtendedRTTI }
- // Note: the tests assume that TObject has no RTTI associated with it.
- // The tests need to be adapted so they will work in both cases.
- TTestExtendedRTTI = class(TTestCase)
- Private
- FCtx: TRttiContext;
- Procedure AssertEquals(Msg : String; aExpected,aActual : TMemberVisibility); overload;
- Procedure AssertEquals(Msg : String; aExpected,aActual : TTypeKind);overload;
- procedure CheckField(aIdx: Integer; aData: TRttiField; aName: String; aKind: TTypeKind; aVisibility: TMemberVisibility;
- aStrict: Boolean=False);
- procedure CheckMethod(aPrefix: string; aIdx: Integer; aData: TRttiMethod; aName: String; aVisibility: TMemberVisibility;
- aStrict: Boolean=False);
- procedure CheckProperty(aIdx: Integer; aData: TRttiProperty; aName: String; aKind: TTypeKind; aVisibility: TMemberVisibility;
- isStrict: Boolean=False);
- public
- Procedure Setup; override;
- Procedure TearDown; override;
- end;
- { TTestClassExtendedRTTI }
- TTestClassExtendedRTTI = class(TTestExtendedRtti)
- published
- Procedure TestFields;
- Procedure TestDeclaredFields;
- Procedure TestProperties;
- Procedure TestDeclaredMethods;
- Procedure TestMethods;
- Procedure TestMethodByAddress;
- Procedure TestMethodsInherited;
- Procedure TestPrivateFieldAttributes;
- Procedure TestProtectedFieldAttributes;
- Procedure TestPublicFieldAttributes;
- Procedure TestPrivatePropertyAttributes;
- Procedure TestProtectedPropertyAttributes;
- Procedure TestPublicPropertyAttributes;
- Procedure TestPublishedPropertyAttributes;
- procedure TestGetStaticProperty;
- procedure TestSetStaticProperty;
- end;
- { TTestRecordExtendedRTTI }
- TTestRecordExtendedRTTI = class(TTestExtendedRtti)
- published
- Procedure TestFields;
- Procedure TestProperties;
- Procedure TestDeclaredMethods;
- Procedure TestMethods;
- Procedure TestPrivateFieldAttributes;
- Procedure TestPublicFieldAttributes;
- end;
- implementation
- uses
- Tests.Rtti.Util,
- {tests.rtti.exttypes, }
- tests.rtti.attrtypes2,
- tests.rtti.attrtypes,
- tests.rtti.types;
- { Note: GetTypes currently only returns those types that had been acquired using
- GetType, so GetTypes itself can't be really tested currently }
- (*procedure TTestRTTI.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='TTestRTTI' then
- IsTestCaseClassFound:=true;
- end;
- LContext.Free;
- CheckTrue(IsTestCaseClassFound, 'RTTI information does not contain class of testcase.');
- end;*)
- {$ifndef fpc}
- procedure TTestRTTI.Ignore(const aMsg: string);
- begin
- { empty }
- end;
- {$endif}
- procedure TTestRTTI.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 TTestRTTI.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 TTestRTTI.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;
- procedure TTestRTTI.TestGetAttribute;
- // TMyAnnotatedClass
- // TMyAttribute
- var
- c: TRttiContext;
- aType: TRttiType;
- aClass : TMyAnnotatedClass;
- custAttr : TCustomAttribute;
- myAttr : TMyAttribute absolute custattr;
- begin
- aType:=nil;
- custAttr:=Nil;
- c := TRttiContext.Create;
- try
- aClass:=TMyAnnotatedClass.Create;
- aType := c.GetType(aClass.ClassInfo);
- custAttr:=aType.GetAttribute(TMyAttribute);
- CheckEquals(custAttr.ClassType,TMyAttribute,'Correct class');
- CheckEquals('something',MyAttr.value,'Correct value');
- finally
- aClass.Free;
- // custAttr.Free;
- C.Free;
- end;
- end;
- procedure TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.TestPropGetValueRecord;
- var
- ATestClass : TRecordRttiClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- R : TRTTIRecord;
- P : PRTTIRecord;
- begin
- R.a:=23;
- r.B:=54;
- c := TRttiContext.Create(False);
- try
- ATestClass := TRecordRttiClass.Create;
- ATestClass.RecordProp:=R;
- try
- ARttiType := c.GetType(TRecordRttiClass);
- CheckNotNull(ARttiType,'Type');
- AProperty := ARttiType.GetProperty('RecordProp');
- CheckNotNull(aProperty,'Prop');
- AValue := AProperty.GetValue(ATestClass);
- P:=PRTTIRecord(AValue.GetReferenceToRawData);
- AssertEquals('a',23,P^.A);
- AssertEquals('b',54,P^.B);
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.TestPropSetValueRecord;
- var
- ATestClass : TRecordRttiClass;
- c: TRttiContext;
- ARttiType: TRttiType;
- AProperty: TRttiProperty;
- AValue: TValue;
- R : TRTTIRecord;
- P : PRTTIRecord;
- begin
- R.a:=23;
- r.B:=54;
- c := TRttiContext.Create(False);
- try
- ATestClass := TRecordRttiClass.Create;
- ATestClass.RecordProp:=R;
- try
- ARttiType := c.GetType(TRecordRttiClass);
- CheckNotNull(ARttiType,'Type');
- AProperty := ARttiType.GetProperty('RecordProp');
- CheckNotNull(aProperty,'Prop');
- AProperty.SetValue(ATestClass,TValue.specialize From<TRttiRecord>(R));
- AssertEquals('a',23,aTestClass.RecordProp.A);
- AssertEquals('b',54,aTestClass.RecordProp.B);
- finally
- AtestClass.Free;
- end;
- finally
- c.Free;
- end;
- end;
- procedure TTestRTTI.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 TTestRTTI.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 TTestRTTI.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.types');
- 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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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 TTestRTTI.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.types', 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 TTestRTTI.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 TTestRTTI.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;
- { TTestExtendedRTTI }
- procedure TTestExtendedRTTI.AssertEquals(Msg: String; aExpected, aActual: TMemberVisibility);
- begin
- AssertEquals(Msg,GetEnumName(TypeInfo(TMemberVisibility),Ord(aExpected)),
- GetEnumName(TypeInfo(TMemberVisibility),Ord(aActual)));
- end;
- procedure TTestExtendedRTTI.AssertEquals(Msg: String; aExpected, aActual: TTypeKind);
- begin
- AssertEquals(Msg,GetEnumName(TypeInfo(TTypeKind),Ord(aExpected)),
- GetEnumName(TypeInfo(TTypeKind),Ord(aActual)));
- end;
- procedure TTestExtendedRTTI.Setup;
- begin
- Inherited;
- FCtx:=TRttiContext.Create;
- FCtx.UsePublishedOnly:=False;
- end;
- procedure TTestExtendedRTTI.TearDown;
- begin
- FCtx.Free;
- inherited TearDown;
- end;
- Procedure TTestExtendedRTTI.CheckField(aIdx : Integer; aData: TRttiField; aName : String; aKind : TTypeKind; aVisibility : TMemberVisibility; aStrict : Boolean = False);
- Var
- Msg : String;
- begin
- Msg:='Checking field '+IntToStr(aIdx)+' ('+aName+') ';
- AssertNotNull(Msg+'Have data',AData);
- AssertEquals(Msg+'name',aName,aData.Name);
- AssertEquals(Msg+'kind',aKind,aData.FieldType.TypeKind);
- AssertEquals(Msg+'visibility',aVisibility,aData.Visibility);
- AssertEquals(Msg+'strict',aStrict,aData.StrictVisibility);
- end;
- Procedure TTestExtendedRTTI.CheckProperty(aIdx : Integer; aData: TRttiProperty; aName : String; aKind : TTypeKind; aVisibility : TMemberVisibility; isStrict : Boolean = False);
- Var
- Msg : String;
- begin
- Msg:='Checking prop '+IntToStr(aIdx)+' ('+aName+') ';
- AssertNotNull(Msg+'Have data',AData);
- AssertEquals(Msg+'name',aName, aData.Name);
- AssertEquals(Msg+'kind',aKind, aData.PropertyType.TypeKind);
- AssertEquals(Msg+'visibility',aVisibility,aData.Visibility);
- AssertEquals(Msg+'strict',isStrict,aData.StrictVisibility);
- end;
- Procedure TTestExtendedRTTI.CheckMethod(aPrefix : string; aIdx : Integer; aData: TRttiMethod; aName : String; aVisibility : TMemberVisibility; aStrict : Boolean = False);
- Var
- Msg : String;
- begin
- Msg:=aPrefix+': Checking method '+IntToStr(aIdx)+' ('+aName+') ';
- AssertNotNull(Msg+'Have data',AData);
- AssertEquals(Msg+'name',aData.Name,aName);
- AssertEquals(Msg+'visibility',aVisibility,aData.Visibility);
- AssertEquals(Msg+'strict',aData.StrictVisibility,aStrict);
- end;
- procedure TTestClassExtendedRTTI.TestFields;
- Var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- A : TRttiFieldArray;
- t : TFieldRTTI;
- begin
- Obj:=FCtx.GetType(TFieldRTTI.ClassInfo);
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- A:=RttiData.GetFields;
- AssertEquals('Class field Count',10,Length(A));
- CheckField(0, A[0],'FPrivateA',tkInteger,mvPrivate);
- CheckField(1, A[1],'FPrivateB',tkInteger,mvPrivate,True);
- CheckField(2, A[2],'FProtectedA',tkInteger,mvProtected);
- CheckField(3, A[3],'FProtectedB',tkInteger,mvProtected,True);
- CheckField(4, A[4],'FPublicA',tkInteger,mvPublic);
- CheckField(5, A[5],'FPublicB',tkInteger,mvPublic);
- CheckField(6, A[6],'FPublishedA',tkInteger,mvPrivate);
- CheckField(7, A[7],'FPublishedB',tkInteger,mvPrivate);
- CheckField(8, A[8],'FPublishedC',tkClass,mvPublished);
- CheckField(9, A[9],'FPublishedD',tkClass,mvPublished);
- t := TFieldRTTI.Create;
- AssertEquals('Legacy Field 0', A[8].Offset, Integer(PByte(t.FieldAddress('FPublishedC')) - PByte(t)));
- AssertEquals('Legacy Field 1', A[9].Offset, Integer(PByte(t.FieldAddress('FPublishedD')) - PByte(t)));
- T.Free;
- end;
- procedure TTestClassExtendedRTTI.TestDeclaredFields;
- Var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- Obj2 : TRttiObject;
- RttiData2 : TRttiInstanceType absolute obj2;
- A : TRttiFieldArray;
- I : Integer;
- begin
- Obj:=FCtx.GetType(TFieldRTTIChild.ClassInfo);
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- Obj2:=FCtx.GetType(TFieldRTTI.ClassInfo);
- AssertEquals('Correct class type',TRttiInstanceType,Obj2.ClassType);
- A:=RttiData.GetFields;
- AssertEquals('Class field Count',11,Length(A));
- For I:=1 to 10 do
- AssertSame('Field parent'+IntToStr(i)+' is parent class', Obj2,A[I].Parent);
- A:=RttiData.GetDeclaredFields;
- AssertEquals('Class declared field Count',1,Length(A));
- AssertSame('Declared Field parent is sels', Obj,A[0].Parent);
- end;
- procedure TTestClassExtendedRTTI.TestProperties;
- Var
- A : TRttiPropertyArray;
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- aCount : Integer;
- begin
- Obj:=FCtx.GetType(TFieldRTTI.ClassInfo);
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- A:=RttiData.GetProperties;
- aCount:=Length(A);
- AssertEquals('Property Count',8,aCount);
- CheckProperty(0, A[0],'PrivateA',tkInteger,mvPrivate);
- CheckProperty(1, A[1],'PrivateB',tkInteger,mvPrivate,True);
- CheckProperty(2, A[2],'ProtectedA',tkInteger,mvProtected);
- CheckProperty(3, A[3],'ProtectedB',tkInteger,mvProtected,True);
- CheckProperty(4, A[4],'PublicA',tkInteger,mvPublic);
- CheckProperty(5, A[5],'PublicB',tkInteger,mvPublic);
- CheckProperty(6, A[6],'PublishedA',tkInteger,mvPublished);
- CheckProperty(7, A[7],'PublishedB',tkInteger,mvPublished);
- end;
- procedure TTestClassExtendedRTTI.TestDeclaredMethods;
- Var
- A : TRttiMethodArray;
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- Parms : TRttiParameterArray;
- aCount : Integer;
- begin
- Obj:=FCtx.GetType(TMethodClassRTTI.ClassInfo);
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- A:=RttiData.GetDeclaredMethods;
- aCount:=Length(A);
- AssertEquals('Full Count',12,aCount);
- CheckMethod('Full',0, A[0],'PrivateMethodA',mvPrivate);
- CheckMethod('Full',1, A[1],'PrivateMethodB',mvPrivate,True);
- CheckMethod('Full',2, A[2],'PrivateMethodC',mvPrivate);
- CheckMethod('Full',3, A[3],'ProtectedMethodA',mvProtected);
- CheckMethod('Full',4, A[4],'ProtectedMethodB',mvProtected,True);
- CheckMethod('Full',5, A[5],'ProtectedMethodC',mvProtected);
- CheckMethod('Full',6, A[6],'PublicMethodA',mvPublic);
- CheckMethod('Full',7, A[7],'PublicMethodB',mvPublic);
- CheckMethod('Full',8, A[8],'PublicMethodC',mvPublic);
- CheckMethod('Full',9, A[9],'PublishedMethodA',mvPublished);
- CheckMethod('Full',10, A[10],'PublishedMethodB',mvPublished);
- CheckMethod('Full',11, A[11],'PublishedMethodC',mvPublished);
- Parms:=A[9].GetParameters;
- AssertEquals('Parameter length',1,Length(Parms));
- AssertEquals('Parameter name','a',Parms[0].Name);
- end;
- procedure TTestClassExtendedRTTI.TestMethods;
- Var
- A : TRttiMethodArray;
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- aCount : Integer;
- begin
- Obj:=FCtx.GetType(TAdditionalMethodClassRTTI.ClassInfo);
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- A:=RttiData.GetMethods;
- aCount:=Length(A);
- AssertEquals('Full Count',13,aCount);
- CheckMethod('Full',0, A[0],'PublicAdditionalMethod',mvPublic);
- A:=RttiData.GetDeclaredMethods;
- aCount:=Length(A);
- AssertEquals('Full declared Count',1,aCount);
- CheckMethod('Full declared',1, A[0],'PublicAdditionalMethod',mvPublic);
- end;
- procedure TTestClassExtendedRTTI.TestMethodByAddress;
- var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- M1,M2 : TRttiMethod;
- begin
- Obj:=FCtx.GetType(TAdditionalMethodClassRTTI.ClassInfo);
- M1:=RttiData.GetMethod('PublicAdditionalMethod');
- AssertNotNull('have method',m1);
- M2:=RttiData.GetMethod(@TAdditionalMethodClassRTTI.PublicAdditionalMethod);
- AssertSame('Correct method ',M1,M2);
- end;
- procedure TTestClassExtendedRTTI.TestMethodsInherited;
- Var
- A : TRttiMethodArray;
- Obj,Obj2 : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- RttiData2 : TRttiInstanceType absolute obj2;
- i,aCount : Integer;
- begin
- Obj:=FCtx.GetType(TAdditionalMethodClassRTTI.ClassInfo);
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- Obj2:=FCtx.GetType(TMethodClassRTTI.ClassInfo);
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- A:=RttiData.GetMethods;
- aCount:=Length(A);
- AssertEquals('Full Count',13,aCount);
- For I:=1 to 12 do
- AssertSame('Parent is RTTI of parent class',Obj2,A[I].Parent);
- A:=RttiData.GetDeclaredMethods;
- aCount:=Length(A);
- AssertEquals('Full declared Count',1,aCount);
- CheckMethod('Full declared',1, A[0],'PublicAdditionalMethod',mvPublic);
- AssertSame('Parent is RTTI of parent class',Obj,A[0].Parent);
- end;
- procedure TTestClassExtendedRTTI.TestPrivateFieldAttributes;
- var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- Attrs : TCustomAttributeArray;
- Fld : TRttiField;
- O : TCustomAttribute;
- M2 : My2Attribute absolute O;
- begin
- Obj:=FCtx.GetType(TypeInfo(TFieldObject));
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- Fld:=RttiData.GetField('PrivateField');
- AssertNotNull('Have field',Fld);
- Attrs:=Fld.GetAttributes;
- AssertNotNull('Have attribute data',Pointer(Attrs));
- AssertEquals('attribute count',3,Length(Attrs));
- AssertEquals('Attribute 1 name','WeakAttribute',Attrs[0].ClassName);
- AssertEquals('Attribute 2 name','MyAttribute',Attrs[1].ClassName);
- AssertEquals('Attribute 2 name','My2Attribute',Attrs[2].ClassName);
- O:=Attrs[2];
- AssertNotNull('Attribute class ',O);
- AssertEquals('Attribute class ',O.ClassType,My2Attribute);
- AssertEquals('Attribute value ',2,M2.Int);
- end;
- procedure TTestClassExtendedRTTI.TestProtectedFieldAttributes;
- var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- Attrs : TCustomAttributeArray;
- Fld : TRttiField;
- O : TCustomAttribute;
- M2 : My2Attribute absolute O;
- begin
- Obj:=FCtx.GetType(TypeInfo(TFieldObject));
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- Fld:=RttiData.GetField('ProtectedField');
- AssertNotNull('Have field',Fld);
- Attrs:=Fld.GetAttributes;
- AssertNotNull('Have attribute data',Pointer(Attrs));
- AssertEquals('attribute count',1,Length(Attrs));
- AssertEquals('Attribute 1 name','My2Attribute',Attrs[0].ClassName);
- O:=Attrs[0];
- AssertNotNull('Attribute class ',O);
- AssertEquals('Attribute class ',O.ClassType,My2Attribute);
- AssertEquals('Attribute value ',3,M2.Int);
- end;
- procedure TTestClassExtendedRTTI.TestPublicFieldAttributes;
- var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- Attrs : TCustomAttributeArray;
- Fld : TRttiField;
- O : TCustomAttribute;
- M3 : My3Attribute absolute O;
- aCount : Integer;
- begin
- Obj:=FCtx.GetType(TypeInfo(TFieldObject));
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- aCount:=0;
- For Fld in RttiData.GetFields do
- if Fld.Visibility=mvPublic then
- inc(aCount);
- AssertEquals('Field count',3,aCount);
- // PublicField
- Fld:=RttiData.GetField('PublicField');
- AssertNotNull('Have field',Fld);
- Attrs:=Fld.GetAttributes;
- AssertNotNull('Have attribute data',Pointer(Attrs));
- AssertEquals('attribute count',1,Length(Attrs));
- AssertEquals('Attribute 1 name','My3Attribute',Attrs[0].ClassName);
- O:=Attrs[0];
- AssertNotNull('Attribute class ',O);
- AssertEquals('Attribute class ',O.ClassType,My3Attribute);
- AssertEquals('Attribute value ',4,M3.Int);
- // A
- Fld:=RttiData.GetField('A');
- AssertNotNull('A Have field',Fld);
- Attrs:=Fld.GetAttributes;
- AssertNotNull('A Have attribute data',Pointer(Attrs));
- AssertEquals('A Attribute count',1,Length(Attrs));
- AssertEquals('A Attribute 1 name','My3Attribute',Attrs[0].ClassName);
- O:=Attrs[0];
- AssertNotNull('A: Attribute class ',O);
- AssertEquals('A: Attribute class ',O.ClassType,My3Attribute);
- AssertEquals('A: Attribute value ',4,M3.Int);
- // B
- Fld:=RttiData.GetField('B');
- AssertNotNull('B Have field',Fld);
- Attrs:=Fld.GetAttributes;
- AssertNotNull('B Have attribute data',Pointer(Attrs));
- AssertEquals('A Attribute count',1,Length(Attrs));
- AssertEquals('A Attribute 1 name','My3Attribute',Attrs[0].ClassName);
- O:=Attrs[0];
- AssertNotNull('B: Attribute class ',O);
- AssertEquals('B: Attribute class ',O.ClassType,My3Attribute);
- AssertEquals('B: Attribute value ',4,M3.Int);
- end;
- procedure TTestClassExtendedRTTI.TestPrivatePropertyAttributes;
- var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- Attrs : TCustomAttributeArray;
- Prop : TRttiProperty;
- O : TCustomAttribute;
- aCount : Integer;
- M2 : My2Attribute absolute O;
- begin
- Obj:=FCtx.GetType(TypeInfo(TPropertyObject));
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- aCount:=0;
- Prop:=RttiData.GetProperty('PrivateProperty');
- AssertNotNull('Have property',Prop);
- Attrs:=Prop.GetAttributes;
- AssertNotNull('Have attribute data',Pointer(Attrs));
- AssertEquals('attribute count',3,Length(Attrs));
- AssertEquals('Attribute 1 name','WeakAttribute',Attrs[0].ClassName);
- AssertEquals('Attribute 2 name','MyAttribute',Attrs[1].ClassName);
- AssertEquals('Attribute 2 name','My2Attribute',Attrs[2].ClassName);
- O:=Attrs[2];
- AssertNotNull('Attribute class ',O);
- AssertEquals('Attribute class ',O.ClassType,My2Attribute);
- AssertEquals('Attribute value ',2,M2.Int);
- end;
- procedure TTestClassExtendedRTTI.TestProtectedPropertyAttributes;
- var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- Attrs : TCustomAttributeArray;
- Prop : TRttiProperty;
- O : TCustomAttribute;
- M2 : My2Attribute absolute O;
- begin
- Obj:=FCtx.GetType(TypeInfo(TPropertyObject));
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- Prop:=RttiData.GetProperty('ProtectedProperty');
- AssertNotNull('Have property',Prop);
- Attrs:=Prop.GetAttributes;
- AssertNotNull('Have attribute data',Pointer(Attrs));
- AssertEquals('attribute count',1,Length(Attrs));
- AssertEquals('Attribute 1 name','My2Attribute',Attrs[0].ClassName);
- O:=Attrs[0];
- AssertNotNull('Attribute class ',O);
- AssertEquals('Attribute class ',O.ClassType,My2Attribute);
- AssertEquals('Attribute value ',3,M2.Int);
- end;
- procedure TTestClassExtendedRTTI.TestPublicPropertyAttributes;
- var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- Attrs : TCustomAttributeArray;
- Prop : TRttiProperty;
- O : TCustomAttribute;
- M3 : My3Attribute absolute O;
- begin
- Obj:=FCtx.GetType(TypeInfo(TPropertyObject));
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- Prop:=RttiData.GetProperty('PublicProperty');
- AssertNotNull('Have property',Prop);
- Attrs:=Prop.GetAttributes;
- AssertNotNull('Have attribute data',Pointer(Attrs));
- AssertEquals('attribute count',1,Length(Attrs));
- AssertEquals('Attribute 1 name','My3Attribute',Attrs[0].ClassName);
- O:=Attrs[0];
- AssertNotNull('Attribute class ',O);
- AssertEquals('Attribute class ',O.ClassType,My3Attribute);
- AssertEquals('Attribute value ',4,M3.Int);
- end;
- procedure TTestClassExtendedRTTI.TestPublishedPropertyAttributes;
- var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- Attrs : TCustomAttributeArray;
- Prop : TRttiProperty;
- O : TCustomAttribute;
- M3 : My3Attribute absolute O;
- begin
- Obj:=FCtx.GetType(TypeInfo(TPropertyObject));
- AssertEquals('Correct class type',TRttiInstanceType,Obj.ClassType);
- Prop:=RttiData.GetProperty('PublishedProperty');
- AssertNotNull('Have property',Prop);
- Attrs:=Prop.GetAttributes;
- AssertNotNull('Have attribute data',Pointer(Attrs));
- AssertEquals('attribute count',1,Length(Attrs));
- AssertEquals('Attribute 1 name','My3Attribute',Attrs[0].ClassName);
- O:=Attrs[0];
- AssertNotNull('Attribute class ',O);
- AssertEquals('Attribute class ',O.ClassType,My3Attribute);
- AssertEquals('Attribute value ',5,M3.Int);
- end;
- procedure TTestClassExtendedRTTI.TestGetStaticProperty;
- var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- Prop : TRttiProperty;
- begin
- Obj:=FCtx.GetType(TypeInfo(TTestAttr2Class));
- TTestAttr2Class.StaticProp:=4539;
- Prop:=rttiData.GetProperty('StaticProp');
- AssertEquals('Class property is set or got incorrectly via methods', 4539, Prop.GetValue(nil).AsInteger);
- end;
- procedure TTestClassExtendedRTTI.TestSetStaticProperty;
- var
- Obj : TRttiObject;
- RttiData : TRttiInstanceType absolute obj;
- Prop : TRttiProperty;
- begin
- Obj:=FCtx.GetType(TypeInfo(TTestAttr2Class));
- Prop:=rttiData.GetProperty('StaticProp');
- // Write
- Prop.SetValue(nil, 4539);
- AssertEquals('Property correctly set',4539,TTestAttr2Class.StaticProp);
- end;
- { TTestRecordExtendedRTTI }
- procedure TTestRecordExtendedRTTI.TestFields;
- Var
- A : TRttiFieldArray;
- Obj : TRttiObject;
- RttiData : TRttiRecordType absolute obj;
- aCount : Integer;
- begin
- Obj:=FCtx.GetType(TypeInfo(TRecordFieldRTTI));
- AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
- A:=RttiData.GetFields;
- aCount:=Length(A);
- AssertEquals('Record fields Count',4,aCount);
- CheckField(0, A[0],'FRPrivateA',tkInteger,mvPrivate);
- CheckField(1, A[1],'FRPrivateB',tkInteger,mvPrivate);
- CheckField(4, A[2],'FRPublicA',tkInteger,mvPublic);
- CheckField(5, A[3],'FRPublicB',tkInteger,mvPublic);
- Obj:=FCtx.GetType(TypeInfo(TRecordFieldRTTIMixed));
- AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
- A:=RttiData.GetFields;
- aCount:=Length(A);
- AssertEquals('Mixed record fields Count',4,aCount);
- CheckField(0, A[0],'FRPrivateA',tkInteger,mvPrivate);
- CheckField(1, A[1],'FRPrivateB',tkInteger,mvPrivate);
- CheckField(4, A[2],'FRPublicA',tkInteger,mvPublic);
- CheckField(5, A[3],'FRPublicB',tkInteger,mvPublic);
- end;
- procedure TTestRecordExtendedRTTI.TestProperties;
- Var
- A : TRttiPropertyArray;
- Obj : TRttiObject;
- RttiData : TRttiRecordType absolute obj;
- aCount : Integer;
- begin
- // TRecordFieldRTTI
- Obj:=FCtx.GetType(TypeInfo(TRecordFieldRTTI));
- AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
- A:=RttiData.GetProperties;
- aCount:=Length(A);
- AssertEquals('Record property Count',4,aCount);
- CheckProperty(0, A[0],'RPrivateA',tkInteger,mvPrivate);
- CheckProperty(1, A[1],'RPrivateB',tkInteger,mvPrivate);
- CheckProperty(2, A[2],'RPublicA',tkInteger,mvPublic);
- CheckProperty(3, A[3],'RPublicB',tkInteger,mvPublic);
- // TRecordFieldRTTIMixed
- Obj:=FCtx.GetType(TypeInfo(TRecordFieldRTTIMixed));
- AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
- A:=RttiData.GetProperties;
- aCount:=Length(A);
- AssertEquals('Record mixed property Count',4,aCount);
- CheckProperty(0, A[0],'RPrivateA',tkInteger,mvPrivate);
- CheckProperty(1, A[1],'RPrivateB',tkInteger,mvPrivate);
- CheckProperty(2, A[2],'RPublicA',tkInteger,mvPublic);
- CheckProperty(3, A[3],'RPublicB',tkInteger,mvPublic);
- end;
- procedure TTestRecordExtendedRTTI.TestDeclaredMethods;
- Var
- A : TRttiMethodArray;
- Obj : TRttiObject;
- RttiData : TRttiRecordType absolute obj;
- aCount : Integer;
- Parms : TRttiParameterArray;
- begin
- Obj:=FCtx.GetType(TypeInfo(TRecordMethodRTTI));
- AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
- A:=RttiData.GetDeclaredMethods;
- aCount:=Length(A);
- AssertEquals('Method Full Count',4,aCount);
- CheckMethod('Full',0, A[0],'PrivateMethodA',mvPrivate);
- CheckMethod('Full',1, A[1],'PrivateMethodB',mvPrivate);
- CheckMethod('Full',2, A[2],'PublicMethodA',mvPublic);
- CheckMethod('Full',3, A[3],'PublicMethodB',mvPublic);
- Parms:=A[3].GetParameters;
- AssertEquals('Parameter length',1,Length(Parms));
- AssertNotNull('Have Parameter',Parms[0]);
- AssertEquals('Parameter name','I',Parms[0].Name);
- end;
- procedure TTestRecordExtendedRTTI.TestMethods;
- Var
- A : TRttiMethodArray;
- Obj : TRttiObject;
- RttiData : TRttiRecordType absolute obj;
- aCount : Integer;
- begin
- Obj:=FCtx.GetType(TypeInfo(TRecordMethodRTTI));
- AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
- A:=RttiData.GetDeclaredMethods;
- aCount:=Length(A);
- // Just check that the count is correct
- AssertEquals('Method Full Count',4,aCount);
- end;
- Procedure TTestRecordExtendedRTTI.TestPrivateFieldAttributes;
- var
- Obj : TRttiObject;
- RttiData : TRttiRecordType absolute obj;
- Attrs : TCustomAttributeArray;
- Fld : TRttiField;
- O : TCustomAttribute;
- M2 : My2Attribute absolute O;
- begin
- Obj:=FCtx.GetType(TypeInfo(TFieldRecord));
- AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
- Fld:=RttiData.GetField('PrivateField');
- AssertNotNull('Have field',Fld);
- Attrs:=Fld.GetAttributes;
- AssertNotNull('Have attribute data',Pointer(Attrs));
- AssertEquals('attribute count',3,Length(Attrs));
- AssertEquals('Attribute 1 name','WeakAttribute',Attrs[0].ClassName);
- AssertEquals('Attribute 2 name','MyAttribute',Attrs[1].ClassName);
- AssertEquals('Attribute 2 name','My2Attribute',Attrs[2].ClassName);
- O:=Attrs[2];
- AssertNotNull('Attribute class ',O);
- AssertEquals('Attribute class ',O.ClassType,My2Attribute);
- AssertEquals('Attribute value ',2,M2.Int);
- end;
- Procedure TTestRecordExtendedRTTI.TestPublicFieldAttributes;
- var
- Obj : TRttiObject;
- RttiData : TRttiRecordType absolute obj;
- Attrs : TCustomAttributeArray;
- Fld : TRttiField;
- O : TCustomAttribute;
- M3 : My3Attribute absolute O;
- aCount : Integer;
- begin
- Obj:=FCtx.GetType(TypeInfo(TFieldRecord));
- AssertEquals('Correct class type',TRttiRecordType,Obj.ClassType);
- aCount:=0;
- For Fld in RttiData.GetFields do
- if Fld.Visibility=mvPublic then
- inc(aCount);
- AssertEquals('Field count',3,aCount);
- // PublicField
- Fld:=RttiData.GetField('PublicField');
- AssertNotNull('Have field',Fld);
- Attrs:=Fld.GetAttributes;
- AssertNotNull('Have attribute data',Pointer(Attrs));
- AssertEquals('attribute count',1,Length(Attrs));
- AssertEquals('Attribute 1 name','My3Attribute',Attrs[0].ClassName);
- O:=Attrs[0];
- AssertNotNull('Attribute class ',O);
- AssertEquals('Attribute class ',O.ClassType,My3Attribute);
- AssertEquals('Attribute value ',3,M3.Int);
- // A
- Fld:=RttiData.GetField('A');
- AssertNotNull('A Have field',Fld);
- Attrs:=Fld.GetAttributes;
- AssertNotNull('A Have attribute data',Pointer(Attrs));
- AssertEquals('A Attribute count',1,Length(Attrs));
- AssertEquals('A Attribute 1 name','My3Attribute',Attrs[0].ClassName);
- O:=Attrs[0];
- AssertNotNull('A: Attribute class ',O);
- AssertEquals('A: Attribute class ',O.ClassType,My3Attribute);
- AssertEquals('A: Attribute value ',4,M3.Int);
- // B
- Fld:=RttiData.GetField('B');
- AssertNotNull('B Have field',Fld);
- Attrs:=Fld.GetAttributes;
- AssertNotNull('B Have attribute data',Pointer(Attrs));
- AssertEquals('A Attribute count',1,Length(Attrs));
- AssertEquals('A Attribute 1 name','My3Attribute',Attrs[0].ClassName);
- O:=Attrs[0];
- AssertNotNull('B: Attribute class ',O);
- AssertEquals('B: Attribute class ',O.ClassType,My3Attribute);
- AssertEquals('B: Attribute value ',4,M3.Int);
- end;
- initialization
- {$ifdef fpc}
- RegisterTest(TTestRTTI);
- RegisterTest(TTestClassExtendedRTTI);
- RegisterTest(TTestRecordExtendedRTTI);
- {$else fpc}
- RegisterTest(TTestRTTI.Suite);
- RegisterTest(TTestClassExtendedRTTI.suite);
- RegisterTest(TTestRecordExtendedRTTI.Suite);
- {$endif fpc}
- end.
|