THRIFT-5620 Option to force usage of COM types to allow for cross-module references
Client: Delphi
Patch: Jens Geyer
diff --git a/lib/delphi/test/serializer/TestSerializer.Tests.pas b/lib/delphi/test/serializer/TestSerializer.Tests.pas
index 83d67b1..443a22d 100644
--- a/lib/delphi/test/serializer/TestSerializer.Tests.pas
+++ b/lib/delphi/test/serializer/TestSerializer.Tests.pas
@@ -41,8 +41,7 @@
   Thrift.WinHTTP,
   Thrift.TypeRegistry,
   System_,
-  DebugProtoTest,
-  TestSerializer.Data;
+  DebugProtoTest;
 
 
 type
@@ -58,7 +57,7 @@
       mt_Stream
     );
 
-  private
+  strict private
     FProtocols : TList< TFactoryPair>;
     procedure AddFactoryCombination( const aProto : IProtocolFactory; const aTrans : ITransportFactory);
     class function UserFriendlyName( const factory : TFactoryPair) : string;  overload;
@@ -73,7 +72,14 @@
     class procedure ValidateReadToEnd( const input : TBytes; const serial : TDeserializer);  overload;
     class procedure ValidateReadToEnd( const input : TStream; const serial : TDeserializer);  overload;
 
+    class function LengthOf( const bytes : TBytes) : Integer; overload; inline;
+    class function LengthOf( const bytes : IThriftBytes) : Integer; overload; inline;
+
+    class function DataPtrOf( const bytes : TBytes) : Pointer; overload; inline;
+    class function DataPtrOf( const bytes : IThriftBytes) : Pointer; overload; inline;
+
     procedure Test_Serializer_Deserializer;
+    procedure Test_COM_Types;
     procedure Test_OneOfEach(     const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
     procedure Test_CompactStruct( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
 
@@ -87,6 +93,12 @@
 
 implementation
 
+const SERIALIZERDATA_DLL = 'SerializerData.dll';
+function CreateOneOfEach : IOneOfEach; stdcall; external SERIALIZERDATA_DLL;
+function CreateNesting : INesting; stdcall; external SERIALIZERDATA_DLL;
+function CreateHolyMoley : IHolyMoley; stdcall; external SERIALIZERDATA_DLL;
+function CreateCompactProtoTestStruct : ICompactProtoTestStruct; stdcall; external SERIALIZERDATA_DLL;
+
 
 { TTestSerializer }
 
@@ -128,13 +140,41 @@
 end;
 
 
+class function TTestSerializer.LengthOf( const bytes : TBytes) : Integer;
+begin
+  result := Length(bytes);
+end;
+
+
+class function TTestSerializer.LengthOf( const bytes : IThriftBytes) : Integer;
+begin
+  if bytes <> nil
+  then result := bytes.Count
+  else result := 0;
+end;
+
+
+class function TTestSerializer.DataPtrOf( const bytes : TBytes) : Pointer;
+begin
+  result := bytes;
+end;
+
+
+class function TTestSerializer.DataPtrOf( const bytes : IThriftBytes) : Pointer;
+begin
+  if bytes <> nil
+  then result := bytes.QueryRawDataPtr
+  else result := nil;
+end;
+
+
 procedure TTestSerializer.Test_OneOfEach( const method : TMethod; const factory : TFactoryPair; const stream : TFileStream);
 var tested, correct : IOneOfEach;
     bytes   : TBytes;
     i : Integer;
 begin
   // write
-  tested := Fixtures.CreateOneOfEach;
+  tested := CreateOneOfEach;
   case method of
     mt_Bytes:  bytes := Serialize( tested, factory);
     mt_Stream: begin
@@ -158,7 +198,7 @@
   end;
 
   // check
-  correct := Fixtures.CreateOneOfEach;
+  correct := CreateOneOfEach;
   ASSERT( tested.Im_true = correct.Im_true);
   ASSERT( tested.Im_false = correct.Im_false);
   ASSERT( tested.A_bite = correct.A_bite);
@@ -170,8 +210,8 @@
   ASSERT( tested.Zomg_unicode = correct.Zomg_unicode);
   ASSERT( tested.What_who = correct.What_who);
 
-  ASSERT( Length(tested.Base64) = Length(correct.Base64));
-  ASSERT( CompareMem( @tested.Base64[0], @correct.Base64[0], Length(correct.Base64)));
+  ASSERT( LengthOf(tested.Base64) = LengthOf(correct.Base64));
+  ASSERT( CompareMem( DataPtrOf(tested.Base64), DataPtrOf(correct.Base64), LengthOf(correct.Base64)));
 
   ASSERT( tested.Byte_list.Count = correct.Byte_list.Count);
   for i := 0 to tested.Byte_list.Count-1
@@ -192,7 +232,7 @@
     bytes   : TBytes;
 begin
   // write
-  tested := Fixtures.CreateCompactProtoTestStruct;
+  tested := CreateCompactProtoTestStruct;
   case method of
     mt_Bytes:  bytes := Serialize( tested, factory);
     mt_Stream: begin
@@ -216,7 +256,7 @@
   end;
 
   // check
-  correct := Fixtures.CreateCompactProtoTestStruct;
+  correct := CreateCompactProtoTestStruct;
   ASSERT( correct.Field500  = tested.Field500);
   ASSERT( correct.Field5000  = tested.Field5000);
   ASSERT( correct.Field20000 = tested.Field20000);
@@ -269,10 +309,25 @@
 end;
 
 
+procedure TTestSerializer.Test_COM_Types;
+var tested : IOneOfEach;
+begin
+  {$IF cDebugProtoTest_Option_COM_types}
+  ASSERT( SizeOf(TSomeEnum) = SizeOf(Int32));  // -> MINENUMSIZE 4
+
+  // try to set values that allocate memory
+  tested := CreateOneOfEach;
+  tested.Zomg_unicode := 'This is a test';
+  tested.Base64 := TThriftBytesImpl.Create( TEncoding.UTF8.GetBytes('abc'));
+  {$IFEND}
+end;
+
+
 procedure TTestSerializer.RunTests;
 begin
   try
     Test_Serializer_Deserializer;
+    Test_COM_Types;
   except
     on e:Exception do begin
       Writeln( e.ClassName+': '+ e.Message);