From d6171be5106f3d43978ed304b350657f6090d0a1 Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Tue, 26 Nov 2024 21:18:57 +0100 Subject: [PATCH] Adds tests for CachedValueProvider --- .../CachedValueProviderTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProviderTest.java diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProviderTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProviderTest.java new file mode 100644 index 000000000..c3b4f7a3c --- /dev/null +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/reflection/instantiation/CachedValueProviderTest.java @@ -0,0 +1,52 @@ +package nl.jqno.equalsverifier.internal.reflection.instantiation; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +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.Attributes; +import org.junit.jupiter.api.Test; + +public class CachedValueProviderTest { + + private static final TypeTag INT = new TypeTag(int.class); + + private CachedValueProvider sut = new CachedValueProvider(); + + @Test + public void aRegisteredValueCanBeFound() { + sut.put(INT, null, Tuple.of(3, 2, 3)); + assertEquals(Tuple.of(3, 2, 3), sut.provide(INT, Attributes.unlabeled()).get()); + } + + @Test + public void aValueRegisteredWithALabelCanBeFoundUnderThatLabel() { + sut.put(INT, "label", Tuple.of(3, 2, 3)); + assertEquals(Tuple.of(3, 2, 3), sut.provide(INT, Attributes.labeled("label")).get()); + } + + @Test + public void aValueRegisteredWithALabelCanNotBeFoundWithoutThatLabel() { + sut.put(INT, "label", Tuple.of(3, 2, 3)); + assertEquals(Optional.empty(), sut.provide(INT, Attributes.unlabeled())); + } + + @Test + public void aQueryWithLabelFallsBackToRegisteredValueWithoutLabel() { + sut.put(INT, null, Tuple.of(3, 2, 3)); + assertEquals(Tuple.of(3, 2, 3), sut.provide(INT, Attributes.labeled("label")).get()); + } + + @Test + public void aQueryWithLabelPrefersRegisteredValueWithThatLabel() { + sut.put(INT, null, Tuple.of(3, 2, 3)); + sut.put(INT, "label", Tuple.of(4, 3, 4)); + assertEquals(Tuple.of(4, 3, 4), sut.provide(INT, Attributes.labeled("label")).get()); + } + + @Test + public void anUnregisteredValueCanNotBeFound() { + assertEquals(Optional.empty(), sut.provide(INT, Attributes.unlabeled())); + } +}