spy 0.2.0

Rust spy functions for testing purposes
Documentation
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Spy crate is inspired by such famous Javascript testing tools
//! as Jasmine and Sinon.js. It provides easy configurable spies
//! with predefined behaviour.
//!
//! # Spy without arguments
//!
//! `spy!()` creates a pair of a spy function that receives no arguments
//! and does nothing and corresponded `Spy` object.
//!
//! ```rust
//! #[macro_use]
//! extern crate spy;
//!
//! use spy::*;
//!
//! fn main() {
//!     let (spy_fn, spy) = spy!();
//!
//!     assert_eq!(spy_fn(), ());
//!     assert_eq!(spy_fn(), ());
//!
//!     // take a snapshot
//!     let snapshot = spy.snapshot();
//!
//!     // make assertions
//!     assert!(
//!         snapshot.called_with(()),
//!         "should be called with () at least once"
//!     );
//!     assert!(
//!         snapshot.each_called_with(()),
//!         "should be called with () each time"
//!     );
//!     assert_eq!(snapshot.num_of_calls(), 2, "should be called 2 times");
//!     assert_eq!(snapshot.all_calls(), &vec![(), ()]);
//!     assert_eq!(snapshot.first_call().expect("should be Some"), &());
//!     assert_eq!(snapshot.last_call().expect("should be Some"), &());
//!     assert_eq!(snapshot.nth_call(1).expect("should be Some"), &());
//! }
//!
//! ```
//!
//! # Spy with arguments
//!
//! If a spy function should receive arguments it can be created in one
//! of following ways:
//!
//! * `spy!(|n, m|)` creates a spy function that can receive two arguments.
//! Any number of arguments could be passed inside `||`.
//! Types of these arguments will be inferred from a spy function usage.
//! This function does nothing and returns `()`.
//!
//! * `spy!(|n, m| a + b)` creates a spy function that can receive two
//! arguments followed by a function body but any number of arguments could be requested.
//! This syntax is pretty much the same as Rust closures. Types of arguments
//! and an output will be inferred from a spy function usage.
//!
//! * `spy!(|n: u8, m: u8| -> u8 { return n + m; })` creates spy function
//! with type annotations both for arguments and for an output.
//!

use std::sync::mpsc::Receiver;

/// Spy object that tracks calls of associated spy function.
pub struct Spy<Args> {
    pub calls_recv: Receiver<Args>,
}

impl<Args: PartialEq> Spy<Args> {
    /// It takes a snapshot of a spy object. Taken snapshot
    /// contains all the calls starting from previous snapshot.
    pub fn snapshot(&self) -> SpySnapshot<Args> {
        let mut calls: Vec<Args> = vec![];
        loop {
            match self
                .calls_recv
                .recv_timeout(::std::time::Duration::from_millis(0))
            {
                Ok(args) => calls.push(args),
                Err(_) => {
                    break;
                }
            }
        }
        SpySnapshot { calls }
    }
}

/// The structure represents a snapshot of a spy object. Taken snapshot
/// contains all the calls starting from the moment when `Spy` object was created
/// or previous snapshot if there was taken one.
///
/// Generic type `Args` is in fact a tuple that describes argument values for each
/// call.
pub struct SpySnapshot<Args: PartialEq> {
    calls: Vec<Args>,
}

impl<Args: PartialEq> SpySnapshot<Args> {
    /// It returns `true` if spy function was called at least once and `false` otherwise.
    pub fn called(&self) -> bool {
        !self.calls.is_empty()
    }

    /// It returns `true` if spy function was called at least once
    /// with provided arguments and `false` otherwise.
    pub fn called_with(&self, args: Args) -> bool {
        for call in &self.calls {
            if &args == call {
                return true;
            }
        }
        false
    }

    /// It returns `true` if spy function was called with provided arguments each time
    /// it was called.
    pub fn each_called_with(&self, args: Args) -> bool {
        for call in &self.calls {
            if &args != call {
                return false;
            }
        }
        true
    }

    /// It returns how many times spy function was called.
    pub fn num_of_calls(&self) -> usize {
        self.calls.len()
    }

    /// It returns a vector that contains all calls arguments.
    pub fn all_calls(&self) -> &Vec<Args> {
        &self.calls
    }

    /// It returns first call arguments. `None` will be returned if spy function was not
    /// called till the moment when a snapshot was taken.
    pub fn first_call(&self) -> Option<&Args> {
        self.calls.first()
    }

