Skip to content

Commit

Permalink
Adds two more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 4, 2024
1 parent e2f3dbd commit c0d03df
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package nl.jqno.equalsverifier.internal.reflection.vintage;

import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;

import java.util.LinkedHashSet;
import nl.jqno.equalsverifier.internal.reflection.FactoryCache;
import nl.jqno.equalsverifier.internal.reflection.Tuple;
import nl.jqno.equalsverifier.internal.reflection.TypeTag;
import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider;
import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.FallbackFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;

public class RecordFallbackFactoryTest {

private FallbackFactory<?> factory;
private VintageValueProvider valueProvider;
private LinkedHashSet<TypeTag> typeStack;

@BeforeEach
public void setUp() {
Objenesis objenesis = new ObjenesisStd();
factory = new FallbackFactory<>(objenesis);
FactoryCache factoryCache = new FactoryCache();
factoryCache.put(int.class, values(42, 1337, 42));
valueProvider = new VintageValueProvider(factoryCache, objenesis);
typeStack = new LinkedHashSet<>();
}

@Test
public void redCopyHasTheSameValuesAsRed_whenSutContainsGenericValueThatNeedsToBeIdenticalInRedAndRedCopy() {
Tuple<?> tuple = factory.createValues(
new TypeTag(GenericRecordContainer.class),
valueProvider,
typeStack
);

assertEquals(tuple.getRed(), tuple.getRedCopy());
assertNotSame(tuple.getRed(), tuple.getRedCopy());
}

record GenericRecord<T>(T t) {}

record GenericRecordContainer(GenericRecord<?> bgr) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package nl.jqno.equalsverifier.internal.reflection.vintage;

import static nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.Factories.values;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;

import java.util.LinkedHashSet;
import java.util.Objects;
import nl.jqno.equalsverifier.internal.reflection.FactoryCache;
import nl.jqno.equalsverifier.internal.reflection.Tuple;
import nl.jqno.equalsverifier.internal.reflection.TypeTag;
import nl.jqno.equalsverifier.internal.reflection.instantiation.VintageValueProvider;
import nl.jqno.equalsverifier.internal.reflection.vintage.prefabvalues.factories.FallbackFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;

public class SealedTypesFallbackFactoryTest {

private FallbackFactory<?> factory;
private VintageValueProvider valueProvider;
private LinkedHashSet<TypeTag> typeStack;

@BeforeEach
public void setUp() {
Objenesis objenesis = new ObjenesisStd();
factory = new FallbackFactory<>(objenesis);
FactoryCache factoryCache = new FactoryCache();
factoryCache.put(int.class, values(42, 1337, 42));
valueProvider = new VintageValueProvider(factoryCache, objenesis);
typeStack = new LinkedHashSet<>();
}

@Test
public void redCopyHasTheSameValuesAsRed_whenSutIsAbstractSealedAndPermittedTypeAddsField() {
Tuple<?> tuple = factory.createValues(
new TypeTag(SealedParentWithFinalChild.class),
valueProvider,
typeStack
);

assertEquals(tuple.getRed(), tuple.getRedCopy());
assertNotSame(tuple.getRed(), tuple.getRedCopy());
}

public abstract static sealed class SealedParentWithFinalChild permits FinalSealedChild {

private final int i;

public SealedParentWithFinalChild(int i) {
this.i = i;
}

@Override
public boolean equals(Object obj) {
return obj instanceof SealedParentWithFinalChild other && i == other.i;
}

@Override
public int hashCode() {
return Objects.hash(i);
}
}

public static final class FinalSealedChild extends SealedParentWithFinalChild {

private final int j;

public FinalSealedChild(int i, int j) {
super(i);
this.j = j;
}

@Override
public boolean equals(Object obj) {
return obj instanceof FinalSealedChild other && super.equals(obj) && j == other.j;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), j);
}
}
}

0 comments on commit c0d03df

Please sign in to comment.