From 7d19a8eae3fc911c0cee372b9a492ec203f4f45c Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 20 Mar 2023 23:42:40 +0100 Subject: feat: add support for multiple expectations of identical methods --- macros/src/expectation.rs | 16 ++++++++++++++++ macros/src/mock.rs | 27 +++++++++++++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/macros/src/expectation.rs b/macros/src/expectation.rs index a6d181f..4095278 100644 --- a/macros/src/expectation.rs +++ b/macros/src/expectation.rs @@ -402,6 +402,22 @@ impl ToTokens for Expectation } } + impl #boundless_impl_generics #ident #ty_generics { + fn is_exhausted(&self) -> bool { + if let ::ridicule::__private::CallCountExpectation::Times(times) = + self.call_cnt_expectation + { + if times == self.call_cnt.load( + ::std::sync::atomic::Ordering::Relaxed + ) { + return true; + } + } + + false + } + } + impl #boundless_impl_generics Drop for #ident #ty_generics { fn drop(&mut self) { 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,9 +239,18 @@ 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) @@ -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() }}) -- cgit v1.2.3-18-g5258