From 402077e109cfa14fa46076290cc8c735a5a8bab3 Mon Sep 17 00:00:00 2001 From: Rico Riedel Date: Fri, 5 Aug 2022 19:35:57 +0200 Subject: [PATCH] More unit tests --- src/pattern/circle.rs | 2 +- src/pattern/line.rs | 2 +- src/pattern/mod.rs | 36 ++++++++++++++++++++++++++++++++++-- src/pattern/rhombus.rs | 2 +- src/pattern/wheel.rs | 2 +- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/pattern/circle.rs b/src/pattern/circle.rs index c5abcbb..8db31aa 100644 --- a/src/pattern/circle.rs +++ b/src/pattern/circle.rs @@ -40,7 +40,7 @@ mod test { size: Vector::new(10.0, 20.0), step: 0.0, }; - let pattern = Circle::new(&config); + let pattern = CircleFactory::default().create(&config); assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(0.0, 0.0)), epsilon = 0.1); assert_abs_diff_eq!(0.0, pattern.sample(Vector::new(5.0, 10.0)), epsilon = 0.1); diff --git a/src/pattern/line.rs b/src/pattern/line.rs index 409faeb..c9d30d9 100644 --- a/src/pattern/line.rs +++ b/src/pattern/line.rs @@ -38,7 +38,7 @@ mod test { size: Vector::new(20.0, 0.0), step: 0.0, }; - let pattern = Line::new(&config); + let pattern = LineFactory::default().create(&config); assert_abs_diff_eq!(0.0, pattern.sample(Vector::new(0.0, 4.0)), epsilon = 0.1); assert_abs_diff_eq!(0.4, pattern.sample(Vector::new(8.0, 8.0)), epsilon = 0.1); diff --git a/src/pattern/mod.rs b/src/pattern/mod.rs index 96b1b77..6ea9489 100644 --- a/src/pattern/mod.rs +++ b/src/pattern/mod.rs @@ -73,6 +73,7 @@ impl Sampler for SamplerImpl { #[cfg(test)] mod test { use super::*; + use approx::*; use mockall::predicate::eq; #[test] @@ -86,7 +87,7 @@ mod test { let sampler = SamplerImpl::new(Box::new(char), Box::new(color)); - assert_eq!(2.5, sampler.char(Vector::new(2.0, 5.0))); + assert_abs_diff_eq!(2.5, sampler.char(Vector::new(2.0, 5.0))); } #[test] @@ -101,6 +102,37 @@ mod test { let sampler = SamplerImpl::new(Box::new(char), Box::new(color)); - assert_eq!(3.2, sampler.color(Vector::new(4.0, 2.0))); + assert_abs_diff_eq!(3.2, sampler.color(Vector::new(4.0, 2.0))); + } + + #[test] + fn factory() { + let mut char = MockPatternFactory::new(); + let mut color = MockPatternFactory::new(); + let config = Config { + size: Vector::new(2.0, 3.0), + step: 0.6, + }; + + char.expect_create().with(eq(config)).once().returning(|_| { + let mut sampler = MockPattern::new(); + sampler.expect_sample().return_const(3.0); + Box::new(sampler) + }); + color + .expect_create() + .with(eq(config)) + .once() + .returning(|_| { + let mut sampler = MockPattern::new(); + sampler.expect_sample().return_const(5.0); + Box::new(sampler) + }); + + let factory = SamplerFactoryImpl::new(Box::new(char), Box::new(color)); + let sampler = factory.create(&config); + + assert_abs_diff_eq!(3.0, sampler.char(Vector::default())); + assert_abs_diff_eq!(5.0, sampler.color(Vector::default())); } } diff --git a/src/pattern/rhombus.rs b/src/pattern/rhombus.rs index aaa019e..4a79f59 100644 --- a/src/pattern/rhombus.rs +++ b/src/pattern/rhombus.rs @@ -40,7 +40,7 @@ mod test { size: Vector::new(10.0, 5.0), step: 0.0, }; - let pattern = Rhombus::new(&config); + let pattern = RhombusFactory::default().create(&config); assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(0.0, 0.0)), epsilon = 0.1); assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(10.0, 5.0)), epsilon = 0.1); diff --git a/src/pattern/wheel.rs b/src/pattern/wheel.rs index b205276..c211d18 100644 --- a/src/pattern/wheel.rs +++ b/src/pattern/wheel.rs @@ -39,7 +39,7 @@ mod test { size: Vector::new(10.0, 20.0), step: 0.0, }; - let pattern = Wheel::new(&config); + let pattern = WheelFactory::default().create(&config); assert_abs_diff_eq!(0.0, pattern.sample(Vector::new(0.0, 9.0)), epsilon = 0.1); assert_abs_diff_eq!(1.0, pattern.sample(Vector::new(0.0, 10.0)), epsilon = 0.1);