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
#![warn(missing_docs)]
#![warn(rustdoc::missing_doc_code_examples)]
#![warn(missing_debug_implementations)]
#![warn(rust_2018_idioms)]

//! ...

use std::fmt::{Debug, Formatter};
use std::sync::{Arc, Mutex};

/// ...
pub fn pair<T>() -> (Verifier<T>, Caller<T>) {
    let calls = Arc::new(Mutex::new(Some(Vec::new())));

    let matcher = Verifier {
        calls: calls.clone(),
    };

    let spy = Caller { calls };

    (matcher, spy)
}

/// ...
pub struct Caller<T> {
    calls: Arc<Mutex<Option<Vec<T>>>>,
}

impl<T> Caller<T> {
    /// ...
    pub fn call(&self, value: T) {
        let mut guard = self.calls.lock().unwrap();

        match guard.as_mut() {
            Some(calls) => calls.push(value),
            None => panic!("verify_call received a call after the verifier was consumed"),
        }
    }
}

impl<T: Debug> Debug for Caller<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Caller")
            .field("calls", &self.calls)
            .finish()
    }
}

/// ...
pub struct Verifier<T> {
    calls: Arc<Mutex<Option<Vec<T>>>>,
}

impl<T> Verifier<T> {
    /// ...
    pub fn calls(self) -> Vec<T> {
        let mut guard = self.calls.lock().unwrap();
        guard.take().unwrap()
    }
}

impl<T: Debug> Debug for Verifier<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Verifier")
            .field("calls", &self.calls)
            .finish()
    }
}

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

    #[test]
    fn should_pass_verifying_never_called() {
        // Given
        let (verifier, _caller) = pair::<()>();

        // When
        let calls = verifier.calls();

        // Then
        assert_eq!(calls.len(), 0);
    }

    #[test]
    fn should_pass_verifying_calls() {
        // Given
        let (verifier, caller) = pair();
        caller.call(1);
        caller.call(2);
        caller.call(3);

        // When
        let calls = verifier.calls();

        // Then
        assert_eq!(calls, &[1, 2, 3]);
    }

    #[test]
    #[should_panic(expected = "verify_call received a call after the verifier was consumed")]
    fn should_panic_when_call_after_consuming_verifier() {
        // Given
        let (verifier, caller) = pair();
        let _calls = verifier.calls();

        // When
        caller.call(3);
    }

    #[test]
    fn should_be_thread_safe() {
        // Given
        let (verifier, caller) = pair();
        let handle = std::thread::spawn(move || {
            caller.call(1);
            caller.call(2);
            caller.call(3);
        });

        // When
        handle.join().unwrap();
        let calls = verifier.calls();

        // Then
        assert_eq!(calls, &[1, 2, 3]);
    }

    #[test]
    fn should_implement_traits() {
        use impls::impls;
        use std::fmt::Debug;

        assert!(impls!(Caller<i32>: Debug & Send & Sync & !Clone));
        assert!(impls!(Verifier<i32>: Debug & Send & Sync & !Clone));

        struct NotDebug;
        assert!(impls!(Caller<NotDebug>: !Debug & Send & Sync & !Clone));
        assert!(impls!(Verifier<NotDebug>: !Debug & Send & Sync & !Clone));
    }
}