Skip to content

Commit

Permalink
Fix mocking methods with mutable arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
asomers committed Jul 4, 2019
1 parent f287164 commit 8058701
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
44 changes: 40 additions & 4 deletions mockall_derive/src/automock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ fn mock_function(vis: &syn::Visibility,
{
let fn_token = &decl.fn_token;
let generics = &decl.generics;
let inputs = &decl.inputs;
let inputs = demutify(&decl.inputs);
let output = match &decl.output{
syn::ReturnType::Default => quote!(-> ()),
_ => {
Expand All @@ -361,7 +361,7 @@ fn mock_function(vis: &syn::Visibility,
return TokenStream::new();
}

for p in decl.inputs.iter() {
for p in inputs.iter() {
match p {
syn::FnArg::Captured(arg) => {
args.push(arg.pat.clone());
Expand Down Expand Up @@ -878,6 +878,42 @@ mod t {
check(&attrs, &desired, &code);
}

#[test]
fn foreign_mutable_arg() {
let attrs = "mod mock;";
let desired = r#"
pub mod mock {
::mockall::expectation!{
pub(in super::super) fn __foo< >(x: u32) -> i64 {
let (p0: &u32) = (&x);
}
}
::mockall::lazy_static!{
static ref foo_expectation:
::std::sync::Mutex< __foo::Expectations>
= ::std::sync::Mutex::new(__foo::Expectations:: < > ::new());
}
pub(in super) unsafe fn foo(x: u32) -> i64 {
foo_expectation.lock().unwrap().call(x)
}
pub(in super) fn expect_foo< 'guard>()
-> __foo::ExpectationGuard< 'guard, >
{
__foo::ExpectationGuard:: < > ::new(foo_expectation.lock().unwrap())
}
pub fn checkpoint() {
foo_expectation.lock().unwrap().checkpoint();
}
}
"#;
let code = r#"
extern "Rust" {
fn foo(mut x: u32) -> i64;
}
"#;
check(&attrs, &desired, &code);
}

#[test]
fn generic_method() {
check("",
Expand Down Expand Up @@ -1589,12 +1625,12 @@ mod t {
pub mod __mock_Foo {
use super:: * ;
::mockall::expectation!{
pub fn foo< >(&self, mut x: u32) -> () {
pub fn foo< >(&self, x: u32) -> () {
let (p1: &u32) = (&x);
}
}
::mockall::expectation!{
pub fn bar< >(mut self) -> () { let () = (); }
pub fn bar< >(self) -> () { let () = (); }
}
}
pub struct MockFoo {
Expand Down
2 changes: 1 addition & 1 deletion mockall_derive/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ fn gen_struct<T>(mock_ident: &syn::Ident,
let attrs = format_attrs(&meth.borrow().attrs);
let method_ident = &meth.borrow().sig.ident;
let meth_types = method_types(&meth.borrow().sig, Some(generics));
let args = &meth.borrow().sig.decl.inputs;
let args = demutify(&meth.borrow().sig.decl.inputs);
let expect_obj = &meth_types.expect_obj;
let expectations = &meth_types.expectations;
let altargs = &meth_types.altargs;
Expand Down

0 comments on commit 8058701

Please sign in to comment.