    /// It returns last call arguments. `None` will be returned if spy function was not
    /// called till the moment when a snapshot was taken.
    pub fn last_call(&self) -> Option<&Args> {
        self.calls.last()
    }

    /// It returns n-th call arguments. `None` will be returned if spy function was not
    /// called enough times till the moment when a snapshot was taken.
    pub fn nth_call(&self, n: usize) -> Option<&Args> {
        self.calls.get(n)
    }
}

/// The macro that creates a pair of a `Spy` object and a spy function tracked by returned
/// spy.
///
/// ```rust
/// use spy::{spy, Spy};
///
/// /// spy function that takes no argument and returns no value
/// let (spy_fn, spy) = spy!();
///
/// spy_fn();
///
/// /// spy function that takes one argument argument and returns no value.
/// let (spy_fn, spy) = spy!(|n|);
///
/// spy_fn(42);
///
/// /// spy function that takes one argument argument and returns some value
/// /// evaluated basing on taken argument.
/// let (spy_fn, spy) = spy!(|n| n % 2 == 0);
///
/// spy_fn(42);
/// ```
#[macro_export]
macro_rules! spy {
    () => {
        {
            let (s, recv) = ::std::sync::mpsc::channel();
            let spy_fn = move || {
                s.send(())
                    .expect("Problem with call agregation: cannot send call arguments to channel");
            };
            (spy_fn, Spy{ calls_recv: recv })
        }
    };
    (|$arg:ident|) => {
        {
            let (s, recv) = ::std::sync::mpsc::channel();
            let spy_fn = move |$arg| {
                s.send($arg)
                    .expect("Problem with call agregation: cannot send call arguments to channel");
            };
            (spy_fn, Spy{ calls_recv: recv })
        }
    };
    (|$($arg:ident),*|) => {
        {
            let (s, recv) = ::std::sync::mpsc::channel();
            let spy_fn = move |$($arg),*| {
                let args_tuple = ($($arg,) *);
                s.send(args_tuple)
                    .expect("Problem with call agregation: cannot send call arguments to channel");
            };
            (spy_fn, Spy{ calls_recv: recv })
        }
    };
    (|$arg:ident| $expression:expr) => {
        {
            let (s, recv) = ::std::sync::mpsc::channel();

            let spy_fn = move |$arg| {
                s.send($arg)
                    .expect("Problem with call agregation: cannot send call arguments to channel");
                $expression
            };
            (spy_fn, Spy{ calls_recv: recv })
        }
    };
    (|$($arg:ident),*| $expression:expr) => {
        {
            let (s, recv) = ::std::sync::mpsc::channel();

            let spy_fn = move |$($arg),*| {
                let args_tuple = ($($arg,) *);
                s.send(args_tuple)
                    .expect("Problem with call agregation: cannot send call arguments to channel");
                $expression
            };
            (spy_fn, Spy{ calls_recv: recv })
        }
    };
    (|$($arg:ident : $arg_type:ty),*| $(-> $return_type:ty)* $expression:block) => {
        {
            let (s, recv) = ::std::sync::mpsc::channel();

            let spy_fn = move |$($arg : $arg_type),*| $(-> $return_type)* {
                let args_tuple = ($($arg,) *);
                s.send(args_tuple)
                    .expect("Problem with call agregation: cannot send call arguments to channel");
                $expression
            };
            (spy_fn, Spy{ calls_recv: recv })
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    fn no_args_cb<F>(cb: F)
    where
        F: Fn() + Sized,
    {
        // 3 times call
        cb();
        cb();
        cb();
    }

    #[test]
    fn create_spy_test() {
        let (spy_fn, spy) = spy!();

        no_args_cb(spy_fn);
        let snapshot = spy.snapshot();

        assert!(snapshot.called(), "should be called");
        assert!(
            snapshot.called_with(()),
            "should be called with () at least once"
        );
        assert!(
            snapshot.each_called_with(()),
            "should be called with () each time"
        );
        assert_eq!(snapshot.num_of_calls(), 3, "should be called 3 times");
        assert_eq!(snapshot.all_calls(), &vec![(), (), ()]);
        assert_eq!(snapshot.first_call().expect("should be Some"), &());
        assert_eq!(snapshot.last_call().expect("should be Some"), &());
        assert_eq!(snapshot.nth_call(1).expect("should be Some"), &());
    }

    #[test]
    fn create_spy_with_args_test() {
        let (spy_fn, spy) = spy!(|n|);

        spy_fn(1u8);
        spy_fn(2u8);

        let snapshot = spy.snapshot();

        assert!(snapshot.called(), "should be called");

        assert!(
            snapshot.called_with(1u8),
            "should be called with &1u8 at least once"
        );
        assert!(
            !snapshot.each_called_with(1u8),
            "should be called with different arguments"
        );
        assert_eq!(snapshot.num_of_calls(), 2, "should be called 3 times");
        assert_eq!(snapshot.all_calls(), &vec![1u8, 2u8]);
        assert_eq!(snapshot.first_call().expect("should be Some"), &1u8);
        assert_eq!(snapshot.last_call().expect("should be Some"), &2u8);
        assert_eq!(snapshot.nth_call(1).expect("should be Some"), &2u8);
    }

    #[test]
    fn create_spy_with_many_args_test() {
        let (spy_fn, spy) = spy!(|n, s|);

        spy_fn(1u8, "first");
        spy_fn(2u8, "second");

        let snapshot = spy.snapshot();

        assert!(snapshot.called(), "should be called");

        assert!(
            snapshot.called_with((1u8, "first")),
            "should be called with &1u8 at least once"
        );
        assert!(
            !snapshot.each_called_with((1u8, "first")),
            "should be called with different arguments"
        );
        assert_eq!(snapshot.num_of_calls(), 2, "should be called 3 times");
        assert_eq!(snapshot.all_calls(), &vec![(1u8, "first"), (2u8, "second")]);
        assert_eq!(
            snapshot.first_call().expect("should be Some"),
            &(1u8, "first")
        );
        assert_eq!(
            snapshot.last_call().expect("should be Some"),
            &(2u8, "second")
        );
        assert_eq!(
            snapshot.nth_call(1).expect("should be Some"),
            &(2u8, "second")
        );
    }

    #[test]
    fn create_spy_with_args_and_return_test() {
        let (spy_fn, spy) = spy!(|n| n % 2 == 0);

        assert!(spy_fn(2), "should return true for an even number");
        assert!(!spy_fn(3), "should return false for an odd number");

        let snapshot = spy.snapshot();

        assert!(snapshot.called(), "should be called");
        assert!(
            snapshot.called_with(2),
            "should be called with 2 at least once"
        );
        assert!(
            !snapshot.each_called_with(2),
            "should be called with different arguments"
        );
        assert_eq!(snapshot.all_calls(), &vec![2, 3]);
        assert_eq!(snapshot.first_call().expect("should be Some"), &2);
        assert_eq!(snapshot.last_call().expect("should be Some"), &3);
        assert_eq!(snapshot.nth_call(1).expect("should be Some"), &3);
    }

    #[test]
    fn create_spy_with_many_args_and_return_test() {
        let (spy_fn, spy) = spy!(|n, m| n + m);

        assert_eq!(spy_fn(2u8, 1u8), 3u8, "should return 3");
        assert_eq!(spy_fn(3u8, 1u8), 4u8, "should return 4");

        let snapshot = spy.snapshot();

        assert!(snapshot.called(), "should be called");
        assert!(
            snapshot.called_with((2u8, 1u8)),
            "should be called with 2 at least once"
        );
        assert!(
            !snapshot.each_called_with((2u8, 1u8)),
            "should be called with different arguments"
        );
        assert_eq!(snapshot.all_calls(), &vec![(2, 1), (3, 1)]);
        assert_eq!(snapshot.first_call().expect("should be Some"), &(2, 1));
        assert_eq!(snapshot.last_call().expect("should be Some"), &(3, 1));
        assert_eq!(snapshot.nth_call(1).expect("should be Some"), &(3, 1));
    }

    #[test]
    fn create_spy_type_annotations() {
        let (spy_fn, spy) = spy!(|n: u8, m: u8| -> u8 { n + m });

        assert_eq!(spy_fn(2, 1), 3, "should return 3");
        assert_eq!(spy_fn(3, 1), 4, "should return 4");

        let snapshot = spy.snapshot();

        assert!(snapshot.called(), "should be called");
        assert!(
            snapshot.called_with((2, 1)),
            "should be called with 2 at least once"
        );
        assert!(
            !snapshot.each_called_with((2, 1)),
            "should be called with different arguments"
        );
        assert_eq!(snapshot.all_calls(), &vec![(2, 1), (3, 1)]);
        assert_eq!(snapshot.first_call().expect("should be Some"), &(2, 1));
        assert_eq!(snapshot.last_call().expect("should be Some"), &(3, 1));
        assert_eq!(snapshot.nth_call(1).expect("should be Some"), &(3, 1));
    }
}