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
#![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 for Caller<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Caller").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 for Verifier<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Verifier").finish()
    }
}

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

    mod caller {
        use super::*;

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

            // Given
            struct NotDebug;

            // Then
            assert!(impls!(Caller<i32>: Debug & Send & Sync & !Clone));
            assert!(impls!(Caller<NotDebug>: Debug & Send & Sync & !Clone));
        }

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

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

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

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

            // When
            caller.call(3);
        }
    }

    mod verifier {
        use super::*;

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

            // Given
            struct NotDebug;

            // Then
            assert!(impls!(Verifier<i32>: Debug & Send & Sync & !Clone));
            assert!(impls!(Verifier<NotDebug>: Debug & Send & Sync & !Clone));
        }

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

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

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

        #[test]
        fn receives_calls_from_caller() {
            // 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]);
        }
    }
}