Navigation Menu

Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Se actualiza Delphi Mocks a ultima version.
Browse files Browse the repository at this point in the history
  • Loading branch information
lcarrasco committed Nov 20, 2015
1 parent 59c1e38 commit 431f449
Show file tree
Hide file tree
Showing 24 changed files with 94 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Delphi.Mocks.MethodData.pas
Expand Up @@ -283,7 +283,7 @@ procedure TMethodData.MockNoBehaviourRecordHit(const Args: TArray<TValue>; const
result := TValue.From<IProxy>(mock);

//Add a behaviour to return the value next time.
behavior := TBehavior.CreateWillReturnWhen(Args, Result, []);
behavior := TBehavior.CreateWillReturnWhen(Args, Result, TArray<IMatcher>.Create());
FBehaviors.Add(behavior);
end
else
Expand Down
4 changes: 2 additions & 2 deletions Delphi.Mocks.ParamMatcher.pas
Expand Up @@ -52,7 +52,7 @@ TMatcher<T> = class(TInterfacedObject, IMatcher)
TMatcherFactory = class
private
class var
FMatchers : TDictionary<TThreadID,TList<IMatcher>>;
FMatchers : TObjectDictionary<TThreadID,TList<IMatcher>>;
FLock : TObject;
protected
class constructor Create;
Expand Down Expand Up @@ -95,7 +95,7 @@ function TMatcher<T>.Match(const value: TValue): boolean;

class constructor TMatcherFactory.Create;
begin
FMatchers := TDictionary<TThreadID,TList<IMatcher>>.Create;
FMatchers := TObjectDictionary<TThreadID,TList<IMatcher>>.Create([doOwnsValues]);
FLock := TObject.Create;
end;

Expand Down
28 changes: 26 additions & 2 deletions Delphi.Mocks.Proxy.pas
Expand Up @@ -66,6 +66,7 @@ TProxy<T> = class(TWeakReferencedObject, IWeakReferenceableObject, IInterface,
//behavior setup
FNextBehavior : TBehaviorType;
FReturnValue : TValue;
FReturnValueNilAllowed : Boolean;
FNextFunc : TExecuteFunc;
FExceptClass : ExceptClass;
FExceptionMessage : string;
Expand Down Expand Up @@ -127,8 +128,10 @@ TProxyVirtualInterface = class(TVirtualInterface, IInterface, IProxyVirtualI
function Expect : IExpect<T>;

{$Message 'TODO: Implement ISetup.Before and ISetup.After.'}
function WillReturn(const value : TValue) : IWhen<T>;
function WillReturn(const value : TValue) : IWhen<T>; overload;
function WillReturn(const value : TValue; const AllowNil: Boolean) : IWhen<T>; overload;
procedure WillReturnDefault(const AMethodName : string; const value : TValue);
function WillReturnNil: IWhen<T>;
function WillRaise(const exceptionClass : ExceptClass; const message : string = '') : IWhen<T>; overload;
procedure WillRaise(const AMethodName : string; const exceptionClass : ExceptClass; const message : string = ''); overload;

Expand Down Expand Up @@ -457,7 +460,8 @@ procedure TProxy<T>.DoInvoke(Method: TRttiMethod; const Args: TArray<TValue>; 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);
Expand Down Expand Up @@ -618,6 +622,8 @@ procedure TProxy<T>.Once(const AMethodName: string);
methodData : IMethodData;
pInfo : PTypeInfo;
begin
pInfo := TypeInfo(T);

methodData := GetMethodData(AMethodName,pInfo.NameStr);
Assert(methodData <> nil);
methodData.Once;
Expand Down Expand Up @@ -797,6 +803,15 @@ function TProxy<T>.WillReturn(const value: TValue): IWhen<T>;
result := TWhen<T>.Create(Self.Proxy);
end;

function TProxy<T>.WillReturn(const value: TValue; const AllowNil: Boolean): IWhen<T>;
begin
FSetupMode := TSetupMode.Behavior;
FReturnValue := value;
FReturnValueNilAllowed := AllowNil;
FNextBehavior := TBehaviorType.WillReturn;
result := TWhen<T>.Create(Self.Proxy);
end;

procedure TProxy<T>.WillReturnDefault(const AMethodName : string; const value : TValue);
var
methodData : IMethodData;
Expand All @@ -810,6 +825,15 @@ procedure TProxy<T>.WillReturnDefault(const AMethodName : string; const value :
ClearSetupState;
end;

function TProxy<T>.WillReturnNil: IWhen<T>;
begin
FSetupMode := TSetupMode.Behavior;
FReturnValue := TValue.From<Pointer>(nil);
FReturnValueNilAllowed := True;
FNextBehavior := TBehaviorType.WillReturn;
result := TWhen<T>.Create(Self.Proxy);
end;

function TProxy<T>._AddRef: Integer;
begin
result := inherited;
Expand Down
9 changes: 8 additions & 1 deletion Delphi.Mocks.pas
Expand Up @@ -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<T>;
function WillReturn(const value : TValue) : IWhen<T>; 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<T>; overload;

//set the nil as return value for a method when called with the parameters specified on the When
function WillReturnNil : IWhen<T>;

//Will exedute the func when called with the specified parameters
function WillExecute(const func : TExecuteFunc) : IWhen<T>;overload;
Expand Down
18 changes: 11 additions & 7 deletions Examples/Delphi.Mocks.Examples.Factory.pas
Expand Up @@ -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}']
Expand Down Expand Up @@ -121,6 +123,7 @@ implementation

function CreateFakeGeneric(const TypeInfo: PTypeInfo) : TObject;
begin
result := nil;
end;

{ TLogExporter }
Expand Down Expand Up @@ -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 }
Expand All @@ -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<ICoreService>;
//var
// logExporterSUT : ILogExporter;
//
// // mockFactory : TMockFactory;
// mockContainer : TAutoMockContainer;
// mockCoreService : TMock<ICoreService>;
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
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions Examples/Delphi.Mocks.Examples.Implement.pas
Expand Up @@ -7,13 +7,15 @@ interface
DUnitX.TestFramework;

type
{$M+}
TExample_InterfaceImplementTests = class
published
procedure Implement_Single_Interface;
procedure Implement_Multiple_Interfaces;
procedure SetupAndVerify_Mulitple_Interfaces;
procedure SetupAndVerify_Object_And_Interfaces;
end;
{$M-}


implementation
Expand Down
3 changes: 3 additions & 0 deletions Examples/Delphi.Mocks.Examples.Interfaces.pas
Expand Up @@ -28,10 +28,13 @@ TSystemUnderTest = class(TInterfacedObject, TSystemUnderTestInf)
procedure CallsSimpleInterfaceMethod;
end;

{$M+}
TMockObjectTests = class
published
procedure Simple_Interface_Mock;
end;
{$M-}


implementation

Expand Down
2 changes: 2 additions & 0 deletions Examples/Delphi.Mocks.Examples.Matchers.pas
Expand Up @@ -18,10 +18,12 @@ interface
end;
{$M-}

{$M+}
TExample_MatchersTests = class
published
procedure Match_parameter_values;
end;
{$M-}


implementation
Expand Down
2 changes: 2 additions & 0 deletions Examples/Delphi.Mocks.Examples.Objects.pas
Expand Up @@ -24,10 +24,12 @@ TSystemUnderTest = class(TObject)
procedure CallsSimpleMethodOnMock;virtual;
end;

{$M+}
TMockObjectTests = class
published
procedure MockObject_Can_Call_Function;virtual;
end;
{$M-}

implementation

Expand Down
2 changes: 2 additions & 0 deletions Tests/Delphi.Mocks.Tests.AutoMock.pas
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Tests/Delphi.Mocks.Tests.Base.pas
Expand Up @@ -64,6 +64,7 @@ TSimpleRecord_WithoutRTTI = record
//--Without RTTI


{$M+}
TTestMock = class
published
procedure CreateMock_With_Object_Which_Has_No_RTTI_Raises_No_Exception;
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions Tests/Delphi.Mocks.Tests.Behavior.pas
Expand Up @@ -12,6 +12,7 @@ interface
type
ETestBehaviourException = class(Exception);

{$M+}
TTestBehaviors = class
private
FContext : TRttiContext;
Expand Down Expand Up @@ -44,6 +45,7 @@ TTestBehaviors = class
procedure CreateWillRaise_Behavior_Type_Set_To_WillAlwaysRaise;
procedure CreateWillRaiseWhen_Behavior_Type_Set_To_WillRaise;
end;
{$M-}

implementation

Expand Down
2 changes: 2 additions & 0 deletions Tests/Delphi.Mocks.Tests.Expectations.pas
Expand Up @@ -10,6 +10,7 @@ interface


type
{$M+}
TTestExpectations = class
protected
FMatchers : TArray<IMatcher>;
Expand Down Expand Up @@ -64,6 +65,7 @@ TTestExpectations = class
procedure ExpectationMet_With_Before;
procedure ExpectationNotMet_With_Before;
end;
{$M-}


implementation
Expand Down
2 changes: 2 additions & 0 deletions Tests/Delphi.Mocks.Tests.InterfaceProxy.pas
Expand Up @@ -58,6 +58,7 @@ interface
end;
{$M-}

{$M+}
TTestInterfaceProxy = class
published
procedure After_Proxy_AddImplement_ProxyProxy_Implements_Original_Interface;
Expand All @@ -73,6 +74,7 @@ TTestInterfaceProxy = class
procedure TestOuParam;
procedure TestVarParam;
end;
{$M-}

implementation

Expand Down
2 changes: 2 additions & 0 deletions Tests/Delphi.Mocks.Tests.Interfaces.pas
Expand Up @@ -25,6 +25,7 @@ interface
end;
{$M-}

{$M+}
TSafeCallTest = class
published
[Test]
Expand All @@ -36,6 +37,7 @@ TSafeCallTest = class
[Test]
procedure CanMockProcedureWithVariantParam;
end;
{$M-}


implementation
Expand Down
4 changes: 3 additions & 1 deletion Tests/Delphi.Mocks.Tests.MethodData.pas
Expand Up @@ -16,6 +16,7 @@ interface
end;
{$M-}

{$M+}
TTestMethodData = class
published
[Test, Ignore]
Expand All @@ -33,6 +34,7 @@ TTestMethodData = class
[Test]
procedure BehaviourMustBeDefined_IsTrue_AndBehaviourIsNotDefined_RaisesException;
end;
{$M-}

implementation

Expand Down Expand Up @@ -81,7 +83,7 @@ procedure TTestMethodData.AllowRedefineBehaviorDefinitions_IsFalse_ExceptionIsTh
Assert.WillRaise(procedure
begin
methodData.WillReturnWhen(TArray<TValue>.Create(someValue1), someValue2, nil);
end, EMockException);
end, EMockSetupException);
end;

procedure TTestMethodData.AllowRedefineBehaviorDefinitions_IsFalse_NoExceptionIsThrown_WhenAddingNormal;
Expand Down
2 changes: 2 additions & 0 deletions Tests/Delphi.Mocks.Tests.ObjectProxy.pas
Expand Up @@ -34,6 +34,7 @@ TCommand = class
procedure TestOutParam(out msg : string);virtual;abstract;
end;

{$M+}
TTestObjectProxy = class
published
[Test]
Expand Down Expand Up @@ -61,6 +62,7 @@ TTestObjectProxy = class
[Test]
procedure TestVarParam;
end;
{$M-}

implementation

Expand Down
3 changes: 3 additions & 0 deletions Tests/Delphi.Mocks.Tests.Objects.pas
Expand Up @@ -23,10 +23,13 @@ TSystemUnderTest = class(TObject)
procedure CallsSimpleMethodOnMock;
end;

{$M+}
TMockObjectTests = class
published
procedure MockObject_Can_Call_Function;
end;
{$M-}


implementation

Expand Down
2 changes: 2 additions & 0 deletions Tests/Delphi.Mocks.Tests.OpenArrayIntf.pas
Expand Up @@ -6,11 +6,13 @@ interface
DUnitX.TestFramework;

type
{$M+}
TestIOpenArray = class
published
procedure TestMyMethodDynamicArray;
procedure TestMyMethodTypedArray;
end;
{$M-}

TMyArray = array of Integer;

Expand Down

0 comments on commit 431f449

Please sign in to comment.