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 Dec 4, 2024
1 parent 829767c commit cb0f83e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,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 cb0f83e

Please sign in to comment.