Skip to content

Commit 510e289

Browse files
authored
Merge pull request #66 from gregdhill/mock-async-assoc-ignore-async-lifetimes
mock associated async functions, ignore async lifetimes
2 parents 4529a3e + aa93669 commit 510e289

File tree

4 files changed

+112
-48
lines changed

4 files changed

+112
-48
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mocktopus"
3-
version = "0.7.10"
3+
version = "0.7.11"
44
authors = [
55
"CodeSandwich <[email protected]>",
66
"gregdhill <[email protected]>"
@@ -21,7 +21,7 @@ travis-ci = { repository = "CodeSandwich/Mocktopus" }
2121
doctest = false
2222

2323
[dependencies]
24-
mocktopus_macros = "0.7.10"
24+
mocktopus_macros = "0.7.11"
2525

2626
[dev-dependencies]
2727
tokio = { version = "0.2", features = ["full"] }

macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mocktopus_macros"
3-
version = "0.7.10"
3+
version = "0.7.11"
44
authors = [
55
"CodeSandwich <[email protected]>",
66
"gregdhill <[email protected]>"

macros/src/item_injector.rs

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -260,59 +260,57 @@ fn inject_async_fn(
260260
});
261261

262262
let mut outer_sig_inputs = outer_sig.inputs.iter_mut();
263-
match outer_sig_inputs.next() {
264-
Some(
265-
arg @ FnArg::Receiver(Receiver {
263+
while let Some(input) = outer_sig_inputs.next() {
264+
match input {
265+
arg
266+
@ FnArg::Receiver(Receiver {
266267
reference: Some(_), ..
267-
}),
268-
) => {
269-
let (self_token, mutability, lifetime) = match arg {
270-
FnArg::Receiver(Receiver {
271-
self_token,
272-
mutability,
273-
reference: Some((_, lifetime)),
274-
..
275-
}) => (self_token, mutability, lifetime),
276-
_ => unreachable!(),
277-
};
278-
*arg = parse_quote! {
279-
&'life_self #lifetime #mutability #self_token
280-
};
281-
}
282-
Some(arg @ FnArg::Receiver(_)) => {
283-
let (self_token, mutability) = match arg {
284-
FnArg::Receiver(Receiver {
285-
self_token,
286-
mutability,
287-
..
288-
}) => (self_token, mutability),
289-
_ => unreachable!(),
290-
};
291-
*arg = parse_quote! {
292-
#mutability #self_token
293-
};
294-
}
295-
_ => {}
296-
};
297-
298-
for input in outer_sig_inputs {
299-
if let arg @ FnArg::Typed(_) = input {
300-
if let FnArg::Typed(PatType {
301-
pat,
302-
colon_token,
303-
ty,
304-
..
305-
}) = arg
306-
{
268+
}) => {
269+
let (self_token, mutability) = match arg {
270+
FnArg::Receiver(Receiver {
271+
self_token,
272+
mutability,
273+
reference: Some((_, _)),
274+
..
275+
}) => (self_token, mutability),
276+
_ => unreachable!(),
277+
};
278+
*arg = parse_quote! {
279+
&'life_self #mutability #self_token
280+
};
281+
}
282+
arg @ FnArg::Receiver(_) => {
283+
let (self_token, mutability) = match arg {
284+
FnArg::Receiver(Receiver {
285+
self_token,
286+
mutability,
287+
..
288+
}) => (self_token, mutability),
289+
_ => unreachable!(),
290+
};
291+
*arg = parse_quote! {
292+
#mutability #self_token
293+
};
294+
}
295+
arg @ FnArg::Typed(_) => {
296+
let (pat, colon_token, ty) = match arg {
297+
FnArg::Typed(PatType {
298+
pat,
299+
colon_token,
300+
ty,
301+
..
302+
}) => (pat, colon_token, ty),
303+
_ => unreachable!(),
304+
};
307305
if let Type::Reference(syn::TypeReference {
308306
and_token,
309-
lifetime,
307+
lifetime: _,
310308
mutability,
311309
elem,
312310
}) = *ty.clone()
313311
{
314312
*arg = parse_quote! {
315-
#pat #colon_token #and_token 'mocktopus #lifetime #mutability #elem
313+
#pat #colon_token #and_token 'mocktopus #mutability #elem
316314
};
317315
}
318316
}

tests/mocking_methods_async/when_struct_complex_method_regular_async.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ impl Struct {
2121
async fn ref_method_with_call(&self, arg: bool) -> String {
2222
format!("{} {}", plus_one(self.0), arg)
2323
}
24+
25+
async fn assoc_method_with_ref(arg: &str) -> String {
26+
format!("Hello {}", arg)
27+
}
28+
29+
async fn assoc_method_with_ref_and_lifetime<'a>(arg1: &'a str, arg2: &'a str) -> String {
30+
format!("{} {}", arg1, arg2)
31+
}
2432
}
2533

2634
mod and_method_is_ref_method_with_binding {
@@ -117,3 +125,61 @@ mod and_method_is_ref_method_with_call {
117125
assert_eq!(2, struct_2.0);
118126
}
119127
}
128+
129+
mod and_method_is_assoc_method_with_ref {
130+
use super::*;
131+
132+
#[tokio::test]
133+
async fn and_not_mocked_then_runs_normally() {
134+
assert_eq!("Hello World", Struct::assoc_method_with_ref("World").await);
135+
}
136+
137+
#[tokio::test]
138+
async fn and_continue_mocked_then_runs_with_modified_args() {
139+
unsafe {
140+
Struct::assoc_method_with_ref.mock_raw(|_| MockResult::Continue(("Universe",)));
141+
}
142+
143+
assert_eq!("Hello Universe", Struct::assoc_method_with_ref("World").await);
144+
}
145+
146+
#[tokio::test]
147+
async fn and_return_mocked_then_returns_mocking_result() {
148+
unsafe {
149+
Struct::assoc_method_with_ref.mock_raw(|s| {
150+
MockResult::Return(Box::pin(async move { format!("Welcome {}", s) }))
151+
});
152+
}
153+
154+
assert_eq!("Welcome World", Struct::assoc_method_with_ref("World").await);
155+
}
156+
}
157+
158+
mod and_method_is_assoc_method_with_ref_and_lifetime {
159+
use super::*;
160+
161+
#[tokio::test]
162+
async fn and_not_mocked_then_runs_normally() {
163+
assert_eq!("Hello World", Struct::assoc_method_with_ref_and_lifetime("Hello", "World").await);
164+
}
165+
166+
#[tokio::test]
167+
async fn and_continue_mocked_then_runs_with_modified_args() {
168+
unsafe {
169+
Struct::assoc_method_with_ref_and_lifetime.mock_raw(|s1, _| MockResult::Continue((s1, "Universe",)));
170+
}
171+
172+
assert_eq!("Hello Universe", Struct::assoc_method_with_ref_and_lifetime("Hello", "World").await);
173+
}
174+
175+
#[tokio::test]
176+
async fn and_return_mocked_then_returns_mocking_result() {
177+
unsafe {
178+
Struct::assoc_method_with_ref_and_lifetime.mock_raw(|_, s2| {
179+
MockResult::Return(Box::pin(async move { format!("Welcome {}", s2) }))
180+
});
181+
}
182+
183+
assert_eq!("Welcome World", Struct::assoc_method_with_ref_and_lifetime("Hello", "World").await);
184+
}
185+
}

0 commit comments

Comments
 (0)