From e4116130d9d539ab9f080508dd201d66247d78ba Mon Sep 17 00:00:00 2001 From: Nicolas <> Date: Sat, 9 Apr 2022 16:34:43 +0200 Subject: [PATCH] Replace with mockall --- src/animation/mod.rs | 3 ++ src/char.rs | 3 ++ src/color.rs | 2 ++ src/fill/mod.rs | 3 ++ src/sampler.rs | 71 ++++++++++++++++++++++++-------------------- 5 files changed, 49 insertions(+), 33 deletions(-) diff --git a/src/animation/mod.rs b/src/animation/mod.rs index 51831f9..74e01d4 100644 --- a/src/animation/mod.rs +++ b/src/animation/mod.rs @@ -2,6 +2,9 @@ pub mod circle; use crate::vec::Vector; +use mockall::automock; + +#[automock] pub trait Animation { fn sample(&self, step: f32, pos: Vector) -> f32; } \ No newline at end of file diff --git a/src/char.rs b/src/char.rs index b6aa435..979c066 100644 --- a/src/char.rs +++ b/src/char.rs @@ -1,4 +1,7 @@ +use mockall::automock; + /// Used to get a character with a given brightness. +#[automock] pub trait CharSampler { /// Gets a character with the given brightness. /// # Arguments diff --git a/src/color.rs b/src/color.rs index 523211d..9830614 100644 --- a/src/color.rs +++ b/src/color.rs @@ -1,6 +1,8 @@ use crossterm::style::Color; +use mockall::automock; /// A collection of colors. +#[automock] pub trait ColorSampler { /// Gets a color for the given fill. /// # Arguments diff --git a/src/fill/mod.rs b/src/fill/mod.rs index cadd7aa..6d12c07 100644 --- a/src/fill/mod.rs +++ b/src/fill/mod.rs @@ -3,7 +3,10 @@ pub mod circle; use crate::vec::Vector; +use mockall::automock; + /// Used to choose the colors of characters. +#[automock] pub trait FillMode { /// Gets the color for this character. fn sample(&self, level: f32, pos: Vector) -> f32; diff --git a/src/sampler.rs b/src/sampler.rs index bb6a7f7..12eccfd 100644 --- a/src/sampler.rs +++ b/src/sampler.rs @@ -53,50 +53,55 @@ impl Sampler for ComposedSampler { #[cfg(test)] mod test { - use crate::char::SimpleCharSampler; + use mockall::predicate::{always, eq}; use super::*; - - struct MockAnimation; - struct MockFillMode; - struct MockColorSampler; - - impl Animation for MockAnimation { - fn sample(&self, step: f32, pos: Vector) -> f32 { - step + pos.x + pos.y - } - } - impl FillMode for MockFillMode { - fn sample(&self, level: f32, pos: Vector) -> f32 { - level + pos.x + pos.y - } - } - impl ColorSampler for MockColorSampler { - fn sample(&self, _: f32) -> Color { - Color::Green - } - } - - fn create_sampler() -> ComposedSampler { - let anim = Box::new(MockAnimation { }); - let fill = Box::new(MockFillMode { }); - let color = Box::new(MockColorSampler { }); - let char = Box::new(SimpleCharSampler::new("0123456789".to_string())); - - ComposedSampler::new(anim, fill, color, char) - } + use crate::animation::MockAnimation; + use crate::fill::MockFillMode; + use crate::color::MockColorSampler; + use crate::char::MockCharSampler; #[test] fn sample_keep() { - assert!(matches!(create_sampler().sample(0.7, Vector::new(0.3, 0.1)), Sample::Keep)); + let mut anim = Box::new(MockAnimation::new()); + let fill = Box::new(MockFillMode::new()); + let color = Box::new(MockColorSampler::new()); + let char = Box::new(MockCharSampler::new()); + + anim.expect_sample().return_const(3.0); + + let sampler = ComposedSampler::new(anim, fill, color, char); + + assert!(matches!(sampler.sample(0.7, Vector::new(0.3, 0.1)), Sample::Keep)); } #[test] fn sample_draw() { - assert!(matches!(create_sampler().sample(0.4, Vector::new(0.1, 0.1)), Sample::Draw { char: '6', color: Color::Green })); + let mut anim = Box::new(MockAnimation::new()); + let mut fill = Box::new(MockFillMode::new()); + let mut color = Box::new(MockColorSampler::new()); + let mut char = Box::new(MockCharSampler::new()); + + anim.expect_sample().once().with(eq(0.2), always()).return_const(0.3); + fill.expect_sample().once().with(eq(0.3), always()).return_const(0.8); + color.expect_sample().once().with(eq(0.8)).return_const(Color::Blue); + char.expect_sample().once().with(eq(0.3)).return_const('Z'); + + let sampler = ComposedSampler::new(anim, fill, color, char); + + assert!(matches!(sampler.sample(0.2, Vector::new(0.3, 0.1)), Sample::Draw { char: 'Z', color: Color::Blue })); } #[test] fn sample_clear() { - assert!(matches!(create_sampler().sample(-1.0, Vector::new(0.4, 0.5)), Sample::Clear)); + let mut anim = Box::new(MockAnimation::new()); + let fill = Box::new(MockFillMode::new()); + let color = Box::new(MockColorSampler::new()); + let char = Box::new(MockCharSampler::new()); + + anim.expect_sample().return_const(-0.4); + + let sampler = ComposedSampler::new(anim, fill, color, char); + + assert!(matches!(sampler.sample(0.7, Vector::new(0.3, 0.1)), Sample::Clear)); } } \ No newline at end of file