diff --git a/src/transform/swap.rs b/src/transform/swap.rs index 9525a80..f0ee618 100644 --- a/src/transform/swap.rs +++ b/src/transform/swap.rs @@ -25,3 +25,61 @@ impl Pattern for Swap { self.child.sample(pos.swap()) } } + +#[cfg(test)] +mod test { + use super::*; + use crate::MockPatternFactory; + use mockall::predicate::eq; + + #[test] + fn create_config_correct() { + let input = Config { + size: Vector::new(4.0, 2.0), + step: 0.4, + }; + let mut output = input.clone(); + output.size = Vector::new(2.0, 4.0); + + let mut child = MockPatternFactory::new(); + child + .expect_create() + .with(eq(output)) + .once() + .returning(|_| Box::new(MockPattern::new())); + + SwapFactory::new(Box::new(child)).create(&input); + } + + #[test] + fn sample_value_correct() { + let mut child = MockPatternFactory::new(); + child.expect_create().returning(|_| { + let mut sampler = MockPattern::new(); + sampler.expect_sample().return_const(0.4); + Box::new(sampler) + }); + + let sampler = SwapFactory::new(Box::new(child)).create(&Config::default()); + + assert_eq!(0.4, 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(9.0, 5.0))) + .once() + .return_const(0.0); + Box::new(sampler) + }); + + let sampler = SwapFactory::new(Box::new(child)).create(&Config::default()); + + sampler.sample(Vector::new(5.0, 9.0)); + } +}