-
-
Notifications
You must be signed in to change notification settings - Fork 331
/
InternalPlatformDsl.kt
234 lines (200 loc) · 8.03 KB
/
InternalPlatformDsl.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package io.mockk
import kotlinx.coroutines.runBlocking
import java.lang.reflect.AccessibleObject
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.util.concurrent.atomic.AtomicLong
import kotlin.coroutines.Continuation
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KMutableProperty1
import kotlin.reflect.KProperty1
import kotlin.reflect.KType
import kotlin.reflect.KTypeParameter
import kotlin.reflect.full.allSuperclasses
import kotlin.reflect.full.functions
import kotlin.reflect.full.memberProperties
import kotlin.reflect.jvm.isAccessible
import kotlin.reflect.jvm.javaMethod
actual object InternalPlatformDsl {
actual fun identityHashCode(obj: Any): Int = System.identityHashCode(obj)
actual fun <T> runCoroutine(block: suspend () -> T): T {
return runBlocking {
block()
}
}
actual fun Any?.toStr() =
try {
when (this) {
null -> "null"
is BooleanArray -> this.contentToString()
is ByteArray -> this.contentToString()
is CharArray -> this.contentToString()
is ShortArray -> this.contentToString()
is IntArray -> this.contentToString()
is LongArray -> this.contentToString()
is FloatArray -> this.contentToString()
is DoubleArray -> this.contentToString()
is Array<*> -> this.contentDeepToString()
Void.TYPE.kotlin -> "void"
kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED -> "SUSPEND_MARKER"
is Continuation<*> -> "continuation {}"
is KClass<*> -> this.simpleName ?: "<null name class>"
is Method -> name + "(" + parameterTypes.map { it.simpleName }.joinToString() + ")"
is Function<*> -> "lambda {}"
else -> toString()
}
} catch (thr: Throwable) {
"<error \"$thr\">"
}
actual fun deepEquals(obj1: Any?, obj2: Any?): Boolean {
return if (obj1 === obj2) {
true
} else if (obj1 == null || obj2 == null) {
obj1 === obj2
} else if (obj1.javaClass.isArray && obj2.javaClass.isArray) {
arrayDeepEquals(obj1, obj2)
} else {
obj1 == obj2
}
}
private fun arrayDeepEquals(obj1: Any, obj2: Any): Boolean {
return when (obj1) {
is BooleanArray -> obj1 contentEquals obj2 as BooleanArray
is ByteArray -> obj1 contentEquals obj2 as ByteArray
is CharArray -> obj1 contentEquals obj2 as CharArray
is ShortArray -> obj1 contentEquals obj2 as ShortArray
is IntArray -> obj1 contentEquals obj2 as IntArray
is LongArray -> obj1 contentEquals obj2 as LongArray
is FloatArray -> obj1 contentEquals obj2 as FloatArray
is DoubleArray -> obj1 contentEquals obj2 as DoubleArray
else -> return obj1 as Array<*> contentDeepEquals obj2 as Array<*>
}
}
actual fun unboxChar(value: Any): Any = value
actual fun Any.toArray(): Array<*> =
when (this) {
is BooleanArray -> this.toTypedArray()
is ByteArray -> this.toTypedArray()
is CharArray -> this.toTypedArray()
is ShortArray -> this.toTypedArray()
is IntArray -> this.toTypedArray()
is LongArray -> this.toTypedArray()
is FloatArray -> this.toTypedArray()
is DoubleArray -> this.toTypedArray()
else -> this as Array<*>
}
actual fun classForName(name: String): Any = Class.forName(name).kotlin
actual fun dynamicCall(
self: Any,
methodName: String,
args: Array<out Any?>,
anyContinuationGen: () -> Continuation<*>
): Any? {
val params = arrayOf(self, *args)
val func = self::class.allAncestorFunctions().firstOrNull {
if (it.name != methodName) {
return@firstOrNull false
}
if (it.parameters.size != params.size) {
return@firstOrNull false
}
for ((idx, param) in it.parameters.withIndex()) {
val matches = when (val classifier = param.type.classifier) {
is KClass<*> -> classifier.isInstance(params[idx])
is KTypeParameter -> classifier.upperBounds.anyIsInstance(params[idx])
else -> false
}
if (!matches) {
return@firstOrNull false
}
}
return@firstOrNull true
}
?: throw MockKException("can't find function $methodName(${args.joinToString(", ")}) of class ${self.javaClass.name} for dynamic call.\n" +
"If you were trying to verify a private function, make sure to provide type information to exactly match the functions signature.")
func.javaMethod?.let { makeAccessible(it) }
return if (func.isSuspend) {
func.call(*params, anyContinuationGen())
} else {
func.call(*params)
}
}
private fun KClass<*>.allAncestorFunctions(): Sequence<KFunction<*>> {
return (sequenceOf(this) + this.allSuperclasses.asSequence())
.flatMap { it.functions }
}
private fun List<KType>.anyIsInstance(value: Any?): Boolean {
return any { bound ->
val classifier = bound.classifier
if (classifier is KClass<*>) {
classifier.isInstance(value)
} else {
false
}
}
}
actual fun dynamicGet(self: Any, name: String): Any? {
val property = self::class.memberProperties
.filterIsInstance<KProperty1<Any, Any?>>()
.firstOrNull {
it.name == name
} ?: throw MockKException("can't find property $name for dynamic property get")
property.isAccessible = true
return property.get(self)
}
actual fun dynamicSet(self: Any, name: String, value: Any?) {
val property = self::class.memberProperties
.filterIsInstance<KMutableProperty1<Any, Any?>>()
.firstOrNull {
it.name == name
} ?: throw MockKException("can't find property $name for dynamic property set")
property.isAccessible = true
return property.set(self, value)
}
actual fun dynamicSetField(self: Any, name: String, value: Any?) {
val field = self.javaClass
.declaredFields.firstOrNull { it.name == name }
?: return
makeAccessible(field)
field.set(self, value)
}
fun makeAccessible(obj: AccessibleObject) {
try {
obj.isAccessible = true
} catch (ex: Throwable) {
// skip
}
}
actual fun <T> threadLocal(initializer: () -> T): InternalRef<T> {
class TL : ThreadLocal<T>(), InternalRef<T> {
override fun initialValue(): T {
return initializer()
}
override val value: T
get() = get()
}
return TL()
}
actual fun counter() = object : InternalCounter {
val atomicValue = AtomicLong()
override val value: Long
get() = atomicValue.get()
override fun increment() = atomicValue.getAndIncrement()
}
actual fun <T> coroutineCall(lambda: suspend () -> T): CoroutineCall<T> = JvmCoroutineCall<T>(lambda)
}
class JvmCoroutineCall<T>(private val lambda: suspend () -> T) : CoroutineCall<T> {
companion object {
val callMethod = JvmCoroutineCall::class.java.getMethod("callCoroutine", Continuation::class.java)
}
suspend fun callCoroutine() = lambda()
override fun callWithContinuation(continuation: Continuation<*>): T {
return try {
@Suppress("UNCHECKED_CAST")
callMethod.invoke(this, continuation) as T
} catch (ex: InvocationTargetException) {
throw ex.targetException
}
}
}