Skip to content

Commit ebab0a5

Browse files
authored
Fixes #257 Quote logs expected and actual values (#296)
* 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
1 parent 7ac1f29 commit ebab0a5

File tree

3 files changed

+105
-36
lines changed

3 files changed

+105
-36
lines changed

Source/DUnitX.Assert.pas

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Assert = class
7979
{$IFNDEF DELPHI_XE_DOWN}
8080
//Delphi 2010 and XE compiler bug breaks this
8181
class procedure AreEqual<T>(const expected, actual : T; const message : string = '');overload;
82+
class procedure AreEqual<T>(const expected, actual : TArray<T>; const message : string = '');overload;
8283
{$ENDIF}
8384
class procedure AreEqual(const expected, actual : word; const message : string = '');overload;
8485
class procedure AreEqual(const expected, actual : Integer; const message : string = '');overload;
@@ -427,6 +428,30 @@ class procedure Assert.AreEqual<T>(const expected, actual: T; const message: str
427428
FailFmt(SNotEqualErrorStr, [expectedValue.ToString, actualValue.ToString, message], ReturnAddress)
428429
end;
429430
end;
431+
432+
class procedure Assert.AreEqual<T>(const expected, actual : TArray<T>; const message : string = '');
433+
var
434+
i : integer;
435+
comparer : IComparer<T>;
436+
expectedValue, actualValue : TValue;
437+
begin
438+
DoAssert;
439+
if Length(expected) <> Length(actual) then
440+
FailFmt(SArraysHaveDifferentLength, [Length(expected), Length(actual), message], ReturnAddress)
441+
else begin
442+
comparer := TComparer<T>.Default;
443+
for i := 0 to High(expected) do
444+
begin
445+
if comparer.Compare(expected[i],actual[i]) <> 0 then
446+
begin
447+
expectedValue := TValue.From<T>(expected[i]);
448+
actualValue := TValue.From<T>(actual[i]);
449+
FailFmt(SArraysAreDifferent, [i, expectedValue.ToString, actualValue.ToString, message], ReturnAddress)
450+
end;
451+
end;
452+
end;
453+
end;
454+
430455
{$ENDIF}
431456

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

552-
FailFmt(SEqualsErrorStr2,[expectedValue.ToString, actualValue.ToString, message], ReturnAddress);
577+
FailFmt(SEqualsErrorStr,[expectedValue.ToString, actualValue.ToString, message], ReturnAddress);
553578
end;
554579
end;
555580
{$ENDIF}

Source/DUnitX.ResStrs.pas

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,47 +33,48 @@ interface
3333
resourcestring
3434
STestRunComplete = 'Test Run Complete';
3535

36-
SUnexpectedErrorExt = 'Expected %g but got %g %s';
37-
SUnexpectedErrorInt = 'Expected %d but got %d %s';
38-
SUnexpectedErrorStr = 'Expected %s but got %s %s';
39-
SUnexpectedErrorDbl = 'Expected %g but got %g %s';
40-
SUnexpectedErrorGUID = 'Expected %s but got %s %s';
36+
SUnexpectedErrorExt = 'Expected [%g] but got [%g] %s';
37+
SUnexpectedErrorInt = 'Expected [%d] but got [%d] %s';
38+
SUnexpectedErrorStr = 'Expected [%s] but got [%s] %s';
39+
SUnexpectedErrorDbl = 'Expected [%g] but got [%g] %s';
40+
SUnexpectedErrorGUID = 'Expected [%s] but got [%s] %s';
4141
SUnexpectedErrorStream = 'Streams are not equal %s';
42-
SNotEqualErrorStr = 'Expected %s is not equal to actual %s %s';
42+
SNotEqualErrorStr = 'Expected [%s] but got [%s] %s';
4343
SMemoryValuesNotEqual = 'Memory values are not equal. ';
4444
SFileDoesNotExist = 'File [%s] does not exist';
4545
SFileDoesExist = 'File [%s] does exist';
4646
SFileUnexpectedErrorCreation = 'Expected creation datetime [%s] but got [%s] %s';
47-
SEqualsErrorExt = '%g equals actual %g %s';
48-
SEqualsErrorStr = '[%s] is equal to [%s] %s';
49-
SEqualsErrorStr2 = 'Expected %s equals actual %s %s';
50-
SEqualsErrorInt = 'Expected %d equals actual %d %s';
51-
SEqualsErrorDbl = '%g equals actual %g %s';
52-
SEqualsErrorObj = 'Object [%s] Equals Object [%s] %s';
53-
SEqualsErrorGUID = 'Expected %s equals actual %s %s';
47+
SEqualsErrorExt = 'Expected [%g] equals actual [%g] %s';
48+
SEqualsErrorStr = 'Expected [%s] equals actual [%s] %s';
49+
SEqualsErrorInt = 'Expected [%d] equals actual [%d] %s';
50+
SEqualsErrorDbl = 'Expected [%g] equals actual [%g] %s';
51+
SEqualsErrorObj = 'Object [%s] equals object [%s] %s';
52+
SEqualsErrorGUID = 'Expected [%s] equals actual [%s] %s';
5453
SEqualsErrorStream = 'Streams are equal %s';
5554
SEqualsErrorIntf = 'references are the same. %s';
5655
SMemoryValuesEqual = 'Memory values are equal. ';
5756
SNotEqualErrorIntf = 'references are Not the same. %s';
58-
SNotEqualErrorObj = 'Object [%s] Not Object [%s] %s';
57+
SNotEqualErrorObj = 'Object [%s] does not equal object [%s] %s';
58+
SArraysHaveDifferentLength = 'Arrays have different length. Expected [%d] but got [%d] %s';
59+
SArraysAreDifferent = 'Arrays are different at position [%d]. Expected [%s] but got [%s] %s';
5960
SValueNotInList = 'List does not contain value %s. %s';
6061
SValueInList = 'List contains value %s. %s';
6162
SIntfNotImplemented = 'value does not implement %s. %s';
62-
SListNotEmpty = 'List is Not empty. %s';
63-
SStrNotEmpty = 'String is Not empty. %s';
64-
SVarNotEmpty = 'Variant is Not empty. %s';
63+
SListNotEmpty = 'List is Not Empty. %s';
64+
SStrNotEmpty = 'String is Not Empty. %s';
65+
SVarNotEmpty = 'Variant is Not Empty. %s';
6566
SIsFalseError = 'Condition is True when False expected. %s';
66-
SListEmpty = 'List is Empty when Not empty expected. %s';
67+
SListEmpty = 'List is Empty when Not Empty expected. %s';
6768
SStrEmpty = 'String is Empty. %s';
6869
SVarEmpty = 'Variant is Empty. %s';
69-
SIntfNil = 'Interface is Nil when not nil expected. %s';
70-
SPointerNil = 'Pointer is Nil when not Nil expected. %s';
70+
SIntfNil = 'Interface is Nil when Not Nil expected. %s';
71+
SPointerNil = 'Pointer is Nil when Not Nil expected. %s';
7172
SObjNil = 'Object is Nil when Not Nil expected. %s';
7273
SVariantNull = 'Variant is Null when Not Null expcted. %s';
7374
SVariantNotNull = 'Variant is Not Null when Null expected. [%s]';
74-
SIntfNotNil = 'Interface is not Nil when nil expected. [%s]';
75-
SObjNotNil = 'Object is not nil when nil expected. [%s]';
76-
SPointerNotNil = 'Pointer is not Nil when nil expected. [%s]';
75+
SIntfNotNil = 'Interface is Not Nil when Nil expected. [%s]';
76+
SObjNotNil = 'Object is Not Nil when Nil expected. [%s]';
77+
SPointerNotNil = 'Pointer is Not Nil when Nil expected. [%s]';
7778
SIsTrueError = 'Condition is False when True expected. [%s]';
7879
STypeError = 'value is not of type T';
7980
SUnexpectedException = 'Method raised [%s] was expecting not to raise Any exception. %s';
@@ -89,35 +90,35 @@ interface
8990
SStrDoesNotEndWith = '[%s] does not end with [%s] %s';
9091
SStrDoesNotMatch = '[%s] does not match [%s] %s';
9192
SStrCannotBeEmpty = 'subString cannot be empty';
92-
SStrDoesNotStartWith = '[%s] does Not Start with [%s] %s';
93+
SStrDoesNotStartWith = '[%s] does not start with [%s] %s';
9394
SNumberOfStringsNotEqual = 'Number of strings is not equal';
9495
SLengthOfStringsNotEqual = 'Length of strings is not equal';
9596
SDiffAtPosition = 'Difference at position %d';
9697

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

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

106-
SOptionExpectedValue = 'Option [ %s ] expected a following :value but none was found';
107+
SOptionExpectedValue = 'Option [%s] expected a following :value but none was found';
107108
SParameterFileDoesNotExist = 'Parameter File [%s] does not exist';
108-
SErrorParsingParameterFile = 'Error parsing Parameter File [%s] : ';
109+
SErrorParsingParameterFile = 'Error parsing parameter file [%s] : ';
109110
SErrorSettingOption = 'Error setting option : %s to %s : ';
110111
SUnknownCommandLineOption = 'Unknown command line option : ';
111-
SOptionNotSpecified = 'Required Option [%s] was not specified';
112+
SOptionNotSpecified = 'Required option [%s] was not specified';
112113

113-
STestIgnoredRepeatSet = 'Repeat Set to 0. Test Ignored.';
114+
STestIgnoredRepeatSet = 'Repeat set to 0. Test ignored.';
114115

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

120-
SNoConsoleWriterClassRegistered = 'No ConsoleWriter Class is registered. You will need to include DUnitX.Windows.Console, DUnitX.MacOS.Console or DUnitX.Linux.Console in you application';
121+
SNoConsoleWriterClassRegistered = 'No ConsoleWriter class is registered. You will need to include DUnitX.Windows.Console, DUnitX.MacOS.Console or DUnitX.Linux.Console in you application';
121122
SExecutingTest = 'Executing Test : ';
122123
SRunningFixtureSetup = 'Running Fixture Setup Method : ';
123124
SRunningSetup = 'Running Setup for : ';
@@ -151,7 +152,7 @@ interface
151152
SSetupTeardownBytesLeaked = '%d bytes were leaked in the setup/teardown methods';
152153
STestBytesLeaked = '%d bytes were leaked in the test method';
153154
SSetupTestTeardownBytesLeaked = '%d bytes were leaked in the setup/test/teardown methods';
154-
STestFailed = 'Test failed : ';
155+
STestFailed = 'Test Failed : ';
155156
STestError = 'Test Error : ';
156157
STestIgnored = 'Test Ignored : ';
157158
STestLeaked = 'Test Leaked Memory : ';

Tests/DUnitX.Tests.Assert.pas

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ TTestsAssert = class
125125

126126
[Test]
127127
procedure AreEqual_T_Throws_ETestFailure_When_Objects_Are_Nil;
128+
129+
[Test]
130+
procedure AreEqual_Array_T_Throws_No_Exception_When_Arrays_Are_Equal;
131+
132+
[Test]
133+
procedure AreEqual_Array_T_Throws_ETestFailure_When_Arrays_Are_Not_Equal;
134+
128135
{$ENDIF}
129136

130137
[Test]
@@ -413,13 +420,13 @@ procedure TTestsAssert.NoDiff_Throws_ETestFailure_When_Strings_Are_NotEqual;
413420
Assert.WillRaiseWithMessage(procedure
414421
begin
415422
Assert.NoDiff(' '#8, ' ');
416-
end, ETestFailure, 'Length of strings is not equal: Expected 3 but got 1 ');
423+
end, ETestFailure, 'Length of strings is not equal: Expected [3] but got [1] ');
417424

418425
Assert.WillRaiseWithMessage(procedure
419426
begin
420427
Assert.NoDiff('lorem ipsum', 'lorem ipsum ', 'characters');
421428
end,
422-
ETestFailure, 'Length of strings is not equal: Expected 11 but got 12 characters');
429+
ETestFailure, 'Length of strings is not equal: Expected [11] but got [12] characters');
423430

