mirror of
https://github.com/ricoriedel/wipe.git
synced 2024-11-22 16:06:38 +00:00
Replace with mockall
This commit is contained in:
parent
43cecf57b3
commit
e4116130d9
@ -2,6 +2,9 @@ pub mod circle;
|
|||||||
|
|
||||||
use crate::vec::Vector;
|
use crate::vec::Vector;
|
||||||
|
|
||||||
|
use mockall::automock;
|
||||||
|
|
||||||
|
#[automock]
|
||||||
pub trait Animation {
|
pub trait Animation {
|
||||||
fn sample(&self, step: f32, pos: Vector) -> f32;
|
fn sample(&self, step: f32, pos: Vector) -> f32;
|
||||||
}
|
}
|
@ -1,4 +1,7 @@
|
|||||||
|
use mockall::automock;
|
||||||
|
|
||||||
/// Used to get a character with a given brightness.
|
/// Used to get a character with a given brightness.
|
||||||
|
#[automock]
|
||||||
pub trait CharSampler {
|
pub trait CharSampler {
|
||||||
/// Gets a character with the given brightness.
|
/// Gets a character with the given brightness.
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
use crossterm::style::Color;
|
use crossterm::style::Color;
|
||||||
|
use mockall::automock;
|
||||||
|
|
||||||
/// A collection of colors.
|
/// A collection of colors.
|
||||||
|
#[automock]
|
||||||
pub trait ColorSampler {
|
pub trait ColorSampler {
|
||||||
/// Gets a color for the given fill.
|
/// Gets a color for the given fill.
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
|
@ -3,7 +3,10 @@ pub mod circle;
|
|||||||
|
|
||||||
use crate::vec::Vector;
|
use crate::vec::Vector;
|
||||||
|
|
||||||
|
use mockall::automock;
|
||||||
|
|
||||||
/// Used to choose the colors of characters.
|
/// Used to choose the colors of characters.
|
||||||
|
#[automock]
|
||||||
pub trait FillMode {
|
pub trait FillMode {
|
||||||
/// Gets the color for this character.
|
/// Gets the color for this character.
|
||||||
fn sample(&self, level: f32, pos: Vector) -> f32;
|
fn sample(&self, level: f32, pos: Vector) -> f32;
|
||||||
|
@ -53,50 +53,55 @@ impl Sampler for ComposedSampler {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::char::SimpleCharSampler;
|
use mockall::predicate::{always, eq};
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::animation::MockAnimation;
|
||||||
struct MockAnimation;
|
use crate::fill::MockFillMode;
|
||||||
struct MockFillMode;
|
use crate::color::MockColorSampler;
|
||||||
struct MockColorSampler;
|
use crate::char::MockCharSampler;
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sample_keep() {
|
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]
|
#[test]
|
||||||
fn sample_draw() {
|
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]
|
#[test]
|
||||||
fn sample_clear() {
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user