Skip to content

Commit

Permalink
Fixing issue #171
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksiyp committed Nov 10, 2018
1 parent c5bbf0d commit f57a3eb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
15 changes: 9 additions & 6 deletions dsl/common/src/main/kotlin/io/mockk/Answers.kt
Expand Up @@ -27,12 +27,15 @@ data class FunctionAnswer<T>(val answerFunc: (Call) -> T) : Answer<T> {
data class CoFunctionAnswer<T>(val answerFunc: suspend (Call) -> T) : Answer<T> {

override fun answer(call: Call): T {
val continuation = call.invocation.args.lastOrNull() as? Continuation<*>
?: throw MockKException("last parameter is not Continuation<*> for suspend call")

return InternalPlatformDsl.coroutineCall {
answerFunc(call)
}.callWithContinuation(continuation)
val lastParam = call.invocation.args.lastOrNull()
return if (lastParam is Continuation<*>)
InternalPlatformDsl.coroutineCall {
answerFunc(call)
}.callWithContinuation(lastParam)
else
InternalPlatformDsl.runCoroutine {
answerFunc(call)
}
}

override suspend fun coAnswer(call: Call) = answerFunc(call)
Expand Down
1 change: 0 additions & 1 deletion dsl/common/src/main/kotlin/io/mockk/InternalPlatformDsl.kt
@@ -1,7 +1,6 @@
package io.mockk

import kotlin.coroutines.experimental.Continuation
import kotlin.reflect.KCallable

expect object InternalPlatformDsl {
fun identityHashCode(obj: Any): Int
Expand Down
Expand Up @@ -6,7 +6,6 @@ import io.mockk.InternalPlatformDsl.toStr
import io.mockk.impl.InternalPlatform
import io.mockk.impl.log.Logger
import io.mockk.impl.log.SafeToString
import kotlin.reflect.KClass

class ChainedCallDetector(safeToString: SafeToString) {
val log = safeToString(Logger<SignatureMatcherDetector>())
Expand Down
Expand Up @@ -30,6 +30,9 @@ class ChainedCallDetectorTest {
every { call1.method.varArgsArg } returns -1
every { call2.method.varArgsArg } returns -1

every { call1.method.isSuspend } returns { false }
every { call2.method.isSuspend } returns { false }

detector.detect(listOf(callRound1, callRound2), 0, hashMapOf())

assertEquals("abc", detector.call.matcher.method.name)
Expand All @@ -56,6 +59,9 @@ class ChainedCallDetectorTest {
every { call1.method.varArgsArg } returns -1
every { call2.method.varArgsArg } returns -1

every { call1.method.isSuspend } returns { false }
every { call2.method.isSuspend } returns { false }

detector.detect(listOf(callRound1, callRound2), 0, matcherMap)

assertEquals("abc", detector.call.matcher.method.name)
Expand Down
Expand Up @@ -87,17 +87,17 @@ object JvmMockFactoryHelper {

private fun Method.toDescription(): MethodDescription {

val isSuspend: () -> Boolean = if (
parameterTypes.lastOrNull()?.let { Continuation::class.java.isAssignableFrom(it) } == true
) {
{
kotlinFunction?.isSuspend ?: false
}
val lastParam = parameterTypes.lastOrNull()
val isLastParamContinuation = lastParam?.let {
Continuation::class.java.isAssignableFrom(it)
} ?: false

val isSuspend: () -> Boolean = if (isLastParamContinuation) {
{ kotlinFunction?.isSuspend ?: false }
} else {
{ false }
}


return MethodDescription(
name,
returnType.kotlin,
Expand Down

0 comments on commit f57a3eb

Please sign in to comment.