/
MockK.kt
38 lines (32 loc) · 1.23 KB
/
MockK.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@file:JvmName("JVMMockKKt")
package io.mockk
import io.mockk.impl.JvmMockKGateway
import kotlin.reflect.KFunction
import kotlin.reflect.jvm.javaMethod
actual object MockK {
actual inline fun <T> useImpl(block: () -> T): T {
MockKGateway.implementation = JvmMockKGateway.defaultImplementationBuilder
return block()
}
}
/**
* Returns the defining top-level extension function's [Class].
*/
inline val KFunction<*>.declaringKotlinFile
get() = checkNotNull(javaMethod) { "$this is not a top-level extension function" }
.declaringClass.kotlin
/**
* Builds a static mock. Any mocks of this function's declaring class are cancelled before it's mocked
*/
inline fun mockkStatic(vararg functions: KFunction<*>) =
mockkStatic(*functions.map { it.declaringKotlinFile }.toTypedArray())
/**
* Cancel static mocks.
*/
inline fun unmockkStatic(vararg functions: KFunction<*>) =
unmockkStatic(*functions.map { it.declaringKotlinFile }.toTypedArray())
/**
* Builds a static mock and unmocks it after the block has been executed.
*/
inline fun mockkStatic(vararg functions: KFunction<*>, block: () -> Unit) =
mockkStatic(classes = *functions.map { it.declaringKotlinFile }.toTypedArray(), block = block)