/
GatewayAPI.kt
273 lines (210 loc) · 6.27 KB
/
GatewayAPI.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package io.mockk
import kotlin.reflect.KClass
/**
* Mediates mocking implementation
*/
interface MockKGateway {
val mockFactory: MockFactory
val staticMockFactory: StaticMockFactory
val objectMockFactory: ObjectMockFactory
val constructorMockFactory: ConstructorMockFactory
val stubber: Stubber
val verifier: Verifier
val excluder: Excluder
val callRecorder: CallRecorder
val instanceFactoryRegistry: InstanceFactoryRegistry
val clearer: Clearer
val mockInitializer: MockInitializer
val verificationAcknowledger: VerificationAcknowledger
val mockTypeChecker: MockTypeChecker
fun verifier(params: VerificationParameters): CallVerifier
data class ClearOptions(
val answers: Boolean,
val recordedCalls: Boolean,
val childMocks: Boolean,
val verificationMarks: Boolean,
val exclusionRules: Boolean
)
companion object {
lateinit var implementation: () -> MockKGateway
}
/**
* Create new mocks or spies
*/
interface MockFactory {
fun <T : Any> mockk(
mockType: KClass<T>,
name: String?,
relaxed: Boolean,
moreInterfaces: Array<out KClass<*>>,
relaxUnitFun: Boolean
): T
fun <T : Any> spyk(
mockType: KClass<T>?,
objToCopy: T?,
name: String?,
moreInterfaces: Array<out KClass<*>>,
recordPrivateCalls: Boolean
): T
fun temporaryMock(mockType: KClass<*>): Any
fun isMock(value: Any): Boolean
}
/**
* Binds static mocks
*/
interface StaticMockFactory {
fun staticMockk(cls: KClass<*>): () -> Unit
fun clear(type: KClass<*>, options: ClearOptions)
fun clearAll(options: ClearOptions)
}
/**
* Binds object mocks
*/
interface ObjectMockFactory {
fun objectMockk(obj: Any, recordPrivateCalls: Boolean): () -> Unit
fun clear(obj: Any, options: ClearOptions)
fun clearAll(options: ClearOptions)
}
/**
* Controls constructor mocking
*/
interface ConstructorMockFactory {
fun constructorMockk(
cls: KClass<*>,
recordPrivateCalls: Boolean,
localToThread: Boolean
): () -> Unit
fun <T : Any> mockPlaceholder(cls: KClass<T>, args: Array<Matcher<*>>? = null): T
fun clear(type: KClass<*>, options: ClearOptions)
fun clearAll(options: ClearOptions)
}
/**
* Clears mocks
*/
interface Clearer {
fun clear(
mocks: Array<out Any>,
options: ClearOptions
)
fun clearAll(
options: ClearOptions
)
}
/**
* Stub calls
*/
interface Stubber {
fun <T> every(
mockBlock: (MockKMatcherScope.() -> T)?,
coMockBlock: (suspend MockKMatcherScope.() -> T)?
): MockKStubScope<T, T>
}
/**
* Verify calls
*/
interface Verifier {
fun verify(
params: VerificationParameters,
mockBlock: (MockKVerificationScope.() -> Unit)?,
coMockBlock: (suspend MockKVerificationScope.() -> Unit)?
)
}
/**
* Verify calls
*/
interface Excluder {
fun exclude(
params: ExclusionParameters,
mockBlock: (MockKMatcherScope.() -> Unit)?,
coMockBlock: (suspend MockKMatcherScope.() -> Unit)?
)
}
/**
* Parameters of verification
*/
data class VerificationParameters(
val ordering: Ordering,
val min: Int,
val max: Int,
val inverse: Boolean,
val timeout: Long
)
/**
* Parameters of exclusion
*/
data class ExclusionParameters(
val current: Boolean
)
interface AnswerOpportunity<T> {
fun provideAnswer(answer: Answer<T>)
}
/**
* Builds a list of calls
*/
interface CallRecorder {
val calls: List<RecordedCall>
fun startStubbing()
fun startVerification(params: VerificationParameters)
fun startExclusion(params: ExclusionParameters)
fun round(n: Int, total: Int = 64)
fun <T : Any> matcher(matcher: Matcher<*>, cls: KClass<T>): T
fun call(invocation: Invocation): Any?
fun answerOpportunity(): AnswerOpportunity<*>
fun done()
fun hintNextReturnType(cls: KClass<*>, n: Int)
fun reset()
fun estimateCallRounds(): Int
fun nCalls(): Int
fun wasNotCalled(list: List<Any>)
fun discardLastCallRound()
fun isLastCallReturnsNothing(): Boolean
}
/**
* Verifier takes the list of calls and checks what invocations happened to the mocks
*/
interface CallVerifier {
fun verify(verificationSequence: List<RecordedCall>, params: VerificationParameters): VerificationResult
fun captureArguments()
}
/**
* Result of verification
*/
sealed class VerificationResult {
data class OK(val verifiedCalls: List<Invocation>) : VerificationResult()
data class Failure(val message: String) : VerificationResult()
val matches: Boolean
get() = this is VerificationResult.OK
}
interface InstanceFactoryRegistry {
fun registerFactory(factory: InstanceFactory)
fun deregisterFactory(factory: InstanceFactory)
}
/**
* Factory of dummy objects
*/
interface InstanceFactory {
fun instantiate(cls: KClass<*>): Any?
}
interface MockInitializer {
fun initAnnotatedMocks(
targets: List<Any>,
overrideRecordPrivateCalls: Boolean,
relaxUnitFun: Boolean,
relaxed: Boolean
)
}
interface VerificationAcknowledger {
fun markCallVerified(invocation: Invocation)
fun acknowledgeVerified(mock: Any)
fun acknowledgeVerified()
fun checkUnnecessaryStub(mock: Any)
fun checkUnnecessaryStub()
}
interface MockTypeChecker {
fun isRegularMock(mock: Any): Boolean
fun isSpy(mock: Any): Boolean
fun isObjectMock(mock: Any): Boolean
fun isStaticMock(mock: Any): Boolean
fun isConstructorMock(mock: Any): Boolean
}
}