From 431f449e04923ce139ce05d1b0d453b1714f1846 Mon Sep 17 00:00:00 2001 From: Luis Carrasco Date: Fri, 20 Nov 2015 10:04:33 -0700 Subject: [PATCH] Se actualiza Delphi Mocks a ultima version. --- Delphi.Mocks.MethodData.pas | 2 +- Delphi.Mocks.ParamMatcher.pas | 4 +-- Delphi.Mocks.Proxy.pas | 28 +++++++++++++++++-- Delphi.Mocks.pas | 9 +++++- Examples/Delphi.Mocks.Examples.Factory.pas | 18 +++++++----- Examples/Delphi.Mocks.Examples.Implement.pas | 2 ++ Examples/Delphi.Mocks.Examples.Interfaces.pas | 3 ++ Examples/Delphi.Mocks.Examples.Matchers.pas | 2 ++ Examples/Delphi.Mocks.Examples.Objects.pas | 2 ++ Tests/Delphi.Mocks.Tests.AutoMock.pas | 2 ++ Tests/Delphi.Mocks.Tests.Base.pas | 2 ++ Tests/Delphi.Mocks.Tests.Behavior.pas | 2 ++ Tests/Delphi.Mocks.Tests.Expectations.pas | 2 ++ Tests/Delphi.Mocks.Tests.InterfaceProxy.pas | 2 ++ Tests/Delphi.Mocks.Tests.Interfaces.pas | 2 ++ Tests/Delphi.Mocks.Tests.MethodData.pas | 4 ++- Tests/Delphi.Mocks.Tests.ObjectProxy.pas | 2 ++ Tests/Delphi.Mocks.Tests.Objects.pas | 3 ++ Tests/Delphi.Mocks.Tests.OpenArrayIntf.pas | 2 ++ Tests/Delphi.Mocks.Tests.Proxy.pas | 2 ++ Tests/Delphi.Mocks.Tests.ProxyBase.pas | 2 ++ Tests/Delphi.Mocks.Tests.TValue.pas | 3 +- Tests/Delphi.Mocks.Tests.Utils.pas | 12 ++++---- Tests/Delphi.Mocks.Utils.Tests.pas | 2 ++ 24 files changed, 94 insertions(+), 20 deletions(-) diff --git a/Delphi.Mocks.MethodData.pas b/Delphi.Mocks.MethodData.pas index 71b9937..f971f18 100644 --- a/Delphi.Mocks.MethodData.pas +++ b/Delphi.Mocks.MethodData.pas @@ -283,7 +283,7 @@ procedure TMethodData.MockNoBehaviourRecordHit(const Args: TArray; const result := TValue.From(mock); //Add a behaviour to return the value next time. - behavior := TBehavior.CreateWillReturnWhen(Args, Result, []); + behavior := TBehavior.CreateWillReturnWhen(Args, Result, TArray.Create()); FBehaviors.Add(behavior); end else diff --git a/Delphi.Mocks.ParamMatcher.pas b/Delphi.Mocks.ParamMatcher.pas index 64ca979..3d75b6e 100644 --- a/Delphi.Mocks.ParamMatcher.pas +++ b/Delphi.Mocks.ParamMatcher.pas @@ -52,7 +52,7 @@ TMatcher = class(TInterfacedObject, IMatcher) TMatcherFactory = class private class var - FMatchers : TDictionary>; + FMatchers : TObjectDictionary>; FLock : TObject; protected class constructor Create; @@ -95,7 +95,7 @@ function TMatcher.Match(const value: TValue): boolean; class constructor TMatcherFactory.Create; begin - FMatchers := TDictionary>.Create; + FMatchers := TObjectDictionary>.Create([doOwnsValues]); FLock := TObject.Create; end; diff --git a/Delphi.Mocks.Proxy.pas b/Delphi.Mocks.Proxy.pas index f5856ab..f037c8a 100644 --- a/Delphi.Mocks.Proxy.pas +++ b/Delphi.Mocks.Proxy.pas @@ -66,6 +66,7 @@ TProxy = class(TWeakReferencedObject, IWeakReferenceableObject, IInterface, //behavior setup FNextBehavior : TBehaviorType; FReturnValue : TValue; + FReturnValueNilAllowed : Boolean; FNextFunc : TExecuteFunc; FExceptClass : ExceptClass; FExceptionMessage : string; @@ -127,8 +128,10 @@ TProxyVirtualInterface = class(TVirtualInterface, IInterface, IProxyVirtualI function Expect : IExpect; {$Message 'TODO: Implement ISetup.Before and ISetup.After.'} - function WillReturn(const value : TValue) : IWhen; + function WillReturn(const value : TValue) : IWhen; overload; + function WillReturn(const value : TValue; const AllowNil: Boolean) : IWhen; overload; procedure WillReturnDefault(const AMethodName : string; const value : TValue); + function WillReturnNil: IWhen; function WillRaise(const exceptionClass : ExceptClass; const message : string = '') : IWhen; overload; procedure WillRaise(const AMethodName : string; const exceptionClass : ExceptClass; const message : string = ''); overload; @@ -457,7 +460,8 @@ procedure TProxy.DoInvoke(Method: TRttiMethod; const Args: TArray; ou //We don't test for the return type being valid as XE5 and below have a RTTI bug which does not return //a return type for function which reference their own class/interface. Even when RTTI is specified on //the declaration and forward declaration. - if (FReturnValue.IsEmpty) then + if (FReturnValue.IsEmpty and not FReturnValueNilAllowed) or + (FReturnValueNilAllowed and ((FReturnValue.TypeInfo = nil) or (FReturnValue.TypeData = nil))) then raise EMockSetupException.CreateFmt('Setup.WillReturn call on method [%s] was not passed a return value.', [Method.Name]); methodData.WillReturnWhen(Args,FReturnValue,matchers); @@ -618,6 +622,8 @@ procedure TProxy.Once(const AMethodName: string); methodData : IMethodData; pInfo : PTypeInfo; begin + pInfo := TypeInfo(T); + methodData := GetMethodData(AMethodName,pInfo.NameStr); Assert(methodData <> nil); methodData.Once; @@ -797,6 +803,15 @@ function TProxy.WillReturn(const value: TValue): IWhen; result := TWhen.Create(Self.Proxy); end; +function TProxy.WillReturn(const value: TValue; const AllowNil: Boolean): IWhen; +begin + FSetupMode := TSetupMode.Behavior; + FReturnValue := value; + FReturnValueNilAllowed := AllowNil; + FNextBehavior := TBehaviorType.WillReturn; + result := TWhen.Create(Self.Proxy); +end; + procedure TProxy.WillReturnDefault(const AMethodName : string; const value : TValue); var methodData : IMethodData; @@ -810,6 +825,15 @@ procedure TProxy.WillReturnDefault(const AMethodName : string; const value : ClearSetupState; end; +function TProxy.WillReturnNil: IWhen; +begin + FSetupMode := TSetupMode.Behavior; + FReturnValue := TValue.From(nil); + FReturnValueNilAllowed := True; + FNextBehavior := TBehaviorType.WillReturn; + result := TWhen.Create(Self.Proxy); +end; + function TProxy._AddRef: Integer; begin result := inherited; diff --git a/Delphi.Mocks.pas b/Delphi.Mocks.pas index 5a3d300..b2be975 100644 --- a/Delphi.Mocks.pas +++ b/Delphi.Mocks.pas @@ -93,7 +93,14 @@ interface procedure SetAllowRedefineBehaviorDefinitions(const value : boolean); //set the return value for a method when called with the parameters specified on the When - function WillReturn(const value : TValue) : IWhen; + function WillReturn(const value : TValue) : IWhen; overload; + + //set the return value for a method when called with the parameters specified on the When + //AllowNil flag allow to define: returning nil value is allowed or not. + function WillReturn(const value : TValue; const AllowNil: Boolean) : IWhen; overload; + + //set the nil as return value for a method when called with the parameters specified on the When + function WillReturnNil : IWhen; //Will exedute the func when called with the specified parameters function WillExecute(const func : TExecuteFunc) : IWhen;overload; diff --git a/Examples/Delphi.Mocks.Examples.Factory.pas b/Examples/Delphi.Mocks.Examples.Factory.pas index 0203345..e1bb69a 100644 --- a/Examples/Delphi.Mocks.Examples.Factory.pas +++ b/Examples/Delphi.Mocks.Examples.Factory.pas @@ -78,11 +78,13 @@ TLogExporter = class(TInterfacedObject, ILogExporter) end; {$M-} + {$M+} TExample_MockFactoryTests = class published procedure Implement_Multiple_Interfaces; procedure Create_T_From_TypeInfo; end; + {$M-} IFakeGeneric = interface ['{682057B0-E265-45F1-ABF7-12A25683AF63}'] @@ -121,6 +123,7 @@ implementation function CreateFakeGeneric(const TypeInfo: PTypeInfo) : TObject; begin + result := nil; end; { TLogExporter } @@ -167,6 +170,8 @@ function TLogExporter.ExportLog(const AMinLogLevel : Integer; const AFilename: T //Write each line out with the formatting from the log. for iLine := 0 to logs.Count - 1 do fileService.WriteLineTo(fileHandle, logs.Line[iLine].FormattedLine); + + result := 0; end; { TExample_MockFactoryTests } @@ -186,12 +191,12 @@ procedure TExample_MockFactoryTests.Create_T_From_TypeInfo; end; procedure TExample_MockFactoryTests.Implement_Multiple_Interfaces; -var - logExporterSUT : ILogExporter; - - // mockFactory : TMockFactory; - mockContainer : TAutoMockContainer; - mockCoreService : TMock; + //var + // logExporterSUT : ILogExporter; + // + // // mockFactory : TMockFactory; + // mockContainer : TAutoMockContainer; + // mockCoreService : TMock; begin //CREATE - Create a mock of the CoreService which we require for the LogExporter // We do this through creating a MockFactory to generate the Mock @@ -216,7 +221,6 @@ procedure TExample_MockFactoryTests.Implement_Multiple_Interfaces; constructor TFakeGeneric.Create(const ATypeInfo: PTypeInfo); var - AValue: TValue; ctx: TRttiContext; rType: TRttiType; AMethCreate: TRttiMethod; diff --git a/Examples/Delphi.Mocks.Examples.Implement.pas b/Examples/Delphi.Mocks.Examples.Implement.pas index 44c09ca..54c573a 100644 --- a/Examples/Delphi.Mocks.Examples.Implement.pas +++ b/Examples/Delphi.Mocks.Examples.Implement.pas @@ -7,6 +7,7 @@ interface DUnitX.TestFramework; type + {$M+} TExample_InterfaceImplementTests = class published procedure Implement_Single_Interface; @@ -14,6 +15,7 @@ TExample_InterfaceImplementTests = class procedure SetupAndVerify_Mulitple_Interfaces; procedure SetupAndVerify_Object_And_Interfaces; end; + {$M-} implementation diff --git a/Examples/Delphi.Mocks.Examples.Interfaces.pas b/Examples/Delphi.Mocks.Examples.Interfaces.pas index 5e726a7..562fbe2 100644 --- a/Examples/Delphi.Mocks.Examples.Interfaces.pas +++ b/Examples/Delphi.Mocks.Examples.Interfaces.pas @@ -28,10 +28,13 @@ TSystemUnderTest = class(TInterfacedObject, TSystemUnderTestInf) procedure CallsSimpleInterfaceMethod; end; + {$M+} TMockObjectTests = class published procedure Simple_Interface_Mock; end; + {$M-} + implementation diff --git a/Examples/Delphi.Mocks.Examples.Matchers.pas b/Examples/Delphi.Mocks.Examples.Matchers.pas index c2dac62..dcc983a 100644 --- a/Examples/Delphi.Mocks.Examples.Matchers.pas +++ b/Examples/Delphi.Mocks.Examples.Matchers.pas @@ -18,10 +18,12 @@ interface end; {$M-} + {$M+} TExample_MatchersTests = class published procedure Match_parameter_values; end; + {$M-} implementation diff --git a/Examples/Delphi.Mocks.Examples.Objects.pas b/Examples/Delphi.Mocks.Examples.Objects.pas index 489bf22..5dea5d4 100644 --- a/Examples/Delphi.Mocks.Examples.Objects.pas +++ b/Examples/Delphi.Mocks.Examples.Objects.pas @@ -24,10 +24,12 @@ TSystemUnderTest = class(TObject) procedure CallsSimpleMethodOnMock;virtual; end; + {$M+} TMockObjectTests = class published procedure MockObject_Can_Call_Function;virtual; end; + {$M-} implementation diff --git a/Tests/Delphi.Mocks.Tests.AutoMock.pas b/Tests/Delphi.Mocks.Tests.AutoMock.pas index 2270008..ba64a32 100644 --- a/Tests/Delphi.Mocks.Tests.AutoMock.pas +++ b/Tests/Delphi.Mocks.Tests.AutoMock.pas @@ -36,11 +36,13 @@ TReturnedObject = class(TObject) end; {$M-} + {$M+} TAutoMockTests = class published procedure AutoMock_Can_Mock_Interface; procedure AutoMock_Automatically_Mocks_Contained_Returned_Interface; end; + {$M-} implementation diff --git a/Tests/Delphi.Mocks.Tests.Base.pas b/Tests/Delphi.Mocks.Tests.Base.pas index 03f25ce..8c3d106 100644 --- a/Tests/Delphi.Mocks.Tests.Base.pas +++ b/Tests/Delphi.Mocks.Tests.Base.pas @@ -64,6 +64,7 @@ TSimpleRecord_WithoutRTTI = record //--Without RTTI + {$M+} TTestMock = class published procedure CreateMock_With_Object_Which_Has_No_RTTI_Raises_No_Exception; @@ -88,6 +89,7 @@ TTestMock = class procedure When_Proxy_With_Implemented_Interface_Returns_IProxy_From_Instance_For_Implemented_Interface; procedure When_Proxy_With_Implmeneted_Interface_Returns_IExpect_Of_Interface_From_Instance_For_Implemented_Interface; end; + {$M-} implementation diff --git a/Tests/Delphi.Mocks.Tests.Behavior.pas b/Tests/Delphi.Mocks.Tests.Behavior.pas index ed1c8a8..eb0fc1c 100644 --- a/Tests/Delphi.Mocks.Tests.Behavior.pas +++ b/Tests/Delphi.Mocks.Tests.Behavior.pas @@ -12,6 +12,7 @@ interface type ETestBehaviourException = class(Exception); + {$M+} TTestBehaviors = class private FContext : TRttiContext; @@ -44,6 +45,7 @@ TTestBehaviors = class procedure CreateWillRaise_Behavior_Type_Set_To_WillAlwaysRaise; procedure CreateWillRaiseWhen_Behavior_Type_Set_To_WillRaise; end; + {$M-} implementation diff --git a/Tests/Delphi.Mocks.Tests.Expectations.pas b/Tests/Delphi.Mocks.Tests.Expectations.pas index a6c03aa..ccc15ac 100644 --- a/Tests/Delphi.Mocks.Tests.Expectations.pas +++ b/Tests/Delphi.Mocks.Tests.Expectations.pas @@ -10,6 +10,7 @@ interface type + {$M+} TTestExpectations = class protected FMatchers : TArray; @@ -64,6 +65,7 @@ TTestExpectations = class procedure ExpectationMet_With_Before; procedure ExpectationNotMet_With_Before; end; + {$M-} implementation diff --git a/Tests/Delphi.Mocks.Tests.InterfaceProxy.pas b/Tests/Delphi.Mocks.Tests.InterfaceProxy.pas index af3b501..5040438 100644 --- a/Tests/Delphi.Mocks.Tests.InterfaceProxy.pas +++ b/Tests/Delphi.Mocks.Tests.InterfaceProxy.pas @@ -58,6 +58,7 @@ interface end; {$M-} + {$M+} TTestInterfaceProxy = class published procedure After_Proxy_AddImplement_ProxyProxy_Implements_Original_Interface; @@ -73,6 +74,7 @@ TTestInterfaceProxy = class procedure TestOuParam; procedure TestVarParam; end; + {$M-} implementation diff --git a/Tests/Delphi.Mocks.Tests.Interfaces.pas b/Tests/Delphi.Mocks.Tests.Interfaces.pas index 61605dd..e7851c3 100644 --- a/Tests/Delphi.Mocks.Tests.Interfaces.pas +++ b/Tests/Delphi.Mocks.Tests.Interfaces.pas @@ -25,6 +25,7 @@ interface end; {$M-} + {$M+} TSafeCallTest = class published [Test] @@ -36,6 +37,7 @@ TSafeCallTest = class [Test] procedure CanMockProcedureWithVariantParam; end; + {$M-} implementation diff --git a/Tests/Delphi.Mocks.Tests.MethodData.pas b/Tests/Delphi.Mocks.Tests.MethodData.pas index 59600fd..f2882d5 100644 --- a/Tests/Delphi.Mocks.Tests.MethodData.pas +++ b/Tests/Delphi.Mocks.Tests.MethodData.pas @@ -16,6 +16,7 @@ interface end; {$M-} + {$M+} TTestMethodData = class published [Test, Ignore] @@ -33,6 +34,7 @@ TTestMethodData = class [Test] procedure BehaviourMustBeDefined_IsTrue_AndBehaviourIsNotDefined_RaisesException; end; + {$M-} implementation @@ -81,7 +83,7 @@ procedure TTestMethodData.AllowRedefineBehaviorDefinitions_IsFalse_ExceptionIsTh Assert.WillRaise(procedure begin methodData.WillReturnWhen(TArray.Create(someValue1), someValue2, nil); - end, EMockException); + end, EMockSetupException); end; procedure TTestMethodData.AllowRedefineBehaviorDefinitions_IsFalse_NoExceptionIsThrown_WhenAddingNormal; diff --git a/Tests/Delphi.Mocks.Tests.ObjectProxy.pas b/Tests/Delphi.Mocks.Tests.ObjectProxy.pas index d46f447..4f9c155 100644 --- a/Tests/Delphi.Mocks.Tests.ObjectProxy.pas +++ b/Tests/Delphi.Mocks.Tests.ObjectProxy.pas @@ -34,6 +34,7 @@ TCommand = class procedure TestOutParam(out msg : string);virtual;abstract; end; + {$M+} TTestObjectProxy = class published [Test] @@ -61,6 +62,7 @@ TTestObjectProxy = class [Test] procedure TestVarParam; end; + {$M-} implementation diff --git a/Tests/Delphi.Mocks.Tests.Objects.pas b/Tests/Delphi.Mocks.Tests.Objects.pas index e04c249..3e28223 100644 --- a/Tests/Delphi.Mocks.Tests.Objects.pas +++ b/Tests/Delphi.Mocks.Tests.Objects.pas @@ -23,10 +23,13 @@ TSystemUnderTest = class(TObject) procedure CallsSimpleMethodOnMock; end; + {$M+} TMockObjectTests = class published procedure MockObject_Can_Call_Function; end; + {$M-} + implementation diff --git a/Tests/Delphi.Mocks.Tests.OpenArrayIntf.pas b/Tests/Delphi.Mocks.Tests.OpenArrayIntf.pas index 0110947..6fee4bf 100644 --- a/Tests/Delphi.Mocks.Tests.OpenArrayIntf.pas +++ b/Tests/Delphi.Mocks.Tests.OpenArrayIntf.pas @@ -6,11 +6,13 @@ interface DUnitX.TestFramework; type + {$M+} TestIOpenArray = class published procedure TestMyMethodDynamicArray; procedure TestMyMethodTypedArray; end; + {$M-} TMyArray = array of Integer; diff --git a/Tests/Delphi.Mocks.Tests.Proxy.pas b/Tests/Delphi.Mocks.Tests.Proxy.pas index 006884f..9d4713d 100644 --- a/Tests/Delphi.Mocks.Tests.Proxy.pas +++ b/Tests/Delphi.Mocks.Tests.Proxy.pas @@ -9,11 +9,13 @@ interface Delphi.Mocks; type + {$M+} TTestMock = class published [Test, Ignore] procedure Expectation_Before_Verifies_To_True_When_Prior_Method_Called_Atleast_Once; end; + {$M-} implementation diff --git a/Tests/Delphi.Mocks.Tests.ProxyBase.pas b/Tests/Delphi.Mocks.Tests.ProxyBase.pas index d81bd98..8cae513 100644 --- a/Tests/Delphi.Mocks.Tests.ProxyBase.pas +++ b/Tests/Delphi.Mocks.Tests.ProxyBase.pas @@ -13,10 +13,12 @@ interface TSimpleTestObject = class(TObject); {$M-} + {$M+} TTestProxyBase = class published procedure SetUp; end; + {$M-} implementation diff --git a/Tests/Delphi.Mocks.Tests.TValue.pas b/Tests/Delphi.Mocks.Tests.TValue.pas index 01b0c28..510af10 100644 --- a/Tests/Delphi.Mocks.Tests.TValue.pas +++ b/Tests/Delphi.Mocks.Tests.TValue.pas @@ -6,9 +6,10 @@ interface DUnitX.TestFramework; type + {$M+} TValueTests = class - published end; + {$M-} implementation diff --git a/Tests/Delphi.Mocks.Tests.Utils.pas b/Tests/Delphi.Mocks.Tests.Utils.pas index ba531da..52d18ea 100644 --- a/Tests/Delphi.Mocks.Tests.Utils.pas +++ b/Tests/Delphi.Mocks.Tests.Utils.pas @@ -8,6 +8,7 @@ interface type //Testing TValue helper methods in TValueHelper + {$M+} TTestTValue = class published procedure Test_TValue_Equals_Interfaces; @@ -19,6 +20,7 @@ TTestTValue = class procedure Test_TValue_Equals_DifferentGuid_Instance; procedure Test_TValue_NotEquals_Guid; end; + {$M-} implementation @@ -62,7 +64,7 @@ procedure TTestTValue.Test_TValue_Equals_SameGuid_Instance; s2 := s1; v1 := TValue.From( s1 ); v2 := TValue.From( s2 ); - CheckTrue(v1.Equals(v2)); + Assert.IsTrue(v1.Equals(v2)); end; procedure TTestTValue.Test_TValue_Equals_DifferentGuid_Instance; @@ -74,7 +76,7 @@ procedure TTestTValue.Test_TValue_Equals_DifferentGuid_Instance; s2 := StringToGUID( '{2933052C-79D0-48C9-86D3-8FF29416033C}' ); v1 := TValue.From( s1 ); v2 := TValue.From( s2 ); - CheckTrue(v1.Equals(v2)); + Assert.IsTrue(v1.Equals(v2)); end; procedure TTestTValue.Test_TValue_NotEquals_Guid; @@ -86,7 +88,7 @@ procedure TTestTValue.Test_TValue_NotEquals_Guid; s2 := StringToGUID( '{2933052C-79D0-48C9-86D3-8FF29416FFFF}' ); v1 := TValue.From( s1 ); v2 := TValue.From( s2 ); - CheckFalse(v1.Equals(v2)); + Assert.IsFalse(v1.Equals(v2)); end; procedure TTestTValue.Test_TValue_NotEquals_Interfaces; @@ -98,7 +100,7 @@ procedure TTestTValue.Test_TValue_NotEquals_Interfaces; i2 := TInterfacedObject.Create; v1 := TValue.From(i1); v2 := TValue.From(i2); - Assert.IsTrue(v1.Equals(v2)); + Assert.IsFalse(v1.Equals(v2)); end; procedure TTestTValue.Test_TValue_NotEquals_Strings; @@ -110,7 +112,7 @@ procedure TTestTValue.Test_TValue_NotEquals_Strings; s2 := 'goodbye'; v1 := s1; v2 := s2; - Assert.IsTrue(v1.Equals(v2)); + Assert.IsFalse(v1.Equals(v2)); end; initialization diff --git a/Tests/Delphi.Mocks.Utils.Tests.pas b/Tests/Delphi.Mocks.Utils.Tests.pas index 420853b..8953dab 100644 --- a/Tests/Delphi.Mocks.Utils.Tests.pas +++ b/Tests/Delphi.Mocks.Utils.Tests.pas @@ -4,12 +4,14 @@ interface uses DUnitX.TestFramework; type + {$M+} TUtilsTests = class published procedure CheckInterfaceHasRTTIWithoutRTTI; procedure CheckInterfaceHasRTTIWithNonInterface; procedure CheckInterfaceHasRTTIWithInterfaceRTTI; end; + {$M-} {$M+} ITestable = interface