-
-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1005 from ghackett/feature/reflection-any-matcher
Add `fun <T> MockKMatcherScope.any(KClass<T>): T`
- Loading branch information
Showing
4 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
modules/mockk/src/commonTest/kotlin/io/mockk/ReflectiveMockTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
@file:Suppress("UNCHECKED_CAST") | ||
|
||
package io.mockk | ||
|
||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.callSuspend | ||
import kotlin.reflect.full.memberFunctions | ||
import kotlin.test.Test | ||
|
||
|
||
class ReflectiveMockTests { | ||
private inline fun <reified T : Any> mockkBuilder(): T { | ||
val builder: T = mockk() | ||
builder.javaClass.kotlin.memberFunctions | ||
.filter { it.returnType.classifier == T::class } | ||
.filter { !it.isSuspend } | ||
.forEach { func -> | ||
every { | ||
val params: List<Any?> = | ||
listOf<Any?>(builder) + func.parameters.drop(1).map { any(it.type.classifier as KClass<Any>) } | ||
func.call(*params.toTypedArray()) | ||
} answers { builder } | ||
} | ||
return builder | ||
} | ||
|
||
interface TestBuilder { | ||
fun f1(p1: String): TestBuilder | ||
fun f2(p1: String, p2: Int): TestBuilder | ||
fun f3(p1: String, p2: Int, p3: Long?): TestBuilder | ||
} | ||
|
||
@Test | ||
fun testAQuickMockBuilder() { | ||
val builder: TestBuilder = mockkBuilder() | ||
|
||
builder.f1("f1") | ||
.f2("f2", 2) | ||
.f3("f3", 3, 3) | ||
|
||
verify { | ||
builder.f1("f1") | ||
builder.f2("f2", 2) | ||
builder.f3("f3", 3, 3) | ||
} | ||
} | ||
|
||
|
||
private inline fun <reified T : Any> mockkWithDefault(noinline defaultAnswer: MockKAnswerScope<Any?, Any?>.(Call) -> Any?): T { | ||
val mock: T = mockk() | ||
mock.javaClass.kotlin.memberFunctions | ||
.filter { !it.isSuspend } | ||
.forEach { func -> | ||
every { | ||
val params: List<Any?> = | ||
listOf<Any?>(mock) + func.parameters.drop(1).map { any(it.type.classifier as KClass<Any>) } | ||
func.call(*params.toTypedArray()) | ||
} answers { | ||
this.defaultAnswer(it) | ||
} | ||
} | ||
|
||
mock.javaClass.kotlin.memberFunctions | ||
.filter { it.isSuspend } | ||
.forEach { func -> | ||
coEvery { | ||
val params: List<Any?> = | ||
listOf<Any?>(mock) + func.parameters.drop(1).map { any(it.type.classifier as KClass<Any>) } | ||
func.callSuspend(*params.toTypedArray()) | ||
} answers { | ||
this.defaultAnswer(it) | ||
} | ||
} | ||
return mock | ||
} | ||
|
||
@Test fun testMockkWithDefault() { | ||
val builder: TestBuilder = mockkWithDefault { | ||
when (it.retType) { | ||
TestBuilder::class -> it.invocation.self | ||
Unit::class -> Unit | ||
else -> null | ||
} | ||
} | ||
|
||
builder.f1("f1") | ||
.f2("f2", 2) | ||
.f3("f3", 3, 3) | ||
|
||
verify { | ||
builder.f1("f1") | ||
builder.f2("f2", 2) | ||
builder.f3("f3", 3, 3) | ||
} | ||
} | ||
} |