rvtest 0.3.1

A Next Level Testing Library for Rust — BDD specs, property-based testing, parametrized tests, rich reporting, and code coverage. Just a library, not a framework.
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
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Lightweight mocking utilities — spy, stub, and scoped function patching.
//!
//! No proc-macro required for basic use cases.  Integrates with rvtest's
//! assertion macros for structured failure messages.
//!
//! # Examples
//!
//! ```ignore
//! use rvtest::mock::*;
//!
//! // Spy — record every call
//! let spy = Spy::new(|x: i32| x * 2);
//! assert_eq!(spy.call(5), 10);
//! assert_eq!(spy.call_count(), 1);
//! assert_eq!(spy.calls(), &[5]);
//!
//! // Stub — return a fixed value
//! let stub = Stub::new(42);
//! assert_eq!(stub.call(()), 42);
//!
//! // Scoped patch (requires a static PATTERN)
//! // let guard = patch!(MY_FN, |x| x + 1);
//! // function_under_test();
//! // drop(guard); // restores original
//! ```

use std::fmt::Debug;
use std::sync::Mutex;
use std::sync::Arc;

// ---------------------------------------------------------------------------
// Spy — call-recording wrapper
// ---------------------------------------------------------------------------

/// A call-recording wrapper around a function.
///
/// `Spy` wraps any `Fn(A) -> R` closure, delegates every call to the inner
/// function, and records the arguments for later inspection.
///
/// Use `spy.call_count()`, `spy.calls()`, `spy.assert_called_with()` to
/// verify invocation history.
pub struct Spy<A, R> {
    calls: Arc<Mutex<Vec<A>>>,
    inner: Arc<dyn Fn(A) -> R + Send + Sync>,
}

impl<A, R> Spy<A, R>
where
    A: Clone + Debug + PartialEq + Send + 'static,
    R: Send + 'static,
{
    /// Create a new spy wrapping the given function.
    pub fn new<F>(f: F) -> Self
    where
        F: Fn(A) -> R + Send + Sync + 'static,
    {
        Spy {
            calls: Arc::new(Mutex::new(Vec::new())),
            inner: Arc::new(f),
        }
    }

    /// Call the wrapped function and record the argument.
    pub fn call(&self, arg: A) -> R {
        self.calls.lock().unwrap().push(arg.clone());
        (self.inner)(arg)
    }

    /// Number of times this spy has been called.
    pub fn call_count(&self) -> usize {
        self.calls.lock().unwrap().len()
    }

    /// Return a clone of all recorded arguments.
    pub fn calls(&self) -> Vec<A> {
        self.calls.lock().unwrap().clone()
    }

    /// Panic with a detailed message if the spy was never called.
    pub fn assert_called(&self) {
        let count = self.call_count();
        if count == 0 {
            panic!(
                "\n\x1b[2mspy assertion failed\x1b[0m\n  \x1b[31;1mexpected\x1b[0m spy to be called at least once\n  \x1b[33mactual\x1b[0m:   never called"
            );
        }
    }

    /// Panic with a structured diff if the recorded calls don't match
    /// the expected sequence.
    pub fn assert_called_with(&self, expected: &[A]) {
        let actual = self.calls();
        if actual.len() != expected.len() || actual.iter().zip(expected).any(|(a, e)| a != e) {
            let msg = format!(
                "\n\x1b[2mspy assertion failed\x1b[0m\n  \x1b[31;1mexpected calls\x1b[0m: {:?}\n  \x1b[33mactual calls\x1b[0m:   {:?}\n  \x1b[36mhint\x1b[0m:          use `rvtest::assert_eq!` for structured diff",
                expected, actual
            );
            panic!("{}", msg);
        }
    }

    /// Clear all recorded calls.
    pub fn reset(&self) {
        self.calls.lock().unwrap().clear();
    }
}

impl<A: Debug, R> Clone for Spy<A, R> {
    fn clone(&self) -> Self {
        Spy {
            calls: Arc::clone(&self.calls),
            inner: Arc::clone(&self.inner),
        }
    }
}

// ---------------------------------------------------------------------------
// Stub — fixed-return mock
// ---------------------------------------------------------------------------

/// A mock that ignores its input and returns a fixed value.
///
/// Useful when you only need a dependency to compile and return a known
/// value, without caring about how many times or with what arguments it
/// is called.
pub struct Stub<A, R> {
    value: R,
    _phantom: std::marker::PhantomData<A>,
}

