From f73c3218b9a004fb405386ce2623d2dfa077388c Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 19 Mar 2023 18:06:28 +0100 Subject: fix: allow mocking methods taking &mut self --- macros/src/expectation.rs | 36 ++---------------------------------- macros/src/mock.rs | 2 +- 2 files changed, 3 insertions(+), 35 deletions(-) (limited to 'macros') diff --git a/macros/src/expectation.rs b/macros/src/expectation.rs index 436d571..5a4922c 100644 --- a/macros/src/expectation.rs +++ b/macros/src/expectation.rs @@ -16,9 +16,6 @@ use syn::{ ImplItemMethod, ItemStruct, Lifetime, - Pat, - PatIdent, - PatType, Path, PathSegment, Receiver, @@ -268,35 +265,6 @@ impl ToTokens for Expectation self.return_type.clone(), )); - let args = opt_self_type - .iter() - .chain(self.arg_types.iter()) - .enumerate() - .map(|(index, ty)| { - FnArg::Typed(PatType { - attrs: vec![], - pat: Box::new(Pat::Ident(PatIdent { - attrs: vec![], - by_ref: None, - mutability: None, - ident: format_ident!("arg_{index}"), - subpat: None, - })), - colon_token: ::default(), - ty: Box::new(ty.clone()), - }) - }) - .collect::>(); - - let arg_idents = opt_self_type - .iter() - .chain(self.arg_types.iter()) - .enumerate() - .map(|(index, _)| format_ident!("arg_{index}")) - .collect::>(); - - let return_type = &self.return_type; - let method_ident = &self.method_ident; let expectation_struct = Self::create_struct( @@ -358,7 +326,7 @@ impl ToTokens for Expectation unsafe { std::mem::transmute(self) } } - fn call_returning(&self, #(#args),*) #return_type + fn get_returning(&self) -> &#returning_fn { let Some(returning) = &self.returning else { panic!(concat!( @@ -398,7 +366,7 @@ impl ToTokens for Expectation self.call_cnt.fetch_add(1, std::sync::atomic::Ordering::Relaxed); - (returning)(#(#arg_idents),*) + returning } } diff --git a/macros/src/mock.rs b/macros/src/mock.rs index d2eb451..2ac27e5 100644 --- a/macros/src/mock.rs +++ b/macros/src/mock.rs @@ -247,7 +247,7 @@ fn create_mock_function( )) .with_generic_params::<#(#type_param_idents,)*>(); - expectation.call_returning(#(#args),*) + (expectation.get_returning())(#(#args),*) } }) .unwrap_or_abort(), -- cgit v1.2.3-18-g5258