-
-
Notifications
You must be signed in to change notification settings - Fork 331
/
UnorderedCallVerifier.kt
193 lines (178 loc) · 8.18 KB
/
UnorderedCallVerifier.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
package io.mockk.impl.verify
import io.mockk.CapturingSlotMatcher
import io.mockk.InternalPlatformDsl.toStr
import io.mockk.Invocation
import io.mockk.InvocationMatcher
import io.mockk.MockKException
import io.mockk.MockKGateway.CallVerifier
import io.mockk.MockKGateway.VerificationParameters
import io.mockk.MockKGateway.VerificationResult
import io.mockk.MockKSettings
import io.mockk.RecordedCall
import io.mockk.impl.InternalPlatform
import io.mockk.impl.Ref
import io.mockk.impl.log.SafeToString
import io.mockk.impl.stub.StubRepository
import io.mockk.impl.verify.VerificationHelpers.formatCalls
import io.mockk.impl.verify.VerificationHelpers.stackTrace
import io.mockk.impl.verify.VerificationHelpers.stackTraces
open class UnorderedCallVerifier(
val stubRepo: StubRepository,
val safeToString: SafeToString
) : CallVerifier {
private val captureBlocks = mutableListOf<() -> Unit>()
override fun verify(
verificationSequence: List<RecordedCall>,
params: VerificationParameters
): VerificationResult {
val min = params.min
val max = params.max
val verifiedCalls = mutableSetOf<Ref>()
for ((i, call) in verificationSequence.withIndex()) {
val callIdxMsg = safeToString.exec { "call ${i + 1} of ${verificationSequence.size}: ${call.matcher}" }
val result = matchCall(call, min, max, callIdxMsg)
when (result) {
is VerificationResult.OK -> verifiedCalls.addAll(
result
.verifiedCalls
.map { InternalPlatform.ref(it) }
)
is VerificationResult.Failure -> return result
}
}
return VerificationResult.OK(verifiedCalls.map { it.value as Invocation })
}
private fun matchCall(recordedCall: RecordedCall, min: Int, max: Int, callIdxMsg: String): VerificationResult {
val matcher = recordedCall.matcher
val stub = stubRepo.stubFor(matcher.self)
val allCallsForMock = stub.allRecordedCalls()
val allCallsForMockMethod = stub.allRecordedCalls(matcher.method)
val matchedCalls = allCallsForMockMethod.filter(matcher::match)
if(matchedCalls.size > 1 && matcher.args.any { it is CapturingSlotMatcher<*> }) {
val msg = "$matcher execution is being verified more than once and its arguments are being captured with a slot.\n" +
"This will store only the argument of the last invocation in the slot.\n" +
"If you want to store all the arguments, use a mutableList to capture arguments."
throw MockKException(msg)
}
val result = if (min == 0 && max == 0) {
if (matchedCalls.isEmpty()) {
VerificationResult.OK(listOf())
} else {
VerificationResult.Failure(
"$callIdxMsg should not be called" +
"\n\nCalls:\n" +
formatCalls(allCallsForMockMethod) +
"\n" +
if (MockKSettings.stackTracesOnVerify)
"\n\nStack traces:\n" + stackTraces(allCallsForMockMethod)
else
""
)
}
} else when (allCallsForMockMethod.size) {
0 -> {
if (min == 0 && max == 0) {
VerificationResult.OK(listOf())
} else if (allCallsForMock.isEmpty()) {
VerificationResult.Failure("$callIdxMsg was not called")
} else {
VerificationResult.Failure(safeToString.exec {
"$callIdxMsg was not called." +
"\n\nCalls to same mock:\n" +
formatCalls(allCallsForMock) +
"\n" +
if (MockKSettings.stackTracesOnVerify)
"\n\nStack traces:\n" + stackTraces(allCallsForMock)
else
""
})
}
}
1 -> {
val onlyCall = allCallsForMockMethod[0]
if (matcher.match(onlyCall)) {
if (1 in min..max) {
VerificationResult.OK(listOf(onlyCall))
} else {
VerificationResult.Failure(
"$callIdxMsg. One matching call found, but needs at least $min${atMostMsg(max)} calls" +
"\nCall: " + allCallsForMock.first() +
if (MockKSettings.stackTracesOnVerify)
"\nStack trace:\n" + stackTrace(0, allCallsForMock.first().callStack())
else
""
)
}
} else {
if (0 in min..max) {
VerificationResult.OK(listOf())
} else {
VerificationResult.Failure(safeToString.exec {
"$callIdxMsg. Only one matching call to ${stub.toStr()}/${matcher.method.toStr()} happened, but arguments are not matching:\n" +
describeArgumentDifference(matcher, onlyCall) +
if (MockKSettings.stackTracesOnVerify)
"\nStack trace:\n" + stackTrace(0, allCallsForMock.first().callStack())
else
""
})
}
}
}
else -> {
val n = matchedCalls.count()
if (n in min..max) {
VerificationResult.OK(matchedCalls)
} else {
if (n == 0) {
VerificationResult.Failure(
safeToString.exec {
"$callIdxMsg. No matching calls found." +
"\n\nCalls to same method:\n" +
formatCalls(allCallsForMockMethod) +
"\n" +
if (MockKSettings.stackTracesOnVerify)
"\n\nStack traces:\n" + stackTraces(allCallsForMockMethod)
else
""
})
} else {
VerificationResult.Failure(
"$callIdxMsg. $n matching calls found, " +
"but needs at least $min${atMostMsg(max)} calls" +
"\nCalls:\n" +
formatCalls(allCallsForMock) +
"\n" +
if (MockKSettings.stackTracesOnVerify)
"\n\nStack traces:\n" + stackTraces(allCallsForMock)
else
""
)
}
}
}
}
captureBlocks.add {
for (call in matchedCalls) {
matcher.captureAnswer(call)
}
}
return result
}
override fun captureArguments() {
captureBlocks.forEach { it() }
}
private fun atMostMsg(max: Int) = if (max == Int.MAX_VALUE) "" else " and at most $max"
private fun describeArgumentDifference(
matcher: InvocationMatcher,
invocation: Invocation
): String {
val str = StringBuilder()
for ((i, arg) in invocation.args.withIndex()) {
val argMatcher = matcher.args[i]
val matches = argMatcher.match(arg)
val argStr = safeToString.exec { arg.toStr() }
str.append("[$i]: argument: $argStr, matcher: $argMatcher, result: ${if (matches) "+" else "-"}\n")
}
return str.toString()
}
}