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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use std::panic;
use crate::property::*;
use crate::*;
impl<I: IntoIterator<Item = Clause>> From<I> for Clause {
fn from(clauses: I) -> Self {
Clause(ClausePrivate::Multiple(clauses.into_iter().collect()))
}
}
pub(crate) enum ClausePrivate {
Single(mock::DynMockImpl),
Multiple(Vec<Clause>),
}
pub struct Each<F: MockFn> {
typed_impl: mock::TypedMockImpl<F>,
}
impl<F> Each<F>
where
F: MockFn + 'static,
{
pub fn call<'e, M>(&'e mut self, matching: M) -> Match<'e, F, InAnyOrder>
where
M: (for<'i> Fn(&<F as MockInputs<'i>>::Inputs) -> bool) + Send + Sync + 'static,
{
self.typed_impl.patterns.push(mock::CallPattern {
input_matcher: Box::new(matching),
call_index_range: Default::default(),
call_counter: counter::CallCounter::new(0, counter::Exactness::AtLeast),
responders: vec![],
});
Match {
pattern: PatternWrapper::Grouped(self.typed_impl.patterns.last_mut().unwrap()),
response_index: 0,
ordering: InAnyOrder,
}
}
pub(crate) fn new() -> Self {
Self {
typed_impl: mock::TypedMockImpl::new(),
}
}
pub(crate) fn to_clause(self) -> Clause {
Clause(ClausePrivate::Single(mock::DynMockImpl::new_full_cascade(
Box::new(self.typed_impl),
)))
}
}
pub(crate) enum PatternWrapper<'p, F: MockFn> {
Grouped(&'p mut mock::CallPattern<F>),
Standalone(mock::TypedMockImpl<F>),
}
impl<'p, F: MockFn> PatternWrapper<'p, F> {
fn get_mut(&mut self) -> &mut mock::CallPattern<F> {
match self {
PatternWrapper::Grouped(p) => *p,
PatternWrapper::Standalone(mock_impl) => mock_impl.patterns.last_mut().unwrap(),
}
}
}
pub(crate) fn new_standalone_match<F, O>(
mock_impl: mock::TypedMockImpl<F>,
ordering: O,
) -> Match<'static, F, O>
where
F: MockFn + 'static,
O: Ordering,
{
Match {
pattern: PatternWrapper::Standalone(mock_impl),
response_index: 0,
ordering,
}
}
pub struct Match<'p, F: MockFn, O: Ordering> {
pattern: PatternWrapper<'p, F>,
response_index: usize,
ordering: O,
}
impl<'p, F, O> Match<'p, F, O>
where
F: MockFn + 'static,
O: Ordering,
{
pub fn returns(self, value: impl Into<F::Output>) -> QuantifyResponse<'p, F, O>
where
F::Output: Send + Sync + Clone + 'static,
{
let value = value.into();
self.responder(mock::Responder::Value(Box::new(mock::StoredValueSlot(
value,
))))
}
pub fn returns_default(self) -> QuantifyResponse<'p, F, O>
where
F::Output: Default,
{
self.responder(mock::Responder::Closure(Box::new(|_| Default::default())))
}
pub fn returns_ref<T>(self, value: T) -> QuantifyResponse<'p, F, O>
where
T: std::borrow::Borrow<F::Output> + Sized + Send + Sync + 'static,
{
self.responder(mock::Responder::Borrowable(Box::new(value)))
}
pub fn returns_static(self, value: &'static F::Output) -> QuantifyResponse<'p, F, O>
where
F::Output: Send + Sync + 'static,
{
self.responder(mock::Responder::StaticRefClosure(Box::new(move |_| value)))
}
pub fn answers<A, R>(self, func: A) -> QuantifyResponse<'p, F, O>
where
A: (for<'i> Fn(<F as MockInputs<'i>>::Inputs) -> R) + Send + Sync + 'static,
R: Into<F::Output>,
F::Output: Sized,
{
self.responder(mock::Responder::Closure(Box::new(move |inputs| {
func(inputs).into()
})))
}
pub fn answers_leaked_ref<A, R>(self, func: A) -> QuantifyResponse<'p, F, O>
where
A: (for<'i> Fn(<F as MockInputs<'i>>::Inputs) -> R) + Send + Sync + 'static,
R: std::borrow::Borrow<F::Output> + 'static,
F::Output: Sized,
{
self.responder(mock::Responder::StaticRefClosure(Box::new(move |inputs| {
let value = func(inputs);
let leaked_ref = Box::leak(Box::new(value));
<R as std::borrow::Borrow<F::Output>>::borrow(leaked_ref)
})))
}
pub fn panics(self, message: impl Into<String>) -> QuantifyResponse<'p, F, O> {
let message = message.into();
self.responder(mock::Responder::Panic(message))
}
pub fn unmocked(self) -> QuantifyResponse<'p, F, O>
where
F: Unmock,
{
self.responder(mock::Responder::Unmock)
}
fn responder(mut self, responder: mock::Responder<F>) -> QuantifyResponse<'p, F, O> {
self.pattern
.get_mut()
.responders
.push(mock::CallOrderResponder {
response_index: self.response_index,
responder,
});
QuantifyResponse {
pattern: self.pattern,
response_index: self.response_index,
ordering: self.ordering,
}
}
}
pub struct QuantifyResponse<'p, F: MockFn, O> {
pattern: PatternWrapper<'p, F>,
response_index: usize,
ordering: O,
}
impl<'p, F, O> QuantifyResponse<'p, F, O>
where
F: MockFn + 'static,
O: Ordering,
{
pub fn once(mut self) -> QuantifiedResponse<'p, F, O, Exact> {
self.pattern
.get_mut()
.call_counter
.add_to_minimum(1, counter::Exactness::Exact);
self.into_exact(1)
}
pub fn n_times(mut self, times: usize) -> QuantifiedResponse<'p, F, O, Exact> {
self.pattern
.get_mut()
.call_counter
.add_to_minimum(times, counter::Exactness::Exact);
self.into_exact(times)
}
pub fn at_least_times(mut self, times: usize) -> QuantifiedResponse<'p, F, O, AtLeast> {
self.pattern
.get_mut()
.call_counter
.add_to_minimum(times, counter::Exactness::AtLeast);
QuantifiedResponse {
pattern: self.pattern,
response_index: self.response_index + times,
ordering: self.ordering,
_repetition: AtLeast,
}
}
pub fn in_any_order(self) -> Clause
where
O: Ordering<Kind = InAnyOrder>,
{
match self.pattern {
PatternWrapper::Standalone(typed_impl) => Clause(ClausePrivate::Single(
mock::DynMockImpl::new_full_cascade(Box::new(typed_impl)),
)),
_ => panic!("Cannot expect a next call among group of call patterns"),
}
}
fn into_exact(self, times: usize) -> QuantifiedResponse<'p, F, O, Exact> {
QuantifiedResponse {
pattern: self.pattern,
response_index: self.response_index + times,
ordering: self.ordering,
_repetition: Exact,
}
}
}
pub struct QuantifiedResponse<'p, F: MockFn, O, R> {
pattern: PatternWrapper<'p, F>,
response_index: usize,
ordering: O,
_repetition: R,
}
impl<'p, F, O, R> QuantifiedResponse<'p, F, O, R>
where
F: MockFn + 'static,
O: Ordering,
R: Repetition,
{
pub fn then(mut self) -> Match<'p, F, O>
where
R: Repetition<Kind = Exact>,
{
self.pattern
.get_mut()
.call_counter
.add_to_minimum(0, counter::Exactness::AtLeastPlusOne);
Match {
pattern: self.pattern,
response_index: self.response_index,
ordering: self.ordering,
}
}
pub fn in_order(self) -> Clause
where
O: Ordering<Kind = InOrder>,
R: Repetition<Kind = Exact>,
{
match self.pattern {
PatternWrapper::Standalone(typed_impl) => Clause(ClausePrivate::Single(
mock::DynMockImpl::new_strict_order(Box::new(typed_impl)),
)),
_ => panic!(),
}
}
pub fn in_any_order(self) -> Clause
where
O: Ordering<Kind = InAnyOrder>,
{
match self.pattern {
PatternWrapper::Standalone(typed_impl) => Clause(ClausePrivate::Single(
mock::DynMockImpl::new_full_cascade(Box::new(typed_impl)),
)),
_ => panic!(),
}
}
}