|
@@ -34,7 +34,18 @@ type
|
|
|
procedure TestPropGetValueString;
|
|
|
procedure TestPropGetValueInteger;
|
|
|
procedure TestPropGetValueBoolean;
|
|
|
+ procedure TestPropGetValueProcString;
|
|
|
+ procedure TestPropGetValueProcInteger;
|
|
|
+ procedure TestPropGetValueProcBoolean;
|
|
|
+
|
|
|
+ procedure TestPropSetValueString;
|
|
|
+ procedure TestPropSetValueInteger;
|
|
|
+ procedure TestPropSetValueBoolean;
|
|
|
+
|
|
|
procedure TestGetValueStringCastError;
|
|
|
+ procedure TestMakeObject;
|
|
|
+ procedure TestGetIsReadable;
|
|
|
+ procedure TestIsWritable;
|
|
|
end;
|
|
|
|
|
|
implementation
|
|
@@ -65,18 +76,50 @@ type
|
|
|
property PubPropSetRW: integer read FPubPropRW write FPubPropRW;
|
|
|
end;
|
|
|
|
|
|
+ { TTestValueClass }
|
|
|
+
|
|
|
TTestValueClass = class
|
|
|
private
|
|
|
FAInteger: integer;
|
|
|
FAString: string;
|
|
|
FABoolean: boolean;
|
|
|
+ function GetAInteger: integer;
|
|
|
+ function GetAString: string;
|
|
|
+ function GetABoolean: boolean;
|
|
|
+ procedure SetWriteOnly(AValue: integer);
|
|
|
published
|
|
|
property AInteger: Integer read FAInteger write FAInteger;
|
|
|
property AString: string read FAString write FAString;
|
|
|
property ABoolean: boolean read FABoolean write FABoolean;
|
|
|
+ property AGetInteger: Integer read GetAInteger;
|
|
|
+ property AGetString: string read GetAString;
|
|
|
+ property AGetBoolean: boolean read GetABoolean;
|
|
|
+ property AWriteOnly: integer write SetWriteOnly;
|
|
|
end;
|
|
|
|
|
|
|
|
|
+{ 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;
|
|
|
+
|
|
|
+procedure TTestValueClass.SetWriteOnly(AValue: integer);
|
|
|
+begin
|
|
|
+ // Do nothing
|
|
|
+end;
|
|
|
+
|
|
|
{ TIntAttribute }
|
|
|
|
|
|
constructor TIntAttribute.create(AInt: integer);
|
|
@@ -135,6 +178,61 @@ begin
|
|
|
end;
|
|
|
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);
|
|
|
+ CheckEquals(TTestValueClass(AValue.AsObject).AInteger, 54329);
|
|
|
+ ATestClass.Free;
|
|
|
+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;
|
|
|
+
|
|
|
procedure TTestCase1.TestPropGetValueBoolean;
|
|
|
var
|
|
|
ATestClass : TTestValueClass;
|
|
@@ -222,7 +320,7 @@ begin
|
|
|
CheckEquals('Hello World',AValue.ToString);
|
|
|
Check(TypeInfo(string)=AValue.TypeInfo);
|
|
|
Check(AValue.TypeData=GetTypeData(AValue.TypeInfo));
|
|
|
-// Check(AValue.IsEmpty=false);
|
|
|
+ Check(AValue.IsEmpty=false);
|
|
|
Check(AValue.IsObject=false);
|
|
|
Check(AValue.IsClass=false);
|
|
|
CheckEquals(AValue.IsOrdinal, false);
|
|
@@ -239,6 +337,182 @@ begin
|
|
|
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.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(s), 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(i), 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(b), AValue);
|
|
|
+ AProperty.SetValue(ATestClass, AValue);
|
|
|
+ CheckEquals(ATestClass.ABoolean, b);
|
|
|
+ b := false;
|
|
|
+ CheckEquals(ATestClass.ABoolean, true);
|
|
|
+ TValue.Make(@b, TypeInfo(b), AValue);
|
|
|
+ AProperty.SetValue(ATestClass, AValue);
|
|
|
+ CheckEquals(ATestClass.ABoolean, false);
|
|
|
+ finally
|
|
|
+ AtestClass.Free;
|
|
|
+ end;
|
|
|
+ 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;
|
|
|
+ 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('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;
|