diff --git a/src/transform/shift.rs b/src/transform/shift.rs index 8027ab8..2840473 100644 --- a/src/transform/shift.rs +++ b/src/transform/shift.rs @@ -23,3 +23,62 @@ impl Pattern for Shift { self.child.sample(pos) + 1.0 - 2.0 * self.shift } } + +#[cfg(test)] +mod test { + use super::*; + use crate::MockPatternFactory; + use mockall::predicate::eq; + + #[test] + fn create_config_correct() { + let config = Config { + size: Vector::new(4.0, 2.0), + step: 0.4, + }; + let mut child = MockPatternFactory::new(); + child + .expect_create() + .with(eq(config)) + .once() + .returning(|_| Box::new(MockPattern::new())); + + ShiftFactory::new(Box::new(child)).create(&config); + } + + #[test] + fn sample_shifted() { + let config = Config { + size: Vector::default(), + step: 0.4, + }; + let mut child = MockPatternFactory::new(); + child.expect_create().returning(|_| { + let mut sampler = MockPattern::new(); + sampler.expect_sample().return_const(0.6); + Box::new(sampler) + }); + + let sampler = ShiftFactory::new(Box::new(child)).create(&config); + + assert_eq!(0.8, sampler.sample(Vector::default())); + } + + #[test] + fn sample_pos_correct() { + let mut child = MockPatternFactory::new(); + child.expect_create().once().returning(|_| { + let mut sampler = MockPattern::new(); + sampler + .expect_sample() + .with(eq(Vector::new(6.0, 7.0))) + .once() + .return_const(0.0); + Box::new(sampler) + }); + + let sampler = ShiftFactory::new(Box::new(child)).create(&Config::default()); + + sampler.sample(Vector::new(6.0, 7.0)); + } +}