424431
Assert.WillRaiseWithMessage(procedure
425432
begin
@@ -1029,7 +1036,7 @@ procedure TTestsAssert.AreEqual_TStrings_Throws_ETestFailure_When_Strings_Are_No
10291036
Assert.WillRaiseWithMessage(procedure
10301037
begin
10311038
Assert.AreEqual(expected, actual);
1032-
end, ETestFailure, 'Number of strings is not equal: Expected 3 but got 4 ');
1039+
end, ETestFailure, 'Number of strings is not equal: Expected [3] but got [4] ');
10331040

10341041
expected.CommaText := '"Lorem ipsum dolor sit amet","consectetur adipisici elit","sed eiusmod tempor incidunt"';
10351042
actual.CommaText := '"Lorem ipsum dolor sit amet","consectetur adisipici elit","sed eiusmod tempor incidunt"';
@@ -1182,6 +1189,42 @@ procedure TTestsAssert.AreEqual_T_Throws_No_Exception_When_Objects_Are_Equal;
11821189
FreeAndNil(mock);
11831190
end;
11841191
end;
1192+
1193+
procedure TTestsAssert.AreEqual_Array_T_Throws_ETestFailure_When_Arrays_Are_Not_Equal;
1194+
begin
1195+
Assert.WillRaise(
1196+
procedure
1197+
begin
1198+
Assert.AreEqual<integer>([1, 2, 3], [1, 2, 3, 4]);
1199+
end, ETestFailure);
1200+
1201+
Assert.WillRaise(
1202+
procedure
1203+
begin
1204+
Assert.AreEqual<real>([3.14, 3.15], [3.15, 3.14]);
1205+
end, ETestFailure);
1206+
1207+
Assert.WillRaise(
1208+
procedure
1209+
begin
1210+
Assert.AreEqual<string>(['a', 'b', 'c'], ['a', 'c', 'b']);
1211+
end, ETestFailure);
1212+
end;
1213+
1214+
procedure TTestsAssert.AreEqual_Array_T_Throws_No_Exception_When_Arrays_Are_Equal;
1215+
begin
1216+
Assert.WillNotRaise(
1217+
procedure
1218+
begin
1219+
Assert.AreEqual<string>(['a', 'b', 'c'], ['a', 'b', 'c']);
1220+
end, ETestFailure);
1221+
1222+
Assert.WillNotRaise(
1223+
procedure
1224+
begin
1225+
Assert.AreEqual<real>([3.14, 3.15], [3.14, 3.15]);
1226+
end, ETestFailure);
1227+
end;
11851228
{$ENDIF}
11861229

11871230
procedure TTestsAssert.Test_AreSameOnSameObjectWithDifferentInterfaces_No_Exception;

0 commit comments

Comments
 (0)