aboutsummaryrefslogtreecommitdiff
path: root/src/test_utils.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-10-23 18:12:23 +0200
committerHampusM <hampus@hampusmat.com>2022-10-23 18:12:23 +0200
commit9e01cdf341a7866180b3a63d745f3b2d7578d28a (patch)
tree0c036b7b4a68e44b6eb2221bf7beb3c34fe9c1c8 /src/test_utils.rs
parent740ef47d49e02ae2f2184f4c347d8eba8aee38fd (diff)
refactor!: reduce DI container coupling
BREAKING CHANGE: You now have to import the DI containers's interfaces to use the DI containers's methods
Diffstat (limited to 'src/test_utils.rs')
-rw-r--r--src/test_utils.rs172
1 files changed, 162 insertions, 10 deletions
diff --git a/src/test_utils.rs b/src/test_utils.rs
index 1fc7015..650e338 100644
--- a/src/test_utils.rs
+++ b/src/test_utils.rs
@@ -7,9 +7,9 @@ pub mod subjects
use syrette_macros::declare_interface;
+ use crate::di_container::blocking::IDIContainer;
use crate::interfaces::injectable::Injectable;
use crate::ptr::TransientPtr;
- use crate::DIContainer;
pub trait IUserManager
{
@@ -45,10 +45,12 @@ pub mod subjects
declare_interface!(UserManager -> IUserManager);
- impl Injectable for UserManager
+ impl<DIContainerType> Injectable<DIContainerType> for UserManager
+ where
+ DIContainerType: IDIContainer,
{
fn resolve(
- _di_container: &Rc<DIContainer>,
+ _di_container: &Rc<DIContainerType>,
_dependency_history: Vec<&'static str>,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where
@@ -109,10 +111,12 @@ pub mod subjects
declare_interface!(Number -> INumber);
- impl Injectable for Number
+ impl<DIContainerType> Injectable<DIContainerType> for Number
+ where
+ DIContainerType: IDIContainer,
{
fn resolve(
- _di_container: &Rc<DIContainer>,
+ _di_container: &Rc<DIContainerType>,
_dependency_history: Vec<&'static str>,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where
@@ -134,9 +138,9 @@ pub mod subjects_async
use async_trait::async_trait;
use syrette_macros::declare_interface;
+ use crate::di_container::asynchronous::IAsyncDIContainer;
use crate::interfaces::async_injectable::AsyncInjectable;
use crate::ptr::TransientPtr;
- use crate::AsyncDIContainer;
pub trait IUserManager: Send + Sync
{
@@ -173,10 +177,12 @@ pub mod subjects_async
declare_interface!(UserManager -> IUserManager);
#[async_trait]
- impl AsyncInjectable for UserManager
+ impl<DIContainerType> AsyncInjectable<DIContainerType> for UserManager
+ where
+ DIContainerType: IAsyncDIContainer,
{
async fn resolve(
- _: &Arc<AsyncDIContainer>,
+ _: &Arc<DIContainerType>,
_dependency_history: Vec<&'static str>,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where
@@ -238,10 +244,12 @@ pub mod subjects_async
declare_interface!(Number -> INumber, async = true);
#[async_trait]
- impl AsyncInjectable for Number
+ impl<DIContainerType> AsyncInjectable<DIContainerType> for Number
+ where
+ DIContainerType: IAsyncDIContainer,
{
async fn resolve(
- _: &Arc<AsyncDIContainer>,
+ _: &Arc<DIContainerType>,
_dependency_history: Vec<&'static str>,
) -> Result<TransientPtr<Self>, crate::errors::injectable::InjectableError>
where
@@ -251,3 +259,147 @@ pub mod subjects_async
}
}
}
+
+pub mod mocks
+{
+ #![allow(clippy::ref_option_ref)] // Caused by Mockall
+ #![allow(dead_code)] // Not all mock functions may be used
+
+ use mockall::mock;
+
+ pub mod blocking_di_container
+ {
+ use std::rc::Rc;
+
+ use super::*;
+ use crate::di_container::blocking::binding::builder::BindingBuilder;
+ use crate::di_container::blocking::details::DIContainerInternals;
+ use crate::di_container::blocking::IDIContainer;
+ use crate::errors::di_container::DIContainerError;
+ use crate::provider::blocking::IProvider;
+ use crate::ptr::SomePtr;
+
+ mock! {
+ pub DIContainer {}
+
+ impl IDIContainer for DIContainer {
+ fn bind<Interface>(self: &mut Rc<Self>) -> BindingBuilder<Interface, Self>
+ where
+ Interface: 'static + ?Sized;
+
+ fn get<Interface>(self: &Rc<Self>) -> Result<SomePtr<Interface>, DIContainerError>
+ where
+ Interface: 'static + ?Sized;
+
+ fn get_named<Interface>(
+ self: &Rc<Self>,
+ name: &'static str,
+ ) -> Result<SomePtr<Interface>, DIContainerError>
+ where
+ Interface: 'static + ?Sized;
+
+ #[doc(hidden)]
+ fn get_bound<Interface>(
+ self: &Rc<Self>,
+ dependency_history: Vec<&'static str>,
+ name: Option<&'static str>,
+ ) -> Result<SomePtr<Interface>, DIContainerError>
+ where
+ Interface: 'static + ?Sized;
+ }
+
+ impl DIContainerInternals for DIContainer
+ {
+ fn has_binding<Interface>(self: &Rc<Self>, name: Option<&'static str>) -> bool
+ where
+ Interface: ?Sized + 'static;
+
+ #[doc(hidden)]
+ fn set_binding<Interface>(
+ self: &Rc<Self>,
+ name: Option<&'static str>,
+ provider: Box<dyn IProvider<Self>>,
+ ) where
+ Interface: 'static + ?Sized;
+
+ fn remove_binding<Interface>(
+ self: &Rc<Self>,
+ name: Option<&'static str>,
+ ) -> Option<Box<dyn IProvider<Self>>>
+ where
+ Interface: 'static + ?Sized;
+ }
+ }
+ }
+
+ #[cfg(feature = "async")]
+ pub mod async_di_container
+ {
+ use std::sync::Arc;
+
+ use super::*;
+ use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder;
+ use crate::di_container::asynchronous::details::DIContainerInternals;
+ use crate::di_container::asynchronous::IAsyncDIContainer;
+ use crate::errors::async_di_container::AsyncDIContainerError;
+ use crate::provider::r#async::IAsyncProvider;
+ use crate::ptr::SomeThreadsafePtr;
+
+ mock! {
+ pub AsyncDIContainer {}
+
+ #[async_trait::async_trait]
+ impl IAsyncDIContainer for AsyncDIContainer {
+ fn bind<Interface>(self: &mut Arc<Self>) -> AsyncBindingBuilder<Interface, Self>
+ where
+ Interface: 'static + ?Sized + Send + Sync;
+
+ async fn get<Interface>(
+ self: &Arc<Self>,
+ ) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError>
+ where
+ Interface: 'static + ?Sized + Send + Sync;
+
+ async fn get_named<Interface>(
+ self: &Arc<Self>,
+ name: &'static str,
+ ) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError>
+ where
+ Interface: 'static + ?Sized + Send + Sync;
+
+ #[doc(hidden)]
+ async fn get_bound<Interface>(
+ self: &Arc<Self>,
+ dependency_history: Vec<&'static str>,
+ name: Option<&'static str>,
+ ) -> Result<SomeThreadsafePtr<Interface>, AsyncDIContainerError>
+ where
+ Interface: 'static + ?Sized + Send + Sync;
+ }
+
+ #[async_trait::async_trait]
+ impl DIContainerInternals for AsyncDIContainer {
+ async fn has_binding<Interface>(
+ self: &Arc<Self>,
+ name: Option<&'static str>,
+ ) -> bool
+ where
+ Interface: ?Sized + 'static;
+
+ async fn set_binding<Interface>(
+ self: &Arc<Self>,
+ name: Option<&'static str>,
+ provider: Box<dyn IAsyncProvider<Self>>,
+ ) where
+ Interface: 'static + ?Sized;
+
+ async fn remove_binding<Interface>(
+ self: &Arc<Self>,
+ name: Option<&'static str>,
+ ) -> Option<Box<dyn IAsyncProvider<Self>>>
+ where
+ Interface: 'static + ?Sized;
+ }
+ }
+ }
+}