From d4b9fe446de12782a61953ed12a8ab5c425f448d Mon Sep 17 00:00:00 2001 From: orbwalk Date: Mon, 14 Feb 2022 17:16:55 +0100 Subject: [PATCH 1/3] Implement Clone for MockStore --- src/mock_store.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/mock_store.rs b/src/mock_store.rs index 3415af6..fd5a6a5 100644 --- a/src/mock_store.rs +++ b/src/mock_store.rs @@ -26,7 +26,7 @@ use std::{ /// implements_default(faux::MaybeFaux::Real(3)); /// ``` -#[derive(Debug)] +#[derive(Clone, Debug)] pub enum MaybeFaux { Real(T), Faux(MockStore), @@ -38,15 +38,6 @@ impl Default for MaybeFaux { } } -impl Clone for MaybeFaux { - fn clone(&self) -> Self { - match self { - MaybeFaux::Real(r) => MaybeFaux::Real(r.clone()), - MaybeFaux::Faux(_) => panic!("cannot clone a mock"), - } - } -} - impl MaybeFaux { pub fn faux() -> Self { MaybeFaux::Faux(MockStore::new()) @@ -124,3 +115,12 @@ impl MockStore { Err(errors.join("\n\n")) } } + +impl Clone for MockStore { + fn clone(&self) -> Self { + let stubs = self.stubs.lock().unwrap(); + Self { + stubs: Mutex::new(stubs.clone()), + } + } +} From d2ebbf76482393a3c9541ae82fb5961672ad0165 Mon Sep 17 00:00:00 2001 From: orbwalk Date: Wed, 23 Feb 2022 10:56:35 +0100 Subject: [PATCH 2/3] Wrap stubs mutex in arc --- src/mock_store.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/mock_store.rs b/src/mock_store.rs index fd5a6a5..d05e6ec 100644 --- a/src/mock_store.rs +++ b/src/mock_store.rs @@ -44,16 +44,16 @@ impl MaybeFaux { } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] #[doc(hidden)] pub struct MockStore { - stubs: Mutex>>>>>, + stubs: Arc>>>>>>, } impl MockStore { fn new() -> Self { MockStore { - stubs: Mutex::new(HashMap::new()), + stubs: Arc::new(Mutex::new(HashMap::new())), } } @@ -115,12 +115,3 @@ impl MockStore { Err(errors.join("\n\n")) } } - -impl Clone for MockStore { - fn clone(&self) -> Self { - let stubs = self.stubs.lock().unwrap(); - Self { - stubs: Mutex::new(stubs.clone()), - } - } -} From 9f94020f615b11ebe4e28f81629cb6749f73483b Mon Sep 17 00:00:00 2001 From: orbwalk Date: Wed, 23 Feb 2022 10:57:02 +0100 Subject: [PATCH 3/3] Fix clone mock test --- tests/clone.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/clone.rs b/tests/clone.rs index 10402e4..20f2785 100644 --- a/tests/clone.rs +++ b/tests/clone.rs @@ -25,8 +25,9 @@ fn can_clone_real() { } #[test] -#[should_panic] -fn cloning_mock_panics() { - let real = Foo::faux(); - let _ = real.clone(); +fn can_clone_mock() { + let mut mock = Foo::faux(); + let cloned = mock.clone(); + faux::when!(mock.get()).then_return(4); + assert_eq!(cloned.get(), 4); }