From 17fb8ef17d140db1dc887220113f18e6fde79bef Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 24 Oct 2022 21:30:52 +0200 Subject: test: add castable factory unit tests --- src/castable_factory/blocking.rs | 36 +++++++++++++++++++++++++++++------- src/castable_factory/threadsafe.rs | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 14 deletions(-) (limited to 'src/castable_factory') diff --git a/src/castable_factory/blocking.rs b/src/castable_factory/blocking.rs index 5dc12e5..9ef9864 100644 --- a/src/castable_factory/blocking.rs +++ b/src/castable_factory/blocking.rs @@ -78,20 +78,42 @@ mod tests { use super::*; + #[derive(Debug, PartialEq, Eq)] + struct Bacon + { + heal_amount: u32, + } + #[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.call((27,)); + + assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 27 })); + } + + #[test] + fn can_call_mut() + { + let mut castable_factory = + CastableFactory::new(&|heal_amount| TransientPtr::new(Bacon { heal_amount })); + + let output = castable_factory.call_mut((103,)); + + assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 103 })); + } + + #[test] + fn can_call_once() + { let castable_factory = CastableFactory::new(&|heal_amount| TransientPtr::new(Bacon { heal_amount })); - let output = castable_factory(27); + let output = castable_factory.call_once((19,)); - assert_eq!(output, Box::new(Bacon { heal_amount: 27 })); + assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 19 })); } } diff --git a/src/castable_factory/threadsafe.rs b/src/castable_factory/threadsafe.rs index 84e15b9..8f9c072 100644 --- a/src/castable_factory/threadsafe.rs +++ b/src/castable_factory/threadsafe.rs @@ -91,21 +91,45 @@ mod tests { use super::*; + #[derive(Debug, PartialEq, Eq)] + struct Bacon + { + heal_amount: u32, + } + #[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.call((27,)); + assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 27 })); + } + + #[test] + fn can_call_mut() + { + let mut castable_factory = ThreadsafeCastableFactory::new(&|heal_amount| { + TransientPtr::new(Bacon { heal_amount }) + }); + + let output = castable_factory.call_mut((1092,)); + + assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 1092 })); + } + + #[test] + fn can_call_once() + { let castable_factory = ThreadsafeCastableFactory::new(&|heal_amount| { TransientPtr::new(Bacon { heal_amount }) }); - let output = castable_factory(27); + let output = castable_factory.call_once((547,)); - assert_eq!(output, Box::new(Bacon { heal_amount: 27 })); + assert_eq!(output, TransientPtr::new(Bacon { heal_amount: 547 })); } } -- cgit v1.2.3-18-g5258