impl<A, R: Clone> Stub<A, R> {
    /// Create a stub that always returns `value`.
    pub fn new(value: R) -> Self {
        Stub {
            value,
            _phantom: std::marker::PhantomData,
        }
    }

    /// Call the stub — ignores the input and returns the fixed value.
    pub fn call(&self, _arg: A) -> R {
        self.value.clone()
    }
}

impl<A, R: Clone> Clone for Stub<A, R> {
    fn clone(&self) -> Self {
        Stub::new(self.value.clone())
    }
}

// ---------------------------------------------------------------------------
// Scoped function patching via static dispatch
// ---------------------------------------------------------------------------

/// Temporarily replace a `static Mutex<Option<...>>` function pointer.
///
/// The macro creates a guard that, when dropped, restores the original
/// function.  This allows you to mock global functions for the duration
/// of a single test.
///
/// # Usage
///
/// ```ignore
/// use rvtest::mock::{make_patchable, patch};
///
/// // 1. Declare a patchable function pointer (usually inside the module
/// //    that owns the function):
/// make_patchable!(MY_HELPER, |x: i32| -> i32 { x * 2 });
///
/// // 2. In your real code, call it via the function pointer:
/// fn double(x: i32) -> i32 {
///     MY_HELPER.call(x)
/// }
///
/// // 3. In tests, replace it temporarily:
/// #[test]
/// fn test_double() {
///     let _guard = patch!(MY_HELPER, |x| x * 100);
///     assert_eq!(double(5), 500);
///     // guard dropped → original restored
/// }
/// ```
#[macro_export]
macro_rules! make_patchable {
    ($name:ident, |$arg:ident: $arg_ty:ty| -> $ret_ty:ty $body:block) => {
        #[allow(non_upper_case_globals, missing_docs)]
        static $name: $crate::mock::PatchableFn<$arg_ty, $ret_ty> = {
            fn __default($arg: $arg_ty) -> $ret_ty $body
            $crate::mock::PatchableFn::new(__default)
        };
    };
}

/// A static function pointer that can be temporarily replaced for testing.
///
/// Created via [`make_patchable!`].  Not intended for direct construction.
pub struct PatchableFn<A, R> {
    current: Mutex<Option<Arc<dyn Fn(A) -> R + Send + Sync>>>,
    default: fn(A) -> R,
}

impl<A, R> PatchableFn<A, R>
where
    A: Send + 'static,
    R: Send + 'static,
{
    /// Create a new patchable function with a default implementation.
    ///
    /// The default MUST be a function pointer (not a closure) so it can
    /// be stored in a static without allocation.
    pub const fn new(f: fn(A) -> R) -> Self {
        PatchableFn {
            current: Mutex::new(None),
            default: f,
        }
    }

    /// Call the function — invokes the currently active implementation
    /// (default or patched).
    pub fn call(&self, arg: A) -> R {
        let guard = self.current.lock().unwrap();
        match guard.as_ref() {
            Some(f) => f(arg),
            None => (self.default)(arg),
        }
    }

    /// Temporarily replace the implementation.  Returns a guard that
    /// restores the previous value when dropped.
    pub fn patch<F>(&self, f: F) -> PatchGuard<'_, A, R>
    where
        F: Fn(A) -> R + Send + Sync + 'static,
    {
        let mut guard = self.current.lock().unwrap();
        let prev = guard.take();
        *guard = Some(Arc::new(f));
        PatchGuard { target: self, prev }
    }
}

/// Restores the previous function when dropped.
pub struct PatchGuard<'a, A, R> {
    target: &'a PatchableFn<A, R>,
    prev: Option<Arc<dyn Fn(A) -> R + Send + Sync>>,
}

impl<A, R> Drop for PatchGuard<'_, A, R> {
    fn drop(&mut self) {
        *self.target.current.lock().unwrap() = self.prev.take();
    }
}

