mirror of
https://github.com/ricoriedel/wipe.git
synced 2024-11-15 20:46:40 +00:00
Add trait for mocking
This commit is contained in:
parent
84cbe22ce7
commit
4acb26b702
81
src/fill/factory.rs
Normal file
81
src/fill/factory.rs
Normal file
@ -0,0 +1,81 @@
|
||||
use clap::ArgEnum;
|
||||
use rand::prelude::IteratorRandom;
|
||||
use rand::Rng;
|
||||
use crate::fill::FillMode;
|
||||
use crate::fill::level::LevelFillMode;
|
||||
use crate::vec::Vector;
|
||||
|
||||
#[derive(Copy, Clone, ArgEnum)]
|
||||
pub enum FillModeEnum {
|
||||
Circle,
|
||||
Level,
|
||||
Stripe
|
||||
}
|
||||
|
||||
pub trait FillModeFactory {
|
||||
fn create(&self, mode: FillModeEnum, size: Vector) -> Box<dyn FillMode>;
|
||||
|
||||
fn choose(&self, options: Vec<FillModeEnum>, rng: &mut impl Rng) -> FillModeEnum;
|
||||
}
|
||||
|
||||
pub struct SimpleFillModeFactory;
|
||||
|
||||
impl SimpleFillModeFactory {
|
||||
pub fn new() -> Self {
|
||||
Self { }
|
||||
}
|
||||
}
|
||||
|
||||
impl FillModeFactory for SimpleFillModeFactory {
|
||||
fn create(&self, mode: FillModeEnum, _: Vector) -> Box<dyn FillMode> {
|
||||
match mode {
|
||||
FillModeEnum::Circle => todo!(),
|
||||
FillModeEnum::Level => Box::new(LevelFillMode::new()),
|
||||
FillModeEnum::Stripe => todo!()
|
||||
}
|
||||
}
|
||||
|
||||
fn choose(&self, mut options: Vec<FillModeEnum>, rng: &mut impl Rng) -> FillModeEnum {
|
||||
if options.is_empty() {
|
||||
options.push(FillModeEnum::Circle);
|
||||
options.push(FillModeEnum::Level);
|
||||
options.push(FillModeEnum::Stripe);
|
||||
}
|
||||
options.into_iter().choose_stable(rng).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use rand::rngs::mock::StepRng;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn create() {
|
||||
let factory = SimpleFillModeFactory::new();
|
||||
let size = Vector::ZERO;
|
||||
|
||||
factory.create(FillModeEnum::Circle, size);
|
||||
factory.create(FillModeEnum::Level, size);
|
||||
factory.create(FillModeEnum::Stripe, size);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn choose_from_all() {
|
||||
let rng = &mut StepRng::new(0, 1);
|
||||
|
||||
let factory = SimpleFillModeFactory::new();
|
||||
|
||||
assert!(matches!(factory.choose(Vec::new(), rng), FillModeEnum::Stripe));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn choose_from_options() {
|
||||
let rng = &mut StepRng::new(0, 1);
|
||||
let options = vec![FillModeEnum::Circle];
|
||||
|
||||
let factory = SimpleFillModeFactory::new();
|
||||
|
||||
assert!(matches!(factory.choose(options, rng), FillModeEnum::Circle));
|
||||
}
|
||||
}
|
@ -1,42 +1,11 @@
|
||||
mod level;
|
||||
|
||||
use clap::ArgEnum;
|
||||
use rand::prelude::IteratorRandom;
|
||||
use rand::Rng;
|
||||
use crate::fill::level::LevelFillMode;
|
||||
pub mod factory;
|
||||
|
||||
use crate::vec::Vector;
|
||||
|
||||
/// Used to choose the colors of characters.
|
||||
pub trait FillMode {
|
||||
/// Gets the color for this character.
|
||||
/// # Arguments
|
||||
/// * `level`:
|
||||
/// * `pos`:
|
||||
///
|
||||
/// returns: f32
|
||||
fn sample(&self, level: f32, pos: Vector) -> f32;
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, ArgEnum)]
|
||||
pub enum FillModeEnum {
|
||||
Circle,
|
||||
Level,
|
||||
Stripe
|
||||
}
|
||||
|
||||
pub fn choose_fill_mode(mut options: Vec<FillModeEnum>, rng: &mut impl Rng) -> FillModeEnum {
|
||||
if options.is_empty() {
|
||||
options.push(FillModeEnum::Circle);
|
||||
options.push(FillModeEnum::Level);
|
||||
options.push(FillModeEnum::Stripe);
|
||||
}
|
||||
options.into_iter().choose(rng).unwrap()
|
||||
}
|
||||
|
||||
pub fn create_fill_mode(mode: FillModeEnum, _: Vector) -> Box<dyn FillMode> {
|
||||
match mode {
|
||||
FillModeEnum::Circle => todo!(),
|
||||
FillModeEnum::Level => Box::new(LevelFillMode::new()),
|
||||
FillModeEnum::Stripe => todo!()
|
||||
}
|
||||
}
|
45
src/vec.rs
45
src/vec.rs
@ -6,7 +6,9 @@ pub struct Vector {
|
||||
}
|
||||
|
||||
impl Vector {
|
||||
pub fn new(x: f32, y: f32) -> Self {
|
||||
pub const ZERO: Vector = Vector::new(0.0, 0.0);
|
||||
|
||||
pub const fn new(x: f32, y: f32) -> Self {
|
||||
Self { x, y }
|
||||
}
|
||||
|
||||
@ -15,6 +17,45 @@ impl Vector {
|
||||
/// * `x`: The x axis of the terminal character.
|
||||
/// * `y`: The y axis of the terminal character.
|
||||
pub fn from_terminal(x: usize, y: usize) -> Self {
|
||||
Vector::new(x as f32, y as f32 * 2.0)
|
||||
Self::new(x as f32, y as f32 * 2.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn new() {
|
||||
let vec = Vector::new(3.0, 5.0);
|
||||
|
||||
assert_eq!(3.0, vec.x);
|
||||
assert_eq!(5.0, vec.y);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_terminal() {
|
||||
let vec = Vector::from_terminal(2, 4);
|
||||
|
||||
assert_eq!(2.0, vec.x);
|
||||
assert_eq!(8.0, vec.y);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy() {
|
||||
let vec = Vector::new(2.0, 4.0);
|
||||
let copy = vec;
|
||||
|
||||
assert_eq!(vec.x, copy.x);
|
||||
assert_eq!(vec.y, copy.y);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clone() {
|
||||
let vec = Vector::new(2.0, 4.0);
|
||||
let clone = vec.clone();
|
||||
|
||||
assert_eq!(vec.x, clone.x);
|
||||
assert_eq!(vec.y, clone.y);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user