-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix for #803: improve error message for assertArrayEquals() when multi-dimensional arrays have different lengths #1054
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,11 @@ public abstract class ComparisonCriteria { | |
*/ | ||
public void arrayEquals(String message, Object expecteds, Object actuals) | ||
throws ArrayComparisonFailure { | ||
arrayEquals(message, expecteds, actuals, true); | ||
} | ||
|
||
private void arrayEquals(String message, Object expecteds, Object actuals, boolean outer) | ||
throws ArrayComparisonFailure { | ||
if (expecteds == actuals | ||
|| Arrays.deepEquals(new Object[] {expecteds}, new Object[] {actuals})) { | ||
// The reflection-based loop below is potentially very slow, especially for primitive | ||
|
@@ -35,18 +40,20 @@ public void arrayEquals(String message, Object expecteds, Object actuals) | |
String header = message == null ? "" : message + ": "; | ||
|
||
int expectedsLength = assertArraysAreSameLength(expecteds, | ||
actuals, header); | ||
actuals, outer ? header : ""); | ||
|
||
for (int i = 0; i < expectedsLength; i++) { | ||
Object expected = Array.get(expecteds, i); | ||
Object actual = Array.get(actuals, i); | ||
|
||
if (isArray(expected) && isArray(actual)) { | ||
try { | ||
arrayEquals(message, expected, actual); | ||
arrayEquals(message, expected, actual, false); | ||
} catch (ArrayComparisonFailure e) { | ||
e.addDimension(i); | ||
throw e; | ||
} catch (AssertionError e) { | ||
throw new ArrayComparisonFailure(header, e, i); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment here: |
||
} | ||
} else { | ||
try { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,6 +211,26 @@ public void multiDimensionalArraysAreNotEqualNoMessage() { | |
} | ||
} | ||
|
||
@Test | ||
public void multiDimensionalArraysDifferentLengthMessage() { | ||
try { | ||
assertArrayEquals("message", new Object[][]{{true, true}, {false, false}}, new Object[][]{{true, true}, {false}}); | ||
fail(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, will do. (I copied this pattern from the surrounding tests. Will only fix those relevant to this PR, if that's okay with you; I prefer minimal patches.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Stephan202 fine to only make this fix for the tests you added. I'll try to find time to commit a change to fix the others. |
||
} catch (AssertionError exception) { | ||
assertEquals("message: arrays first differed at element [1]; array lengths differed, expected.length=2 actual.length=1", exception.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void multiDimensionalArraysDifferentLengthNoMessage() { | ||
try { | ||
assertArrayEquals(new Object[][]{{true, true}, {false, false}}, new Object[][]{{true, true}, {false}}); | ||
fail(); | ||
} catch (AssertionError exception) { | ||
assertEquals("arrays first differed at element [1]; array lengths differed, expected.length=2 actual.length=1", exception.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void arraysWithNullElementEqual() { | ||
Object[] objects1 = new Object[]{null}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this deserves a comment. Something like "Only include the user-provided message in the outer exception"