hegeltest 0.29.8

Property-based testing for Rust, built on Hypothesis
Documentation
use super::{Generator, TestCase};

/// Generate the unit value `()`.
// nocov start
pub fn unit() -> JustGenerator<()> {
    just(())
    // nocov end
}

/// Generator that always produces the same value. Created by [`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()
    }
}

/// Generate a constant value.
pub fn just<T: Clone + Send + Sync>(value: T) -> JustGenerator<T> {
    JustGenerator { value }
}

/// Generator for boolean values. Created by [`booleans()`] and [`weighted_booleans()`].
pub struct BoolGenerator {
    p: f64,
}

impl Generator<bool> for BoolGenerator {
    fn do_draw(&self, tc: &TestCase) -> bool {
        tc.generate_boolean(self.p)
    }
}

/// Generate boolean values.
pub fn booleans() -> BoolGenerator {
    BoolGenerator { p: 0.5 }
}

/// Generate boolean values with probability p in [0.0, 1.0] of true.
pub fn weighted_booleans(p: f64) -> BoolGenerator {
    BoolGenerator { p }
}