forked from mockk/mockk
/
VerifyingState.kt
105 lines (87 loc) · 3.34 KB
/
VerifyingState.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
package io.mockk.impl.recording.states
import io.mockk.MockKException
import io.mockk.MockKGateway.VerificationParameters
import io.mockk.MockKGateway.VerificationResult
import io.mockk.impl.log.Logger
import io.mockk.impl.recording.CommonCallRecorder
import io.mockk.impl.stub.Stub
import io.mockk.impl.verify.VerificationHelpers
class VerifyingState(
recorder: CommonCallRecorder,
val params: VerificationParameters
) : RecordingState(recorder) {
override fun wasNotCalled(list: List<Any>) {
addWasNotCalled(list)
}
override fun recordingDone(): CallRecordingState {
checkMissingCalls()
val verifier = recorder.factories.verifier(params)
val sorter = recorder.factories.verificationCallSorter()
sorter.sort(recorder.calls)
val outcome =
recorder.safeExec {
verifier.verify(
sorter.regularCalls,
params
)
}
if (outcome.matches) {
verifier.captureArguments()
}
log.trace { "Done verification. Outcome: $outcome" }
failIfNotPassed(outcome, params.inverse)
markVerified(outcome)
checkWasNotCalled(sorter.wasNotCalledCalls.map { it.matcher.self })
return recorder.factories.answeringState(recorder)
}
private fun checkMissingCalls() {
if (recorder.calls.isEmpty()) {
throw MockKException("Missing calls inside verify { ... } block.")
}
}
private fun failIfNotPassed(outcome: VerificationResult, inverse: Boolean) {
when (outcome) {
is VerificationResult.OK -> if (inverse) {
val callsReport = VerificationHelpers.formatCalls(outcome.verifiedCalls)
throw AssertionError("Inverse verification failed.\n\nVerified calls:\n$callsReport")
}
is VerificationResult.Failure -> if (!inverse) {
throw AssertionError("Verification failed: ${outcome.message}")
}
}
}
private fun markVerified(outcome: VerificationResult) {
if (outcome is VerificationResult.OK) {
for (invocation in outcome.verifiedCalls) {
recorder.ack.markCallVerified(invocation)
}
}
}
private fun checkWasNotCalled(mocks: List<Any>) {
val calledStubs = mutableListOf<Stub>()
for (mock in mocks) {
val stub = recorder.stubRepo.stubFor(mock)
val calls = stub.allRecordedCalls()
if (calls.isNotEmpty()) {
calledStubs += stub
}
}
if (calledStubs.isNotEmpty()) {
if (calledStubs.size == 1) {
val calledStub = calledStubs[0]
throw AssertionError(recorder.safeExec {
"Verification failed: ${calledStub.toStr()} should not be called:\n" +
calledStub.allRecordedCalls().joinToString("\n")
})
} else {
throw AssertionError(recorder.safeExec {
"Verification failed: ${calledStubs.joinToString(", ") { it.toStr() }} should not be called:\n" +
calledStubs.flatMap { it.allRecordedCalls() }.joinToString("\n")
})
}
}
}
companion object {
val log = Logger<VerifyingState>()
}
}