From 56349a530811bbf0657fed14912c7e02001b2c8a Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 6 Nov 2022 19:03:56 +0100 Subject: test: split up cast unit tests into their respective modules --- src/libs/intertrait/cast/arc.rs | 50 +++++++++++++++++++++++++++++++++++++++++ src/libs/intertrait/cast/box.rs | 49 ++++++++++++++++++++++++++++++++++++++++ src/libs/intertrait/cast/rc.rs | 49 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+) (limited to 'src/libs/intertrait/cast') 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 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 = concrete_ninja; + + let debug_ninja_result = abstract_ninja.cast::(); + + assert!(debug_ninja_result.is_ok()); + } + + #[test] + fn cannot_cast_arc_wrong() + { + let concrete_ninja = Arc::new(subjects::Ninja); + + let abstract_ninja: Arc = concrete_ninja; + + let display_ninja_result = abstract_ninja.cast::(); + + 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 = concrete_ninja; + + let debug_ninja_result = any_ninja.cast::(); + + 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 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 = concrete_ninja; + + let debug_ninja_result = abstract_ninja.cast::(); + + assert!(debug_ninja_result.is_ok()); + } + + #[test] + fn cannot_cast_box_wrong() + { + let concrete_ninja = Box::new(subjects::Ninja); + + let abstract_ninja: Box = concrete_ninja; + + let display_ninja_result = abstract_ninja.cast::(); + + 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 = concrete_ninja; + + let debug_ninja_result = any_ninja.cast::(); + + 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 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 = concrete_ninja; + + let debug_ninja_result = abstract_ninja.cast::(); + + assert!(debug_ninja_result.is_ok()); + } + + #[test] + fn cannot_cast_rc_wrong() + { + let concrete_ninja = Rc::new(subjects::Ninja); + + let abstract_ninja: Rc = concrete_ninja; + + let display_ninja_result = abstract_ninja.cast::(); + + 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 = concrete_ninja; + + let debug_ninja_result = any_ninja.cast::(); + + assert!(debug_ninja_result.is_ok()); + } +} -- cgit v1.2.3-18-g5258