Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix InstantiationError when using any() where a sealed type is expected #939

Merged
merged 3 commits into from Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions modules/mockk/src/commonTest/kotlin/io/mockk/it/SealedClassTest.kt
Expand Up @@ -2,6 +2,8 @@ package io.mockk.it

import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.condition.DisabledForJreRange
import org.junit.jupiter.api.condition.JRE
import kotlin.test.Test
import kotlin.test.assertEquals

Expand Down Expand Up @@ -30,6 +32,18 @@ class SealedClassTest {
assertEquals(Leaf(1), result)
}

@Test
fun serviceTakesSealedClassAsInput() {
val formattedNode = "Formatted node"
val factory = mockk<Factory> {
every { format(any()) } answers { formattedNode }
}

val result = factory.format(Root(0))

assertEquals(formattedNode, result)
}

companion object {

sealed class Node
Expand All @@ -39,10 +53,14 @@ class SealedClassTest {

interface Factory {
fun create(): Node

fun format(node: Node): String
}

class FactoryImpl : Factory {
override fun create(): Node = Root(0)

override fun format(node: Node): String = node.toString()
}

}
Expand Down
Expand Up @@ -2,6 +2,8 @@ package io.mockk.it

import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.condition.DisabledForJreRange
import org.junit.jupiter.api.condition.JRE
import kotlin.test.Test
import kotlin.test.assertEquals

Expand Down Expand Up @@ -30,6 +32,18 @@ class SealedInterfaceTest {
assertEquals(Leaf(1), result)
}

@Test
fun serviceTakesSealedInterfaceAsInput() {
val formattedNode = "Formatted node"
val factory = mockk<Factory> {
every { format(any()) } answers { formattedNode }
}

val result = factory.format(Root(0))

assertEquals(formattedNode, result)
}

companion object {

sealed interface Node
Expand All @@ -39,10 +53,14 @@ class SealedInterfaceTest {

interface Factory {
fun create(): Node

fun format(node: Node): String
}

class FactoryImpl : Factory {
override fun create(): Node = Root(0)

override fun format(node: Node): String = node.toString()
}

}
Expand Down
Expand Up @@ -24,24 +24,34 @@ class JvmSignatureValueGenerator(val rnd: Random) : SignatureValueGenerator {
return constructor.call(valueSig)
}

return cls.cast(
when (cls) {
java.lang.Boolean::class -> rnd.nextBoolean()
java.lang.Byte::class -> rnd.nextInt().toByte()
java.lang.Short::class -> rnd.nextInt().toShort()
java.lang.Character::class -> rnd.nextInt().toChar()
java.lang.Integer::class -> rnd.nextInt()
java.lang.Long::class -> rnd.nextLong()
java.lang.Float::class -> rnd.nextFloat()
java.lang.Double::class -> rnd.nextDouble()
java.lang.String::class -> rnd.nextLong().toString(16)
return cls.cast(instantiate(cls, anyValueGeneratorProvider, instantiator))
}

private fun <T : Any> instantiate(
cls: KClass<T>,
anyValueGeneratorProvider: () -> AnyValueGenerator,
instantiator: AbstractInstantiator
): Any = when (cls) {
Boolean::class -> rnd.nextBoolean()
Byte::class -> rnd.nextInt().toByte()
Short::class -> rnd.nextInt().toShort()
Character::class -> rnd.nextInt().toChar()
Integer::class -> rnd.nextInt()
Long::class -> rnd.nextLong()
Float::class -> rnd.nextFloat()
Double::class -> rnd.nextDouble()
String::class -> rnd.nextLong().toString(16)

else ->
@Suppress("UNCHECKED_CAST")
anyValueGeneratorProvider().anyValue(cls, isNullable = false) {
instantiator.instantiate(cls)
} as T
else ->
if (cls.isSealed) {
cls.sealedSubclasses.firstNotNullOfOrNull {
instantiate(it, anyValueGeneratorProvider, instantiator)
} ?: error("Unable to create proxy for sealed class $cls, available subclasses: ${cls.sealedSubclasses}")
} else {
@Suppress("UNCHECKED_CAST")
anyValueGeneratorProvider().anyValue(cls, isNullable = false) {
instantiator.instantiate(cls)
} as T
}
)
}
}