Skip to content

Commit

Permalink
Add fix in
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaohuaZeng-at committed Mar 4, 2024
1 parent 1025c9b commit 25a63ba
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
Expand Up @@ -2,14 +2,15 @@ package io.mockk.core

import java.lang.reflect.Method
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.KProperty1
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.javaField
import kotlin.reflect.jvm.internal.KotlinReflectionInternalError
import kotlin.reflect.jvm.javaGetter
import kotlin.reflect.jvm.kotlinFunction


actual object ValueClassSupport {

/**
Expand All @@ -23,20 +24,38 @@ actual object ValueClassSupport {
if (!resultType.isValue_safe) {
return this
}
val kFunction = method.kotlinFunction ?: return this

// Only unbox a value class if the method's return type is actually the type of the inlined property.
// For example, in a normal case where a value class `Foo` with underlying `Int` property is inlined:
// method.returnType == int (the actual representation of inlined property on JVM)
// method.kotlinFunction.returnType.classifier == Foo
val expectedReturnType = kFunction.returnType.classifier
return if (resultType == expectedReturnType) {
this.boxedValue
val kFunction = method.kotlinFunction
if (kFunction != null) {
// Only unbox a value class if the method's return type is actually the type of the inlined property.
// For example, in a normal case where a value class `Foo` with underlying `Int` property is inlined:
// method.returnType == int (the actual representation of inlined property on JVM)
// method.kotlinFunction.returnType.classifier == Foo
val expectedReturnType = kFunction.returnType.classifier
return if (resultType == expectedReturnType) {
this.boxedValue
} else {
this
}
}
// It is possible that the method is a getter for a property, in which case we can check the property's return
// type in kotlin.
val kProperty = findMatchingPropertyWithJavaGetter(method)
if (kProperty == null) {
return this
} else {
this
val expectedReturnType = kProperty.returnType.classifier
return if (resultType == expectedReturnType) {
this.boxedValue
} else {
this
}
}
}

private fun findMatchingPropertyWithJavaGetter(method: Method): KProperty<*>? {
return method.declaringClass.kotlin.declaredMemberProperties.find { it.javaGetter == method }
}

/**
* Underlying property value of a **`value class`** or self.
*
Expand Down Expand Up @@ -92,8 +111,7 @@ actual object ValueClassSupport {
false
} catch (_: UnsupportedOperationException) {
false
} catch (_: AbstractMethodError) {
} catch (e: AbstractMethodError) {
false
}

}
39 changes: 33 additions & 6 deletions modules/mockk/src/commonTest/kotlin/io/mockk/it/ValueClassTest.kt
@@ -1,10 +1,13 @@
package io.mockk.it

import io.mockk.*
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import io.mockk.spyk
import io.mockk.verify
import org.junit.jupiter.api.assertTimeoutPreemptively
import java.time.Duration
import java.util.UUID
import kotlin.jvm.JvmInline
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -31,6 +34,17 @@ class ValueClassTest {
verify { mock.argValueClassReturnValueClass(dummyValueClassArg) }
}

@Test
fun `field is ValueClass, returns ValueClass`() {
val mock = mockk<DummyService> {
every { valueClassField } returns dummyValueClassReturn
}

assertEquals(dummyValueClassReturn, mock.valueClassField)

verify { mock.valueClassField }
}

@Test
fun `arg is any(ValueClass), returns ValueClass`() {
val mock = mockk<DummyService> {
Expand Down Expand Up @@ -121,7 +135,10 @@ class ValueClassTest {
every { argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg) } returns dummyComplexValueClassReturn
}

assertEquals(dummyComplexValueClassReturn, mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg))
assertEquals(
dummyComplexValueClassReturn,
mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg)
)

verify { mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg) }
}
Expand All @@ -132,7 +149,10 @@ class ValueClassTest {
every { argComplexValueClassReturnComplexValueClass(any()) } returns dummyComplexValueClassReturn
}

assertEquals(dummyComplexValueClassReturn, mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg))
assertEquals(
dummyComplexValueClassReturn,
mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg)
)

verify { mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg) }
}
Expand All @@ -159,7 +179,10 @@ class ValueClassTest {
every { argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg) } answers { dummyComplexValueClassReturn }
}

assertEquals(dummyComplexValueClassReturn, mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg))
assertEquals(
dummyComplexValueClassReturn,
mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg)
)

verify { mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg) }
}
Expand All @@ -170,7 +193,10 @@ class ValueClassTest {
every { argComplexValueClassReturnComplexValueClass(any()) } answers { dummyComplexValueClassReturn }
}

assertEquals(dummyComplexValueClassReturn, mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg))
assertEquals(
dummyComplexValueClassReturn,
mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg)
)

verify { mock.argComplexValueClassReturnComplexValueClass(dummyComplexValueClassArg) }
}
Expand Down Expand Up @@ -625,6 +651,7 @@ class ValueClassTest {

@Suppress("UNUSED_PARAMETER")
class DummyService {
val valueClassField = DummyValue(0)

fun argWrapperReturnWrapper(wrapper: DummyValueWrapper): DummyValueWrapper =
DummyValueWrapper(DummyValue(0))
Expand Down

0 comments on commit 25a63ba

Please sign in to comment.