+ Added Mock Objects implementation by Uberto Barbini

This commit is contained in:
michael 2005-05-06 19:38:43 +00:00
parent 4af99ad8ec
commit be4dd205d9
6 changed files with 303 additions and 70 deletions

View File

@ -1,5 +1,5 @@
#
# Don't edit, this file is generated by FPCMake Version 2.0.0 [2005/05/05]
# Don't edit, this file is generated by FPCMake Version 1.9.8 [2005/04/10]
#
default: all
MAKEFILETARGETS=i386-linux i386-go32v2 i386-win32 i386-os2 i386-freebsd i386-beos i386-netbsd i386-solaris i386-qnx i386-netware i386-openbsd i386-wdosx i386-emx i386-watcom i386-netwlibc m68k-linux m68k-freebsd m68k-netbsd m68k-amiga m68k-atari m68k-openbsd m68k-palmos powerpc-linux powerpc-netbsd powerpc-macos powerpc-darwin powerpc-morphos sparc-linux sparc-netbsd sparc-solaris x86_64-linux x86_64-freebsd arm-linux
@ -233,103 +233,103 @@ PACKAGESDIR:=$(wildcard $(FPCDIR) $(FPCDIR)/packages/base $(FPCDIR)/packages/ext
override PACKAGE_NAME=fcl
PACKAGEDIR_MAIN:=$(firstword $(subst /Makefile.fpc,,$(strip $(wildcard $(addsuffix /fcl/Makefile.fpc,$(PACKAGESDIR))))))
ifeq ($(FULL_TARGET),i386-linux)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-go32v2)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-win32)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-os2)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-freebsd)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-beos)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-netbsd)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-solaris)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-qnx)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-netware)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-openbsd)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-wdosx)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-emx)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-watcom)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-netwlibc)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),m68k-linux)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),m68k-freebsd)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),m68k-netbsd)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),m68k-amiga)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),m68k-atari)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),m68k-openbsd)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),m68k-palmos)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),powerpc-linux)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),powerpc-netbsd)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),powerpc-macos)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),powerpc-darwin)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),powerpc-morphos)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),sparc-linux)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),sparc-netbsd)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),sparc-solaris)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),x86_64-linux)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),x86_64-freebsd)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),arm-linux)
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator
override TARGET_UNITS+=fpcunit testregistry testreport testutils testdecorator ubmockobject
endif
ifeq ($(FULL_TARGET),i386-linux)
override TARGET_EXAMPLEDIRS+=exampletests

View File

@ -6,7 +6,7 @@
main=fcl
[target]
units=fpcunit testregistry testreport testutils testdecorator
units=fpcunit testregistry testreport testutils testdecorator ubmockobject
exampledirs=exampletests
[compiler]

View File

@ -1,5 +1,5 @@
#
# Don't edit, this file is generated by FPCMake Version 2.0.0 [2005/05/05]
# Don't edit, this file is generated by FPCMake Version 1.9.8 [2005/04/10]
#
default: all
MAKEFILETARGETS=i386-linux i386-go32v2 i386-win32 i386-os2 i386-freebsd i386-beos i386-netbsd i386-solaris i386-qnx i386-netware i386-openbsd i386-wdosx i386-emx i386-watcom i386-netwlibc m68k-linux m68k-freebsd m68k-netbsd m68k-amiga m68k-atari m68k-openbsd m68k-palmos powerpc-linux powerpc-netbsd powerpc-macos powerpc-darwin powerpc-morphos sparc-linux sparc-netbsd sparc-solaris x86_64-linux x86_64-freebsd arm-linux
@ -233,103 +233,103 @@ PACKAGESDIR:=$(wildcard $(FPCDIR) $(FPCDIR)/packages/base $(FPCDIR)/packages/ext
override PACKAGE_NAME=fcl
PACKAGEDIR_MAIN:=$(firstword $(subst /Makefile.fpc,,$(strip $(wildcard $(addsuffix /fcl/Makefile.fpc,$(PACKAGESDIR))))))
ifeq ($(FULL_TARGET),i386-linux)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-go32v2)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-win32)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-os2)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-freebsd)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-beos)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-netbsd)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-solaris)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-qnx)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-netware)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-openbsd)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-wdosx)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-emx)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-watcom)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),i386-netwlibc)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),m68k-linux)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),m68k-freebsd)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),m68k-netbsd)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),m68k-amiga)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),m68k-atari)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),m68k-openbsd)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),m68k-palmos)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),powerpc-linux)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),powerpc-netbsd)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),powerpc-macos)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),powerpc-darwin)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),powerpc-morphos)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),sparc-linux)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),sparc-netbsd)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),sparc-solaris)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),x86_64-linux)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),x86_64-freebsd)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
ifeq ($(FULL_TARGET),arm-linux)
override TARGET_UNITS+=fpcunittests money moneytest
override TARGET_UNITS+=fpcunittests money moneytest testmockobject
endif
override INSTALL_FPCPACKAGE=y
ifeq ($(FULL_TARGET),i386-linux)

View File

@ -6,7 +6,7 @@
main=fcl
[target]
units=fpcunittests money moneytest
units=fpcunittests money moneytest testmockobject
[compiler]
options=-S2h

View File

