Skip to content

Commit

Permalink
Remove duplicate fields after RecursiveComparisonAssert extended Abst…
Browse files Browse the repository at this point in the history
…ractAssert.

Propagate assertions metadata to honor description and representation
  • Loading branch information
joel-costigliola committed Feb 13, 2019
1 parent 6d65477 commit bef7120
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 55 deletions.
11 changes: 6 additions & 5 deletions src/main/java/org/assertj/core/api/AbstractAssert.java
Expand Up @@ -66,13 +66,14 @@ public abstract class AbstractAssert<SELF extends AbstractAssert<SELF, ACTUAL>,
Conditions conditions = Conditions.instance();

@VisibleForTesting
public final WritableAssertionInfo info;
public WritableAssertionInfo info;

// visibility is protected to allow us write custom assertions that need access to actual
@VisibleForTesting
protected final ACTUAL actual;
protected final SELF myself;

// = ConfigurationProvider.CONFIGURATION_PROVIDER.representation(); ?
private static Representation customRepresentation = null;

@VisibleForTesting
Expand Down Expand Up @@ -675,9 +676,9 @@ public SELF satisfies(Consumer<ACTUAL> requirements) {
* <p>
* This allows users to perform <b>OR like assertions</b> since only one the assertions group has to be met.
* <p>
* {@link #overridingErrorMessage(String, Object...) Overriding error message} is not supported as it would prevent from
* {@link #overridingErrorMessage(String, Object...) Overriding error message} is not supported as it would prevent from
* getting the error messages of the failing assertions, these are valuable to figure out what went wrong.<br>
* Describing the assertion is supported (for example with {@link #as(String, Object...)}).
* Describing the assertion is supported (for example with {@link #as(String, Object...)}).
* <p>
* Example:
* <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", HOBBIT);
Expand Down Expand Up @@ -710,9 +711,9 @@ public SELF satisfiesAnyOf(Consumer<ACTUAL> assertions1, Consumer<ACTUAL> assert
* <p>
* This allows users to perform <b>OR like assertions</b> since only one the assertions group has to be met.
* <p>
* {@link #overridingErrorMessage(String, Object...) Overriding error message} is not supported as it would prevent from
* {@link #overridingErrorMessage(String, Object...) Overriding error message} is not supported as it would prevent from
* getting the error messages of the failing assertions, these are valuable to figure out what went wrong.<br>
* Describing the assertion is supported (for example with {@link #as(String, Object...)}).
* Describing the assertion is supported (for example with {@link #as(String, Object...)}).
* <p>
* Example:
* <pre><code class='java'> TolkienCharacter frodo = new TolkienCharacter("Frodo", HOBBIT);
Expand Down
Expand Up @@ -837,7 +837,7 @@ public RecursiveComparisonAssert<?> usingRecursiveComparison() {
*/
@Beta
public RecursiveComparisonAssert<?> usingRecursiveComparison(RecursiveComparisonConfiguration recursiveComparisonConfiguration) {
return new RecursiveComparisonAssert<>(actual, recursiveComparisonConfiguration);
return new RecursiveComparisonAssert<>(actual, recursiveComparisonConfiguration).withAssertionState(myself);
}

// override for proxyable friendly AbstractObjectAssert
Expand Down
29 changes: 8 additions & 21 deletions src/main/java/org/assertj/core/api/RecursiveComparisonAssert.java
Expand Up @@ -23,34 +23,21 @@
import org.assertj.core.api.recursive.comparison.FieldLocation;
import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;
import org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator;
import org.assertj.core.configuration.ConfigurationProvider;
import org.assertj.core.internal.Failures;
import org.assertj.core.internal.Objects;
import org.assertj.core.presentation.Representation;
import org.assertj.core.util.CheckReturnValue;
import org.assertj.core.util.VisibleForTesting;
import org.assertj.core.util.introspection.IntrospectionError;

public class RecursiveComparisonAssert<SELF extends RecursiveComparisonAssert<SELF>> extends AbstractAssert<SELF, Object> {

private Representation customRepresentation = ConfigurationProvider.CONFIGURATION_PROVIDER.representation();
private RecursiveComparisonConfiguration recursiveComparisonConfiguration;
private RecursiveComparisonDifferenceCalculator recursiveComparisonDifferenceCalculator;
@VisibleForTesting
WritableAssertionInfo assertionInfo;
@VisibleForTesting
Failures failures;
@VisibleForTesting
Objects objects;

// TODO propagate assertion info from ...
public RecursiveComparisonAssert(Object actual, RecursiveComparisonConfiguration recursiveComparisonConfiguration) {
super(actual, RecursiveComparisonAssert.class);
this.recursiveComparisonConfiguration = recursiveComparisonConfiguration;
this.assertionInfo = new WritableAssertionInfo(customRepresentation);
recursiveComparisonDifferenceCalculator = new RecursiveComparisonDifferenceCalculator();
failures = Failures.instance();
objects = Objects.instance();
Failures.instance();
}

void setRecursiveComparisonConfiguration(RecursiveComparisonConfiguration recursiveComparisonConfiguration) {
Expand Down Expand Up @@ -162,17 +149,17 @@ public SELF isEqualTo(Object expected) {
if (expected == null) {
// for the assertion to pass, actual must be null but this is not the case since actual != expected
// => we fail expecting actual to be null
objects.assertNull(assertionInfo, actual);
objects.assertNull(info, actual);
}
// at this point expected is not null, which means actual must not be null for the assertion to pass
objects.assertNotNull(assertionInfo, actual);
objects.assertNotNull(info, actual);
// at this point both actual and expected are not null, we can compare them recursively!
List<ComparisonDifference> differences = determineDifferencesWith(expected);
if (!differences.isEmpty()) throw failures.failure(assertionInfo, shouldBeEqualByComparingFieldByFieldRecursively(actual,
expected,
differences,
recursiveComparisonConfiguration,
assertionInfo.representation()));
if (!differences.isEmpty()) throw objects.getFailures().failure(info, shouldBeEqualByComparingFieldByFieldRecursively(actual,
expected,
differences,
recursiveComparisonConfiguration,
info.representation()));
return myself;
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/assertj/core/internal/Objects.java
Expand Up @@ -117,6 +117,11 @@ public ComparisonStrategy getComparisonStrategy() {
return comparisonStrategy;
}

@VisibleForTesting
public Failures getFailures() {
return failures;
}

/**
* Verifies that the given object is an instance of the given type.
*
Expand Down
Expand Up @@ -13,7 +13,6 @@
package org.assertj.core.api;

import static org.assertj.core.error.ShouldBeEqualByComparingFieldByFieldRecursively.shouldBeEqualByComparingFieldByFieldRecursively;
import static org.assertj.core.test.TestData.someInfo;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
import static org.assertj.core.util.Lists.list;
import static org.mockito.Mockito.verify;
Expand All @@ -25,7 +24,7 @@

public class RecursiveComparisonAssert_isEqualTo_BaseTest extends ObjectsBaseTest {

public static final WritableAssertionInfo INFO = someInfo();
public static WritableAssertionInfo info;
public RecursiveComparisonConfiguration recursiveComparisonConfiguration;

@BeforeEach
Expand All @@ -35,19 +34,18 @@ public void setup() {

public void verifyShouldBeEqualByComparingFieldByFieldRecursivelyCall(Object actual, Object expected,
ComparisonDifference... differences) {
verify(failures).failure(INFO, shouldBeEqualByComparingFieldByFieldRecursively(actual,
verify(failures).failure(info, shouldBeEqualByComparingFieldByFieldRecursively(actual,
expected,
list(differences),
recursiveComparisonConfiguration,
INFO.representation()));
info.representation()));
}

public void compareRecursivelyFailsAsExpected(Object actual, Object expected) {
RecursiveComparisonAssert<?> recursiveComparisonAssert = new RecursiveComparisonAssert<>(actual,
recursiveComparisonConfiguration);
recursiveComparisonAssert.failures = failures;
recursiveComparisonConfiguration);
info = recursiveComparisonAssert.info;
recursiveComparisonAssert.objects = objects;
recursiveComparisonAssert.assertionInfo = INFO;
expectAssertionError(() -> recursiveComparisonAssert.isEqualTo(expected));
}

Expand Down
Expand Up @@ -27,6 +27,7 @@
import static org.assertj.core.test.AlwaysEqualComparator.ALWAY_EQUALS_TIMESTAMP;
import static org.assertj.core.test.NeverEqualComparator.NEVER_EQUALS;
import static org.assertj.core.test.TestFailures.failBecauseExpectedAssertionErrorWasNotThrown;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
import static org.assertj.core.util.Lists.list;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.Mockito.verify;
Expand All @@ -45,7 +46,6 @@
import java.util.TreeSet;
import java.util.stream.Stream;

import org.assertj.core.api.AssertionInfo;
import org.assertj.core.api.RecursiveComparisonAssert_isEqualTo_BaseTest;
import org.assertj.core.internal.AtPrecisionComparator;
import org.assertj.core.internal.DeepDifference.Difference;
Expand Down Expand Up @@ -81,7 +81,7 @@ public void should_fail_when_actual_is_null_and_expected_is_not() {
// WHEN
compareRecursivelyFailsAsExpected(actual, expected);
// THEN
verify(failures).failure(INFO, shouldNotBeNull());
verify(failures).failure(info, shouldNotBeNull());
}

@Test
Expand All @@ -92,7 +92,7 @@ public void should_fail_when_actual_is_not_null_and_expected_is() {
// WHEN
compareRecursivelyFailsAsExpected(actual, expected);
// THEN
verify(failures).failure(INFO, shouldBeEqual(actual, null, objects.getComparisonStrategy(), INFO.representation()));
verify(failures).failure(info, shouldBeEqual(actual, null, objects.getComparisonStrategy(), info.representation()));
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -255,16 +255,16 @@ private static Stream<Arguments> recursivelyEqualObjectsIgnoringGivenFields() {
person8.neighbour.neighbour.home.address.number = 457;

return Stream.of(arguments(person1, person2, "same data and type, except for one ignored field",
list("name")),
list("name")),
arguments(giant1, person1,
"different type, same data except name and height which is not even a field from person1",
list("name", "height")),
"different type, same data except name and height which is not even a field from person1",
list("name", "height")),
arguments(person3, person4, "same data, different type, except for several ignored fields",
list("name", "home.address.number")),
list("name", "home.address.number")),
arguments(person5, person6, "same data except for one subfield of an ignored field",
list("home")),
list("home")),
arguments(person7, person8, "same data except for one subfield of an ignored field",
list("neighbour.neighbour.home.address.number", "neighbour.name")));
list("neighbour.neighbour.home.address.number", "neighbour.name")));
}

@Test
Expand Down Expand Up @@ -385,8 +385,6 @@ public void should_be_able_to_compare_objects_recursively_using_given_comparator

@Test
public void should_fail_when_fields_differ() {
AssertionInfo info = INFO;

Person actual = new Person();
actual.name = "John";

Expand All @@ -409,8 +407,6 @@ public void should_fail_when_fields_differ() {

@Test
public void should_fail_when_fields_of_child_objects_differ() {
AssertionInfo info = INFO;

Person actual = new Person();
actual.name = "John";
actual.home.address.number = 1;
Expand Down Expand Up @@ -482,8 +478,6 @@ public void should_have_error_message_with_path_to_difference_when_difference_is

@Test
public void should_not_use_equal_implementation_of_objects_to_compare() {
AssertionInfo info = INFO;

AlwaysEqualPerson actual = new AlwaysEqualPerson();
actual.name = "John";
actual.home.address.number = 1;
Expand Down Expand Up @@ -627,7 +621,7 @@ public void should_treat_date_as_equal_to_timestamp() {
other.name = "Fred";
other.dateOfBirth = new Timestamp(1000L);

objects.assertIsEqualToComparingFieldByFieldRecursively(INFO, actual, other, noFieldComparators(),
objects.assertIsEqualToComparingFieldByFieldRecursively(info, actual, other, noFieldComparators(),
defaultTypeComparators());
}

Expand All @@ -644,9 +638,9 @@ public void should_treat_timestamp_as_equal_to_date_when_registering_a_Date_symm
TypeComparators typeComparators = new TypeComparators();
typeComparators.put(Timestamp.class, SYMMETRIC_DATE_COMPARATOR);

objects.assertIsEqualToComparingFieldByFieldRecursively(INFO, actual, other, noFieldComparators(),
objects.assertIsEqualToComparingFieldByFieldRecursively(info, actual, other, noFieldComparators(),
typeComparators);
objects.assertIsEqualToComparingFieldByFieldRecursively(INFO, other, actual, noFieldComparators(),
objects.assertIsEqualToComparingFieldByFieldRecursively(info, other, actual, noFieldComparators(),
typeComparators);
}

Expand All @@ -662,7 +656,7 @@ public void should_treat_timestamp_as_equal_to_date_when_registering_a_Date_symm

Map<String, Comparator<?>> fieldComparators = new HashMap<>();
fieldComparators.put("dateOfBirth", SYMMETRIC_DATE_COMPARATOR);
objects.assertIsEqualToComparingFieldByFieldRecursively(INFO, actual, other, fieldComparators,
objects.assertIsEqualToComparingFieldByFieldRecursively(info, actual, other, fieldComparators,
defaultTypeComparators());
}

Expand All @@ -675,7 +669,7 @@ public void should_be_able_to_compare_objects_with_percentages() {
other.name = "%foo";

try {
objects.assertIsEqualToComparingFieldByFieldRecursively(INFO, actual, other, noFieldComparators(),
objects.assertIsEqualToComparingFieldByFieldRecursively(info, actual, other, noFieldComparators(),
defaultTypeComparators());
} catch (AssertionError err) {
assertThat(err).hasMessageContaining("Path to difference: <name>")
Expand Down Expand Up @@ -730,6 +724,21 @@ public void should_be_able_to_compare_objects_with_cycles_recursively() {
.isEqualTo(expected);
}

@Test
public void should_honor_test_description() {
// GIVEN
Person actual = new Person("John");
actual.home.address.number = 1;
Person expected = new Person("John");
expected.home.address.number = 2;
// WHEN
AssertionError error = expectAssertionError(() -> assertThat(actual).as("test description")
.usingRecursiveComparison()
.isEqualTo(expected));
// THEN
assertThat(error).hasMessageContaining("[test description]");
}

public static class WithMap<K, V> {
public Map<K, V> map;

Expand Down
Expand Up @@ -27,7 +27,7 @@
* with {@link ComparatorBasedComparisonStrategy}.
* <p>
* Is in <code>org.assertj.core.internal</code> package to be able to set {@link Objects#failures} appropriately.
*
*
* @author Joel Costigliola
*/
public class ObjectsBaseTest {
Expand Down

0 comments on commit bef7120

Please sign in to comment.