From fd5b6786d29d056ff0721a59435b50005f13f05c Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 9 Oct 2022 20:41:09 +0200 Subject: test: add more unit tests --- src/castable_factory/blocking.rs | 23 +++++ src/castable_factory/threadsafe.rs | 24 +++++ src/di_container/binding_map.rs | 201 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 + 4 files changed, 251 insertions(+) (limited to 'src') diff --git a/src/castable_factory/blocking.rs b/src/castable_factory/blocking.rs index 5ff4db0..5dc12e5 100644 --- a/src/castable_factory/blocking.rs +++ b/src/castable_factory/blocking.rs @@ -72,3 +72,26 @@ where ReturnInterface: 'static + ?Sized, { } + +#[cfg(test)] +mod tests +{ + use super::*; + + #[test] + fn can_call() + { + #[derive(Debug, PartialEq, Eq)] + struct Bacon + { + heal_amount: u32, + } + + let castable_factory = + CastableFactory::new(&|heal_amount| TransientPtr::new(Bacon { heal_amount })); + + let output = castable_factory(27); + + assert_eq!(output, Box::new(Bacon { heal_amount: 27 })); + } +} diff --git a/src/castable_factory/threadsafe.rs b/src/castable_factory/threadsafe.rs index 08c5ecf..84e15b9 100644 --- a/src/castable_factory/threadsafe.rs +++ b/src/castable_factory/threadsafe.rs @@ -85,3 +85,27 @@ where ReturnInterface: 'static + ?Sized, { } + +#[cfg(test)] +mod tests +{ + use super::*; + + #[test] + fn can_call() + { + #[derive(Debug, PartialEq, Eq)] + struct Bacon + { + heal_amount: u32, + } + + let castable_factory = ThreadsafeCastableFactory::new(&|heal_amount| { + TransientPtr::new(Bacon { heal_amount }) + }); + + let output = castable_factory(27); + + assert_eq!(output, Box::new(Bacon { heal_amount: 27 })); + } +} diff --git a/src/di_container/binding_map.rs b/src/di_container/binding_map.rs index eb71ff7..6ecd34c 100644 --- a/src/di_container/binding_map.rs +++ b/src/di_container/binding_map.rs @@ -89,3 +89,204 @@ where self.bindings.len() } } + +#[cfg(test)] +mod tests +{ + use super::*; + + mod subjects + { + pub trait SomeProvider + { + fn get_id(&self) -> u8; + } + + pub struct SomeProviderImpl + { + pub id: u8, + } + + impl SomeProvider for SomeProviderImpl + { + fn get_id(&self) -> u8 + { + self.id + } + } + } + + #[test] + fn can_get() + { + type Interface = (); + + let mut binding_map = DIContainerBindingMap::::new(); + + binding_map.bindings.insert( + DIContainerBindingKey { + type_id: TypeId::of::(), + name: None, + }, + Box::new(subjects::SomeProviderImpl { id: 20 }), + ); + + assert!(binding_map + .get::(None) + .map_or_else(|| false, |provider| provider.get_id() == 20)); + } + + #[test] + fn can_get_with_name() + { + type Interface = (); + + let mut binding_map = DIContainerBindingMap::::new(); + + binding_map.bindings.insert( + DIContainerBindingKey { + type_id: TypeId::of::(), + name: Some("hello"), + }, + Box::new(subjects::SomeProviderImpl { id: 11 }), + ); + + assert!(binding_map + .get::(Some("hello")) + .map_or_else(|| false, |provider| provider.get_id() == 11)); + + assert!(binding_map.get::(None).is_none()); + } + + #[test] + fn can_set() + { + type Interface = (); + + let mut binding_map = DIContainerBindingMap::::new(); + + binding_map + .set::(None, Box::new(subjects::SomeProviderImpl { id: 65 })); + + let expected_key = &DIContainerBindingKey { + type_id: TypeId::of::(), + name: None, + }; + + assert!(binding_map.bindings.contains_key(expected_key)); + + assert_eq!(binding_map.bindings[expected_key].get_id(), 65); + } + + #[test] + fn can_set_with_name() + { + type Interface = (); + + let mut binding_map = DIContainerBindingMap::::new(); + + binding_map.set::( + Some("special"), + Box::new(subjects::SomeProviderImpl { id: 3 }), + ); + + let expected_key = &DIContainerBindingKey { + type_id: TypeId::of::(), + name: Some("special"), + }; + + assert!(binding_map.bindings.contains_key(expected_key)); + + assert_eq!(binding_map.bindings[expected_key].get_id(), 3); + } + + #[test] + fn can_remove() + { + type Interface = (); + + let mut binding_map = DIContainerBindingMap::::new(); + + binding_map.bindings.insert( + DIContainerBindingKey { + type_id: TypeId::of::(), + name: None, + }, + Box::new(subjects::SomeProviderImpl { id: 103 }), + ); + + binding_map.remove::(None); + + let expected_key = &DIContainerBindingKey { + type_id: TypeId::of::(), + name: None, + }; + + assert!(!binding_map.bindings.contains_key(expected_key)); + } + + #[test] + fn can_remove_with_name() + { + type Interface = (); + + let mut binding_map = DIContainerBindingMap::::new(); + + binding_map.bindings.insert( + DIContainerBindingKey { + type_id: TypeId::of::(), + name: Some("cool"), + }, + Box::new(subjects::SomeProviderImpl { id: 42 }), + ); + + binding_map.remove::(Some("cool")); + + let expected_key = &DIContainerBindingKey { + type_id: TypeId::of::(), + name: Some("cool"), + }; + + assert!(!binding_map.bindings.contains_key(expected_key)); + } + + #[test] + fn can_get_has() + { + type Interface = (); + + let mut binding_map = DIContainerBindingMap::::new(); + + assert!(!binding_map.has::(None)); + + binding_map.bindings.insert( + DIContainerBindingKey { + type_id: TypeId::of::(), + name: None, + }, + Box::new(subjects::SomeProviderImpl { id: 103 }), + ); + + assert!(binding_map.has::(None)); + } + + #[test] + fn can_get_has_with_name() + { + type Interface = (); + + let mut binding_map = DIContainerBindingMap::::new(); + + assert!(!binding_map.has::(Some("awesome"))); + + binding_map.bindings.insert( + DIContainerBindingKey { + type_id: TypeId::of::(), + name: Some("awesome"), + }, + Box::new(subjects::SomeProviderImpl { id: 101 }), + ); + + assert!(binding_map.has::(Some("awesome"))); + } +} diff --git a/src/lib.rs b/src/lib.rs index d01ecc2..2cc3cc4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(feature = "factory", feature(unboxed_closures, fn_traits))] #![cfg_attr(doc_cfg, feature(doc_cfg))] +#![cfg_attr(test, feature(register_tool))] +#![cfg_attr(test, register_tool(tarpaulin))] #![deny(clippy::all)] #![deny(clippy::pedantic)] #![allow(clippy::module_name_repetitions)] @@ -41,6 +43,7 @@ pub mod libs; mod provider; #[cfg(test)] +#[tarpaulin::skip] mod test_utils; /// Shortcut for creating a DI container binding for a injectable without a declared -- cgit v1.2.3-18-g5258