Skip to content

Commit

Permalink
Removes PrefabValueProvider (and adds functionality to Tuple in the p…
Browse files Browse the repository at this point in the history
…rocess)
  • Loading branch information
jqno committed Nov 15, 2024
1 parent 12bf102 commit 1d0838a
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import nl.jqno.equalsverifier.api.SingleTypeEqualsVerifierApi;
import nl.jqno.equalsverifier.internal.reflection.PackageScanner;
import nl.jqno.equalsverifier.internal.reflection.instantiation.GenericPrefabValueProvider.GenericFactories;
import nl.jqno.equalsverifier.internal.reflection.instantiation.PrefabValueProvider;
import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache;
import nl.jqno.equalsverifier.internal.util.ListBuilders;
import nl.jqno.equalsverifier.internal.util.PrefabValuesApi;
Expand All @@ -23,7 +22,6 @@ public final class ConfiguredEqualsVerifier implements EqualsVerifierApi<Void> {

private final EnumSet<Warning> warningsToSuppress;
private final FactoryCache factoryCache;
private final PrefabValueProvider prefabValueProvider;
private final GenericFactories genericFactories;
private boolean usingGetClass;
private Function<String, String> fieldnameToGetter;
Expand All @@ -34,7 +32,6 @@ public ConfiguredEqualsVerifier() {
this(
EnumSet.noneOf(Warning.class),
new FactoryCache(),
new PrefabValueProvider(),
new GenericFactories(),
false,
null
Expand All @@ -45,14 +42,12 @@ public ConfiguredEqualsVerifier() {
private ConfiguredEqualsVerifier(
EnumSet<Warning> warningsToSuppress,
FactoryCache factoryCache,
PrefabValueProvider prefabValueProvider,
GenericFactories genericFactories,
boolean usingGetClass,
Function<String, String> fieldnameToGetter
) {
this.warningsToSuppress = warningsToSuppress;
this.factoryCache = factoryCache;
this.prefabValueProvider = prefabValueProvider;
this.genericFactories = genericFactories;
this.usingGetClass = usingGetClass;
this.fieldnameToGetter = fieldnameToGetter;
Expand All @@ -67,7 +62,6 @@ public ConfiguredEqualsVerifier copy() {
return new ConfiguredEqualsVerifier(
EnumSet.copyOf(warningsToSuppress),
new FactoryCache().merge(factoryCache),
prefabValueProvider.copy(),
genericFactories.copy(),
usingGetClass,
fieldnameToGetter
Expand Down Expand Up @@ -145,7 +139,6 @@ public <T> SingleTypeEqualsVerifierApi<T> forClass(Class<T> type) {
type,
EnumSet.copyOf(warningsToSuppress),
new FactoryCache().merge(factoryCache),
prefabValueProvider.copy(),
genericFactories.copy(),
objenesis,
usingGetClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import nl.jqno.equalsverifier.internal.checkers.*;
import nl.jqno.equalsverifier.internal.exceptions.MessagingException;
import nl.jqno.equalsverifier.internal.reflection.instantiation.GenericPrefabValueProvider.GenericFactories;
import nl.jqno.equalsverifier.internal.reflection.instantiation.PrefabValueProvider;
import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache;
import nl.jqno.equalsverifier.internal.util.*;
import nl.jqno.equalsverifier.internal.util.Formatter;
Expand All @@ -33,7 +32,6 @@ public class SingleTypeEqualsVerifierApi<T> implements EqualsVerifierApi<T> {
private boolean hasRedefinedSuperclass = false;
private Class<? extends T> redefinedSubclass = null;
private FactoryCache factoryCache = new FactoryCache();
private PrefabValueProvider prefabValueProvider = new PrefabValueProvider();
private GenericFactories genericFactories = new GenericFactories();
private CachedHashCodeInitializer<T> cachedHashCodeInitializer =
CachedHashCodeInitializer.passthrough();
Expand Down Expand Up @@ -74,7 +72,6 @@ public SingleTypeEqualsVerifierApi(Class<T> type, Objenesis objenesis) {
* @param type The class for which the {@code equals} method should be tested.
* @param warningsToSuppress A list of warnings to suppress in {@code EqualsVerifier}.
* @param factoryCache Factories that can be used to create values.
* @param prefabValueProvider ValueProvider that records prefab values.
* @param genericFactories ValueProvider that records generic prefab values.
* @param objenesis To instantiate non-record classes.
* @param usingGetClass Whether {@code getClass} is used in the implementation of the {@code
Expand All @@ -85,12 +82,10 @@ public SingleTypeEqualsVerifierApi(Class<T> type, Objenesis objenesis) {
value = "EI_EXPOSE_REP2",
justification = "FactoryCache is inherently mutable"
)
// CHECKSTYLE OFF: ParameterNumber
public SingleTypeEqualsVerifierApi(
Class<T> type,
EnumSet<Warning> warningsToSuppress,
FactoryCache factoryCache,
PrefabValueProvider prefabValueProvider,
GenericFactories genericFactories,
Objenesis objenesis,
boolean usingGetClass,
Expand All @@ -99,14 +94,11 @@ public SingleTypeEqualsVerifierApi(
this(type, objenesis);
this.warningsToSuppress = EnumSet.copyOf(warningsToSuppress);
this.factoryCache = factoryCache;
this.prefabValueProvider = prefabValueProvider;
this.genericFactories = genericFactories;
this.usingGetClass = usingGetClass;
this.fieldnameToGetter = converter;
}

// CHECKSTYLE ON: ParameterNumber

/**
* Constructor, only to be called by {@link RelaxedEqualsVerifierApi#andUnequalExamples(Object,
* Object[])}.
Expand Down Expand Up @@ -456,13 +448,7 @@ private void performVerification() {
Validations.validateClassCanBeVerified(type);

Configuration<T> config = buildConfig();
Context<T> context = new Context<>(
config,
factoryCache,
prefabValueProvider,
genericFactories,
objenesis
);
Context<T> context = new Context<>(config, factoryCache, genericFactories, objenesis);
Validations.validateProcessedAnnotations(
type,
config.getAnnotationCache(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package nl.jqno.equalsverifier.internal.reflection;

import java.util.function.BiFunction;
import java.util.function.Function;

/**
* Container for three values of the same type: a "red" one, a "blue" one, and a shallow copy of the
* "red" one.
Expand Down Expand Up @@ -52,6 +55,18 @@ public T getRedCopy() {
return redCopy;
}

public <R> Tuple<R> map(Function<T, R> fn) {
return Tuple.of(fn.apply(red), fn.apply(blue), fn.apply(redCopy));
}

public static <T, U, R> Tuple<R> combine(Tuple<T> t, Tuple<U> u, BiFunction<T, U, R> fn) {
return Tuple.of(
fn.apply(t.getRed(), u.getRed()),
fn.apply(t.getBlue(), u.getBlue()),
fn.apply(t.getRedCopy(), u.getRedCopy())
);
}

/** {@inheritDoc} */
@Override
public boolean equals(Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,8 @@
public class ChainedValueProvider implements ValueProvider {

private boolean locked = false;
private final PrefabValueProvider prefabValueProvider;
private final List<ValueProvider> providers = new ArrayList<>();

/**
* Constructor.
*
* @param prefabValueProvider The prefabValueProvider is always the first in the chain,
* and it will act as a cache for values that were found by other ValueProviders.
*/
public ChainedValueProvider(PrefabValueProvider prefabValueProvider) {
this.prefabValueProvider = prefabValueProvider;
providers.add(prefabValueProvider);
}

/**
* Adds providers to the chain, so they can be delegated to when providing a value.
*
Expand All @@ -52,10 +40,6 @@ public <T> Optional<Tuple<T>> provide(TypeTag tag, String label) {
.map(vp -> vp.<T>provide(tag, label))
.filter(Optional::isPresent)
.findFirst()
.orElse(Optional.empty())
.map(tuple -> {
prefabValueProvider.register(tag.getType(), label, tuple);
return tuple;
});
.orElse(Optional.empty());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package nl.jqno.equalsverifier.internal.testhelpers;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;
import nl.jqno.equalsverifier.internal.reflection.Tuple;
import nl.jqno.equalsverifier.internal.reflection.TypeTag;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider;

@SuppressFBWarnings(
value = "DM_STRING_CTOR",
justification = "We really do need a separate instances with the same value"
)
public final class TestValueProviders {

public static final Tuple<Integer> INTS = Tuple.of(42, 1337, 42);
public static final Tuple<String> STRINGS = Tuple.of("abc", "xyz", new String("abc"));

private TestValueProviders() {}

public static ValueProvider empty() {
Expand All @@ -17,4 +25,23 @@ public <T> Optional<Tuple<T>> provide(TypeTag tag, String label) {
}
};
}

public static ValueProvider simple() {
return new ValueProvider() {
@SuppressWarnings("unchecked")
@Override
public <T> Optional<Tuple<T>> provide(TypeTag tag, String label) {
if (tag.getType().equals(int.class)) {
return Optional.of((Tuple<T>) INTS);
}
if (tag.getType().equals(Integer.class)) {
return Optional.of((Tuple<T>) INTS);
}
if (tag.getType().equals(String.class)) {
return Optional.of((Tuple<T>) STRINGS);
}
return Optional.empty();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import nl.jqno.equalsverifier.internal.reflection.ClassProbe;
import nl.jqno.equalsverifier.internal.reflection.JavaApiPrefabValues;
import nl.jqno.equalsverifier.internal.reflection.instantiation.GenericPrefabValueProvider.GenericFactories;
import nl.jqno.equalsverifier.internal.reflection.instantiation.PrefabValueProvider;
import nl.jqno.equalsverifier.internal.reflection.instantiation.SubjectCreator;
import nl.jqno.equalsverifier.internal.reflection.instantiation.ValueProvider;
import nl.jqno.equalsverifier.internal.reflection.vintage.FactoryCache;
Expand All @@ -26,7 +25,6 @@ public final class Context<T> {
public Context(
Configuration<T> configuration,
FactoryCache factoryCache,
PrefabValueProvider prefabValueProvider,
GenericFactories genericFactories,
Objenesis objenesis
) {
Expand All @@ -35,8 +33,7 @@ public Context(
this.classProbe = new ClassProbe<>(configuration.getType());

FactoryCache cache = JavaApiPrefabValues.build().merge(factoryCache);
this.valueProvider =
DefaultValueProviders.create(cache, prefabValueProvider, genericFactories, objenesis);
this.valueProvider = DefaultValueProviders.create(cache, genericFactories, objenesis);
this.subjectCreator = new SubjectCreator<>(configuration, valueProvider, objenesis);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ private DefaultValueProviders() {}

public static ValueProvider create(
FactoryCache factoryCache,
PrefabValueProvider prefabValueProvider,
GenericFactories genericFactories,
Objenesis objenesis
) {
ChainedValueProvider mainChain = new ChainedValueProvider(prefabValueProvider);
ChainedValueProvider vintageChain = new ChainedValueProvider(prefabValueProvider);
ChainedValueProvider mainChain = new ChainedValueProvider();
ChainedValueProvider vintageChain = new ChainedValueProvider();

ValueProvider vintage = new VintageValueProvider(vintageChain, factoryCache, objenesis);
ValueProvider genericPrefab = new GenericPrefabValueProvider(genericFactories, mainChain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,16 @@ public void redAndRedCopyInvariant() {
assertEquals(tuple.getRed(), tuple.getRedCopy());
assertNotSame(tuple.getRed(), tuple.getRedCopy());
}

@Test
public void map() {
assertEquals(Tuple.of("redx", "bluex", "redx"), tuple.map(s -> s + "x"));
}

@Test
public void combine() {
Tuple<Integer> ints = Tuple.of(2, 3, 2);
Tuple<String> actual = Tuple.combine(tuple, ints, (s, n) -> s + n);
assertEquals(Tuple.of("red2", "blue3", "red2"), actual);
}
}
Loading

0 comments on commit 1d0838a

Please sign in to comment.