Add trait for mocking

This commit is contained in:
Nicolas 2022-04-03 14:42:12 +02:00
parent 0e0f012810
commit e6e1c6fb25

View File

@ -1,22 +1,55 @@
/// Used to get a character with a given brightness.
pub struct CharSampler {
pub trait CharSampler {
/// Gets a character with the given brightness.
/// # Arguments
/// * `level`: `0 <= level` and `level < 1`
fn sample(&self, level: f32) -> char;
}
pub struct SimpleCharSampler {
chars: String
}
impl CharSampler {
impl SimpleCharSampler {
/// # Arguments
/// * `chars`: The characters ordered by brightness.
pub fn new(chars: String) -> Self {
Self { chars }
}
}
/// Gets a character with the given brightness.
/// # Arguments
/// * `level`: `0 <= level` and `level < 1`
pub fn sample(&self, level: f32) -> char {
let pos = level * self.chars.chars().count() as f32;
let index = pos as usize;
impl CharSampler for SimpleCharSampler {
fn sample(&self, level: f32) -> char {
let index = level * self.chars.chars().count() as f32;
self.chars.chars().nth(index).unwrap()
assert!(index >= 0.0);
self.chars.chars().nth(index as usize).unwrap()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn sample() {
let sampler = SimpleCharSampler::new("abc".to_string());
assert_eq!('a', sampler.sample(0.1));
assert_eq!('b', sampler.sample(0.4));
assert_eq!('c', sampler.sample(0.7));
}
#[test]
#[should_panic]
fn sample_index_negative() {
SimpleCharSampler::new("abc".to_string()).sample(-0.1);
}
#[test]
#[should_panic]
fn sample_index_equals_one() {
SimpleCharSampler::new("abc".to_string()).sample(1.0);
}
}