@ -0,0 +1,129 @@
{$mode objfpc}
{$h+}
{
$Id$
This file is part of the Free Component Library (FCL)
Copyright (c) 2005 Uberto Barbini
mock object tests
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit testmockobject;
interface
uses
Classes, SysUtils, fpcunit, testutils, testregistry, ubmockobject;
type
TTestMock= class(TTestCase)
protected
_MockObj: TUbMockObject;
procedure SetUp; override;
procedure TearDown; override;
published
procedure TestRightCalls;
procedure TestWrongCallCount;
procedure TestWrongOrderOfCalls;
procedure TestWrongSignatures;
procedure TestExpectations;
procedure TestResetExpectations;
end;
implementation
{ TTestMock }
procedure TTestMock.SetUp;
begin
_MockObj := TUbMockObject.Create;
end;
procedure TTestMock.TearDown;
begin
_MockObj.Free;
end;
procedure TTestMock.TestRightCalls;
begin
_MockObj.StartSetUp;
_MockObj.AddExpectation('abc');
_MockObj.AddExpectation('123');
_MockObj.AddExpectation('The quick brown fox jumped over the lazy dog.');
AssertEquals(3, _MockObj.UncoveredExpectations);
_MockObj.EndSetUp;
_MockObj.AddExpectation('abc');
_MockObj.AddExpectation('123');
_MockObj.AddExpectation('The quick brown fox jumped over the lazy dog.');
_MockObj.Verify;
AssertEquals(0, _MockObj.UncoveredExpectations);
end;
procedure TTestMock.TestWrongCallCount;
begin
_MockObj.StartSetUp;
_MockObj.EndSetUp;
_MockObj.AddExpectation('abc');
AssertException(EAssertionFailedError, @_MockObj.Verify);
AssertEquals(-1, _MockObj.UncoveredExpectations);
_MockObj.StartSetUp;
_MockObj.AddExpectation('abc');
_MockObj.EndSetUp;
AssertException(EAssertionFailedError, @_MockObj.Verify);
AssertEquals(1, _MockObj.UncoveredExpectations);
end;
procedure TTestMock.TestWrongSignatures;
begin
_MockObj.StartSetUp;
_MockObj.AddExpectation('aaa');
_MockObj.EndSetUp;
_MockObj.AddExpectation('bbb');
AssertException(EAssertionFailedError, @_MockObj.Verify);
end;
procedure TTestMock.TestWrongOrderOfCalls;
begin
_MockObj.StartSetUp;
_MockObj.AddExpectation('abc');
_MockObj.AddExpectation('123');
_MockObj.AddExpectation('the quick brown fox jumped over the lazy dog');
_MockObj.EndSetUp;
_MockObj.AddExpectation('abc');
_MockObj.AddExpectation('the quick brown fox jumped over the lazy dog');
_MockObj.AddExpectation('123');
AssertException(EAssertionFailedError, @_MockObj.Verify);
end;
procedure TTestMock.TestExpectations;
begin
AssertTrue(_MockObj.SetUpMode);
_MockObj.StartSetUp;
_MockObj.EndSetUp;
AssertTrue(not _MockObj.SetUpMode);
_MockObj.Verify;
end;
procedure TTestMock.TestResetExpectations;
begin
_MockObj.AddExpectation('abc');
_MockObj.StartSetUp;
_MockObj.AddExpectation('123');
_MockObj.EndSetUp;
_MockObj.AddExpectation('123');
_MockObj.Verify;
end;
initialization
RegisterTests([TTestMock]);
end.

104
fcl/fpcunit/ubmockobject.pp Normal file
View File

@ -0,0 +1,104 @@
{$mode objfpc}
{$h+}
{
$Id$
This file is part of the Free Component Library (FCL)
Copyright (c) 2005 by Uberto Barbini
Ultra basic implementation of mock objects for endo-testing
with fpcunit.
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit ubmockobject;
interface
uses
Classes, SysUtils, fpcunit;
type
TUbMockObject = class(TObject)
protected
FSetUpMode: Boolean;
FSetUpList: TStringList;
FCallsList: TStringList;
public
constructor Create; virtual;
destructor Destroy; override;
procedure AddExpectation(const ASignatureCall: string);
property SetUpMode: Boolean read FSetUpMode;
procedure Verify;
procedure StartSetUp;
procedure EndSetUp;
function UncoveredExpectations: integer;
end;
implementation
{ TUbMockObject }
procedure TUbMockObject.AddExpectation(const ASignatureCall: string);
begin
if SetUpMode then
FSetUpList.Add(ASignatureCall)
else
FCallsList.Add(ASignatureCall);
end;
function TUbMockObject.UncoveredExpectations: integer;
begin
Result := FSetUpList.Count - FCallsList.Count;
end;
constructor TUbMockObject.Create;
begin
FSetUpList := TStringList.Create;
FCallsList := TStringList.Create;
FSetUpMode := True;
end;
destructor TUbMockObject.Destroy;
begin
FSetUpList.Free;
FCallsList.Free;
inherited;
end;
procedure TUbMockObject.EndSetUp;
begin
FSetUpMode := False;
FCallsList.Clear;
end;
procedure TUbMockObject.StartSetUp;
begin
FSetUpMode := True;
FSetUpList.Clear;
end;
procedure TUbMockObject.Verify;
var
i: integer;
s1, s2: string;
begin
TAssert.AssertEquals('Wrong number of expectations!', FSetUpList.Count, FCallsList.Count);
for i := 0 to FSetUpList.Count - 1 do
begin
s1 := FSetUpList[i];
s2 := FCallsList[i];
TAssert.AssertEquals(s1, s2);
end;
FSetUpList.Clear;
FCallsList.Clear;
end;
end.