use super::generators::draw_and_print_value;
use super::{Generator, PrintableGenerator, TestCase, label_from_name};
use crate::pretty::{PrettyPrintable, PrettyPrinter};
const BOOLEAN_LABEL: u64 = label_from_name("hegel.booleans");
pub fn unit() -> JustGenerator<()> {
just(())
}
pub struct JustGenerator<T> {
value: T,
label: u64,
}
impl<T: Clone + Send + Sync> Generator<T> for JustGenerator<T> {
fn label(&self) -> u64 {
self.label
}
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,
label: label_from_name(std::any::type_name::<JustGenerator<T>>()),
}
}
pub struct BoolGenerator {
p: f64,
}
impl Generator<bool> for BoolGenerator {
fn label(&self) -> u64 {
BOOLEAN_LABEL
}
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 }
}