forked from mockk/mockk
/
PermanentMocker.kt
148 lines (121 loc) · 4.36 KB
/
PermanentMocker.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
package io.mockk.impl.recording
import io.mockk.EqMatcher
import io.mockk.EquivalentMatcher
import io.mockk.RecordedCall
import io.mockk.impl.InternalPlatform
import io.mockk.impl.log.Logger
import io.mockk.impl.log.SafeToString
import io.mockk.impl.stub.StubRepository
class PermanentMocker(
val stubRepo: StubRepository,
val safeToString: SafeToString
) {
val log = safeToString(Logger<PermanentMocker>())
val permanentMocks = InternalPlatform.identityMap<Any, Any>()
val callRef = InternalPlatform.weakMap<Any, RecordedCall>()
fun mock(calls: List<RecordedCall>): List<RecordedCall> {
val result = mutableListOf<RecordedCall>()
for (call in calls) {
val permanentCall = permamentize(call)
result.add(permanentCall)
}
val callTree = safeToString.exec { describeCallTree(result) }
if (callTree.size == 1) {
log.trace { "Mocked permanently: " + callTree[0] }
} else {
log.trace { "Mocked permanently:\n" + callTree.joinToString(", ") }
}
return result
}
private fun permamentize(call: RecordedCall): RecordedCall {
val newCall = makeCallPermanent(call)
val retValue = call.retValue
if (call.isRetValueMock && retValue != null) {
val equivalentCall = makeEquivalent(newCall)
log.trace { "Child search key: ${equivalentCall.matcher}" }
val childMock = stubRepo.stubFor(newCall.matcher.self)
.childMockK(equivalentCall.matcher, equivalentCall.retType)
val newNewCall = newCall.copy(retValue = childMock)
permanentMocks[retValue] = childMock
callRef[retValue] = newNewCall
return newNewCall
}
return newCall
}
private fun makeEquivalent(newCall: RecordedCall): RecordedCall {
val equivalentArgs = newCall.matcher.args.map {
when (it) {
is EquivalentMatcher -> it.equivalent()
else -> it
}
}
val equivalentIM = newCall.matcher.copy(args = equivalentArgs)
return newCall.copy(matcher = equivalentIM)
}
private fun makeCallPermanent(call: RecordedCall): RecordedCall {
val selfChain = callRef[call.matcher.self]
val argChains = call.matcher.args
.map {
when (it) {
is EqMatcher -> callRef[it.value] ?: it
else -> it
}
}
val newSelf = permanentMocks[call.matcher.self] ?: call.matcher.self
val newArgs = call.matcher.args.map { it.substitute(permanentMocks) }
val newMatcher = call.matcher.copy(self = newSelf, args = newArgs)
return call.copy(
matcher = newMatcher,
selfChain = selfChain,
argChains = argChains
)
}
private fun describeCallTree(calls: MutableList<RecordedCall>): List<String> {
val callTree = linkedMapOf<RecordedCall, String>()
val usedCalls = hashSetOf<RecordedCall>()
for (call in calls) {
callTree[call] = formatCall(
call,
callTree,
usedCalls
)
}
return calls.filter {
it !in usedCalls
}.map {
callTree[it] ?: "<bad call>"
}
}
private fun formatCall(
call: RecordedCall,
tree: Map<RecordedCall, String>,
usedCalls: MutableSet<RecordedCall>
): String {
val methodName = call.matcher.method.name
val args = call.argChains!!.map {
when (it) {
is RecordedCall -> {
usedCalls.add(it)
tree[it] ?: "<bad link>"
}
else -> it.toString()
}
}
val selfChain = call.selfChain
val prefix = if (selfChain != null) {
usedCalls.add(selfChain)
(tree[selfChain] ?: "<bad link>") + "."
} else {
call.matcher.self.toString() + "."
}
if (methodName.startsWith("get") &&
methodName.length > 3 &&
args.isEmpty()
) {
return prefix +
methodName[3].lowercase() +
methodName.substring(4)
}
return prefix + methodName + "(" + args.joinToString(", ") + ")"
}
}