diff options
author | HampusM <hampus@hampusmat.com> | 2022-10-09 12:05:24 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-10-09 17:03:01 +0200 |
commit | 5b0c6a52022e67a2d9cee251b3d08b9cb2b5f6cb (patch) | |
tree | c33f06eaab96ec43e477ea5ecd2af93e9d739097 /src/test_utils.rs | |
parent | 97c789e38bb8e61389a3808d241689e623144344 (diff) |
refactor!: reorganize DI containers
BREAKING CHANGE: DIContainer, AsyncDIContainer & the binding structs have been relocated
Diffstat (limited to 'src/test_utils.rs')
-rw-r--r-- | src/test_utils.rs | 253 |
1 files changed, 253 insertions, 0 deletions
diff --git a/src/test_utils.rs b/src/test_utils.rs new file mode 100644 index 0000000..1fc7015 --- /dev/null +++ b/src/test_utils.rs @@ -0,0 +1,253 @@ +pub mod subjects +{ + //! Test subjects. + + use std::fmt::Debug; + use std::rc::Rc; + + use syrette_macros::declare_interface; + + use crate::interfaces::injectable::Injectable; + use crate::ptr::TransientPtr; + use crate::DIContainer; + + pub trait IUserManager + { + fn add_user(&self, user_id: i128); + + fn remove_user(&self, user_id: i128); + } + + pub struct UserManager {} + + impl UserManager + { + pub fn new() -> Self + { + Self {} + } + } + + impl IUserManager for UserManager + { + fn add_user(&self, _user_id: i128) + { + // ... + } + + fn remove_user(&self, _user_id: i128) + { + // ... + } + } + + use crate as syrette; + + declare_interface!(UserManager -> IUserManager); + + impl Injectable for UserManager + { + fn resolve( + _di_container: &Rc<DIContainer>, + _dependency_history: Vec<&'static str>, + ) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError> + where + Self: Sized, + { + Ok(TransientPtr::new(Self::new())) + } + } + + pub trait INumber + { + fn get(&self) -> i32; + + fn set(&mut self, number: i32); + } + + impl PartialEq for dyn INumber + { + fn eq(&self, other: &Self) -> bool + { + self.get() == other.get() + } + } + + impl Debug for dyn INumber + { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + f.write_str(format!("{}", self.get()).as_str()) + } + } + + pub struct Number + { + pub num: i32, + } + + impl Number + { + pub fn new() -> Self + { + Self { num: 0 } + } + } + + impl INumber for Number + { + fn get(&self) -> i32 + { + self.num + } + + fn set(&mut self, number: i32) + { + self.num = number; + } + } + + declare_interface!(Number -> INumber); + + impl Injectable for Number + { + fn resolve( + _di_container: &Rc<DIContainer>, + _dependency_history: Vec<&'static str>, + ) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError> + where + Self: Sized, + { + Ok(TransientPtr::new(Self::new())) + } + } +} + +#[cfg(feature = "async")] +pub mod subjects_async +{ + //! Test subjects. + + use std::fmt::Debug; + use std::sync::Arc; + + use async_trait::async_trait; + use syrette_macros::declare_interface; + + use crate::interfaces::async_injectable::AsyncInjectable; + use crate::ptr::TransientPtr; + use crate::AsyncDIContainer; + + pub trait IUserManager: Send + Sync + { + fn add_user(&self, user_id: i128); + + fn remove_user(&self, user_id: i128); + } + + pub struct UserManager {} + + impl UserManager + { + pub fn new() -> Self + { + Self {} + } + } + + impl IUserManager for UserManager + { + fn add_user(&self, _user_id: i128) + { + // ... + } + + fn remove_user(&self, _user_id: i128) + { + // ... + } + } + + use crate as syrette; + + declare_interface!(UserManager -> IUserManager); + + #[async_trait] + impl AsyncInjectable for UserManager + { + async fn resolve( + _: &Arc<AsyncDIContainer>, + _dependency_history: Vec<&'static str>, + ) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError> + where + Self: Sized, + { + Ok(TransientPtr::new(Self::new())) + } + } + + pub trait INumber: Send + Sync + { + fn get(&self) -> i32; + + fn set(&mut self, number: i32); + } + + impl PartialEq for dyn INumber + { + fn eq(&self, other: &Self) -> bool + { + self.get() == other.get() + } + } + + impl Debug for dyn INumber + { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result + { + f.write_str(format!("{}", self.get()).as_str()) + } + } + + pub struct Number + { + pub num: i32, + } + + impl Number + { + pub fn new() -> Self + { + Self { num: 0 } + } + } + + impl INumber for Number + { + fn get(&self) -> i32 + { + self.num + } + + fn set(&mut self, number: i32) + { + self.num = number; + } + } + + declare_interface!(Number -> INumber, async = true); + + #[async_trait] + impl AsyncInjectable for Number + { + async fn resolve( + _: &Arc<AsyncDIContainer>, + _dependency_history: Vec<&'static str>, + ) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError> + where + Self: Sized, + { + Ok(TransientPtr::new(Self::new())) + } + } +} |