Skip to content

Commit

Permalink
Merge pull request CodeSandwich#66 from gregdhill/mock-async-assoc-ig…
Browse files Browse the repository at this point in the history
…nore-async-lifetimes

mock associated async functions, ignore async lifetimes
  • Loading branch information
gregdhill authored Apr 2, 2021
2 parents 4529a3e + aa93669 commit 510e289
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 48 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mocktopus"
version = "0.7.10"
version = "0.7.11"
authors = [
"CodeSandwich <igor.zuk@protonmail.com>",
"gregdhill <gregorydhill@outlook.com>"
Expand All @@ -21,7 +21,7 @@ travis-ci = { repository = "CodeSandwich/Mocktopus" }
doctest = false

[dependencies]
mocktopus_macros = "0.7.10"
mocktopus_macros = "0.7.11"

[dev-dependencies]
tokio = { version = "0.2", features = ["full"] }
Expand Down
2 changes: 1 addition & 1 deletion macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mocktopus_macros"
version = "0.7.10"
version = "0.7.11"
authors = [
"CodeSandwich <igor.zuk@protonmail.com>",
"gregdhill <gregorydhill@outlook.com>"
Expand Down
88 changes: 43 additions & 45 deletions macros/src/item_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,59 +260,57 @@ fn inject_async_fn(
});

let mut outer_sig_inputs = outer_sig.inputs.iter_mut();
match outer_sig_inputs.next() {
Some(
arg @ FnArg::Receiver(Receiver {
while let Some(input) = outer_sig_inputs.next() {
match input {
arg
@ FnArg::Receiver(Receiver {
reference: Some(_), ..
}),
) => {
let (self_token, mutability, lifetime) = match arg {
FnArg::Receiver(Receiver {
self_token,
mutability,
reference: Some((_, lifetime)),
..
}) => (self_token, mutability, lifetime),
_ => unreachable!(),
};
*arg = parse_quote! {
&'life_self #lifetime #mutability #self_token
};
}
Some(arg @ FnArg::Receiver(_)) => {
let (self_token, mutability) = match arg {
FnArg::Receiver(Receiver {
self_token,
mutability,
..
}) => (self_token, mutability),
_ => unreachable!(),
};
*arg = parse_quote! {
#mutability #self_token
};
}
_ => {}
};

for input in outer_sig_inputs {
if let arg @ FnArg::Typed(_) = input {
if let FnArg::Typed(PatType {
pat,
colon_token,
ty,
..
}) = arg
{
}) => {
let (self_token, mutability) = match arg {
FnArg::Receiver(Receiver {
self_token,
mutability,
reference: Some((_, _)),
..
}) => (self_token, mutability),
_ => unreachable!(),
};
*arg = parse_quote! {
&'life_self #mutability #self_token
};
}
arg @ FnArg::Receiver(_) => {
let (self_token, mutability) = match arg {
FnArg::Receiver(Receiver {
self_token,
mutability,
..
}) => (self_token, mutability),
_ => unreachable!(),
};
*arg = parse_quote! {
#mutability #self_token
};
}
arg @ FnArg::Typed(_) => {
let (pat, colon_token, ty) = match arg {
FnArg::Typed(PatType {
pat,
colon_token,
ty,
..
}) => (pat, colon_token, ty),
_ => unreachable!(),
};
if let Type::Reference(syn::TypeReference {
and_token,
lifetime,
lifetime: _,
mutability,
elem,
}) = *ty.clone()
{
*arg = parse_quote! {
#pat #colon_token #and_token 'mocktopus #lifetime #mutability #elem
#pat #colon_token #and_token 'mocktopus #mutability #elem
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ impl Struct {
async fn ref_method_with_call(&self, arg: bool) -> String {
format!("{} {}", plus_one(self.0), arg)
}

async fn assoc_method_with_ref(arg: &str) -> String {
format!("Hello {}", arg)
}

async fn assoc_method_with_ref_and_lifetime<'a>(arg1: &'a str, arg2: &'a str) -> String {
format!("{} {}", arg1, arg2)
}
}

mod and_method_is_ref_method_with_binding {
Expand Down Expand Up @@ -117,3 +125,61 @@ mod and_method_is_ref_method_with_call {
assert_eq!(2, struct_2.0);
}
}

mod and_method_is_assoc_method_with_ref {
use super::*;

#[tokio::test]
async fn and_not_mocked_then_runs_normally() {
assert_eq!("Hello World", Struct::assoc_method_with_ref("World").await);
}

#[tokio::test]
async fn and_continue_mocked_then_runs_with_modified_args() {
unsafe {
Struct::assoc_method_with_ref.mock_raw(|_| MockResult::Continue(("Universe",)));
}

assert_eq!("Hello Universe", Struct::assoc_method_with_ref("World").await);
}

#[tokio::test]
async fn and_return_mocked_then_returns_mocking_result() {
unsafe {
Struct::assoc_method_with_ref.mock_raw(|s| {
MockResult::Return(Box::pin(async move { format!("Welcome {}", s) }))
});
}

assert_eq!("Welcome World", Struct::assoc_method_with_ref("World").await);
}
}

mod and_method_is_assoc_method_with_ref_and_lifetime {
use super::*;

#[tokio::test]
async fn and_not_mocked_then_runs_normally() {
assert_eq!("Hello World", Struct::assoc_method_with_ref_and_lifetime("Hello", "World").await);
}

#[tokio::test]
async fn and_continue_mocked_then_runs_with_modified_args() {
unsafe {
Struct::assoc_method_with_ref_and_lifetime.mock_raw(|s1, _| MockResult::Continue((s1, "Universe",)));
}

assert_eq!("Hello Universe", Struct::assoc_method_with_ref_and_lifetime("Hello", "World").await);
}

#[tokio::test]
async fn and_return_mocked_then_returns_mocking_result() {
unsafe {
Struct::assoc_method_with_ref_and_lifetime.mock_raw(|_, s2| {
MockResult::Return(Box::pin(async move { format!("Welcome {}", s2) }))
});
}

assert_eq!("Welcome World", Struct::assoc_method_with_ref_and_lifetime("Hello", "World").await);
}
}

0 comments on commit 510e289

Please sign in to comment.