Skip to content

Commit

Permalink
- related to #288
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksiyp committed Apr 19, 2020
1 parent e79f1fb commit 4ded898
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
6 changes: 3 additions & 3 deletions agent/android/build.gradle
Expand Up @@ -80,9 +80,9 @@ android {
dependencies {
api project(':mockk-agent-api')
api project(':mockk-agent-common')
api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
api "com.linkedin.dexmaker:dexmaker:$dexmaker_version"
api "org.objenesis:objenesis:$objenesis_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.linkedin.dexmaker:dexmaker:$dexmaker_version"
implementation "org.objenesis:objenesis:$objenesis_version"
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
Expand Down
5 changes: 4 additions & 1 deletion dsl/common/src/main/kotlin/io/mockk/API.kt
Expand Up @@ -683,7 +683,9 @@ open class MockKMatcherScope(
inline fun <reified T : Any> any(): T = match(ConstantMatcher(true))
inline fun <reified T : Any> capture(lst: MutableList<T>): T = match(CaptureMatcher(lst, T::class))
inline fun <reified T : Any> capture(lst: CapturingSlot<T>): T = match(CapturingSlotMatcher(lst, T::class))
inline fun <reified T : Any> captureNullable(lst: MutableList<T?>): T? = match(CaptureNullableMatcher(lst, T::class))
inline fun <reified T : Any> captureNullable(lst: MutableList<T?>): T? =
match(CaptureNullableMatcher(lst, T::class))

inline fun <reified T : Comparable<T>> cmpEq(value: T): T = match(ComparingMatcher(value, 0, T::class))
inline fun <reified T : Comparable<T>> more(value: T, andEquals: Boolean = false): T =
match(ComparingMatcher(value, if (andEquals) 2 else 1, T::class))
Expand Down Expand Up @@ -3631,6 +3633,7 @@ data class MethodDescription(
val returnsUnit: Boolean,
val returnsNothing: Boolean,
val isSuspend: Boolean,
val isFnCall: Boolean,
val declaringClass: KClass<*>,
val paramTypes: List<KClass<*>>,
val varArgsArg: Int,
Expand Down
Expand Up @@ -6,6 +6,7 @@ import io.mockk.InternalPlatformDsl.toStr
import io.mockk.impl.InternalPlatform
import io.mockk.impl.log.Logger
import io.mockk.impl.log.SafeToString
import kotlin.coroutines.Continuation

class ChainedCallDetector(safeToString: SafeToString) {
val log = safeToString(Logger<SignatureMatcherDetector>())
Expand Down Expand Up @@ -119,7 +120,15 @@ class ChainedCallDetector(safeToString: SafeToString) {

@Suppress("UNCHECKED_CAST")
fun buildRecordedCall(): RecordedCall {
if (zeroCall.method.isSuspend) {
fun SignedCall.isSuspend() = when {
method.isSuspend -> true
method.isFnCall -> args.lastOrNull()?.let {
Continuation::class.isInstance(it)
} ?: false
else -> false
}

if (zeroCall.isSuspend()) {
log.trace { "Suspend function found. Replacing continuation with any() matcher" }
argMatchers[argMatchers.size - 1] = ConstantMatcher<Any>(true)
}
Expand Down
Expand Up @@ -9,6 +9,7 @@ object WasNotCalled {
true,
false,
false,
false,
Unit::class,
listOf(),
-1,
Expand Down
2 changes: 2 additions & 0 deletions mockk/common/src/test/kotlin/io/mockk/it/AnswersTest.kt
@@ -1,3 +1,5 @@
@file:Suppress("UNUSED_PARAMETER")

package io.mockk.it

import io.mockk.*
Expand Down
Expand Up @@ -131,6 +131,8 @@ object JvmMockFactoryHelper {
} ?: false
}

val isFnCall = Function::class.java.isAssignableFrom(declaringClass)

val returnType = kotlinFunc?.returnType as? KClass<*> ?: returnType.kotlin

val result = MethodDescription(
Expand All @@ -139,6 +141,7 @@ object JvmMockFactoryHelper {
returnTypeIsUnit,
returnTypeIsNothing,
isSuspend,
isFnCall,
declaringClass.kotlin,
parameterTypes.map { it.kotlin },
vararg,
Expand Down
17 changes: 17 additions & 0 deletions mockk/jvm/src/test/kotlin/io/mockk/gh/Issue288Test.kt
@@ -0,0 +1,17 @@
package io.mockk.gh

import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
import kotlin.test.assertEquals

class Issue288Test {
@Test
fun suspendFnMocking(): Unit {
val call = mockk<suspend () -> Int>()
coEvery { call() } returns 5
runBlocking { assertEquals(5, call()) }
}
}

0 comments on commit 4ded898

Please sign in to comment.