Skip to content

Commit 41ab9af

Browse files
Call original virtual method if the parameters does not match (#135)
* Support for records in ItRec Support for custom EqualityComparer in ItRec * Fixed warnings and unnecessary code * Support record types for argument matching (without matchers) * Call original virtual method if the parameters does not match
1 parent 494e2d3 commit 41ab9af

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

Source/Delphi.Mocks.Interfaces.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ interface
115115
//Verification
116116
function Verify(var report : string) : boolean;
117117

118-
function BehaviorDefined: Boolean;
118+
function FindBestBehavior(const Args: TArray<TValue>) : IBehavior;
119119
end;
120120

121121
IVerify = interface

Source/Delphi.Mocks.MethodData.pas

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ TMethodData = class(TInterfacedObject,IMethodData)
9696
procedure After(const AAfterMethodName : string);
9797

9898
function Verify(var report : string) : boolean;
99-
100-
function BehaviorDefined: Boolean;
10199
public
102100
constructor Create(const ATypeName : string; const AMethodName : string; const ASetupParameters: TSetupMethodDataParameters; const AAutoMocker : IAutoMock = nil);
103101
destructor Destroy;override;
@@ -265,11 +263,6 @@ function TMethodData.FindExpectation(const expectationTypes : TExpectationTypes)
265263
end;
266264
end;
267265

268-
function TMethodData.BehaviorDefined: Boolean;
269-
begin
270-
Result := (FBehaviors.Count <> 0);
271-
end;
272-
273266
procedure TMethodData.MockNoBehaviourRecordHit(const Args: TArray<TValue>; const AExpectationHitCtr : Integer; const returnType: TRttiType; out Result: TValue);
274267
var
275268
behavior : IBehavior;

Source/Delphi.Mocks.ObjectProxy.pas

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,6 @@ procedure TObjectProxy<T>.DoBefore(Instance: TObject; Method: TRttiMethod; const
106106
pInfo := TypeInfo(T);
107107
methodData := GetMethodData(method.Name,pInfo.NameStr);
108108

109-
//Call the original (virtual) method if:
110-
//-we are not a stub
111-
//-we have not defined any behavior (of course we count hits)
112-
//-the actual method is not an abstract method
113-
//-we are not setting up
114-
DoInvoke := not (FIsStubOnly or methodData.BehaviorDefined or Method.IsAbstract or (FSetupMode <> TSetupMode.None));
115-
116109
//Included instance as first argument because TExpectation.Match
117110
//deduces that the first argument is the object instance.
118111
l := Length(Args);
@@ -124,6 +117,13 @@ procedure TObjectProxy<T>.DoBefore(Instance: TObject; Method: TRttiMethod; const
124117
vArgs[i] := Args[i-1];
125118
end;
126119

120+
//Call the original (virtual) method if:
121+
//-we are not a stub
122+
//-we have not defined any behavior (of course we count hits)
123+
//-the actual method is not an abstract method
124+
//-we are not setting up
125+
DoInvoke := not (FIsStubOnly or (methodData.FindBestBehavior(vArgs) <> nil) or Method.IsAbstract or (FSetupMode <> TSetupMode.None));
126+
127127
Self.DoInvoke(Method,vArgs,Result);
128128

129129
for i := 1 to l do

Tests/Delphi.Mocks.Tests.ObjectProxy.pas

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ TCommand = class
3636
procedure Run(value: Integer);virtual;abstract;
3737
procedure TestVarParam(var msg : string);virtual;abstract;
3838
procedure TestOutParam(out msg : string);virtual;abstract;
39-
function VirtualMethod: Integer; virtual;
39+
function VirtualMethod: Integer; overload; virtual;
40+
function VirtualMethod(const Arg: String): Integer; overload; virtual;
4041
function NonVirtualMethod: Integer;
4142

4243
property VirtualMethodCalled: Boolean read FVirtualMethodCalled;
@@ -76,6 +77,12 @@ TTestObjectProxy = class
7677
procedure WillRaiseMockNonVirtualMethod;
7778
[Test]
7879
procedure VirtualMethodNotCalledDuringMockSetup;
80+
[Test]
81+
procedure VirtualMethodCalledIfNoBehaviorDefined;
82+
[Test]
83+
procedure VirtualMethodNotCalledIfBehaviorMatches;
84+
[Test]
85+
procedure VirtualMethodCalledIfBehaviorNotMatches;
7986
end;
8087
{$M-}
8188

@@ -167,6 +174,27 @@ procedure TTestObjectProxy.TestVarParam;
167174
Assert.Pass;
168175
end;
169176

177+
procedure TTestObjectProxy.VirtualMethodCalledIfBehaviorNotMatches;
178+
var
179+
mock : TMock<TCommand>;
180+
begin
181+
mock := TMock<TCommand>.Create;
182+
mock.Setup.WillReturn(2).When.VirtualMethod('test');
183+
184+
Assert.AreEqual(1, mock.Instance.VirtualMethod('test2'));
185+
Assert.IsTrue(mock.Instance.VirtualMethodCalled);
186+
end;
187+
188+
procedure TTestObjectProxy.VirtualMethodCalledIfNoBehaviorDefined;
189+
var
190+
mock : TMock<TCommand>;
191+
begin
192+
mock := TMock<TCommand>.Create;
193+
194+
Assert.AreEqual(1, mock.Instance.VirtualMethod('test'));
195+
Assert.IsTrue(mock.Instance.VirtualMethodCalled);
196+
end;
197+
170198
procedure TTestObjectProxy.VirtualMethodNotCalledDuringMockSetup;
171199
var
172200
mock : TMock<TCommand>;
@@ -179,6 +207,17 @@ procedure TTestObjectProxy.VirtualMethodNotCalledDuringMockSetup;
179207
Assert.IsFalse(mock.Instance.VirtualMethodCalled);
180208
end;
181209

210+
procedure TTestObjectProxy.VirtualMethodNotCalledIfBehaviorMatches;
211+
var
212+
mock : TMock<TCommand>;
213+
begin
214+
mock := TMock<TCommand>.Create;
215+
mock.Setup.WillReturn(2).When.VirtualMethod('test');
216+
Assert.AreEqual(2, mock.Instance.VirtualMethod('test'));
217+
218+
Assert.IsFalse(mock.Instance.VirtualMethodCalled);
219+
end;
220+
182221
procedure TTestObjectProxy.MockNoArgProcedureUsingAtLeastOnceWhen;
183222
var
184223
mock : TMock<TCommand>;
@@ -324,6 +363,12 @@ function TCommand.NonVirtualMethod: Integer;
324363
Result := 1;
325364
end;
326365

366+
function TCommand.VirtualMethod(const Arg: String): Integer;
367+
begin
368+
FVirtualMethodCalled := True;
369+
Result := 1;
370+
end;
371+
327372
function TCommand.VirtualMethod: Integer;
328373
begin
329374
FVirtualMethodCalled := True;

0 commit comments

Comments
 (0)