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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//! Mock object internals. You can use this API to construct mock objects manually.
use std::thread;
use super::MethodName;
use super::method::Method;
use super::store::ExpectationStore;
pub struct Expectations {
store: ExpectationStore
}
impl Expectations {
/// Create a new `Expectations` instance. Call this when your mock object is created.
pub fn new() -> Self {
Expectations {
store: ExpectationStore::new()
}
}
/// Returns a `Method` struct which you can use to add expectations for the
/// method with the given name.
pub fn expect<I, O>(&mut self, name: MethodName) -> Method<I, O> where
I: 'static,
O: 'static
{
Method::new(&mut self.store, name)
}
/// Begin a new Era. Expectations in one Era must be met before expectations
/// in future eras will be evaluated.
///
/// Note that Eras are evaluated eagerly. This means that Eras may advance more
/// quickly than you'd intuitively expect in certain situations. For example,
/// `called_any()` is marked as complete after the first call is received.
/// This menas that, for the purposes of telling if an Era should be advanced or
/// not, `called_any()` and `called_once()` are the same.
pub fn then(&mut self) -> &mut Self {
self.store.new_era();
self
}
/// When a tracked method is called on the mock object, call this with the method's name
/// in order to tell the `Expectations` that the method was called.
///
/// Unlike `was_called_returning`, this method does not return a value.
pub fn was_called<I, O>(&self, name: MethodName, params: I) where
I: 'static,
O: 'static
{
self.store
.matcher_for::<I, O>(name)
.was_called(params);
}
/// Same as the `was_called` method, but also returns the result.
pub fn was_called_returning<I, O>(&self, name: MethodName, params: I) -> O where
I: 'static,
O: 'static
{
self.store
.matcher_for::<I, O>(name)
.was_called_returning(params)
}
fn verify(&self) {
if let Err(e) = self.store.verify() {
panic!("{}", e);
}
}
}
impl Drop for Expectations {
/// All expectations will be verified when the mock object is dropped,
/// panicking if any of them are unmet.
///
/// In the case where the Expectations object is being dropped because the
/// thread is _already_ panicking, the Expectations object is not verified.
fn drop(&mut self) {
if !thread::panicking() {
self.verify();
}
}
}
#[cfg(test)]
mod tests {
use simulacrum_user::*;
use std::panic;
use super::*;
#[test]
fn test_called_once() {
let mut e = Expectations::new();
e.expect::<(), ()>("spoo").called_once();
e.was_called::<(), ()>("spoo", ());
// Verified on drop
}
#[test]
#[should_panic]
fn test_called_once_fail() {
let mut e = Expectations::new();
// Panic: "spoo" should have been called once, but was never called
e.expect::<(), ()>("spoo").called_once();
}
#[test]
fn test_called_twice() {
let mut e = Expectations::new();
e.expect::<(), ()>("nom").called_times(2);
e.was_called::<(), ()>("nom", ());
e.was_called::<(), ()>("nom", ());
}
#[test]
#[should_panic]
fn test_called_twice_fail() {
let mut e = Expectations::new();
e.expect::<(), ()>("nom").called_times(2);
// Panic: "nom" should have been called twice, but was only called once
e.was_called::<(), ()>("nom", ());
}
#[test]
fn test_called_never_pass() {
let mut e = Expectations::new();
e.expect::<(), ()>("blitz").called_never();
}
#[test]
#[should_panic]
fn test_called_never_fail() {
let mut e = Expectations::new();
e.expect::<(), ()>("blitz").called_never();
// Panic: "blitz" should have never been called
e.was_called::<(), ()>("blitz", ());
}
#[test]
fn test_called_any_zero() {
let mut e = Expectations::new();
e.expect::<(), ()>("mega").called_any();
}
#[test]
fn test_called_any_two() {
let mut e = Expectations::new();
e.expect::<(), ()>("mega").called_any();
e.was_called::<(), ()>("mega", ());
e.was_called::<(), ()>("mega", ());
}
#[test]
fn test_param() {
let mut e = Expectations::new();
e.expect::<i32, ()>("doog").called_once().with(gt(5));
e.was_called::<i32, ()>("doog", 10);
}
#[test]
#[should_panic]
fn test_param_fail() {
let mut e = Expectations::new();
e.expect::<i32, ()>("doog").called_once().with(gt(5));
// Panic: "doog"'s parameter was not > 5
e.was_called::<i32, ()>("doog", 1);
}
#[test]
fn test_returning() {
let mut e = Expectations::new();
e.expect::<(), i32>("boye").called_any().returning(|_| 5);
let r = e.was_called_returning::<(), i32>("boye", ());
assert_eq!(r, 5);
}
#[test]
#[should_panic]
fn test_returning_no_matches() {
let e = Expectations::new();
// Not expecting "boye" here, so when it's called, we should panic
// Panic: No expectation matches, so we can't return a value
e.was_called_returning::<(), i32>("boye", ());
}
#[test]
fn test_modifications() {
let mut e = Expectations::new();
e.expect::<*mut i32, ()>("dawg")
.called_any()
.modifying(|&mut arg| {
unsafe {
*arg = 3;
}
});
let mut i = 2;
e.was_called::<*mut i32, ()>("dawg", &mut i);
assert_eq!(i, 3);
}
#[test]
fn test_then() {
let mut e = Expectations::new();
e.expect::<i32, ()>("fren").called_once().with(gt(5));
e.then().expect::<i32, ()>("fren").called_once().with(lt(3));
e.was_called::<i32, ()>("fren", 10); // Matches first era, completing it
e.was_called::<i32, ()>("fren", 1); // Matches second era, completing it
}
#[test]
fn test_then_never() {
// Test to see that `called_never()` is only enforced until the era is completed
let mut e = Expectations::new();
e.expect::<(), ()>("bruh").called_never();
e.expect::<(), ()>("fren").called_once();
e.then().expect::<(), ()>("bruh").called_once();
e.was_called::<(), ()>("fren", ()); // Matches first era, completing it
e.was_called::<(), ()>("bruh", ()); // Matches second era, completing it
}
#[test]
#[should_panic]
fn test_then_partial_fail() {
let mut e = Expectations::new();
e.expect::<i32, ()>("fren").called_once().with(gt(5));
e.then().expect::<i32, ()>("fren").called_times(2).with(lt(3));
e.was_called::<i32, ()>("fren", 10); // Matches first era, completing it
e.was_called::<i32, ()>("fren", 1); // Matches second era, but still incomplete
// Panic: "fren" was expected to be called twice in the second era
}
#[test]
fn test_then_multi_call() {
let mut e = Expectations::new();
// These expectations can be called in any order
e.expect::<(), ()>("eh").called_once();
e.expect::<(), ()>("donk").called_times(2);
// These expectations are called afterwards in any order
e.then().expect::<(), ()>("calxx").called_times(3);
e.expect::<(), ()>("mer").called_once();
e.was_called::<(), ()>("donk", ());
e.was_called::<(), ()>("eh", ());
e.was_called::<(), ()>("donk", ()); // Completes first era
e.was_called::<(), ()>("calxx", ());
e.was_called::<(), ()>("mer", ());
e.was_called::<(), ()>("calxx", ());
e.was_called::<(), ()>("calxx", ()); // Completes second era
}
#[test]
#[should_panic]
fn test_then_wrong_order_fail() {
let mut e = Expectations::new();
// These expectations can be called in any order
e.expect::<(), ()>("eh").called_once();
e.expect::<(), ()>("donk").called_once();
// These expectations are called afterwards in any order
e.then().expect::<(), ()>("calxx").called_once();
e.expect::<(), ()>("mer").called_once();
e.was_called::<(), ()>("mer", ()); // No matching expectations
e.was_called::<(), ()>("calxx", ()); // No matching expectations
e.was_called::<(), ()>("donk", ());
e.was_called::<(), ()>("eh", ()); // Completes first era
// Panic: Second era was never completed
}
#[test]
fn test_then_specific() {
let mut e = Expectations::new();
e.expect::<(), ()>("eh").called_once();
e.then().expect::<(), ()>("donk").called_once();
e.then().expect::<(), ()>("mer").called_once();
e.expect::<(), ()>("eh").called_once();
e.was_called::<(), ()>("eh", ()); // Completes first era
e.was_called::<(), ()>("donk", ()); // Completes second era
e.was_called::<(), ()>("eh", ());
e.was_called::<(), ()>("mer", ()); // Completes third era
}
#[test]
fn test_empty_era_leading() {
let mut e = Expectations::new();
// Create an empty era at the start
e.then();
e.expect::<(), ()>("eh").called_once();
e.was_called::<(), ()>("eh", ()); // Completes first and second eras
}
#[test]
fn test_empty_era_middle() {
let mut e = Expectations::new();
// Create an empty era in the middle
e.expect::<(), ()>("eh").called_once();
e.then();
e.then();
e.expect::<(), ()>("eh").called_once();
e.was_called::<(), ()>("eh", ()); // Completes first and second eras
e.was_called::<(), ()>("eh", ()); // Completes third era
}
#[test]
fn test_empty_era_end() {
let mut e = Expectations::new();
e.expect::<(), ()>("eh").called_once();
// Create an empty era at the end
e.then();
e.was_called::<(), ()>("eh", ()); // Completes first and second eras
}
#[test]
fn test_one_expectation_per_era() {
let mut e = Expectations::new();
e.expect::<(), ()>("eh").called_never();
let result = panic::catch_unwind(
panic::AssertUnwindSafe(|| {
e.expect::<(), ()>("eh").called_never(); // Panic: only one expectation should be registered for "eh" in a given era
})
);
assert!(result.is_err());
}
#[test]
fn test_eras_complete_eagerly() {
let mut e = Expectations::new();
// Expectations
e.expect::<(), ()>("c").called_any();
e.then();
e.expect::<(), ()>("d").called_once();
// Calls
e.was_called::<(), ()>("c", ()); // Completes first era
e.was_called::<(), ()>("d", ()); // Completes second era
}
#[test]
fn test_calls_ignored_after_final_era_completes() {
let mut e = Expectations::new();
// Expectations
e.expect::<(), ()>("c").called_once();
e.then();
// Calls
e.was_called::<(), ()>("c", ()); // Completes first era
e.was_called::<(), ()>("c", ()); // Doesn't matter, all eras are complete already
}
}