diff --git a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/resolver/FactoryResolver.kt b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/resolver/FactoryResolver.kt index 807b03e8..2536f43f 100644 --- a/fixture/src/main/kotlin/com/appmattus/kotlinfixture/resolver/FactoryResolver.kt +++ b/fixture/src/main/kotlin/com/appmattus/kotlinfixture/resolver/FactoryResolver.kt @@ -19,16 +19,27 @@ package com.appmattus.kotlinfixture.resolver import com.appmattus.kotlinfixture.Context import com.appmattus.kotlinfixture.Unresolved import com.appmattus.kotlinfixture.config.DefaultGenerator +import com.appmattus.kotlinfixture.decorator.nullability.wrapNullability import kotlin.reflect.KType +import kotlin.reflect.full.withNullability internal class FactoryResolver : Resolver { + @Suppress("ReturnCount") override fun resolve(context: Context, obj: Any): Any? { if (obj is KType) { context.configuration.factories[obj]?.let { return with(DefaultGenerator(context)) { it() } } + + if (obj.isMarkedNullable) { + context.configuration.factories[obj.withNullability(false)]?.let { + return context.wrapNullability(obj) { + with(DefaultGenerator(context)) { it() } + } + } + } } return Unresolved diff --git a/fixture/src/test/kotlin/com/appmattus/kotlinfixture/resolver/FactoryResolverTest.kt b/fixture/src/test/kotlin/com/appmattus/kotlinfixture/resolver/FactoryResolverTest.kt index 2a89219d..3a015296 100644 --- a/fixture/src/test/kotlin/com/appmattus/kotlinfixture/resolver/FactoryResolverTest.kt +++ b/fixture/src/test/kotlin/com/appmattus/kotlinfixture/resolver/FactoryResolverTest.kt @@ -18,6 +18,7 @@ package com.appmattus.kotlinfixture.resolver import com.appmattus.kotlinfixture.TestContext import com.appmattus.kotlinfixture.Unresolved +import com.appmattus.kotlinfixture.assertIsRandom import com.appmattus.kotlinfixture.config.Configuration import com.appmattus.kotlinfixture.config.ConfigurationBuilder import com.appmattus.kotlinfixture.typeOf @@ -36,7 +37,7 @@ class FactoryResolverTest { } @Test - fun `Factory returned when mapping found`() { + fun `Factory returned when non-null mapping found for non-null`() { val configuration = ConfigurationBuilder().apply { factory { 12 } }.build() @@ -56,4 +57,36 @@ class FactoryResolverTest { assertTrue(context.resolve(typeOf()) in 1..5) } } + + @Test + fun `Unresolved returned when non-nullable requested and only nullable factory found`() { + val configuration = ConfigurationBuilder().apply { + factory { 12 } + }.build() + val context = TestContext(configuration, FactoryResolver()) + + assertEquals(Unresolved, context.resolve(typeOf())) + } + + @Test + fun `Factory returned when nullable mapping found for nullable`() { + val configuration = ConfigurationBuilder().apply { + factory { 12 } + }.build() + val context = TestContext(configuration, FactoryResolver()) + + assertEquals(12, context.resolve(typeOf())) + } + + @Test + fun `Random nullability returned when non-null mapping found for nullable`() { + val configuration = ConfigurationBuilder().apply { + factory { 12 } + }.build() + val context = TestContext(configuration, FactoryResolver()) + + assertIsRandom { + context.resolve(typeOf()) == null + } + } }