mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-10-25 11:42:10 +02:00
TTestCase.CheckAssertCalled implemented (bug ID 28408)
git-svn-id: trunk@32771 -
This commit is contained in:
parent
a2985a5572
commit
7ab8472a70
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -2378,6 +2378,7 @@ packages/fcl-fpcunit/src/exampletests/Makefile.fpc svneol=native#text/plain
|
|||||||
packages/fcl-fpcunit/src/exampletests/fpcunittests.pp svneol=native#text/plain
|
packages/fcl-fpcunit/src/exampletests/fpcunittests.pp svneol=native#text/plain
|
||||||
packages/fcl-fpcunit/src/exampletests/money.pp svneol=native#text/plain
|
packages/fcl-fpcunit/src/exampletests/money.pp svneol=native#text/plain
|
||||||
packages/fcl-fpcunit/src/exampletests/moneytest.pp svneol=native#text/plain
|
packages/fcl-fpcunit/src/exampletests/moneytest.pp svneol=native#text/plain
|
||||||
|
packages/fcl-fpcunit/src/exampletests/needassert.pp svneol=native#text/plain
|
||||||
packages/fcl-fpcunit/src/exampletests/testmockobject.pp svneol=native#text/plain
|
packages/fcl-fpcunit/src/exampletests/testmockobject.pp svneol=native#text/plain
|
||||||
packages/fcl-fpcunit/src/fpcunit.pp svneol=native#text/plain
|
packages/fcl-fpcunit/src/fpcunit.pp svneol=native#text/plain
|
||||||
packages/fcl-fpcunit/src/fpcunitreport.pp svneol=native#text/plain
|
packages/fcl-fpcunit/src/fpcunitreport.pp svneol=native#text/plain
|
||||||
|
|||||||
38
packages/fcl-fpcunit/src/exampletests/needassert.pp
Normal file
38
packages/fcl-fpcunit/src/exampletests/needassert.pp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
program needassert;
|
||||||
|
|
||||||
|
uses fpcunit, testregistry, consoletestrunner;
|
||||||
|
|
||||||
|
Type
|
||||||
|
TTestNeedAssert = Class(TTestCase)
|
||||||
|
Published
|
||||||
|
Procedure NeedsToFail;
|
||||||
|
Procedure NeedsToBeOK;
|
||||||
|
end;
|
||||||
|
|
||||||
|
Procedure TTestNeedAssert.NeedsToFail;
|
||||||
|
|
||||||
|
begin
|
||||||
|
// Do not call assert.
|
||||||
|
end;
|
||||||
|
|
||||||
|
Procedure TTestNeedAssert.NeedsToBeOK;
|
||||||
|
|
||||||
|
begin
|
||||||
|
AssertTrue('Some message',True);
|
||||||
|
end;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Var
|
||||||
|
Application : TTestRunner;
|
||||||
|
|
||||||
|
begin
|
||||||
|
RegisterTest(TTestNeedAssert);
|
||||||
|
TTestCase.CheckAssertCalled:=true;
|
||||||
|
DefaultFormat:=fPlain;
|
||||||
|
DefaultRunAllTests:=True;
|
||||||
|
Application:=TTestRunner.Create(Nil);
|
||||||
|
Application.Initialize;
|
||||||
|
Application.Run;
|
||||||
|
Application.Free;
|
||||||
|
end.
|
||||||
@ -78,6 +78,8 @@ type
|
|||||||
{ TAssert }
|
{ TAssert }
|
||||||
|
|
||||||
TAssert = class(TTest)
|
TAssert = class(TTest)
|
||||||
|
protected
|
||||||
|
Class var AssertCount : Integer;
|
||||||
public
|
public
|
||||||
class procedure Fail(const AMessage: string; AErrorAddrs: Pointer = nil);
|
class procedure Fail(const AMessage: string; AErrorAddrs: Pointer = nil);
|
||||||
class procedure Fail(const AFmt: string; Args : Array of const; AErrorAddrs: Pointer = nil);
|
class procedure Fail(const AFmt: string; Args : Array of const; AErrorAddrs: Pointer = nil);
|
||||||
@ -206,7 +208,10 @@ type
|
|||||||
procedure SetTestName(const Value: string); virtual;
|
procedure SetTestName(const Value: string); virtual;
|
||||||
procedure SetEnableIgnores(Value: boolean); override;
|
procedure SetEnableIgnores(Value: boolean); override;
|
||||||
procedure RunBare; virtual;
|
procedure RunBare; virtual;
|
||||||
|
Public
|
||||||
|
Class Var CheckAssertCalled : Boolean;
|
||||||
public
|
public
|
||||||
|
|
||||||
constructor Create; virtual;
|
constructor Create; virtual;
|
||||||
constructor CreateWith(const ATestName: string; const ATestSuiteName: string); virtual;
|
constructor CreateWith(const ATestName: string; const ATestSuiteName: string); virtual;
|
||||||
constructor CreateWithName(const AName: string); virtual;
|
constructor CreateWithName(const AName: string); virtual;
|
||||||
@ -330,7 +335,8 @@ Resourcestring
|
|||||||
SNoValidInheritance = ' does not inherit from TTestCase';
|
SNoValidInheritance = ' does not inherit from TTestCase';
|
||||||
SNoValidTests = 'No valid tests found in ';
|
SNoValidTests = 'No valid tests found in ';
|
||||||
SNoException = 'no exception';
|
SNoException = 'no exception';
|
||||||
|
SAssertNotCalled = 'Assert not called during test.';
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
uses
|
uses
|
||||||
@ -545,6 +551,7 @@ end;
|
|||||||
|
|
||||||
class procedure TAssert.Fail(const AMessage: string; AErrorAddrs: Pointer);
|
class procedure TAssert.Fail(const AMessage: string; AErrorAddrs: Pointer);
|
||||||
begin
|
begin
|
||||||
|
Inc(AssertCount);
|
||||||
if AErrorAddrs = nil then
|
if AErrorAddrs = nil then
|
||||||
raise EAssertionFailedError.Create(AMessage) at CallerAddr
|
raise EAssertionFailedError.Create(AMessage) at CallerAddr
|
||||||
else
|
else
|
||||||
@ -553,6 +560,7 @@ end;
|
|||||||
|
|
||||||
class procedure TAssert.Fail(const AFmt: string; Args: array of const; AErrorAddrs: Pointer = nil);
|
class procedure TAssert.Fail(const AFmt: string; Args: array of const; AErrorAddrs: Pointer = nil);
|
||||||
begin
|
begin
|
||||||
|
Inc(AssertCount);
|
||||||
if AErrorAddrs = nil then
|
if AErrorAddrs = nil then
|
||||||
raise EAssertionFailedError.CreateFmt(AFmt,Args) at CallerAddr
|
raise EAssertionFailedError.CreateFmt(AFmt,Args) at CallerAddr
|
||||||
else
|
else
|
||||||
@ -574,7 +582,9 @@ begin
|
|||||||
if AErrorAddrs=Nil then
|
if AErrorAddrs=Nil then
|
||||||
AErrorAddrs:=CallerAddr;
|
AErrorAddrs:=CallerAddr;
|
||||||
if (not ACondition) then
|
if (not ACondition) then
|
||||||
Fail(AMessage,AErrorAddrs);
|
Fail(AMessage,AErrorAddrs)
|
||||||
|
else
|
||||||
|
Inc(AssertCount); // Fail will increae AssertCount
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
@ -1013,10 +1023,13 @@ begin
|
|||||||
RunMethod := TRunMethod(m);
|
RunMethod := TRunMethod(m);
|
||||||
ExpectException('',Nil,'',0);
|
ExpectException('',Nil,'',0);
|
||||||
try
|
try
|
||||||
|
AssertCount:=0;
|
||||||
FailMessage:='';
|
FailMessage:='';
|
||||||
RunMethod;
|
RunMethod;
|
||||||
if (FExpectedException<>Nil) then
|
if (FExpectedException<>Nil) then
|
||||||
FailMessage:=Format(SExceptionCompare, [FExpectedException.ClassName, SNoException])
|
FailMessage:=Format(SExceptionCompare, [FExpectedException.ClassName, SNoException]);
|
||||||
|
if CheckAssertCalled and (AssertCount=0) then
|
||||||
|
FailMessage:=SAssertNotCalled;
|
||||||
except
|
except
|
||||||
On E : Exception do
|
On E : Exception do
|
||||||
begin
|
begin
|
||||||
@ -1465,5 +1478,7 @@ begin
|
|||||||
ITestListener(FListeners[i]).EndTestSuite(ATestSuite);
|
ITestListener(FListeners[i]).EndTestSuite(ATestSuite);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
initialization
|
||||||
|
TTestCase.CheckAssertCalled:=False;
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user