From dcc7c8877813b452392e49ffc4e5b1df08b40487 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 24 Oct 2022 21:07:45 +0200 Subject: test: add provider unit tests --- src/provider/async.rs | 117 +++++++++++++++++++++++++++++++++++++++++++++++ src/provider/blocking.rs | 88 +++++++++++++++++++++++++++++++++++ 2 files changed, 205 insertions(+) (limited to 'src/provider') diff --git a/src/provider/async.rs b/src/provider/async.rs index 5b4bf89..06e1d1d 100644 --- a/src/provider/async.rs +++ b/src/provider/async.rs @@ -255,3 +255,120 @@ impl Clone for AsyncFactoryProvider } } } + +#[cfg(test)] +mod tests +{ + use std::error::Error; + + use super::*; + use crate::test_utils::{mocks, subjects_async}; + + #[tokio::test] + async fn async_transient_type_provider_works() -> Result<(), Box> + { + let transient_type_provider = AsyncTransientTypeProvider::< + subjects_async::UserManager, + mocks::async_di_container::MockAsyncDIContainer, + >::new(); + + let di_container = mocks::async_di_container::MockAsyncDIContainer::new(); + + assert!( + matches!( + transient_type_provider + .provide(&Arc::new(di_container), vec![]) + .await?, + AsyncProvidable::Transient(_) + ), + "The provided type is not transient" + ); + + Ok(()) + } + + #[tokio::test] + async fn async_singleton_provider_works() -> Result<(), Box> + { + let singleton_provider = AsyncSingletonProvider::< + subjects_async::UserManager, + mocks::async_di_container::MockAsyncDIContainer, + >::new(ThreadsafeSingletonPtr::new( + subjects_async::UserManager {}, + )); + + let di_container = mocks::async_di_container::MockAsyncDIContainer::new(); + + assert!( + matches!( + singleton_provider + .provide(&Arc::new(di_container), vec![]) + .await?, + AsyncProvidable::Singleton(_) + ), + "The provided type is not a singleton" + ); + + Ok(()) + } + + #[tokio::test] + #[cfg(feature = "factory")] + async fn async_factory_provider_works() -> Result<(), Box> + { + use crate::interfaces::any_factory::AnyThreadsafeFactory; + use crate::ptr::ThreadsafeFactoryPtr; + + struct FooFactory; + + impl AnyThreadsafeFactory for FooFactory {} + + let factory_provider = AsyncFactoryProvider::new( + ThreadsafeFactoryPtr::new(FooFactory), + AsyncFactoryVariant::Normal, + ); + + let default_factory_provider = AsyncFactoryProvider::new( + ThreadsafeFactoryPtr::new(FooFactory), + AsyncFactoryVariant::Default, + ); + + let async_default_factory_provider = AsyncFactoryProvider::new( + ThreadsafeFactoryPtr::new(FooFactory), + AsyncFactoryVariant::AsyncDefault, + ); + + let di_container = + Arc::new(mocks::async_di_container::MockAsyncDIContainer::new()); + + assert!( + matches!( + factory_provider.provide(&di_container, vec![]).await?, + AsyncProvidable::Factory(_) + ), + "The provided type is not a factory" + ); + + assert!( + matches!( + default_factory_provider + .provide(&di_container, vec![]) + .await?, + AsyncProvidable::DefaultFactory(_) + ), + "The provided type is not a default factory" + ); + + assert!( + matches!( + async_default_factory_provider + .provide(&di_container, vec![]) + .await?, + AsyncProvidable::AsyncDefaultFactory(_) + ), + "The provided type is not a async default factory" + ); + + Ok(()) + } +} diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs index b768b55..435a8e5 100644 --- a/src/provider/blocking.rs +++ b/src/provider/blocking.rs @@ -154,3 +154,91 @@ where }) } } + +#[cfg(test)] +mod tests +{ + use std::error::Error; + + use super::*; + use crate::test_utils::{mocks, subjects}; + + #[test] + fn transient_type_provider_works() -> Result<(), Box> + { + let transient_type_provider = TransientTypeProvider::< + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(); + + let di_container = mocks::blocking_di_container::MockDIContainer::new(); + + assert!( + matches!( + transient_type_provider.provide(&Rc::new(di_container), vec![])?, + Providable::Transient(_) + ), + "The provided type is not transient" + ); + + Ok(()) + } + + #[test] + fn singleton_provider_works() -> Result<(), Box> + { + let singleton_provider = + SingletonProvider::< + subjects::UserManager, + mocks::blocking_di_container::MockDIContainer, + >::new(SingletonPtr::new(subjects::UserManager {})); + + let di_container = mocks::blocking_di_container::MockDIContainer::new(); + + assert!( + matches!( + singleton_provider.provide(&Rc::new(di_container), vec![])?, + Providable::Singleton(_) + ), + "The provided type is not a singleton" + ); + + Ok(()) + } + + #[test] + #[cfg(feature = "factory")] + fn factory_provider_works() -> Result<(), Box> + { + use crate::interfaces::any_factory::AnyFactory; + use crate::ptr::FactoryPtr; + + struct FooFactory; + + impl AnyFactory for FooFactory {} + + let factory_provider = FactoryProvider::new(FactoryPtr::new(FooFactory), false); + let default_factory_provider = + FactoryProvider::new(FactoryPtr::new(FooFactory), true); + + let di_container = Rc::new(mocks::blocking_di_container::MockDIContainer::new()); + + assert!( + matches!( + factory_provider.provide(&di_container, vec![])?, + Providable::Factory(_) + ), + "The provided type is not a factory" + ); + + assert!( + matches!( + default_factory_provider.provide(&di_container, vec![])?, + Providable::DefaultFactory(_) + ), + "The provided type is not a default factory" + ); + + Ok(()) + } +} -- cgit v1.2.3-18-g5258