murf_macros/lib.rs
1#![warn(
2 clippy::pedantic,
3 future_incompatible,
4 missing_debug_implementations,
5 missing_docs,
6 nonstandard_style,
7 rust_2018_idioms,
8 rust_2021_compatibility,
9 unused
10)]
11#![allow(
12 clippy::module_name_repetitions,
13 clippy::no_effect_underscore_binding,
14 clippy::similar_names
15)]
16#![doc = include_str!("../README.md")]
17#![cfg_attr(feature = "debug-to-file", feature(proc_macro_span))]
18
19use expect_call::CallMode;
20use proc_macro::TokenStream;
21
22mod expect_call;
23mod misc;
24mod mock;
25
26/// Macro to generate a mockable version of a type or trait.
27///
28/// # Example
29///
30/// The following example will generate a mocked version of `MyStruct` that
31/// implements the `Fuu` trait.
32///
33/// ```
34/// trait Fuu {
35/// fn fuu(&self) -> usize;
36/// }
37///
38/// mock! {
39/// #[derive(Default)]
40/// pub struct MyStruct;
41///
42/// impl Fuu for MyStruct {
43/// fn fuu(&self) -> usize;
44/// }
45/// }
46///
47/// let (handle, mock) = MyStruct::mock_with_handle();
48///
49/// expect_method_call!(handle as Fuu, fuu()).will_once(Return(1));
50///
51/// assert_eq!(1, mock.fuu());
52/// ```
53#[proc_macro]
54#[cfg(not(doctest))]
55pub fn mock(input: TokenStream) -> TokenStream {
56 mock::exec(input.into()).into()
57}
58
59/// Helper macro to define an call expectation of a specific function.
60///
61/// # Example
62///
63/// ```
64/// let (handle, mock) = MyStruct::mock_with_handle();
65///
66/// expect_call!(handle as Fuu, fuu(_)).will_once(Return(1));
67/// ```
68#[proc_macro]
69#[cfg(not(doctest))]
70pub fn expect_call(input: TokenStream) -> TokenStream {
71 expect_call::exec(input.into(), CallMode::Static).into()
72}
73
74/// Helper macro to define an call expectation of a specific method. Same as
75/// [`expect_call!`] but will automatically add a `any` matcher for the `self`
76/// argument.
77///
78/// # Example
79///
80/// ```
81/// let (handle, mock) = MyStruct::mock_with_handle();
82///
83/// expect_method_call!(handle as Fuu, fuu()).will_once(Return(1));
84/// ```
85#[proc_macro]
86#[cfg(not(doctest))]
87pub fn expect_method_call(input: TokenStream) -> TokenStream {
88 expect_call::exec(input.into(), CallMode::Method).into()
89}