diff options
| author | HampusM <hampus@hampusmat.com> | 2022-11-06 19:03:56 +0100 | 
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2022-11-06 19:03:56 +0100 | 
| commit | 56349a530811bbf0657fed14912c7e02001b2c8a (patch) | |
| tree | 165247000ceef0ceeaf3d0b7dbe48f9a6af10f29 | |
| parent | e282375de4ba75c69f7d619fc33c6250f6caba18 (diff) | |
test: split up cast unit tests into their respective modules
| -rw-r--r-- | src/libs/intertrait/cast/arc.rs | 50 | ||||
| -rw-r--r-- | src/libs/intertrait/cast/box.rs | 49 | ||||
| -rw-r--r-- | src/libs/intertrait/cast/rc.rs | 49 | ||||
| -rw-r--r-- | src/libs/intertrait/mod.rs | 104 | ||||
| -rw-r--r-- | src/test_utils.rs | 8 | 
5 files changed, 164 insertions, 96 deletions
| diff --git a/src/libs/intertrait/cast/arc.rs b/src/libs/intertrait/cast/arc.rs index 135cf64..6d3867c 100644 --- a/src/libs/intertrait/cast/arc.rs +++ b/src/libs/intertrait/cast/arc.rs @@ -45,3 +45,53 @@ impl<CastFromSelf: ?Sized + CastFromSync> CastArc for CastFromSelf          })      }  } + +#[cfg(test)] +mod tests +{ +    use std::any::Any; +    use std::fmt::{Debug, Display}; +    use std::sync::Arc; + +    use super::*; +    use crate::test_utils::subjects; + +    #[test] +    fn can_cast_arc() +    { +        let concrete_ninja = Arc::new(subjects::Ninja); + +        let abstract_ninja: Arc<dyn subjects::INinja> = concrete_ninja; + +        let debug_ninja_result = abstract_ninja.cast::<dyn Debug>(); + +        assert!(debug_ninja_result.is_ok()); +    } + +    #[test] +    fn cannot_cast_arc_wrong() +    { +        let concrete_ninja = Arc::new(subjects::Ninja); + +        let abstract_ninja: Arc<dyn subjects::INinja> = concrete_ninja; + +        let display_ninja_result = abstract_ninja.cast::<dyn Display>(); + +        assert!(matches!( +            display_ninja_result, +            Err(CastError::GetCasterFailed(_)) +        )); +    } + +    #[test] +    fn can_cast_arc_from_any() +    { +        let concrete_ninja = Arc::new(subjects::Ninja); + +        let any_ninja: Arc<dyn Any + Send + Sync> = concrete_ninja; + +        let debug_ninja_result = any_ninja.cast::<dyn Debug>(); + +        assert!(debug_ninja_result.is_ok()); +    } +} diff --git a/src/libs/intertrait/cast/box.rs b/src/libs/intertrait/cast/box.rs index 67fd949..0e160f4 100644 --- a/src/libs/intertrait/cast/box.rs +++ b/src/libs/intertrait/cast/box.rs @@ -40,3 +40,52 @@ impl<CastFromSelf: ?Sized + CastFrom> CastBox for CastFromSelf          })      }  } + +#[cfg(test)] +mod tests +{ +    use std::any::Any; +    use std::fmt::{Debug, Display}; + +    use super::*; +    use crate::test_utils::subjects; + +    #[test] +    fn can_cast_box() +    { +        let concrete_ninja = Box::new(subjects::Ninja); + +        let abstract_ninja: Box<dyn subjects::INinja> = concrete_ninja; + +        let debug_ninja_result = abstract_ninja.cast::<dyn Debug>(); + +        assert!(debug_ninja_result.is_ok()); +    } + +    #[test] +    fn cannot_cast_box_wrong() +    { +        let concrete_ninja = Box::new(subjects::Ninja); + +        let abstract_ninja: Box<dyn subjects::INinja> = concrete_ninja; + +        let display_ninja_result = abstract_ninja.cast::<dyn Display>(); + +        assert!(matches!( +            display_ninja_result, +            Err(CastError::GetCasterFailed(_)) +        )); +    } + +    #[test] +    fn can_cast_box_from_any() +    { +        let concrete_ninja = Box::new(subjects::Ninja); + +        let any_ninja: Box<dyn Any> = concrete_ninja; + +        let debug_ninja_result = any_ninja.cast::<dyn Debug>(); + +        assert!(debug_ninja_result.is_ok()); +    } +} diff --git a/src/libs/intertrait/cast/rc.rs b/src/libs/intertrait/cast/rc.rs index ec70544..906490d 100644 --- a/src/libs/intertrait/cast/rc.rs +++ b/src/libs/intertrait/cast/rc.rs @@ -40,3 +40,52 @@ impl<CastFromSelf: ?Sized + CastFrom> CastRc for CastFromSelf          })      }  } + +#[cfg(test)] +mod tests +{ +    use std::any::Any; +    use std::fmt::{Debug, Display}; + +    use super::*; +    use crate::test_utils::subjects; + +    #[test] +    fn can_cast_rc() +    { +        let concrete_ninja = Rc::new(subjects::Ninja); + +        let abstract_ninja: Rc<dyn subjects::INinja> = concrete_ninja; + +        let debug_ninja_result = abstract_ninja.cast::<dyn Debug>(); + +        assert!(debug_ninja_result.is_ok()); +    } + +    #[test] +    fn cannot_cast_rc_wrong() +    { +        let concrete_ninja = Rc::new(subjects::Ninja); + +        let abstract_ninja: Rc<dyn subjects::INinja> = concrete_ninja; + +        let display_ninja_result = abstract_ninja.cast::<dyn Display>(); + +        assert!(matches!( +            display_ninja_result, +            Err(CastError::GetCasterFailed(_)) +        )); +    } + +    #[test] +    fn can_cast_rc_from_any() +    { +        let concrete_ninja = Rc::new(subjects::Ninja); + +        let any_ninja: Rc<dyn Any> = concrete_ninja; + +        let debug_ninja_result = any_ninja.cast::<dyn Debug>(); + +        assert!(debug_ninja_result.is_ok()); +    } +} diff --git a/src/libs/intertrait/mod.rs b/src/libs/intertrait/mod.rs index 78f98b1..4a9322f 100644 --- a/src/libs/intertrait/mod.rs +++ b/src/libs/intertrait/mod.rs @@ -244,46 +244,39 @@ impl CastFromSync for dyn Any + Sync + Send + 'static  #[cfg(test)]  mod tests  { -    use std::any::{Any, TypeId}; -    use std::fmt::{Debug, Display}; +    use std::any::TypeId; +    use std::fmt::Debug;      use linkme::distributed_slice; -    #[allow(clippy::wildcard_imports)] -    use super::cast::*;      use super::*; +    use crate::test_utils::subjects;      #[distributed_slice(super::CASTERS)]      static TEST_CASTER: fn() -> (TypeId, BoxedCaster) = create_test_caster; -    #[derive(Debug)] -    struct TestStruct; - -    trait SourceTrait: CastFromSync {} - -    impl SourceTrait for TestStruct {} -      fn create_test_caster() -> (TypeId, BoxedCaster)      { -        let type_id = TypeId::of::<TestStruct>(); +        let type_id = TypeId::of::<subjects::Ninja>(); +          let caster = Box::new(Caster::<dyn Debug> {              cast_box: |from| {                  let concrete = from -                    .downcast::<TestStruct>() +                    .downcast::<subjects::Ninja>()                      .map_err(|_| CasterError::CastBoxFailed)?;                  Ok(concrete as Box<dyn Debug>)              },              cast_rc: |from| {                  let concrete = from -                    .downcast::<TestStruct>() +                    .downcast::<subjects::Ninja>()                      .map_err(|_| CasterError::CastRcFailed)?;                  Ok(concrete as Rc<dyn Debug>)              },              opt_cast_arc: Some(|from| {                  let concrete = from -                    .downcast::<TestStruct>() +                    .downcast::<subjects::Ninja>()                      .map_err(|_| CasterError::CastArcFailed)?;                  Ok(concrete as Arc<dyn Debug>) @@ -291,85 +284,4 @@ mod tests          });          (type_id, caster)      } - -    #[test] -    fn cast_box() -    { -        let ts = Box::new(TestStruct); -        let st: Box<dyn SourceTrait> = ts; -        let debug = st.cast::<dyn Debug>(); -        assert!(debug.is_ok()); -    } - -    #[test] -    fn cast_rc() -    { -        let ts = Rc::new(TestStruct); -        let st: Rc<dyn SourceTrait> = ts; -        let debug = st.cast::<dyn Debug>(); -        assert!(debug.is_ok()); -    } - -    #[test] -    fn cast_arc() -    { -        let ts = Arc::new(TestStruct); -        let st: Arc<dyn SourceTrait> = ts; -        let debug = st.cast::<dyn Debug>(); -        assert!(debug.is_ok()); -    } - -    #[test] -    fn cast_box_wrong() -    { -        let ts = Box::new(TestStruct); -        let st: Box<dyn SourceTrait> = ts; -        let display = st.cast::<dyn Display>(); -        assert!(display.is_err()); -    } - -    #[test] -    fn cast_rc_wrong() -    { -        let ts = Rc::new(TestStruct); -        let st: Rc<dyn SourceTrait> = ts; -        let display = st.cast::<dyn Display>(); -        assert!(display.is_err()); -    } - -    #[test] -    fn cast_arc_wrong() -    { -        let ts = Arc::new(TestStruct); -        let st: Arc<dyn SourceTrait> = ts; -        let display = st.cast::<dyn Display>(); -        assert!(display.is_err()); -    } - -    #[test] -    fn cast_box_from_any() -    { -        let ts = Box::new(TestStruct); -        let st: Box<dyn Any> = ts; -        let debug = st.cast::<dyn Debug>(); -        assert!(debug.is_ok()); -    } - -    #[test] -    fn cast_rc_from_any() -    { -        let ts = Rc::new(TestStruct); -        let st: Rc<dyn Any> = ts; -        let debug = st.cast::<dyn Debug>(); -        assert!(debug.is_ok()); -    } - -    #[test] -    fn cast_arc_from_any() -    { -        let ts = Arc::new(TestStruct); -        let st: Arc<dyn Any + Send + Sync> = ts; -        let debug = st.cast::<dyn Debug>(); -        assert!(debug.is_ok()); -    }  } diff --git a/src/test_utils.rs b/src/test_utils.rs index b4ec951..db055d4 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -10,6 +10,7 @@ pub mod subjects      use crate::dependency_history::IDependencyHistory;      use crate::di_container::blocking::IDIContainer;      use crate::interfaces::injectable::Injectable; +    use crate::libs::intertrait::CastFromSync;      use crate::ptr::TransientPtr;      pub trait IUserManager @@ -130,6 +131,13 @@ pub mod subjects              Ok(TransientPtr::new(Self::new()))          }      } + +    #[derive(Debug)] +    pub struct Ninja; + +    pub trait INinja: CastFromSync {} + +    impl INinja for Ninja {}  }  #[cfg(feature = "async")] | 
