diff --git a/CHANGELOG.md b/CHANGELOG.md index d5035b759..1dfd5dde0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- It's now possible to pass a subclass of a field's type into `#withPrefabValuesForField()`. ([Issue 1012](https://github.com/jqno/equalsverifier/issues/1012)) + ### Changed - The internal instantiation logic has been further refactored, to be more robust and extensible for future enhancements. diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Validations.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Validations.java index 2b0ed19e9..f8689b308 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Validations.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Validations.java @@ -95,12 +95,12 @@ public static void validateFieldTypeMatches( ) { try { Field f = container.getDeclaredField(fieldName); - boolean sameFields = f.getType().equals(fieldType); - boolean compatibleFields = fieldType.equals( + boolean typeCompatible = f.getType().isAssignableFrom(fieldType); + boolean wrappingCompatible = fieldType.equals( PrimitiveMappers.PRIMITIVE_OBJECT_MAPPER.get(f.getType()) ); validate( - !sameFields && !compatibleFields, + !typeCompatible && !wrappingCompatible, "Prefab values for field " + fieldName + " should be of type " + diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithPrefabValuesForFieldTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithPrefabValuesForFieldTest.java index dad621f2d..1f8161e8b 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithPrefabValuesForFieldTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/operational/WithPrefabValuesForFieldTest.java @@ -2,8 +2,7 @@ import java.time.LocalDate; import java.time.Month; -import java.util.Arrays; -import java.util.Objects; +import java.util.*; import nl.jqno.equalsverifier.EqualsVerifier; import nl.jqno.equalsverifier.Warning; import nl.jqno.equalsverifier.internal.testhelpers.ExpectedException; @@ -166,6 +165,18 @@ public void succeed_whenPrefabForArrayIsOverridden() { .verify(); } + @Test + public void succeed_whenClassContainsSomethingThatAllowsSubclassesAndASubclassIsGiven() { + EqualsVerifier + .forClass(ListContainer.class) + .withPrefabValuesForField( + "list", + Collections.singletonList("x"), + Collections.singletonList("y") + ) + .verify(); + } + static final class SinglePrecondition { private final FinalPoint point; @@ -274,4 +285,27 @@ public int hashCode() { return Arrays.hashCode(field); } } + + static final class ListContainer { + + private final List list; + + public ListContainer(List list) { + this.list = list; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ListContainer)) { + return false; + } + ListContainer other = (ListContainer) obj; + return Objects.equals(list, other.list); + } + + @Override + public int hashCode() { + return Objects.hash(list); + } + } }