/
MockK.kt
69 lines (58 loc) · 2.18 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@file:JvmName("JVMMockKKt")
package io.mockk
import io.mockk.impl.JvmMockKGateway
import kotlin.reflect.KFunction
import kotlin.reflect.KProperty
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
*/
fun mockkStatic(vararg functions: KFunction<*>) =
mockkStatic(*functions.map { it.declaringKotlinFile }.toTypedArray())
/**
* Builds a static mock. Any mocks of this function's declaring class are cancelled before it's mocked
*/
fun mockkStatic(vararg functions: KProperty<*>) =
mockkStatic(*functions.map(KProperty<*>::getter).toTypedArray())
/**
* Cancel static mocks.
*/
fun unmockkStatic(vararg functions: KFunction<*>) =
unmockkStatic(*functions.map { it.declaringKotlinFile }.toTypedArray())
/**
* Cancel static mocks.
*/
fun unmockkStatic(vararg functions: KProperty<*>) =
unmockkStatic(*functions.map(KProperty<*>::getter).toTypedArray())
/**
* Clear static mocks.
*/
fun clearStaticMockk(vararg functions: KFunction<*>) =
clearStaticMockk(*functions.map { it.declaringKotlinFile }.toTypedArray())
/**
* Clear static mocks.
*/
fun clearStaticMockk(vararg functions: KProperty<*>) =
clearStaticMockk(*functions.map(KProperty<*>::getter).toTypedArray())
/**
* Builds a static mock and unmocks it after the block has been executed.
*/
inline fun mockkStatic(vararg functions: KFunction<*>, block: () -> Unit) =
mockkStatic(*functions.map { it.declaringKotlinFile }.toTypedArray(), block = block)
/**
* Builds a static mock and unmocks it after the block has been executed.
*/
inline fun mockkStatic(vararg functions: KProperty<*>, block: () -> Unit) =
mockkStatic(*functions.map(KProperty<*>::getter).toTypedArray(), block = block)