Permalink
Browse files

Added param matchers to Expectations

  • Loading branch information...
staticcat committed Sep 9, 2014
1 parent dfd3fc0 commit bf8576704d593a88f0b5aa44eb9482fd3f76a181
@@ -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
@@ -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;
@@ -31,6 +31,7 @@ interface
uses
Rtti,
Delphi.Mocks,
Delphi.Mocks.ParamMatcher,
Delphi.Mocks.Interfaces;
@@ -49,6 +50,7 @@ TExpectation = class(TInterfacedObject,IExpectation)
FTimes : Cardinal;
FHitCount : Cardinal;
FMethodName : string;
FMatchers : TArray<IMatcher>;
protected
function GetExpectationType : TExpectationType;
function GetExpectationMet : boolean;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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
@@ -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;
@@ -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
Oops, something went wrong.

0 comments on commit bf85767

Please sign in to comment.