Skip to content

Commit

Permalink
Merge pull request #98 from VSoftTechnologies/pull-96
Browse files Browse the repository at this point in the history
Added tests for implements method
  • Loading branch information
staticcat committed Jun 1, 2016
2 parents c8a0a5b + 910ad7a commit 03496f8
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Examples/Delphi.Mocks.Examples.Interfaces.pas
Expand Up @@ -32,10 +32,13 @@ TSystemUnderTest = class(TInterfacedObject, TSystemUnderTestInf)
TMockObjectTests = class
published
procedure Simple_Interface_Mock;
procedure Multiple_Interface_Mock;
end;
{$M-}




implementation

uses
Expand Down
186 changes: 185 additions & 1 deletion Tests/Delphi.Mocks.Tests.Interfaces.pas
Expand Up @@ -7,6 +7,29 @@ interface
Delphi.Mocks;

type
{$M+}
IA = interface
['{551FA8FF-E038-49BB-BCBC-E1F82544CA97}']
procedure Method1;
end;

IB = interface
['{956B7421-7F45-4E22-BD5F-E5898EC186F4}']
procedure Method2;
end;

IC = interface
['{4B52C6FE-43EE-44B5-AC01-B4371944322D}']
procedure Method3;
end;

ID = interface
['{E222BB8F-6E89-4E54-B2DC-E54C5F280E86}']
procedure Method4;
end;
{$M-}


{$M+}
ISafeCallInterface = interface
['{50960499-4347-421A-B28B-3C05AE9CB351}']
Expand Down Expand Up @@ -39,6 +62,17 @@ TSafeCallTest = class
end;
{$M-}

{$M+}
TInterfaceTests = class
published
[Test]
procedure Cast_Mock_As_Interfaces_It_Implements;
[Test]
procedure Cast_MockInstance_As_Interfaces_It_Implements;
[Test]
procedure MockInstance_As_Interfaces_It_Implements;
end;
{$M-}

implementation
uses
Expand Down Expand Up @@ -94,8 +128,158 @@ procedure TSafeCallTest.CanMockSimpleProcedureCall;
Assert.NotImplemented;
end;

procedure TInterfaceTests.Cast_Mock_As_Interfaces_It_Implements;
var
mock : TMock<IA>;
a : IA;
b : IB;
c : IC;
d : ID;
i : IInterface;
begin
mock := TMock<IA>.Create;
mock.Setup.Expect.Exactly(2).When.Method1;

mock.Implement<IB>;
mock.Setup<IB>.Expect.Exactly(2).When.Method2;

mock.Implement<IC>;
mock.Setup<IC>.Expect.Exactly(2).When.Method3;

mock.Implement<ID>;
mock.Setup<ID>.Expect.Exactly(2).When.Method4;

//Transfer through mock references.
a := mock;
a.Method1;

b := a as IB;
b.Method2;

c := a as IC;
c.Method3;

d := a as ID;
d.Method4;

i := a as IInterface;


//Transfer through interfaces references.
a := mock;
a.Method1;

b := a as IB;
b.Method2;

c := b as IC;
c.Method3;

d := c as ID;
d.Method4;

i := d as IInterface;

mock.VerifyAll();
Assert.Pass();
end;

procedure TInterfaceTests.MockInstance_As_Interfaces_It_Implements;
var
mock : TMock<IA>;
a : IA;
b : IB;
c : IC;
d : ID;
i : IInterface;
begin
mock := TMock<IA>.Create;
mock.Setup.Expect.Exactly(1).When.Method1;

mock.Implement<IB>;
mock.Setup<IB>.Expect.Exactly(1).When.Method2;

mock.Implement<IC>;
mock.Setup<IC>.Expect.Exactly(1).When.Method3;

mock.Implement<ID>;
mock.Setup<ID>.Expect.Exactly(1).When.Method4;

//Transfer through mock instance.
a := mock.Instance;
a.Method1;

b := mock.Instance<IB>;
b.Method2;

c := mock.Instance<IC>;
c.Method3;

d := mock.Instance<ID>;
d.Method4;

i := a as IInterface;

mock.VerifyAll();
Assert.Pass();
end;

procedure TInterfaceTests.Cast_MockInstance_As_Interfaces_It_Implements;
var
mock : TMock<IA>;
a : IA;
b : IB;
c : IC;
d : ID;
i : IInterface;
begin
mock := TMock<IA>.Create;
mock.Setup.Expect.Exactly(2).When.Method1;

mock.Implement<IB>;
mock.Setup<IB>.Expect.Exactly(2).When.Method2;

mock.Implement<IC>;
mock.Setup<IC>.Expect.Exactly(2).When.Method3;

mock.Implement<ID>;
mock.Setup<ID>.Expect.Exactly(2).When.Method4;

//Transfer through mock instance.
a := mock.Instance;
a.Method1;

b := a as IB;
b.Method2;

c := a as IC;
c.Method3;

d := a as ID;
d.Method4;

i := a as IInterface;

a := mock.Instance;
a.Method1;

b := a as IB;
b.Method2;

c := b as IC;
c.Method3;

d := c as ID;
d.Method4;

i := d as IInterface;

mock.VerifyAll();
Assert.Pass();
end;

initialization
TDUnitX.RegisterTestFixture(TSafeCallTest);

TDUnitX.RegisterTestFixture(TInterfaceTests);

end.

0 comments on commit 03496f8

Please sign in to comment.