/// Create a scoped patch for a [`PatchableFn`].
///
/// See [`make_patchable!`] for a complete usage example.
#[macro_export]
macro_rules! patch {
    ($target:ident, $replacement:expr) => {
        $target.patch($replacement)
    };
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // --- Spy ---

    #[test]
    fn spy_records_calls() {
        let spy = Spy::new(|x: i32| x * 2);
        assert_eq!(spy.call(5), 10);
        assert_eq!(spy.call_count(), 1);
        assert_eq!(spy.calls(), &[5]);
    }

    #[test]
    fn spy_records_multiple_calls() {
        let spy = Spy::new(|x: i32| x + 1);
        spy.call(1);
        spy.call(2);
        spy.call(3);
        assert_eq!(spy.call_count(), 3);
        assert_eq!(spy.calls(), &[1, 2, 3]);
    }

    #[test]
    fn spy_assert_called_panics_when_not_called() {
        let spy = Spy::new(|x: i32| x);
        let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            spy.assert_called();
        }));
        assert!(r.is_err());
    }

    #[test]
    fn spy_assert_called_ok_when_called() {
        let spy = Spy::new(|x: i32| x);
        spy.call(1);
        spy.assert_called(); // should not panic
    }

    #[test]
    fn spy_assert_called_with_matches() {
        let spy = Spy::new(|x: i32| x);
        spy.call(1);
        spy.call(2);
        spy.assert_called_with(&[1, 2]);
    }

    #[test]
    fn spy_assert_called_with_panics_on_mismatch() {
        let spy = Spy::new(|x: i32| x);
        spy.call(1);
        let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            spy.assert_called_with(&[99]);
        }));
        assert!(r.is_err());
    }

    #[test]
    fn spy_reset_clears_calls() {
        let spy = Spy::new(|x: i32| x);
        spy.call(1);
        spy.call(2);
        assert_eq!(spy.call_count(), 2);
        spy.reset();
        assert_eq!(spy.call_count(), 0);
    }

    #[test]
    fn spy_clone_shares_call_history() {
        let spy = Spy::new(|x: i32| x);
        let spy2 = spy.clone();
        spy.call(1);
        assert_eq!(spy2.call_count(), 1);
    }

    #[test]
    fn spy_works_with_strings() {
        let spy = Spy::new(|s: String| s.len());
        assert_eq!(spy.call("hello".into()), 5);
        assert_eq!(spy.call("world".into()), 5);
        assert_eq!(spy.call_count(), 2);
    }

    // --- Stub ---

    #[test]
    fn stub_returns_fixed_value() {
        let stub = Stub::new(42);
        assert_eq!(stub.call("anything"), 42);
        assert_eq!(stub.call("also anything"), 42);
    }

    #[test]
    fn stub_works_with_different_types() {
        let stub_int: Stub<i32, &str> = Stub::new("fixed");
        assert_eq!(stub_int.call(1), "fixed");
        let stub_unit: Stub<(), &str> = Stub::new("fixed");
        assert_eq!(stub_unit.call(()), "fixed");
    }

    #[test]
    fn stub_clone() {
        let stub = Stub::new(99);
        let stub2 = stub.clone();
        assert_eq!(stub2.call(0), 99);
    }

    // --- PatchableFn ---

    make_patchable!(TEST_FN, |x: i32| -> i32 { x * 2 });

    #[test]
    fn patchable_default() {
        assert_eq!(TEST_FN.call(5), 10);
    }

    #[test]
    fn patch_temporarily_replaces() {
        let guard = patch!(TEST_FN, |x| x * 100);
        assert_eq!(TEST_FN.call(5), 500);
        drop(guard);
        assert_eq!(TEST_FN.call(5), 10);
    }

    #[test]
    fn patch_restores_on_drop() {
        {
            let _guard = patch!(TEST_FN, |_| -1);
            assert_eq!(TEST_FN.call(0), -1);
        }
        assert_eq!(TEST_FN.call(0), 0);
    }

    #[test]
    fn patch_works_multiple_times() {
        let g1 = patch!(TEST_FN, |x| x + 1);
        assert_eq!(TEST_FN.call(5), 6);
        let g2 = patch!(TEST_FN, |x| x + 10);
        assert_eq!(TEST_FN.call(5), 15);
        drop(g2);
        assert_eq!(TEST_FN.call(5), 6);
        drop(g1);
        assert_eq!(TEST_FN.call(5), 10);
    }

    // --- Integration with rvtest's describe/it ---

    #[test]
    fn spy_inside_describe() {
        describe("Mock inside describe")
            .it("spy works", || {
                let spy = Spy::new(|x: i32| x * 2);
                assert_eq!(spy.call(3), 6);
                spy.assert_called_with(&[3]);
            })
            .it("stub works", || {
                let stub = Stub::new(true);
                assert!(stub.call(()));
            })
            .run()
            .assert_all_pass();
    }

    use crate::spec::describe;
}