Skip to content

Commit

Permalink
Use deep comparison on Object2DArrayAssert#isDeepEqualTo() (fixes #1976)
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed Aug 25, 2020
1 parent 6b8696b commit 796862e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/assertj/core/api/Object2DArrayAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.error.array2d.Array2dElementShouldBeDeepEqual.elementShouldBeEqual;

import java.util.Comparator;
import java.util.Objects;

import org.assertj.core.data.Index;
import org.assertj.core.internal.Failures;
Expand Down Expand Up @@ -86,7 +87,7 @@ public Object2DArrayAssert<ELEMENT> isDeepEqualTo(ELEMENT[][] expected) {
info.representation().toStringOf(actual), info.representation().toStringOf(expected));
}
for (int j = 0; j < actualSubArray.length; j++) {
if (actualSubArray[j] != expectedSubArray[j]) {
if (!Objects.deepEquals(actualSubArray[j], expectedSubArray[j])) {
throw failures.failure(info, elementShouldBeEqual(actualSubArray[j], expectedSubArray[j], i, j),
info.representation().toStringOf(actual), info.representation().toStringOf(expected));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.assertj.core.api.Object2DArrayAssert;
import org.assertj.core.error.ErrorMessageFactory;
import org.assertj.core.test.Person;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -53,14 +54,58 @@ void should_pass_if_both_actual_and_expected_are_same_arrays() {
}

@Test
void should_pass_if_both_actual_and_expected_are_equal() {
void should_pass_if_both_actual_and_expected_have_same_references() {
// GIVEN
String[][] actual = new String[][] { { "1", "2" }, { "3" }, { "4", "5", "6" } };
String[][] expected = new String[][] { { "1", "2" }, { "3" }, { "4", "5", "6" } };
// WHEN/THEN
then(actual).isDeepEqualTo(expected);
}

@Test
void should_pass_if_both_actual_and_expected_are_equal() {
// GIVEN
Person[][] actual = new Person[][] {
{ new Person("Homer"), new Person("Marge") },
{ new Person("Liza"), new Person("Bart"), new Person("Maggie") },
{ null, null }
};
Person[][] expected = new Person[][] {
{ new Person("Homer"), new Person("Marge") },
{ new Person("Liza"), new Person("Bart"), new Person("Maggie") },
{ null, null }
};
// WHEN/THEN
then(actual).isDeepEqualTo(expected);
}

@Test
void should_pass_with_three_dimensional_equal_arrays() {
// GIVEN
Person[][][] actual = new Person[][][] {
{
{ new Person("Homer"), new Person("Marge") },
{ new Person("Liza"), new Person("Bart"), new Person("Maggie") },
{ null, null }
},
{
{ new Person("Apu"), new Person("Ned"), new Person("Milhouse") }
},
};
Person[][][] expected = new Person[][][] {
{
{ new Person("Homer"), new Person("Marge") },
{ new Person("Liza"), new Person("Bart"), new Person("Maggie") },
{ null, null }
},
{
{ new Person("Apu"), new Person("Ned"), new Person("Milhouse") }
},
};
// WHEN/THEN
then(actual).isDeepEqualTo(expected);
}

@Test
void should_fail_if_actual_is_null() {
// GIVEN
Expand Down

0 comments on commit 796862e

Please sign in to comment.