Skip to content

Commit

Permalink
Merge pull request #1176 from OitoH/issue#977
Browse files Browse the repository at this point in the history
Fixes infinite recursion when stubbing a fun that returns value class
  • Loading branch information
Raibaz committed Dec 17, 2023
2 parents fa0fb5a + 205514e commit f879502
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ internal class InliningClassTransformer(
isMethod<MethodDescription>()
.and(not<OfByteCodeElement>(isStatic<OfByteCodeElement>()))
.and(not<MethodDescription>(isDefaultFinalizer<MethodDescription>()))
.and(not<MethodDescription>(this::matchValueClassGetValueMethod))
)

@Suppress("RemoveExplicitTypeArguments")
Expand All @@ -125,6 +126,15 @@ internal class InliningClassTransformer(
private fun matchRestrictedMethods(desc: MethodDescription) =
desc.declaringType.typeName + "." + desc.name in restrictedMethods

private fun matchValueClassGetValueMethod(desc: MethodDescription): Boolean {
return isFinal<MethodDescription>()
.and(isDeclaredBy(isAnnotatedWith(JvmInline::class.java)))
.and(named("getValue"))
.and(isPublic())
.and(takesNoArguments())
.matches(desc)
}

@Suppress("RemoveExplicitTypeArguments")
private fun constructorAdvice(): AsmVisitorWrapper.ForDeclaredMethods {
return Advice.withCustomMapping()
Expand Down
13 changes: 13 additions & 0 deletions modules/mockk/src/commonTest/kotlin/io/mockk/it/ValueClassTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.mockk.it

import io.mockk.*
import org.junit.jupiter.api.assertTimeoutPreemptively
import java.time.Duration
import kotlin.jvm.JvmInline
import kotlin.test.Ignore
import kotlin.test.Test
Expand Down Expand Up @@ -490,6 +492,17 @@ class ValueClassTest {
assertEquals(givenResult, result)
}

@Test
fun `ensure no infinite recursion when mocking fun that returns value class`() {
val f: () -> DummyValue = mockk()

assertTimeoutPreemptively(Duration.ofMillis(500L)) {
runCatching {
every { f.invoke() } returns DummyValue(42)
}
}
}

companion object {

@JvmInline
Expand Down

0 comments on commit f879502

Please sign in to comment.