Skip to content

Commit

Permalink
Added param matchers to Expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
staticcat committed Sep 9, 2014
1 parent dfd3fc0 commit bf85767
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 93 deletions.
4 changes: 2 additions & 2 deletions Delphi.Mocks.Behavior.pas
Expand Up @@ -188,7 +188,7 @@ function TBehavior.Match(const Args: TArray<TValue>): Boolean;
result := True;
end;

function MatchWithMatchers(const Args: TArray<TValue>): Boolean;
function MatchWithMatchers: Boolean;
var
i : integer;
begin
Expand All @@ -206,7 +206,7 @@ function TBehavior.Match(const Args: TArray<TValue>): Boolean;

if (Length(FMatchers) > 0) and (Length(Args) = (Length(FMatchers) + 1)) then
begin
result := MatchWithMatchers(Args);
result := MatchWithMatchers;
exit;
end;

Expand Down
84 changes: 54 additions & 30 deletions Delphi.Mocks.Expectation.pas
Expand Up @@ -31,6 +31,7 @@ interface
uses
Rtti,
Delphi.Mocks,
Delphi.Mocks.ParamMatcher,
Delphi.Mocks.Interfaces;


Expand All @@ -49,6 +50,7 @@ TExpectation = class(TInterfacedObject,IExpectation)
FTimes : Cardinal;
FHitCount : Cardinal;
FMethodName : string;
FMatchers : TArray<IMatcher>;
protected
function GetExpectationType : TExpectationType;
function GetExpectationMet : boolean;
Expand All @@ -58,33 +60,33 @@ TExpectation = class(TInterfacedObject,IExpectation)
function ArgsToString : string;
procedure CopyArgs(const Args: TArray<TValue>);
constructor Create(const AMethodName : string);
constructor CreateWhen(const AMethodName : string; const Args: TArray<TValue>);
constructor CreateWhen(const AMethodName : string; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
public
constructor CreateOnceWhen(const AMethodName : string; const Args : TArray<TValue>);
constructor CreateOnceWhen(const AMethodName : string; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
constructor CreateOnce(const AMethodName : string);

constructor CreateNeverWhen(const AMethodName : string; const Args : TArray<TValue>);
constructor CreateNeverWhen(const AMethodName : string; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
constructor CreateNever(const AMethodName : string);

constructor CreateAtLeastOnceWhen(const AMethodName : string; const Args : TArray<TValue>);
constructor CreateAtLeastOnceWhen(const AMethodName : string; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
constructor CreateAtLeastOnce(const AMethodName : string);

constructor CreateAtLeastWhen(const AMethodName : string; const times : Cardinal; const Args : TArray<TValue>);
constructor CreateAtLeastWhen(const AMethodName : string; const times : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
constructor CreateAtLeast(const AMethodName : string; const times : Cardinal);

constructor CreateAtMostWhen(const AMethodName : string; const times : Cardinal; const Args : TArray<TValue>);
constructor CreateAtMostWhen(const AMethodName : string; const times : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
constructor CreateAtMost(const AMethodName : string; const times : Cardinal);

constructor CreateBetweenWhen(const AMethodName : string; const a,b : Cardinal; const Args : TArray<TValue>);
constructor CreateBetweenWhen(const AMethodName : string; const a,b : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
constructor CreateBetween(const AMethodName : string; const a,b : Cardinal);

constructor CreateExactlyWhen(const AMethodName : string; const times : Cardinal; const Args : TArray<TValue>);
constructor CreateExactlyWhen(const AMethodName : string; const times : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
constructor CreateExactly(const AMethodName : string; const times : Cardinal);

constructor CreateBeforeWhen(const AMethodName : string; const ABeforeMethodName : string ; const Args : TArray<TValue>);
constructor CreateBeforeWhen(const AMethodName : string; const ABeforeMethodName : string ; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
constructor CreateBefore(const AMethodName : string; const ABeforeMethodName : string);

constructor CreateAfterWhen(const AMethodName : string; const AAfterMethodName : string;const Args : TArray<TValue>);
constructor CreateAfterWhen(const AMethodName : string; const AAfterMethodName : string;const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
constructor CreateAfter(const AMethodName : string; const AAfterMethodName : string);
end;

Expand Down Expand Up @@ -125,17 +127,20 @@ procedure TExpectation.CopyArgs(const Args: TArray<TValue>);

constructor TExpectation.Create(const AMethodName : string);
begin
SetLength(FMatchers, 0);

FExpectationMet := False;
FHitCount := 0;
FMethodName := AMethodName;
end;

constructor TExpectation.CreateWhen(const AMethodName: string; const Args: TArray<TValue>);
constructor TExpectation.CreateWhen(const AMethodName: string; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
begin
FExpectationMet := False;
FHitCount := 0;
FMethodName := AMethodName;
CopyArgs(Args);
FMatchers := matchers;
end;


Expand All @@ -147,9 +152,9 @@ constructor TExpectation.CreateAfter(const AMethodName : string; const AAfterMet
FBeforeAfterMethodName := AAfterMethodName;
end;

constructor TExpectation.CreateAfterWhen(const AMethodName : string; const AAfterMethodName: string; const Args: TArray<TValue>);
constructor TExpectation.CreateAfterWhen(const AMethodName : string; const AAfterMethodName: string; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
begin
CreateWhen(AMethodName,Args);
CreateWhen(AMethodName, Args, matchers);
FExpectationType := TExpectationType.AfterWhen;
FBeforeAfterMethodName := AAfterMethodName;
end;
Expand All @@ -168,16 +173,16 @@ constructor TExpectation.CreateAtLeastOnce(const AMethodName : string);
FTimes := 1;
end;

constructor TExpectation.CreateAtLeastOnceWhen(const AMethodName : string; const Args: TArray<TValue>);
constructor TExpectation.CreateAtLeastOnceWhen(const AMethodName : string; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
begin
CreateWhen(AMethodName,Args);
CreateWhen(AMethodName, Args, matchers);
FExpectationType := TExpectationType.AtLeastOnceWhen;
FTimes := 1;
end;

constructor TExpectation.CreateAtLeastWhen(const AMethodName : string; const times: Cardinal; const Args: TArray<TValue>);
constructor TExpectation.CreateAtLeastWhen(const AMethodName : string; const times: Cardinal; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
begin
CreateWhen(AMethodName,Args);
CreateWhen(AMethodName, Args, matchers);
FExpectationType := TExpectationType.AtLeastWhen;
FTimes := times;
end;
Expand All @@ -189,9 +194,9 @@ constructor TExpectation.CreateAtMost(const AMethodName : string; const times: C
FTimes := times;
end;

constructor TExpectation.CreateAtMostWhen(const AMethodName : string; const times: Cardinal; const Args: TArray<TValue>);
constructor TExpectation.CreateAtMostWhen(const AMethodName : string; const times: Cardinal; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
begin
CreateWhen(AMethodName,Args);
CreateWhen(AMethodName, Args, matchers);
FExpectationType := TExpectationType.AtMostWhen;
FTimes := times;
end;
Expand All @@ -203,9 +208,9 @@ constructor TExpectation.CreateBefore(const AMethodName : string; const ABeforeM
FBeforeAfterMethodName := ABeforeMethodName;
end;

constructor TExpectation.CreateBeforeWhen(const AMethodName : string; const ABeforeMethodName: string; const Args: TArray<TValue>);
constructor TExpectation.CreateBeforeWhen(const AMethodName : string; const ABeforeMethodName: string; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
begin
CreateWhen(AMethodName,Args);
CreateWhen(AMethodName, Args, matchers);
FExpectationType := TExpectationType.BeforeWhen;
FBeforeAfterMethodName := ABeforeMethodName;
end;
Expand All @@ -218,9 +223,9 @@ constructor TExpectation.CreateBetween(const AMethodName : string; const a, b: C
FBetween[1] := b;
end;

constructor TExpectation.CreateBetweenWhen(const AMethodName : string; const a, b: Cardinal; const Args: TArray<TValue>);
constructor TExpectation.CreateBetweenWhen(const AMethodName : string; const a, b: Cardinal; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
begin
CreateWhen(AMethodName,Args);
CreateWhen(AMethodName, Args, matchers);
FExpectationType := TExpectationType.BetweenWhen;
FBetween[0] := a;
FBetween[1] := b;
Expand All @@ -233,9 +238,9 @@ constructor TExpectation.CreateExactly(const AMethodName : string; const times:
FTimes := times;
end;

constructor TExpectation.CreateExactlyWhen(const AMethodName : string; const times: Cardinal; const Args: TArray<TValue>);
constructor TExpectation.CreateExactlyWhen(const AMethodName : string; const times: Cardinal; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
begin
CreateWhen(AMethodName,Args);
CreateWhen(AMethodName, Args, matchers);
FExpectationType := TExpectationType.ExactlyWhen;
FTimes := times;
end;
Expand All @@ -247,9 +252,9 @@ constructor TExpectation.CreateNever(const AMethodName : string) ;
FExpectationMet := True;
end;

constructor TExpectation.CreateNeverWhen(const AMethodName : string; const Args: TArray<TValue>);
constructor TExpectation.CreateNeverWhen(const AMethodName : string; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
begin
CreateWhen(AMethodName,Args);
CreateWhen(AMethodName, Args, matchers);
FExpectationType := TExpectationType.NeverWhen;
FExpectationMet := True;
end;
Expand All @@ -261,9 +266,9 @@ constructor TExpectation.CreateOnce(const AMethodName : string );
FTimes := 1;
end;

constructor TExpectation.CreateOnceWhen(const AMethodName : string; const Args: TArray<TValue>);
constructor TExpectation.CreateOnceWhen(const AMethodName : string; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);
begin
CreateWhen(AMethodName,Args);
CreateWhen(AMethodName, Args, matchers);
FExpectationType := TExpectationType.OnceWhen;
FTimes := 1;
end;
Expand Down Expand Up @@ -295,6 +300,19 @@ function TExpectation.Match(const Args: TArray<TValue>): boolean;
end;
result := True;
end;

function MatchWithMatchers: Boolean;
var
i : integer;
begin
result := False;
for i := 0 to High(FMatchers) do
begin
if not FMatchers[i].Match(Args[i+1]) then
exit;
end;
result := True;
end;
begin
result := False;
case FExpectationType of
Expand All @@ -318,7 +336,13 @@ function TExpectation.Match(const Args: TArray<TValue>): boolean;
BetweenWhen,
ExactlyWhen,
BeforeWhen,
AfterWhen: result := MatchArgs;
AfterWhen:
begin
if Length(FMatchers) > 0 then
result := MatchWithMatchers
else
result := MatchArgs;
end;
end;
end;

Expand Down
18 changes: 9 additions & 9 deletions Delphi.Mocks.Interfaces.pas
Expand Up @@ -93,23 +93,23 @@ interface
procedure WillExecuteWhen(const func : TExecuteFunc; const Args: TArray<TValue>; const matchers : TArray<IMatcher>);

//expectations
procedure OnceWhen(const Args : TArray<TValue>);
procedure OnceWhen(const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure Once;
procedure NeverWhen(const Args : TArray<TValue>);
procedure NeverWhen(const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure Never;
procedure AtLeastOnceWhen(const Args : TArray<TValue>);
procedure AtLeastOnceWhen(const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure AtLeastOnce;
procedure AtLeastWhen(const times : Cardinal; const Args : TArray<TValue>);
procedure AtLeastWhen(const times : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure AtLeast(const times : Cardinal);
procedure AtMostWhen(const times : Cardinal; const Args : TArray<TValue>);
procedure AtMostWhen(const times : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure AtMost(const times : Cardinal);
procedure BetweenWhen(const a,b : Cardinal; const Args : TArray<TValue>);
procedure BetweenWhen(const a,b : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure Between(const a,b : Cardinal);
procedure ExactlyWhen(const times : Cardinal; const Args : TArray<TValue>);
procedure ExactlyWhen(const times : Cardinal; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure Exactly(const times : Cardinal);
procedure BeforeWhen(const ABeforeMethodName : string ; const Args : TArray<TValue>);
procedure BeforeWhen(const ABeforeMethodName : string ; const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure Before(const ABeforeMethodName : string);
procedure AfterWhen(const AAfterMethodName : string;const Args : TArray<TValue>);
procedure AfterWhen(const AAfterMethodName : string;const Args : TArray<TValue>; const matchers : TArray<IMatcher>);
procedure After(const AAfterMethodName : string);

//Verification
Expand Down

0 comments on commit bf85767

Please sign in to comment.