use super::generators::draw_and_print_value;
use super::{Generator, PrintableGenerator, TestCase};
use crate::pretty::{PrettyPrintable, PrettyPrinter};
pub fn unit() -> JustGenerator<()> {
just(())
}
pub struct JustGenerator<T> {
value: T,
}
impl<T: Clone + Send + Sync> Generator<T> for JustGenerator<T> {
fn do_draw(&self, _tc: &TestCase) -> T {
self.value.clone()
}
}
impl<T: Clone + Send + Sync + PrettyPrintable> PrintableGenerator<T> for JustGenerator<T> {
fn do_draw_and_print(&self, tc: &TestCase, printer: &mut PrettyPrinter) -> T {
draw_and_print_value(self, tc, printer)
}
}
pub fn just<T: Clone + Send + Sync>(value: T) -> JustGenerator<T> {
JustGenerator { value }
}
pub struct BoolGenerator {
p: f64,
}
impl Generator<bool> for BoolGenerator {
fn do_draw(&self, tc: &TestCase) -> bool {
tc.generate_boolean(self.p)
}
}
impl PrintableGenerator<bool> for BoolGenerator {
fn do_draw_and_print(&self, tc: &TestCase, printer: &mut PrettyPrinter) -> bool {
draw_and_print_value(self, tc, printer)
}
}
pub fn booleans() -> BoolGenerator {
BoolGenerator { p: 0.5 }
}
pub fn weighted_booleans(p: f64) -> BoolGenerator {
BoolGenerator { p }
}