injectorpp 0.5.1

Injectorpp is a powerful tool designed to facilitate the writing of unit tests without the need to introduce traits solely for testing purposes. It streamlines the testing process by providing a seamless and efficient way to abstract dependencies, ensuring that your code remains clean and maintainable.
Documentation
use std::sync::atomic::{AtomicUsize, Ordering};

// Define a verifier guard that checks the counter on Drop.
/// A verifier type that holds a reference to an atomic counter and the expected call count.
pub enum CallCountVerifier {
    /// A real verifier that checks if the fake function was called the expected number of times.
    WithCount {
        counter: &'static AtomicUsize,
        expected: usize,
    },

    /// A dummy verifier that performs no check.
    Dummy,
}

impl Drop for CallCountVerifier {
    fn drop(&mut self) {
        if let CallCountVerifier::WithCount { counter, expected } = self {
            let call_times = counter.load(Ordering::SeqCst);
            if call_times != *expected {
                // Avoid double panic
                if std::thread::panicking() {
                    return;
                }

                panic!(
                    "Fake function was expected to be called {expected} time(s), but it is actually called {call_times} time(s)"
                );
            }
        }

        // Dummy variant does nothing on drop.
    }
}