Skip to content

Commit 4d1b211

Browse files
m-koopsmkoops-simacanJoshua Selbo
authored
Improve Result support in matchers and captors (#582)
Co-authored-by: Mark Koops <mark.koops@simacan.com> Co-authored-by: Joshua Selbo <jselbo@meta.com>
1 parent d0492b0 commit 4d1b211

File tree

5 files changed

+171
-73
lines changed

5 files changed

+171
-73
lines changed

mockito-kotlin/src/main/kotlin/org/mockito/kotlin/internal/CoroutineAwareAnswer.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ package org.mockito.kotlin.internal
2828
import kotlin.coroutines.Continuation
2929
import kotlin.coroutines.intrinsics.startCoroutineUninterceptedOrReturn
3030
import kotlin.reflect.KFunction
31-
import kotlin.reflect.full.memberProperties
3231
import kotlin.reflect.jvm.jvmErasure
3332
import kotlin.reflect.jvm.kotlinFunction
3433
import org.mockito.internal.invocation.InterceptedInvocation
@@ -88,15 +87,13 @@ internal class CoroutineAwareAnswer<T> private constructor(private val delegate:
8887
return this
8988
}
9089

91-
private fun Result<*>.unboxResult(): Any? {
92-
if (isSuccess) return getOrNull()
93-
94-
// In case of failure, extract the nested Failure instance and pass that on
95-
val valueProperty = this::class.memberProperties.single { it.name == "value" }
96-
val failure /* : kotlin.Result.Failure */ = valueProperty.call(this)
97-
98-
return failure
99-
}
90+
private fun Result<*>.unboxResult(): Any? =
91+
if (isSuccess) {
92+
getOrNull()
93+
} else {
94+
// In case of failure, unbox the nested Failure instance and pass that on
95+
this.unboxValueClass()
96+
}
10097

10198
private val InvocationOnMock.invokedKotlinFunction: KFunction<*>?
10299
get() =

mockito-kotlin/src/main/kotlin/org/mockito/kotlin/internal/ValueClassSupport.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ import kotlin.reflect.KClass
3232
fun <T : Any?> Any?.toKotlinType(clazz: KClass<*>): T {
3333
if (this == null) return null as T
3434

35-
if (clazz == Result::class) {
36-
return this as T
37-
}
38-
3935
if (clazz.isValue && this::class != clazz) {
4036
return this.boxAsValueClass(clazz) as T
4137
}

0 commit comments

Comments
 (0)