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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
//! Mock any function types in Rust.
//!
//! Make sure to only use this crate for testing purposes, as it will add a lot of overhead to your code.
//! `.mock_once(..)` expects a closure that takes the arguments of the function and returns the same return type as the function.
//!
//! ## Basic Usage
//!
//! On the function you want to mock, add the `#[mock]` attribute.
//!
//! ```rust
//! #[cfg_attr(test, mockem::mock)]
//! fn foo() -> String {
//! format!("foo")
//! }
//!
//! fn bar() -> String {
//! format!("Hello, {}!", foo())
//! }
//!
//! #[test]
//! fn test_fn() {
//! use mockem::MockCall;
//!
//! foo.mock_once(|| "mockem".to_owned());
//! foo.mock_once(|| "mockem2".to_owned());
//!
//! assert_eq!(&bar(), "Hello, mockem!");
//! assert_eq!(&bar(), "Hello, mockem2!");
//!
//! // works normally after all mocks are used
//! assert_eq!(&bar(), "Hello, foo!");
//! }
//! ```
//!
//! ### Mocking Repeatedly
//!
//! If you want to mock a function more than once or indefinitely, use `mock_repeat` instead of `mock_once`.
//!
//! `mock_repeat` takes an `Option<usize>` as its first argument, which is the number of times to mock the function;
//!
//! `None` means to mock the function indefinitely.
//!
//!
//! ```rust
//! #[cfg_attr(test, mockem::mock)]
//! fn foo(a: &str) -> String {
//! format!("{a}")
//! }
//!
//! fn bar(a: &str) -> String {
//! format!("Hello, {}!", foo(a))
//! }
//!
//! #[test]
//! fn test_fn() {
//! use mockem::{MockCall, ClearMocks};
//!
//! foo.mock_repeat(None, |a| format!("mocked {a}"));
//!
//! assert_eq!(&bar("bar"), "Hello, mocked bar!");
//! assert_eq!(&bar("foo"), "Hello, mocked foo!");
//! assert_eq!(&bar("baz"), "Hello, mocked baz!");
//!
//! // this clears all mocks, which will stop the indefinite mock
//! foo.clear_mocks();
//!
//! assert_eq!(&bar("baz"), "Hello, baz!");
//! }
//! ```
//!
//!
//!
//! ## Impl Blocks
//!
//! If you want to mock impl methods, add the `#[mock]` attribute to the impl block.
//! Do the same for impl trait methods.
//!
//! This will mock all methods in the impl block.
//!
//! ```rust
//! struct Foo;
//!
//! #[cfg_attr(test, mockem::mock)]
//! impl Foo {
//! fn foo(&self) -> String {
//! format!("foo")
//! }
//! }
//!
//! trait Baz {
//! fn baz(&self) -> String;
//! }
//!
//! #[cfg_attr(test, mockem::mock)]
//! impl Baz for Foo {
//! fn baz(&self) -> String {
//! format!("baz")
//! }
//! }
//!
//! fn bar() -> String {
//! format!("Hello, {} and {}!", Foo.foo(), Foo.baz())
//! }
//!
//! #[test]
//! fn test_fn() {
//! use mockem::MockCall;
//!
//! Foo::foo.mock_once(|_| "mockem".to_owned());
//! Foo::baz.mock_once(|_| "mockem2".to_owned());
//!
//! assert_eq!(&bar(), "Hello, mockem and mockem2!");
//! }
//! ```
//!
//! ## Async Functions
//!
//! Async functions are also supported.
//!
//! ```rust
//! use async_trait::async_trait;
//!
//! struct Foo;
//!
//! #[cfg_attr(test, mockem::mock)]
//! impl Foo {
//! async fn foo(&self) -> String {
//! format!("foo")
//! }
//! }
//!
//! #[async_trait]
//! trait Baz {
//! async fn baz(&self) -> String;
//! }
//!
//! // also works with async_trait
//! // but you must place #[mock] above #[async_trait]
//! #[cfg_attr(test, mockem::mock)]
//! #[async_trait]
//! impl Baz for Foo {
//! async fn baz(&self) -> String {
//! format!("baz")
//! }
//! }
//!
//! async fn bar() -> String {
//! format!("Hello, {} and {}!", Foo.foo().await, Foo.baz().await)
//! }
//!
//! #[test]
//! fn test_fn() {
//! use mockem::MockCall;
//!
//! Foo::foo.mock_once(|_| "mockem".to_owned());
//! Foo::baz.mock_once(|_| "mockem2".to_owned());
//!
//! assert_eq!(&bar().await, "Hello, mockem and mockem2!");
//! }
//! ```
use std::{
any::{Any, TypeId},
future::Future,
marker::PhantomData,
mem::transmute,
rc::Rc,
};
mod store;
use store::MockStore;
pub use mockem_derive::mock;
thread_local! {
static MOCK_STORE: MockStore = MockStore::default()
}
/// Clear all mocks in the ThreadLocal; only necessary if tests share threads
pub fn clear_mocks() {
MOCK_STORE.with(|mock_store| mock_store.clear())
}
#[doc(hidden)]
pub struct MockReturn(Rc<Box<dyn FnMut() -> ()>>, Option<usize>);
/// Auto-implemented trait for mocking return values of functions.
///
/// Works for:
/// - functions/methods,
/// - async functions/methods,
/// - trait methods, and
/// - async_trait methods.
///
/// The trait is implemented for functions with up to 12 arguments.
pub trait MockCall<I, O, W, Fut>: CallMock<I, O, Fut> {
/// Mock the return value of this function.
/// This expects a closure with the arguments of the function.
fn mock_once(&self, with: W);
fn mock_repeat(&self, repeat: Option<usize>, with: W);
}
/// Clear all mocked return values related to this function.
/// You can use this if you have a recursive mock closure that continously mocks.
pub trait ClearMocks<I, O, Fut>: CallMock<I, O, Fut> {
fn clear_mocks(&self) {
let id = self.get_mock_id();
MOCK_STORE.with(|mock_store| mock_store.remove(id));
}
}
impl<I, O, Fut, F: CallMock<I, O, Fut>> ClearMocks<I, O, Fut> for F {}
#[doc(hidden)]
pub trait CallMock<I, O, Fut> {
fn mock_exists(&self, _ret: PhantomData<O>) -> bool {
let id = self.get_mock_id();
MOCK_STORE.with(|mock_store| mock_store.mock_exists(id))
}
fn call_mock(&self, input: I) -> O;
fn get_mock_id(&self) -> TypeId {
(|| ()).type_id()
}
}
#[doc(hidden)]
pub struct NotFuture;
impl<O, W: FnMut() -> O + 'static, F: Fn() -> O> MockCall<(), O, W, NotFuture> for F {
fn mock_once(&self, with: W) {
self.mock_repeat(Some(1), with)
}
fn mock_repeat(&self, repeat: Option<usize>, with: W) {
let erased: Box<dyn FnMut() -> O + 'static> = Box::new(with);
let transmuted: Rc<Box<dyn FnMut() -> ()>> = unsafe { transmute(Rc::new(erased)) };
MOCK_STORE.with(|mock_store| {
mock_store.add(self.get_mock_id(), MockReturn(transmuted, repeat));
});
}
}
impl<O, F: Fn() -> O> CallMock<(), O, NotFuture> for F {
fn call_mock(&self, _: ()) -> O {
let id = self.get_mock_id();
if let Some(MockReturn(with, repeat)) = MOCK_STORE.with(|mock_store| mock_store.get(id)) {
let with: Rc<Box<dyn FnMut() -> O + 'static>> = unsafe { transmute(with) };
let mut boxed = Rc::into_inner(with).expect("mock should exist");
let ret = boxed();
if let Some(repeat) = repeat {
if repeat > 1 {
let transmuted: Rc<Box<dyn FnMut() -> ()>> =
unsafe { transmute(Rc::new(boxed)) };
MOCK_STORE.with(|mock_store| {
mock_store
.add(self.get_mock_id(), MockReturn(transmuted, Some(repeat - 1)));
});
}
} else {
let transmuted: Rc<Box<dyn FnMut() -> ()>> = unsafe { transmute(Rc::new(boxed)) };
MOCK_STORE.with(|mock_store| {
mock_store.add(self.get_mock_id(), MockReturn(transmuted, None));
});
}
ret
} else {
panic!("mock should exist")
}
}
}
impl<O, W: FnMut() -> O + 'static, F: Fn() -> Fut + 'static, Fut: Future<Output = O>>
MockCall<(), O, W, Fut> for F
{
fn mock_once(&self, f: W) {
self.mock_repeat(Some(1), f)
}
fn mock_repeat(&self, repeat: Option<usize>, with: W) {
let erased: Box<dyn FnMut() -> O + 'static> = Box::new(with);
let transmuted: Rc<Box<dyn FnMut() -> ()>> = unsafe { transmute(Rc::new(erased)) };
MOCK_STORE.with(|mock_store| {
mock_store.add(
<Self as CallMock<(), O, Fut>>::get_mock_id(self),
MockReturn(transmuted, repeat),
);
});
}
}
impl<O, F: Fn() -> Fut, Fut: Future<Output = O>> CallMock<(), O, Fut> for F {
fn call_mock(&self, _: ()) -> O {
let id = <Self as CallMock<(), O, Fut>>::get_mock_id(self);
if let Some(MockReturn(with, repeat)) = MOCK_STORE.with(|mock_store| mock_store.get(id)) {
let with: Rc<Box<dyn FnMut() -> O + 'static>> = unsafe { transmute(with) };
let mut boxed = Rc::into_inner(with).expect("mock should exist");
let ret = boxed();
if let Some(repeat) = repeat {
if repeat > 1 {
let transmuted: Rc<Box<dyn FnMut() -> ()>> =
unsafe { transmute(Rc::new(boxed)) };
MOCK_STORE.with(|mock_store| {
mock_store.add(
<Self as CallMock<(), O, Fut>>::get_mock_id(self),
MockReturn(transmuted, Some(repeat - 1)),
);
});
}
} else {
let transmuted: Rc<Box<dyn FnMut() -> ()>> = unsafe { transmute(Rc::new(boxed)) };
MOCK_STORE.with(|mock_store| {
mock_store.add(
<Self as CallMock<(), O, Fut>>::get_mock_id(self),
MockReturn(transmuted, None),
);
});
}
ret
} else {
panic!("mock should exist")
}
}
}
#[rustfmt::skip]
macro_rules! all_the_tuples {
($name:ident) => {
$name!(T1);
$name!(T1, T2);
$name!(T1, T2, T3);
$name!(T1, T2, T3, T4);
$name!(T1, T2, T3, T4, T5);
$name!(T1, T2, T3, T4, T5, T6);
$name!(T1, T2, T3, T4, T5, T6, T7);
$name!(T1, T2, T3, T4, T5, T6, T7, T8);
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
$name!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
};
}
macro_rules! impl_mock_call {
($($T:ident),*) => {
impl<$($T),*, O, W: FnMut($($T),*) -> O + 'static, F: Fn($($T),*) -> O> MockCall<($($T,)*), O, W, NotFuture>
for F
{
fn mock_once(&self, f: W) {
self.mock_repeat(Some(1), f)
}
fn mock_repeat(&self, repeat: Option<usize>, with: W) {
let erased: Box<dyn FnMut($($T),*) -> O + 'static> = Box::new(with);
let transmuted: Rc<Box<dyn FnMut() -> ()>> = unsafe { transmute(Rc::new(erased)) };
MOCK_STORE.with(|mock_store| {
mock_store.add(
<Self as CallMock<($($T,)*), O, NotFuture>>::get_mock_id(self),
MockReturn(transmuted, repeat),
);
});
}
}
impl<$($T),*, O, F: Fn($($T),*) -> O> CallMock<($($T,)*), O, NotFuture>
for F
{
#[allow(non_snake_case)]
fn call_mock(&self, ($($T,)*): ($($T,)*)) -> O {
let id = <Self as CallMock<($($T,)*), O, NotFuture>>::get_mock_id(self);
if let Some(MockReturn(with, repeat)) = MOCK_STORE.with(|mock_store| mock_store.get(id)) {
let with: Rc<Box<dyn FnMut($($T),*) -> O + 'static>> = unsafe { transmute(with) };
let mut boxed = Rc::into_inner(with).expect("mock should exist");
let ret = boxed($($T),*);
if let Some(repeat) = repeat {
if repeat > 1 {
let transmuted: Rc<Box<dyn FnMut() -> ()>> =
unsafe { transmute(Rc::new(boxed)) };
MOCK_STORE.with(|mock_store| {
mock_store.add(
<Self as CallMock<($($T,)*), O, NotFuture>>::get_mock_id(self),
MockReturn(transmuted, Some(repeat - 1)),
);
});
}
} else {
let transmuted: Rc<Box<dyn FnMut() -> ()>> = unsafe { transmute(Rc::new(boxed)) };
MOCK_STORE.with(|mock_store| {
mock_store.add(
<Self as CallMock<($($T,)*), O, NotFuture>>::get_mock_id(self),
MockReturn(transmuted, None),
);
});
}
ret
} else {
panic!("mock should exist")
}
}
}
}
}
all_the_tuples!(impl_mock_call);
macro_rules! impl_mock_async_call {
($($T:ident),*) => {
impl<$($T),*, O, W: FnMut($($T),*) -> O + 'static, F: Fn($($T),*) -> Fut, Fut: Future<Output = O>> MockCall<($($T,)*), O, W, Fut>
for F
{
fn mock_once(&self, f: W) {
self.mock_repeat(Some(1), f)
}
fn mock_repeat(&self, repeat: Option<usize>, with: W) {
let erased: Box<dyn FnMut($($T),*) -> O + 'static> = Box::new(with);
let transmuted: Rc<Box<dyn FnMut() -> ()>> = unsafe { transmute(Rc::new(erased)) };
MOCK_STORE.with(|mock_store| {
mock_store.add(
<Self as CallMock<($($T,)*), O, Fut>>::get_mock_id(self),
MockReturn(transmuted, repeat),
);
});
}
}
impl<$($T),*, O, F: Fn($($T),*) -> Fut, Fut: Future<Output = O>> CallMock<($($T,)*), O, Fut>
for F
{
#[allow(non_snake_case)]
fn call_mock(&self, ($($T,)*): ($($T,)*)) -> O {
let id = <Self as CallMock<($($T,)*), O, Fut>>::get_mock_id(self);
if let Some(MockReturn(with, repeat)) = MOCK_STORE.with(|mock_store| mock_store.get(id)) {
let with: Rc<Box<dyn FnMut($($T),*) -> O + 'static>> = unsafe { transmute(with) };
let mut boxed = Rc::into_inner(with).expect("mock should exist");
let ret = boxed($($T),*);
if let Some(repeat) = repeat {
if repeat > 1 {
let transmuted: Rc<Box<dyn FnMut() -> ()>> =
unsafe { transmute(Rc::new(boxed)) };
MOCK_STORE.with(|mock_store| {
mock_store.add(
<Self as CallMock<($($T,)*), O, Fut>>::get_mock_id(self),
MockReturn(transmuted, Some(repeat - 1)),
);
});
}
} else {
let transmuted: Rc<Box<dyn FnMut() -> ()>> = unsafe { transmute(Rc::new(boxed)) };
MOCK_STORE.with(|mock_store| {
mock_store.add(
<Self as CallMock<($($T,)*), O, Fut>>::get_mock_id(self),
MockReturn(transmuted, None),
);
});
}
ret
} else {
panic!("mock should exist")
}
}
}
}
}
all_the_tuples!(impl_mock_async_call);