Skip to content

Commit

Permalink
Allows subclasses to be passed into withPrefabValuesForField()
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Nov 5, 2024
1 parent fb2cd1a commit 3d839b4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ public static <T> 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 " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -274,4 +285,27 @@ public int hashCode() {
return Arrays.hashCode(field);
}
}

static final class ListContainer {

private final List<String> list;

public ListContainer(List<String> 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);
}
}
}

0 comments on commit 3d839b4

Please sign in to comment.