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);