forked from mockk/mockk
/
ConstructorStub.kt
102 lines (83 loc) · 2.92 KB
/
ConstructorStub.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
package io.mockk.impl.stub
import io.mockk.*
import io.mockk.MockKGateway.ExclusionParameters
import io.mockk.impl.InternalPlatform
import kotlin.reflect.KClass
class ConstructorStub(
val mock: Any,
val representativeMock: Any,
val stub: Stub,
val recordPrivateCalls: Boolean
) : Stub {
private val represent = identityMapOf(mock to representativeMock)
private val revertRepresentation = identityMapOf(representativeMock to mock)
private fun <K, V> identityMapOf(vararg pairs: Pair<K, V>): Map<K, V> =
InternalPlatform.identityMap<K, V>()
.also { map -> map.putAll(pairs) }
override val name: String
get() = stub.name
override val type: KClass<*>
get() = stub.type
override fun addAnswer(matcher: InvocationMatcher, answer: Answer<*>) =
stub.addAnswer(matcher.substitute(represent), answer)
override fun answer(invocation: Invocation) = stub.answer(
invocation.substitute(represent)
).internalSubstitute(revertRepresentation)
override fun childMockK(matcher: InvocationMatcher, childType: KClass<*>) =
stub.childMockK(
matcher.substitute(represent),
childType
)
override fun recordCall(invocation: Invocation) {
val record = if (recordPrivateCalls)
true
else
!invocation.method.privateCall
if (record) {
stub.recordCall(invocation.substitute(represent))
}
}
override fun excludeRecordedCalls(params: ExclusionParameters, matcher: InvocationMatcher) {
stub.excludeRecordedCalls(
params,
matcher.substitute(represent)
)
}
override fun markCallVerified(invocation: Invocation) {
stub.markCallVerified(
invocation.substitute(represent)
)
}
override fun allRecordedCalls() = stub.allRecordedCalls()
.map {
it.substitute(revertRepresentation)
}
override fun allRecordedCalls(method: MethodDescription) =
stub.allRecordedCalls(method)
.map {
it.substitute(revertRepresentation)
}
override fun verifiedCalls() = stub.verifiedCalls()
.map {
it.substitute(revertRepresentation)
}
override fun matcherUsages() = stub.matcherUsages()
override fun clear(options: MockKGateway.ClearOptions) =
stub.clear(options)
override fun handleInvocation(
self: Any,
method: MethodDescription,
originalCall: () -> Any?,
args: Array<out Any?>,
fieldValueProvider: BackingFieldValueProvider
) = stub.handleInvocation(
self,
method,
originalCall,
args,
fieldValueProvider
)
override fun toStr() = stub.toStr()
override fun stdObjectAnswer(invocation: Invocation) = stub.stdObjectAnswer(invocation.substitute(represent))
override fun dispose() = stub.dispose()
}