Skip to content

Commit

Permalink
Fixes #257 Quote logs expected and actual values (#296)
Browse files Browse the repository at this point in the history
* Put expected and actual values into square brackets
Make wording consistent
Make capitalization consistent (only boolean or special values start with uppercase)
Remove redundant string
Adjusted expected test results accordingly

* implement #168 "class procedure Assert.AreEqual<T>(const expected, actual : TArray<T>; const message : string = '')"
- Implement Assert
- Add Unit tests
  • Loading branch information
luebbe committed Sep 23, 2021
1 parent 7ac1f29 commit ebab0a5
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 36 deletions.
27 changes: 26 additions & 1 deletion Source/DUnitX.Assert.pas
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Assert = class
{$IFNDEF DELPHI_XE_DOWN}
//Delphi 2010 and XE compiler bug breaks this
class procedure AreEqual<T>(const expected, actual : T; const message : string = '');overload;
class procedure AreEqual<T>(const expected, actual : TArray<T>; const message : string = '');overload;
{$ENDIF}
class procedure AreEqual(const expected, actual : word; const message : string = '');overload;
class procedure AreEqual(const expected, actual : Integer; const message : string = '');overload;
Expand Down Expand Up @@ -427,6 +428,30 @@ class procedure Assert.AreEqual<T>(const expected, actual: T; const message: str
FailFmt(SNotEqualErrorStr, [expectedValue.ToString, actualValue.ToString, message], ReturnAddress)
end;
end;

class procedure Assert.AreEqual<T>(const expected, actual : TArray<T>; const message : string = '');
var
i : integer;
comparer : IComparer<T>;
expectedValue, actualValue : TValue;
begin
DoAssert;
if Length(expected) <> Length(actual) then
FailFmt(SArraysHaveDifferentLength, [Length(expected), Length(actual), message], ReturnAddress)
else begin
comparer := TComparer<T>.Default;
for i := 0 to High(expected) do
begin
if comparer.Compare(expected[i],actual[i]) <> 0 then
begin
expectedValue := TValue.From<T>(expected[i]);
actualValue := TValue.From<T>(actual[i]);
FailFmt(SArraysAreDifferent, [i, expectedValue.ToString, actualValue.ToString, message], ReturnAddress)
end;
end;
end;
end;

{$ENDIF}

class function Assert.AddLineBreak(const msg: string): string;
Expand Down Expand Up @@ -549,7 +574,7 @@ class procedure Assert.AreNotEqual<T>(const expected, actual: T; const message:
expectedValue := TValue.From<T>(expected);
actualValue := TValue.From<T>(actual);

FailFmt(SEqualsErrorStr2,[expectedValue.ToString, actualValue.ToString, message], ReturnAddress);
FailFmt(SEqualsErrorStr,[expectedValue.ToString, actualValue.ToString, message], ReturnAddress);
end;
end;
{$ENDIF}
Expand Down
65 changes: 33 additions & 32 deletions Source/DUnitX.ResStrs.pas
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,48 @@ interface
resourcestring
STestRunComplete = 'Test Run Complete';

SUnexpectedErrorExt = 'Expected %g but got %g %s';
SUnexpectedErrorInt = 'Expected %d but got %d %s';
SUnexpectedErrorStr = 'Expected %s but got %s %s';
SUnexpectedErrorDbl = 'Expected %g but got %g %s';
SUnexpectedErrorGUID = 'Expected %s but got %s %s';
SUnexpectedErrorExt = 'Expected [%g] but got [%g] %s';
SUnexpectedErrorInt = 'Expected [%d] but got [%d] %s';
SUnexpectedErrorStr = 'Expected [%s] but got [%s] %s';
SUnexpectedErrorDbl = 'Expected [%g] but got [%g] %s';
SUnexpectedErrorGUID = 'Expected [%s] but got [%s] %s';
SUnexpectedErrorStream = 'Streams are not equal %s';
SNotEqualErrorStr = 'Expected %s is not equal to actual %s %s';
SNotEqualErrorStr = 'Expected [%s] but got [%s] %s';
SMemoryValuesNotEqual = 'Memory values are not equal. ';
SFileDoesNotExist = 'File [%s] does not exist';
SFileDoesExist = 'File [%s] does exist';
SFileUnexpectedErrorCreation = 'Expected creation datetime [%s] but got [%s] %s';
SEqualsErrorExt = '%g equals actual %g %s';
SEqualsErrorStr = '[%s] is equal to [%s] %s';
SEqualsErrorStr2 = 'Expected %s equals actual %s %s';
SEqualsErrorInt = 'Expected %d equals actual %d %s';
SEqualsErrorDbl = '%g equals actual %g %s';
SEqualsErrorObj = 'Object [%s] Equals Object [%s] %s';
SEqualsErrorGUID = 'Expected %s equals actual %s %s';
SEqualsErrorExt = 'Expected [%g] equals actual [%g] %s';
SEqualsErrorStr = 'Expected [%s] equals actual [%s] %s';
SEqualsErrorInt = 'Expected [%d] equals actual [%d] %s';
SEqualsErrorDbl = 'Expected [%g] equals actual [%g] %s';
SEqualsErrorObj = 'Object [%s] equals object [%s] %s';
SEqualsErrorGUID = 'Expected [%s] equals actual [%s] %s';
SEqualsErrorStream = 'Streams are equal %s';
SEqualsErrorIntf = 'references are the same. %s';
SMemoryValuesEqual = 'Memory values are equal. ';
SNotEqualErrorIntf = 'references are Not the same. %s';
SNotEqualErrorObj = 'Object [%s] Not Object [%s] %s';
SNotEqualErrorObj = 'Object [%s] does not equal object [%s] %s';
SArraysHaveDifferentLength = 'Arrays have different length. Expected [%d] but got [%d] %s';
SArraysAreDifferent = 'Arrays are different at position [%d]. Expected [%s] but got [%s] %s';
SValueNotInList = 'List does not contain value %s. %s';
SValueInList = 'List contains value %s. %s';
SIntfNotImplemented = 'value does not implement %s. %s';
SListNotEmpty = 'List is Not empty. %s';
SStrNotEmpty = 'String is Not empty. %s';
SVarNotEmpty = 'Variant is Not empty. %s';
SListNotEmpty = 'List is Not Empty. %s';
SStrNotEmpty = 'String is Not Empty. %s';
SVarNotEmpty = 'Variant is Not Empty. %s';
SIsFalseError = 'Condition is True when False expected. %s';
SListEmpty = 'List is Empty when Not empty expected. %s';
SListEmpty = 'List is Empty when Not Empty expected. %s';
SStrEmpty = 'String is Empty. %s';
SVarEmpty = 'Variant is Empty. %s';
SIntfNil = 'Interface is Nil when not nil expected. %s';
SPointerNil = 'Pointer is Nil when not Nil expected. %s';
SIntfNil = 'Interface is Nil when Not Nil expected. %s';
SPointerNil = 'Pointer is Nil when Not Nil expected. %s';
SObjNil = 'Object is Nil when Not Nil expected. %s';
SVariantNull = 'Variant is Null when Not Null expcted. %s';
SVariantNotNull = 'Variant is Not Null when Null expected. [%s]';
SIntfNotNil = 'Interface is not Nil when nil expected. [%s]';
SObjNotNil = 'Object is not nil when nil expected. [%s]';
SPointerNotNil = 'Pointer is not Nil when nil expected. [%s]';
SIntfNotNil = 'Interface is Not Nil when Nil expected. [%s]';
SObjNotNil = 'Object is Not Nil when Nil expected. [%s]';
SPointerNotNil = 'Pointer is Not Nil when Nil expected. [%s]';
SIsTrueError = 'Condition is False when True expected. [%s]';
STypeError = 'value is not of type T';
SUnexpectedException = 'Method raised [%s] was expecting not to raise Any exception. %s';
Expand All @@ -89,35 +90,35 @@ interface
SStrDoesNotEndWith = '[%s] does not end with [%s] %s';
SStrDoesNotMatch = '[%s] does not match [%s] %s';
SStrCannotBeEmpty = 'subString cannot be empty';
SStrDoesNotStartWith = '[%s] does Not Start with [%s] %s';
SStrDoesNotStartWith = '[%s] does not start with [%s] %s';
SNumberOfStringsNotEqual = 'Number of strings is not equal';
SLengthOfStringsNotEqual = 'Length of strings is not equal';
SDiffAtPosition = 'Difference at position %d';

SInvalidValueBool = 'Invalid value, not boolean';
SInvalidOptionType = 'Invalid Option type - only string, integer, float, boolean, enum and sets are supported';
SInvalidEnum = 'Invalid Enum Value : ';
SInvalidOptionType = 'Invalid option type - only string, integer, float, boolean, enum and sets are supported';
SInvalidEnum = 'Invalid enum value : ';
SInvalidOpt = 'invalid option type';

SNameRequired = 'Name required - use RegisterUnamed to register unamed options';
SOptionAlreadyRegistered = 'Options : %s already registered';
SUnknownOptionStart = 'Unknown option start : ';

SOptionExpectedValue = 'Option [ %s ] expected a following :value but none was found';
SOptionExpectedValue = 'Option [%s] expected a following :value but none was found';
SParameterFileDoesNotExist = 'Parameter File [%s] does not exist';
SErrorParsingParameterFile = 'Error parsing Parameter File [%s] : ';
SErrorParsingParameterFile = 'Error parsing parameter file [%s] : ';
SErrorSettingOption = 'Error setting option : %s to %s : ';
SUnknownCommandLineOption = 'Unknown command line option : ';
SOptionNotSpecified = 'Required Option [%s] was not specified';
SOptionNotSpecified = 'Required option [%s] was not specified';

STestIgnoredRepeatSet = 'Repeat Set to 0. Test Ignored.';
STestIgnoredRepeatSet = 'Repeat set to 0. Test ignored.';

SRegisteredImplementationError = 'The implementation registered (%s) does not implement %s';
SImplementationAlreadyRegistered = 'An implementation for type %s with name %s is already registered with IoC';
SNoImplementationRegistered = 'No implementation registered for type %s';
SNoInstance = 'The activator delegate failed to return an instance %s';

SNoConsoleWriterClassRegistered = 'No ConsoleWriter Class is registered. You will need to include DUnitX.Windows.Console, DUnitX.MacOS.Console or DUnitX.Linux.Console in you application';
SNoConsoleWriterClassRegistered = 'No ConsoleWriter class is registered. You will need to include DUnitX.Windows.Console, DUnitX.MacOS.Console or DUnitX.Linux.Console in you application';
SExecutingTest = 'Executing Test : ';
SRunningFixtureSetup = 'Running Fixture Setup Method : ';
SRunningSetup = 'Running Setup for : ';
Expand Down Expand Up @@ -151,7 +152,7 @@ interface
SSetupTeardownBytesLeaked = '%d bytes were leaked in the setup/teardown methods';
STestBytesLeaked = '%d bytes were leaked in the test method';
SSetupTestTeardownBytesLeaked = '%d bytes were leaked in the setup/test/teardown methods';
STestFailed = 'Test failed : ';
STestFailed = 'Test Failed : ';
STestError = 'Test Error : ';
STestIgnored = 'Test Ignored : ';
STestLeaked = 'Test Leaked Memory : ';
Expand Down
49 changes: 46 additions & 3 deletions Tests/DUnitX.Tests.Assert.pas
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ TTestsAssert = class

[Test]
procedure AreEqual_T_Throws_ETestFailure_When_Objects_Are_Nil;

[Test]
procedure AreEqual_Array_T_Throws_No_Exception_When_Arrays_Are_Equal;

[Test]
procedure AreEqual_Array_T_Throws_ETestFailure_When_Arrays_Are_Not_Equal;

{$ENDIF}

[Test]
Expand Down Expand Up @@ -413,13 +420,13 @@ procedure TTestsAssert.NoDiff_Throws_ETestFailure_When_Strings_Are_NotEqual;
Assert.WillRaiseWithMessage(procedure
begin
Assert.NoDiff(' '#8, ' ');
end, ETestFailure, 'Length of strings is not equal: Expected 3 but got 1 ');
end, ETestFailure, 'Length of strings is not equal: Expected [3] but got [1] ');

Assert.WillRaiseWithMessage(procedure
begin
Assert.NoDiff('lorem ipsum', 'lorem ipsum ', 'characters');
end,
ETestFailure, 'Length of strings is not equal: Expected 11 but got 12 characters');
ETestFailure, 'Length of strings is not equal: Expected [11] but got [12] characters');

Assert.WillRaiseWithMessage(procedure
begin
Expand Down Expand Up @@ -1029,7 +1036,7 @@ procedure TTestsAssert.AreEqual_TStrings_Throws_ETestFailure_When_Strings_Are_No
Assert.WillRaiseWithMessage(procedure
begin
Assert.AreEqual(expected, actual);
end, ETestFailure, 'Number of strings is not equal: Expected 3 but got 4 ');
end, ETestFailure, 'Number of strings is not equal: Expected [3] but got [4] ');

expected.CommaText := '"Lorem ipsum dolor sit amet","consectetur adipisici elit","sed eiusmod tempor incidunt"';
actual.CommaText := '"Lorem ipsum dolor sit amet","consectetur adisipici elit","sed eiusmod tempor incidunt"';
Expand Down Expand Up @@ -1182,6 +1189,42 @@ procedure TTestsAssert.AreEqual_T_Throws_No_Exception_When_Objects_Are_Equal;
FreeAndNil(mock);
end;
end;

procedure TTestsAssert.AreEqual_Array_T_Throws_ETestFailure_When_Arrays_Are_Not_Equal;
begin
Assert.WillRaise(
procedure
begin
Assert.AreEqual<integer>([1, 2, 3], [1, 2, 3, 4]);
end, ETestFailure);

Assert.WillRaise(
procedure
begin
Assert.AreEqual<real>([3.14, 3.15], [3.15, 3.14]);
end, ETestFailure);

Assert.WillRaise(
procedure
begin
Assert.AreEqual<string>(['a', 'b', 'c'], ['a', 'c', 'b']);
end, ETestFailure);
end;

procedure TTestsAssert.AreEqual_Array_T_Throws_No_Exception_When_Arrays_Are_Equal;
begin
Assert.WillNotRaise(
procedure
begin
Assert.AreEqual<string>(['a', 'b', 'c'], ['a', 'b', 'c']);
end, ETestFailure);

Assert.WillNotRaise(
procedure
begin
Assert.AreEqual<real>([3.14, 3.15], [3.14, 3.15]);
end, ETestFailure);
end;
{$ENDIF}

procedure TTestsAssert.Test_AreSameOnSameObjectWithDifferentInterfaces_No_Exception;
Expand Down

0 comments on commit ebab0a5

Please sign in to comment.