Skip to content

Commit

Permalink
Refines #withPrefabValuesForField
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Sep 19, 2024
1 parent b551d3f commit b567c6b
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ public void succeed_whenRecordHasDualPrecondition_givenPrefabValueForBothFields(
record SinglePrecondition(int i) {
public SinglePrecondition {
if (i < 100 || i > 200) {
throw new IllegalArgumentException("i must be between 100 and 200!");
throw new IllegalArgumentException("i must be between 100 and 200! But was " + i);
}
}
}

record DualPrecondition(int x, int y) {
public DualPrecondition {
if (x < 100 || x > 200) {
throw new IllegalArgumentException("x must be between 100 and 200!");
throw new IllegalArgumentException("x must be between 100 and 200! But was " + x);
}
if (y < 500 || y > 600) {
throw new IllegalArgumentException("y must be between 500 and 600!");
throw new IllegalArgumentException("y must be between 500 and 600! But was " + y);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public <S> SingleTypeEqualsVerifierApi<T> withPrefabValuesForField(
) {
Validations.validateFieldNameExists(type, fieldName, actualFields);
PrefabValuesApi.addPrefabValuesForField(fieldCache, type, fieldName, red, blue);
withNonnullFields(fieldName);
suppress(Warning.ZERO_FIELDS);
return this;
}
Expand Down Expand Up @@ -439,6 +440,7 @@ private Configuration<T> buildConfig() {
allExcludedFields,
allIncludedFields,
nonnullFields,
fieldCache.getFieldNames(),
cachedHashCodeInitializer,
hasRedefinedSuperclass,
redefinedSubclass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import java.util.EnumSet;
import java.util.Set;
import nl.jqno.equalsverifier.Warning;
import nl.jqno.equalsverifier.internal.reflection.ClassProbe;
import nl.jqno.equalsverifier.internal.reflection.FieldProbe;
import nl.jqno.equalsverifier.internal.reflection.Tuple;
import nl.jqno.equalsverifier.internal.reflection.TypeTag;
import nl.jqno.equalsverifier.internal.reflection.*;
import nl.jqno.equalsverifier.internal.reflection.annotations.AnnotationCache;
import nl.jqno.equalsverifier.internal.reflection.annotations.SupportedAnnotations;
import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator;
Expand All @@ -26,7 +23,9 @@ public class ReflexivityFieldCheck<T> implements FieldCheck<T> {
private final ValueProvider valueProvider;
private final EnumSet<Warning> warningsToSuppress;
private final Set<String> nonnullFields;
private final Set<String> prefabbedFields;
private final AnnotationCache annotationCache;
private final FieldCache fieldCache;

public ReflexivityFieldCheck(Context<T> context) {
this.subjectCreator = context.getSubjectCreator();
Expand All @@ -36,7 +35,9 @@ public ReflexivityFieldCheck(Context<T> context) {
this.typeTag = config.getTypeTag();
this.warningsToSuppress = config.getWarningsToSuppress();
this.nonnullFields = config.getNonnullFields();
this.prefabbedFields = config.getPrefabbedFields();
this.annotationCache = config.getAnnotationCache();
this.fieldCache = context.getFieldCache();
}

@Override
Expand Down Expand Up @@ -77,8 +78,12 @@ private void checkValueReflexivity(FieldProbe probe) {
}

Field field = probe.getField();
String fieldName = field.getName();
TypeTag tag = TypeTag.of(field, typeTag);
Tuple<?> tuple = valueProvider.provide(tag);
Tuple<?> tuple = prefabbedFields.contains(fieldName)
? fieldCache.get(fieldName)
: valueProvider.provide(tag);

Object left = subjectCreator.withFieldSetTo(field, tuple.getRed());
Object right = subjectCreator.withFieldSetTo(field, tuple.getRedCopy());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator;

/** Contains a cache for values connected to specific fields, for {@link SubjectCreator}. */
Expand Down Expand Up @@ -51,4 +52,11 @@ public <T> Tuple<T> get(String fieldName) {
public boolean contains(String fieldName) {
return cache.containsKey(fieldName);
}

/**
* @return The fields preset in the cache.
*/
public Set<String> getFieldNames() {
return cache.keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public final class Configuration<T> {

private final Class<T> type;
private final Set<String> nonnullFields;
private final Set<String> prefabbedFields;
private final CachedHashCodeInitializer<T> cachedHashCodeInitializer;
private final boolean hasRedefinedSuperclass;
private final Class<? extends T> redefinedSubclass;
Expand All @@ -36,6 +37,7 @@ private Configuration(
TypeTag typeTag,
Set<String> ignoredFields,
Set<String> nonnullFields,
Set<String> prefabbedFields,
AnnotationCache annotationCache,
CachedHashCodeInitializer<T> cachedHashCodeInitializer,
boolean hasRedefinedSuperclass,
Expand All @@ -50,6 +52,7 @@ private Configuration(
this.typeTag = typeTag;
this.ignoredFields = ignoredFields;
this.nonnullFields = nonnullFields;
this.prefabbedFields = prefabbedFields;
this.annotationCache = annotationCache;
this.cachedHashCodeInitializer = cachedHashCodeInitializer;
this.hasRedefinedSuperclass = hasRedefinedSuperclass;
Expand All @@ -66,6 +69,7 @@ public static <T> Configuration<T> build(
Set<String> excludedFields,
Set<String> includedFields,
Set<String> nonnullFields,
Set<String> prefabbedFields,
CachedHashCodeInitializer<T> cachedHashCodeInitializer,
boolean hasRedefinedSuperclass,
Class<? extends T> redefinedSubclass,
Expand Down Expand Up @@ -96,6 +100,7 @@ public static <T> Configuration<T> build(
typeTag,
ignoredFields,
nonnullFields,
prefabbedFields,
annotationCache,
cachedHashCodeInitializer,
hasRedefinedSuperclass,
Expand Down Expand Up @@ -190,6 +195,10 @@ public Set<String> getNonnullFields() {
return Collections.unmodifiableSet(nonnullFields);
}

public Set<String> getPrefabbedFields() {
return Collections.unmodifiableSet(prefabbedFields);
}

public CachedHashCodeInitializer<T> getCachedHashCodeInitializer() {
return cachedHashCodeInitializer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ public final class Context<T> {
private final Class<T> type;
private final Configuration<T> configuration;
private final ClassProbe<T> classProbe;
private final FieldCache fieldCache;

private final SubjectCreator<T> subjectCreator;
private final ValueProvider valueProvider;

@SuppressFBWarnings(
value = "EI_EXPOSE_REP2",
justification = "FieldCache is inherently mutable"
)
public Context(
Configuration<T> configuration,
FactoryCache factoryCache,
Expand All @@ -22,6 +28,7 @@ public Context(
this.type = configuration.getType();
this.configuration = configuration;
this.classProbe = new ClassProbe<>(configuration.getType());
this.fieldCache = fieldCache;

FactoryCache cache = JavaApiPrefabValues.build().merge(factoryCache);
this.valueProvider = new VintageValueProvider(cache);
Expand All @@ -40,6 +47,11 @@ public ClassProbe<T> getClassProbe() {
return classProbe;
}

@SuppressFBWarnings(value = "EI_EXPOSE_REP", justification = "A cache is inherently mutable")
public FieldCache getFieldCache() {
return fieldCache;
}

@SuppressFBWarnings(
value = "EI_EXPOSE_REP",
justification = "VintageValueProvider can use a mutable cache."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,15 @@ public static <T> void validateFieldTypeMatches(
) {
try {
Field f = container.getDeclaredField(fieldName);
boolean sameFields = f.getType().equals(fieldType);
boolean compatibleFields = fieldType.equals(
PrimitiveMappers.PRIMITIVE_OBJECT_MAPPER.get(f.getType())
);
validate(
f.getType().equals(fieldType),
!sameFields && !compatibleFields,
"Prefab values for field " +
fieldName +
" should be of type " +
" should be of type " +
f.getType().getSimpleName() +
" but are " +
fieldType.getSimpleName() +
Expand Down
Loading

0 comments on commit b567c6b

Please sign in to comment.