summaryrefslogtreecommitdiff
path: root/macros/src/mock.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-03-20 23:42:40 +0100
committerHampusM <hampus@hampusmat.com>2023-03-20 23:42:40 +0100
commit7d19a8eae3fc911c0cee372b9a492ec203f4f45c (patch)
treed9b0fb703dc5b06d686fcf2815b1792ea75a24e9 /macros/src/mock.rs
parent888fd0336e64ccb68f5675b9fc8c56f21f5a418e (diff)
feat: add support for multiple expectations of identical methods
Diffstat (limited to 'macros/src/mock.rs')
-rw-r--r--macros/src/mock.rs27
1 files changed, 21 insertions, 6 deletions
diff --git a/macros/src/mock.rs b/macros/src/mock.rs
index 2ac27e5..9aa03f4 100644
--- a/macros/src/mock.rs
+++ b/macros/src/mock.rs
@@ -144,7 +144,8 @@ impl ToTokens for Mock
{
Self {
#(
- #expectations_field_idents: ::std::collections::HashMap::new()
+ #expectations_field_idents:
+ ::std::collections::HashMap::new()
),*
}
}
@@ -182,7 +183,7 @@ impl ToTokens for ExpectationsField
quote! {
#field_ident: ::std::collections::HashMap<
Vec<::ridicule::__private::type_id::TypeID>,
- #expectation_ident<#(#generic_args),*>
+ ::std::collections::VecDeque<#expectation_ident<#(#generic_args),*>>
>
}
.to_tokens(tokens);
@@ -238,12 +239,21 @@ fn create_mock_function(
#(::ridicule::__private::type_id::TypeID::of::<#type_param_idents>()),*
];
- let expectation = self
+ let func_expectations = self
.#expectations_field
.get(&ids)
.expect(concat!(
"No expectation found for function ",
stringify!(#func_ident)
+ ));
+
+ let expectation = func_expectations
+ .iter()
+ .skip_while(|expectation| expectation.is_exhausted())
+ .next()
+ .expect(concat!(
+ "No expectation found for function ",
+ stringify!(#func_ident)
))
.with_generic_params::<#(#type_param_idents,)*>();
@@ -318,10 +328,15 @@ fn create_expect_function(
#expectation::<#(#type_param_idents),*>::new()
.strip_generic_params();
- self.#expectations_field.insert(ids.clone(), expectation);
+ let func_expectations = self
+ .#expectations_field
+ .entry(ids.clone())
+ .or_insert_with(::std::collections::VecDeque::new);
+
+ func_expectations.push_back(expectation);
- self.#expectations_field
- .get_mut(&ids)
+ func_expectations
+ .back_mut()
.unwrap()
.with_generic_params_mut()
}})