From d9fb89865165bdd31807c3fbec2def6d3ebb7b53 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 18 Aug 2023 21:53:36 +0200 Subject: refactor: replace use_dependency_history with a more generic macro --- src/di_container/asynchronous/binding/builder.rs | 4 +- .../asynchronous/binding/scope_configurator.rs | 4 +- src/di_container/asynchronous/mod.rs | 4 +- src/di_container/blocking/binding/builder.rs | 4 +- .../blocking/binding/scope_configurator.rs | 4 +- src/di_container/blocking/mod.rs | 4 +- src/interfaces/async_injectable.rs | 4 +- src/interfaces/injectable.rs | 4 +- src/provider/async.rs | 4 +- src/provider/blocking.rs | 4 +- src/test_utils.rs | 24 +++++------ src/util.rs | 46 +++++++++++++++++----- 12 files changed, 68 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/di_container/asynchronous/binding/builder.rs b/src/di_container/asynchronous/binding/builder.rs index 9fa5115..5862d63 100644 --- a/src/di_container/asynchronous/binding/builder.rs +++ b/src/di_container/asynchronous/binding/builder.rs @@ -11,9 +11,9 @@ use crate::di_container::asynchronous::binding::when_configurator::AsyncBindingW use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::async_di_container::AsyncBindingBuilderError; use crate::interfaces::async_injectable::AsyncInjectable; -use crate::util::use_dependency_history; +use crate::util::use_double; -use_dependency_history!(); +use_double!(crate::dependency_history::DependencyHistory); /// Alias for a threadsafe boxed function. #[cfg(feature = "factory")] diff --git a/src/di_container/asynchronous/binding/scope_configurator.rs b/src/di_container/asynchronous/binding/scope_configurator.rs index a4a684b..0b5bad8 100644 --- a/src/di_container/asynchronous/binding/scope_configurator.rs +++ b/src/di_container/asynchronous/binding/scope_configurator.rs @@ -10,9 +10,9 @@ use crate::errors::async_di_container::AsyncBindingScopeConfiguratorError; use crate::interfaces::async_injectable::AsyncInjectable; use crate::provider::r#async::{AsyncSingletonProvider, AsyncTransientTypeProvider}; use crate::ptr::ThreadsafeSingletonPtr; -use crate::util::use_dependency_history; +use crate::util::use_double; -use_dependency_history!(); +use_double!(crate::dependency_history::DependencyHistory); /// Scope configurator for a binding for type `Interface` inside a [`IAsyncDIContainer`]. /// diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index 0db6782..2939ddd 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -66,9 +66,9 @@ use crate::private::cast::boxed::CastBox; use crate::private::cast::error::CastError; use crate::provider::r#async::{AsyncProvidable, IAsyncProvider}; use crate::ptr::SomePtr; -use crate::util::use_dependency_history; +use crate::util::use_double; -use_dependency_history!(); +use_double!(crate::dependency_history::DependencyHistory); pub mod binding; pub mod prelude; diff --git a/src/di_container/blocking/binding/builder.rs b/src/di_container/blocking/binding/builder.rs index 91855f5..0c323ec 100644 --- a/src/di_container/blocking/binding/builder.rs +++ b/src/di_container/blocking/binding/builder.rs @@ -11,9 +11,9 @@ use crate::di_container::blocking::binding::when_configurator::BindingWhenConfig use crate::di_container::blocking::IDIContainer; use crate::errors::di_container::BindingBuilderError; use crate::interfaces::injectable::Injectable; -use crate::util::use_dependency_history; +use crate::util::use_double; -use_dependency_history!(); +use_double!(crate::dependency_history::DependencyHistory); /// Binding builder for type `Interface` inside a [`IDIContainer`]. /// diff --git a/src/di_container/blocking/binding/scope_configurator.rs b/src/di_container/blocking/binding/scope_configurator.rs index 0e2437f..0aefa93 100644 --- a/src/di_container/blocking/binding/scope_configurator.rs +++ b/src/di_container/blocking/binding/scope_configurator.rs @@ -10,9 +10,9 @@ use crate::errors::di_container::BindingScopeConfiguratorError; use crate::interfaces::injectable::Injectable; use crate::provider::blocking::{SingletonProvider, TransientTypeProvider}; use crate::ptr::SingletonPtr; -use crate::util::use_dependency_history; +use crate::util::use_double; -use_dependency_history!(); +use_double!(crate::dependency_history::DependencyHistory); /// Scope configurator for a binding for type `Interface` inside a [`IDIContainer`]. /// diff --git a/src/di_container/blocking/mod.rs b/src/di_container/blocking/mod.rs index aa89aad..5a27f78 100644 --- a/src/di_container/blocking/mod.rs +++ b/src/di_container/blocking/mod.rs @@ -61,9 +61,9 @@ use crate::private::cast::boxed::CastBox; use crate::private::cast::rc::CastRc; use crate::provider::blocking::{IProvider, Providable}; use crate::ptr::SomePtr; -use crate::util::use_dependency_history; +use crate::util::use_double; -use_dependency_history!(); +use_double!(crate::dependency_history::DependencyHistory); pub mod binding; pub mod prelude; diff --git a/src/interfaces/async_injectable.rs b/src/interfaces/async_injectable.rs index 2364ae1..56b263d 100644 --- a/src/interfaces/async_injectable.rs +++ b/src/interfaces/async_injectable.rs @@ -7,9 +7,9 @@ use crate::errors::injectable::InjectableError; use crate::future::BoxFuture; use crate::private::cast::CastFromArc; use crate::ptr::TransientPtr; -use crate::util::use_dependency_history; +use crate::util::use_double; -use_dependency_history!(); +use_double!(crate::dependency_history::DependencyHistory); /// Interface for structs that can be injected into or be injected to. pub trait AsyncInjectable: CastFromArc diff --git a/src/interfaces/injectable.rs b/src/interfaces/injectable.rs index 82b773a..2622320 100644 --- a/src/interfaces/injectable.rs +++ b/src/interfaces/injectable.rs @@ -6,9 +6,9 @@ use crate::di_container::blocking::IDIContainer; use crate::errors::injectable::InjectableError; use crate::private::cast::CastFrom; use crate::ptr::TransientPtr; -use crate::util::use_dependency_history; +use crate::util::use_double; -use_dependency_history!(); +use_double!(crate::dependency_history::DependencyHistory); /// Interface for structs that can be injected into or be injected to. pub trait Injectable: CastFrom diff --git a/src/provider/async.rs b/src/provider/async.rs index c2d0bfe..2a241a7 100644 --- a/src/provider/async.rs +++ b/src/provider/async.rs @@ -7,9 +7,9 @@ use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::injectable::InjectableError; use crate::interfaces::async_injectable::AsyncInjectable; use crate::ptr::{ThreadsafeSingletonPtr, TransientPtr}; -use crate::util::use_dependency_history; +use crate::util::use_double; -use_dependency_history!(); +use_double!(crate::dependency_history::DependencyHistory); #[derive(strum_macros::Display, Debug)] pub enum AsyncProvidable diff --git a/src/provider/blocking.rs b/src/provider/blocking.rs index a18e997..373dabd 100644 --- a/src/provider/blocking.rs +++ b/src/provider/blocking.rs @@ -5,9 +5,9 @@ use crate::di_container::blocking::IDIContainer; use crate::errors::injectable::InjectableError; use crate::interfaces::injectable::Injectable; use crate::ptr::{SingletonPtr, TransientPtr}; -use crate::util::use_dependency_history; +use crate::util::use_double; -use_dependency_history!(); +use_double!(crate::dependency_history::DependencyHistory); #[derive(strum_macros::Display, Debug)] pub enum Providable diff --git a/src/test_utils.rs b/src/test_utils.rs index 3d51ffe..1fe4417 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -12,7 +12,7 @@ pub mod subjects use crate::private::cast::CastFromArc; use crate::ptr::TransientPtr; - use_dependency_history!(); + use_double!(crate::dependency_history::DependencyHistory); pub trait IUserManager { @@ -45,7 +45,7 @@ pub mod subjects } use crate as syrette; - use crate::util::use_dependency_history; + use crate::util::use_double; declare_interface!(UserManager -> IUserManager); @@ -153,7 +153,7 @@ pub mod subjects_async use crate::interfaces::async_injectable::AsyncInjectable; use crate::ptr::TransientPtr; - use_dependency_history!(); + use_double!(crate::dependency_history::DependencyHistory); pub trait IUserManager: Send + Sync { @@ -186,7 +186,7 @@ pub mod subjects_async } use crate as syrette; - use crate::util::use_dependency_history; + use crate::util::use_double; declare_interface!(UserManager -> IUserManager); @@ -292,9 +292,9 @@ pub mod mocks use crate::errors::di_container::DIContainerError; use crate::provider::blocking::IProvider; use crate::ptr::SomePtr; - use crate::util::use_dependency_history; + use crate::util::use_double; - use_dependency_history!(); + use_double!(crate::dependency_history::DependencyHistory); mock! { pub DIContainer {} @@ -362,9 +362,9 @@ pub mod mocks use crate::errors::async_di_container::AsyncDIContainerError; use crate::provider::r#async::IAsyncProvider; use crate::ptr::SomePtr; - use crate::util::use_dependency_history; + use crate::util::use_double; - use_dependency_history!(); + use_double!(crate::dependency_history::DependencyHistory); mock! { pub AsyncDIContainer {} @@ -438,9 +438,9 @@ pub mod mocks use crate::di_container::asynchronous::IAsyncDIContainer; use crate::errors::injectable::InjectableError; use crate::provider::r#async::{AsyncProvidable, IAsyncProvider}; - use crate::util::use_dependency_history; + use crate::util::use_double; - use_dependency_history!(); + use_double!(crate::dependency_history::DependencyHistory); mock! { pub AsyncProvider {} @@ -472,9 +472,9 @@ pub mod mocks use crate::di_container::blocking::IDIContainer; use crate::errors::injectable::InjectableError; use crate::provider::blocking::{IProvider, Providable}; - use crate::util::use_dependency_history; + use crate::util::use_double; - use_dependency_history!(); + use_double!(crate::dependency_history::DependencyHistory); mock! { pub Provider diff --git a/src/util.rs b/src/util.rs index 2d2d911..373d784 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,15 +1,41 @@ -#[cfg(not(test))] -macro_rules! use_dependency_history { - () => { - use $crate::dependency_history::DependencyHistory; +//! Internal utilities. + +/// Imports the specified item, prepending 'Mock' to the item identifier if the `test` +/// configuration option is set. +/// +/// # Examples +/// ```ignore +/// use_double!(crate::dependency_history::DependencyHistory); +/// ``` +///
+/// +/// Expands to the following when `cfg(not(test))` +/// ```ignore +/// use crate::dependency_history::DependencyHistory; +/// ``` +///
+/// +/// Expands to the following when `cfg(test)` +/// ```ignore +/// use crate::dependency_history::MockDependencyHistory as DependencyHistory; +/// ``` +macro_rules! use_double { + ($([$($part: ident),*])? $item_path_part: ident :: $($next_part: tt)+) => { + use_double!( + [$($($part,)*)? $item_path_part] + $($next_part)+ + ); }; -} -#[cfg(test)] -macro_rules! use_dependency_history { - () => { - use $crate::dependency_history::MockDependencyHistory as DependencyHistory; + ([$($part: ident),*] $item_path_part: ident) => { + #[cfg(not(test))] + use $($part::)* $item_path_part; + + ::paste::paste! { + #[cfg(test)] + use $($part::)* [] as $item_path_part; + } }; } -pub(crate) use use_dependency_history; +pub(crate) use use_double; -- cgit v1.2.3-18-g5258