Skip to content

Commit

Permalink
Added more test cases for Assert class.
Browse files Browse the repository at this point in the history
  • Loading branch information
staticcat committed Aug 23, 2013
1 parent f426360 commit fb9f90a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 7 deletions.
9 changes: 4 additions & 5 deletions DUnitX.TestFramework.pas
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ class procedure Assert.AreEqual<T>(const left, right: T; const message: string);
leftValue := TValue.From<T>(left);
rightValue := TValue.From<T>(right);

Fail(Format('left %s but got %s',[leftValue.AsString,rightValue.AsString]), ReturnAddress);
Fail(Format('left %s but got %s', [leftValue.AsString, rightValue.AsString]), ReturnAddress);
end;
end;
{$ENDIF}
Expand Down Expand Up @@ -939,7 +939,6 @@ class procedure Assert.Fail(const message : string; const errorAddrs : pointer);
//Otherwise use the return address we can currently get to for where to raise the exception
raise ETestFailure.Create(message) at ReturnAddress;

// raise ETestFailure.Create(Format('Fail. %s',[message]));
end;


Expand Down Expand Up @@ -1168,7 +1167,7 @@ class procedure Assert.WillRaise(const AMethod : TTestLocalMethod; const excepti
if exceptionClass <> nil then
begin
if e.ClassType <> exceptionClass then
Fail('Method did not throw exception of type : ' + exceptionClass.ClassName + GetMsg, ReturnAddress)
Fail(Format('Method raised [%s] was expecting [%s]. %s', [e.ClassName, exceptionClass.ClassName, e.message]), ReturnAddress)
else
exit;
end;
Expand Down Expand Up @@ -1202,10 +1201,10 @@ class procedure Assert.WillNotRaise(const AMethod : TTestLocalMethod; const exce
if exceptionClass <> nil then
begin
if e.ClassType = exceptionClass then
Fail('Method raised an exception of type : ' + exceptionClass.ClassName + #13#10 + e.Message + GetMsg, ReturnAddress);
Fail('Method raised an exception of type : ' + exceptionClass.ClassName + #13#10 + e.Message + GetMsg, ReturnAddress);
end
else
Fail('Method raised and exception of type : ' + e.ClassName + #13#10 + e.Message + GetMsg, ReturnAddress);
Fail(Format('Method raised [%s] was expecting not to raise [%s]. %s', [e.ClassName, exceptionClass.ClassName, e.message]), ReturnAddress);
end;
end;
end;
Expand Down
4 changes: 2 additions & 2 deletions DUnitX.TestRunner.pas
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ procedure TDUnitXTestRunner.ExecuteFixtures(const context: ITestExecuteContext;
begin
//TODO: Does test failure require its own results interface and class?
Log(TLogLevel.ltError, 'Test failed : ' + test.Name + ' : ' + e.Message);
testError := TDUnitXTestError.Create(test as ITestInfo, TTestResultType.Failure, e, ExceptAddr);
testError := TDUnitXTestError.Create(test as ITestInfo, TTestResultType.Failure, e, ExceptAddr, e.Message);
context.RecordResult(testError);
Self.Loggers_AddFailure(threadId, testError);
end;
Expand All @@ -550,7 +550,7 @@ procedure TDUnitXTestRunner.ExecuteFixtures(const context: ITestExecuteContext;
on e: Exception do
begin
Log(TLogLevel.ltError, 'Test Error : ' + test.Name + ' : ' + e.Message);
testError := TDUnitXTestError.Create(test as ITestInfo, TTestResultType.Error, e, ExceptAddr);
testError := TDUnitXTestError.Create(test as ITestInfo, TTestResultType.Error, e, ExceptAddr, e.Message);
context.RecordResult(testError);
Self.Loggers_AddError(threadId, testError);
end;
Expand Down
102 changes: 102 additions & 0 deletions Tests/DUnitX.Tests.Assert.pas
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ TTestsAssert = class
procedure AreEqual_String_Throws_No_Exception_When_Values_Are_Equal;
[TestCase]
procedure AreEqual_String_Throws_ETestFailure_When_Values_Are_NotEqual;
[TestCase]
procedure AreEqual_Extended_Throws_No_Exception_When_Values_Are_Equal;
[TestCase]
procedure AreEqual_Extended_Throws_ETestFailure_When_Values_Are_NotEqual;
[TestCase]
procedure AreEqual_TClass_Throws_No_Exception_When_Classes_Are_Equal;
[TestCase]
procedure AreEqual_TClass_Throws_ETestFailure_When_Classes_Are_NotEqual;
[TestCase]
procedure AreEqual_T_Throws_No_Exception_When_Classes_Are_Equal;
[TestCase]
procedure AreEqual_T_Throws_ETestFailure_When_Classes_Are_NotEqual;

[TestCase]
procedure Warn_Throws_ETestWarning_Exception;
[TestCase]
Expand All @@ -63,8 +76,22 @@ TTestsAssert = class
implementation

uses
Delphi.Mocks,
SysUtils;

type
{$M+}
IMockInterface = interface
['{8DB1A216-0E95-4241-9522-C50DF99AFB71}']
procedure Stub;
end;
{$M-}

TMockClassOne = class(TObject)
end;
TMockClassTwo = class(TObject)
end;

{ TTestAssert }

procedure TTestsAssert.Fail_Throws_ETestFailure_Exception;
Expand Down Expand Up @@ -154,6 +181,33 @@ procedure TTestsAssert.Warn_Throws_ETestWarning_Exception;
end, ETestWarning);
end;

procedure TTestsAssert.AreEqual_Extended_Throws_ETestFailure_When_Values_Are_NotEqual;
const
ACTUAL_EXTENDED = 1.19E20;
EXPECTED_EXTENDED = 1.18E20;
TOLERANCE_EXTENDED = 0.001E20;
begin
Assert.WillRaise(
procedure
begin
Assert.AreEqual(ACTUAL_EXTENDED, EXPECTED_EXTENDED, TOLERANCE_EXTENDED);
end, ETestFailure, Format('[%e] with in [%e] from [%e]', [ACTUAL_EXTENDED, TOLERANCE_EXTENDED, EXPECTED_EXTENDED]));

end;

procedure TTestsAssert.AreEqual_Extended_Throws_No_Exception_When_Values_Are_Equal;
const
ACTUAL_EXTENDED = 1.19E20;
EXPECTED_EXTENDED = 1.18E20;
TOLERANCE_EXTENDED = 0.011E20;
begin
Assert.WillNotRaise(
procedure
begin
Assert.AreEqual(ACTUAL_EXTENDED, EXPECTED_EXTENDED, TOLERANCE_EXTENDED);
end, Exception);
end;

procedure TTestsAssert.AreEqual_String_Throws_ETestFailure_When_Values_Are_NotEqual;
const
ACTUAL_STRING = 'the brown dog jumped something';
Expand All @@ -177,6 +231,24 @@ procedure TTestsAssert.AreEqual_String_Throws_No_Exception_When_Values_Are_Equal
end, Exception);
end;

procedure TTestsAssert.AreEqual_TClass_Throws_ETestFailure_When_Classes_Are_NotEqual;
begin
Assert.WillRaise(
procedure
begin
Assert.AreEqual(TMockClassOne, TMockClassTwo);
end, ETestFailure);
end;

procedure TTestsAssert.AreEqual_TClass_Throws_No_Exception_When_Classes_Are_Equal;
begin
Assert.WillNotRaise(
procedure
begin
Assert.AreEqual(TMockClassOne, TMockClassOne);
end, ETestFailure);
end;

procedure TTestsAssert.AreEqual_Throws_No_Exception_When_Values_Are_Exactly_Equal;
var
actualAndExpected, tolerance : Extended;
Expand All @@ -192,6 +264,36 @@ procedure TTestsAssert.AreEqual_Throws_No_Exception_When_Values_Are_Exactly_Equa
end, Exception);
end;

procedure TTestsAssert.AreEqual_T_Throws_ETestFailure_When_Classes_Are_NotEqual;
var
mock : IInterface;
mock2 : IInterface;
begin
mock := TInterfacedObject.Create();
mock2 := TInterfacedObject.Create();

Assert.WillRaise(
procedure
begin
Assert.AreEqual<IInterface>(mock, mock2);
end, ETestFailure);

//TODO: Fix generic are equals invalid cast error. TValue of a class does not allow AsString
end;

procedure TTestsAssert.AreEqual_T_Throws_No_Exception_When_Classes_Are_Equal;
var
mock : IInterface;
begin
mock := TInterfacedObject.Create();

Assert.WillNotRaise(
procedure
begin
Assert.AreEqual<IInterface>(mock, mock);
end, ETestFailure);
end;

initialization
TDUnitX.RegisterTestFixture(TTestsAssert);
end.

0 comments on commit fb9f90a

Please sign in to comment.