diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/di_container/asynchronous/binding/scope_configurator.rs | 52 | ||||
-rw-r--r-- | src/di_container/asynchronous/binding/when_configurator.rs | 39 | ||||
-rw-r--r-- | src/di_container/blocking/binding/scope_configurator.rs | 50 | ||||
-rw-r--r-- | src/di_container/blocking/binding/when_configurator.rs | 38 | ||||
-rw-r--r-- | src/provider/async.rs | 1 | ||||
-rw-r--r-- | src/provider/blocking.rs | 1 |
6 files changed, 181 insertions, 0 deletions
diff --git a/src/di_container/asynchronous/binding/scope_configurator.rs b/src/di_container/asynchronous/binding/scope_configurator.rs index fd42fea..e2e916a 100644 --- a/src/di_container/asynchronous/binding/scope_configurator.rs +++ b/src/di_container/asynchronous/binding/scope_configurator.rs @@ -90,3 +90,55 @@ where Ok(AsyncBindingWhenConfigurator::new(self.di_container.clone())) } } + +#[cfg(test)] +mod tests +{ + use super::*; + use crate::test_utils::{mocks, subjects_async}; + + #[tokio::test] + async fn in_transient_scope_works() + { + let mut di_container_mock = + mocks::async_di_container::MockAsyncDIContainer::new(); + + di_container_mock + .expect_set_binding::<dyn subjects_async::IUserManager>() + .withf(|name, _provider| name.is_none()) + .return_once(|_name, _provider| ()) + .once(); + + let binding_scope_configurator = AsyncBindingScopeConfigurator::< + dyn subjects_async::IUserManager, + subjects_async::UserManager, + mocks::async_di_container::MockAsyncDIContainer, + >::new(Arc::new(di_container_mock)); + + binding_scope_configurator.in_transient_scope().await; + } + + #[tokio::test] + async fn in_singleton_scope_works() + { + let mut di_container_mock = + mocks::async_di_container::MockAsyncDIContainer::new(); + + di_container_mock + .expect_set_binding::<dyn subjects_async::IUserManager>() + .withf(|name, _provider| name.is_none()) + .return_once(|_name, _provider| ()) + .once(); + + let binding_scope_configurator = AsyncBindingScopeConfigurator::< + dyn subjects_async::IUserManager, + subjects_async::UserManager, + mocks::async_di_container::MockAsyncDIContainer, + >::new(Arc::new(di_container_mock)); + + assert!(matches!( + binding_scope_configurator.in_singleton_scope().await, + Ok(_) + )); + } +} diff --git a/src/di_container/asynchronous/binding/when_configurator.rs b/src/di_container/asynchronous/binding/when_configurator.rs index 3175420..9a1505b 100644 --- a/src/di_container/asynchronous/binding/when_configurator.rs +++ b/src/di_container/asynchronous/binding/when_configurator.rs @@ -62,3 +62,42 @@ where Ok(()) } } + +#[cfg(test)] +mod tests +{ + use mockall::predicate::eq; + + use super::*; + use crate::provider::r#async::MockIAsyncProvider; + use crate::test_utils::{mocks, subjects_async}; + + #[tokio::test] + async fn when_named_works() + { + let mut di_container_mock = + mocks::async_di_container::MockAsyncDIContainer::new(); + + di_container_mock + .expect_remove_binding::<dyn subjects_async::INumber>() + .with(eq(None)) + .return_once(|_name| Some(Box::new(MockIAsyncProvider::new()))) + .once(); + + di_container_mock + .expect_set_binding::<dyn subjects_async::INumber>() + .withf(|name, _provider| name == &Some("awesome")) + .return_once(|_name, _provider| ()) + .once(); + + let binding_when_configurator = AsyncBindingWhenConfigurator::< + dyn subjects_async::INumber, + mocks::async_di_container::MockAsyncDIContainer, + >::new(Arc::new(di_container_mock)); + + assert!(matches!( + binding_when_configurator.when_named("awesome").await, + Ok(_) + )); + } +} diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs index 5553415..dc33cbc 100644 --- a/src/di_container/blocking/binding/scope_configurator.rs +++ b/src/di_container/blocking/binding/scope_configurator.rs @@ -79,3 +79,53 @@ where Ok(BindingWhenConfigurator::new(self.di_container.clone())) } } + +#[cfg(test)] +mod tests +{ + use super::*; + use crate::test_utils::{mocks, subjects}; + + #[test] + fn in_transient_scope_works() + { + let mut di_container_mock = mocks::blocking_di_container::MockDIContainer::new(); + + di_container_mock + .expect_set_binding::<dyn subjects::IUserManager>() + .withf(|name, _provider| name.is_none()) + .return_once(|_name, _provider| ()) + .once(); + + let binding_scope_configurator = BindingScopeConfigurator::< + dyn subjects::IUserManager, + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(di_container_mock)); + + binding_scope_configurator.in_transient_scope(); + } + + #[test] + fn in_singleton_scope_works() + { + let mut di_container_mock = mocks::blocking_di_container::MockDIContainer::new(); + + di_container_mock + .expect_set_binding::<dyn subjects::IUserManager>() + .withf(|name, _provider| name.is_none()) + .return_once(|_name, _provider| ()) + .once(); + + let binding_scope_configurator = BindingScopeConfigurator::< + dyn subjects::IUserManager, + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(di_container_mock)); + + assert!(matches!( + binding_scope_configurator.in_singleton_scope(), + Ok(_) + )); + } +} diff --git a/src/di_container/blocking/binding/when_configurator.rs b/src/di_container/blocking/binding/when_configurator.rs index 5b9a8c0..49c9d9e 100644 --- a/src/di_container/blocking/binding/when_configurator.rs +++ b/src/di_container/blocking/binding/when_configurator.rs @@ -61,3 +61,41 @@ where Ok(()) } } + +#[cfg(test)] +mod tests +{ + use mockall::predicate::eq; + + use super::*; + use crate::provider::blocking::MockIProvider; + use crate::test_utils::{mocks, subjects}; + + #[test] + fn when_named_works() + { + let mut di_container_mock = mocks::blocking_di_container::MockDIContainer::new(); + + di_container_mock + .expect_remove_binding::<dyn subjects::INumber>() + .with(eq(None)) + .return_once(|_name| Some(Box::new(MockIProvider::new()))) + .once(); + + di_container_mock + .expect_set_binding::<dyn subjects::INumber>() + .withf(|name, _provider| name == &Some("cool")) + .return_once(|_name, _provider| ()) + .once(); + + let binding_when_configurator = BindingWhenConfigurator::< + dyn subjects::INumber, + mocks::blocking_di_container::MockDIContainer, + >::new(Rc::new(di_container_mock)); + + assert!(matches!( + binding_when_configurator.when_named("cool"), + Ok(_) + )); + } +} diff --git a/src/provider/async.rs b/src/provider/async.rs index 06e1d1d..53cadc8 100644 --- a/src/provider/async.rs +++ b/src/provider/async.rs @@ -36,6 +36,7 @@ where } #[async_trait] +#[cfg_attr(test, mockall::automock, allow(dead_code))] pub trait IAsyncProvider<DIContainerType>: Send + Sync where DIContainerType: IAsyncDIContainer, diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs index 435a8e5..3de77e4 100644 --- a/src/provider/blocking.rs +++ b/src/provider/blocking.rs @@ -21,6 +21,7 @@ where ), } +#[cfg_attr(test, mockall::automock, allow(dead_code))] pub trait IProvider<DIContainerType> where DIContainerType: IDIContainer, |