From f5730be8354e0303167623ead4ef1d190dbfa93a Mon Sep 17 00:00:00 2001 From: Rico Riedel Date: Mon, 1 Aug 2022 11:46:07 +0200 Subject: [PATCH] Replace sampler --- src/main.rs | 3 +- src/pattern/mod.rs | 77 ++++++++++++++++++++++++ src/sampler.rs | 142 --------------------------------------------- 3 files changed, 78 insertions(+), 144 deletions(-) delete mode 100644 src/sampler.rs diff --git a/src/main.rs b/src/main.rs index 1f22017..d6d642f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ +pub mod convert; pub mod error; pub mod pattern; pub mod printer; -pub mod convert; pub mod term; mod vec; -mod sampler; fn main() -> Result<(), error::Error> { Ok(()) diff --git a/src/pattern/mod.rs b/src/pattern/mod.rs index 6cb1682..f6527d7 100644 --- a/src/pattern/mod.rs +++ b/src/pattern/mod.rs @@ -21,3 +21,80 @@ pub trait PatternFactory { pub trait Pattern { fn sample(&self, pos: Vector) -> f32; } + +pub trait SamplerFactory { + fn create(&self, config: &Config) -> Box; +} + +#[cfg_attr(test, mockall::automock)] +pub trait Sampler { + fn char(&self, pos: Vector) -> f32; + + fn color(&self, pos: Vector) -> f32; +} + +#[derive(derive_more::Constructor)] +pub struct SamplerFactoryImpl { + char: Box, + color: Box, +} + +#[derive(derive_more::Constructor)] +pub struct SamplerImpl { + char: Box, + color: Box, +} + +impl SamplerFactory for SamplerFactoryImpl { + fn create(&self, config: &Config) -> Box { + Box::new(SamplerImpl::new( + self.char.create(config), + self.color.create(config), + )) + } +} + +impl Sampler for SamplerImpl { + fn char(&self, pos: Vector) -> f32 { + self.char.sample(pos) + } + + fn color(&self, pos: Vector) -> f32 { + self.color.sample(pos) + } +} + +#[cfg(test)] +mod test { + use super::*; + use mockall::predicate::eq; + + #[test] + fn char() { + let mut char = MockPattern::new(); + let color = MockPattern::new(); + + char.expect_sample() + .with(eq(Vector::new(2.0, 5.0))) + .return_const(2.5); + + let sampler = SamplerImpl::new(Box::new(char), Box::new(color)); + + assert_eq!(2.5, sampler.char(Vector::new(2.0, 5.0))); + } + + #[test] + fn color() { + let char = MockPattern::new(); + let mut color = MockPattern::new(); + + color + .expect_sample() + .with(eq(Vector::new(4.0, 2.0))) + .return_const(3.2); + + let sampler = SamplerImpl::new(Box::new(char), Box::new(color)); + + assert_eq!(3.2, sampler.color(Vector::new(4.0, 2.0))); + } +} diff --git a/src/sampler.rs b/src/sampler.rs deleted file mode 100644 index 32de27f..0000000 --- a/src/sampler.rs +++ /dev/null @@ -1,142 +0,0 @@ -use crate::convert::char::CharSample; -use crate::convert::Converter; -use crate::pattern::{Config, Pattern, PatternFactory}; -use crate::vec::Vector; -use crossterm::style::Color; -use std::rc::Rc; - -#[derive(Copy, Clone, PartialEq, Debug)] -pub enum Sample { - Keep, - Draw { char: char, color: Color }, - Clear, -} - -pub trait SamplerFactory { - fn create(&self, config: &Config) -> Box; -} - -pub trait Sampler { - fn sample(&self, pos: Vector) -> Sample; -} - -#[derive(derive_more::Constructor)] -pub struct SamplerFactoryImpl { - char: Box, - color: Box, - converter: Rc, -} - -#[derive(derive_more::Constructor)] -pub struct SamplerImpl { - char: Box, - color: Box, - converter: Rc, -} - -impl SamplerFactory for SamplerFactoryImpl { - fn create(&self, config: &Config) -> Box { - Box::new(SamplerImpl::new( - self.char.create(config), - self.color.create(config), - self.converter.clone(), - )) - } -} - -impl Sampler for SamplerImpl { - fn sample(&self, pos: Vector) -> Sample { - match self.converter.char(self.char.sample(pos)) { - CharSample::Keep => Sample::Keep, - CharSample::Draw(char) => Sample::Draw { - char, - color: self.converter.color(self.color.sample(pos)), - }, - CharSample::Clear => Sample::Clear, - } - } -} - -#[cfg(test)] -mod test { - use super::*; - use crate::convert::MockConverter; - use crate::pattern::MockPattern; - use mockall::predicate::eq; - - #[test] - fn sample_keep() { - let color = MockPattern::new(); - let mut char = MockPattern::new(); - let mut converter = MockConverter::new(); - - char.expect_sample().return_const(3.0); - converter.expect_char().return_const(CharSample::Keep); - - let sampler = SamplerImpl::new(Box::new(char), Box::new(color), Rc::new(converter)); - - assert_eq!(Sample::Keep, sampler.sample(Vector::default())); - } - - #[test] - fn sample_draw() { - let mut char = MockPattern::new(); - let mut color = MockPattern::new(); - let mut converter = MockConverter::new(); - - char.expect_sample().return_const(3.0); - color.expect_sample().return_const(2.0); - converter.expect_char().return_const(CharSample::Draw('M')); - converter.expect_color().return_const(Color::Red); - - let sampler = SamplerImpl::new(Box::new(char), Box::new(color), Rc::new(converter)); - - assert_eq!( - Sample::Draw { - char: 'M', - color: Color::Red - }, - sampler.sample(Vector::default()) - ); - } - - #[test] - fn sample_clear() { - let color = MockPattern::new(); - let mut char = MockPattern::new(); - let mut converter = MockConverter::new(); - - char.expect_sample().return_const(3.0); - converter.expect_char().return_const(CharSample::Clear); - - let sampler = SamplerImpl::new(Box::new(char), Box::new(color), Rc::new(converter)); - - assert_eq!(Sample::Clear, sampler.sample(Vector::default())); - } - - #[test] - fn sample_args_correct() { - let mut char = MockPattern::new(); - let mut color = MockPattern::new(); - let mut converter = MockConverter::new(); - - char.expect_sample() - .with(eq(Vector::new(4.0, 2.0))) - .return_const(6.0); - color - .expect_sample() - .with(eq(Vector::new(4.0, 2.0))) - .return_const(7.0); - converter - .expect_char() - .with(eq(6.0)) - .return_const(CharSample::Draw('A')); - converter - .expect_color() - .with(eq(7.0)) - .return_const(Color::Reset); - - SamplerImpl::new(Box::new(char), Box::new(color), Rc::new(converter)) - .sample(Vector::new(4.0, 2.0)); - } -}