Trait contrafact::Fact

source ·
pub trait Fact<'a, T>: Send + Sync + Clone + Debugwhere
    T: Target<'a>,{
    // Required methods
    fn labeled(self, label: impl ToString) -> Self;
    fn label(&self) -> String;
    fn mutate(&mut self, g: &mut Generator<'a>, t: T) -> Mutation<T>;

    // Provided methods
    fn check(self, t: &T) -> Check { ... }
    fn satisfy_attempts(&self) -> usize { ... }
    fn satisfy(&mut self, g: &mut Generator<'a>, t: T) -> ContrafactResult<T> { ... }
    fn build_fallible(self, g: &mut Generator<'a>) -> ContrafactResult<T> { ... }
    fn build(self, g: &mut Generator<'a>) -> T { ... }
}
Expand description

A declarative representation of a constraint on some data, which can be used to both make an assertion (check) or to mold some arbitrary existing data into a shape which passes that same assertion (mutate)

Required Methods§

source

fn labeled(self, label: impl ToString) -> Self

Change the label

source

fn label(&self) -> String

Get the label of this fact

source

fn mutate(&mut self, g: &mut Generator<'a>, t: T) -> Mutation<T>

Apply a mutation which moves the t closer to satisfying the overall constraint.

Provided Methods§

source

fn check(self, t: &T) -> Check

Assert that the constraint is satisfied for given data.

If the mutation function is written properly, we get a check for free by using a special Generator which fails upon mutation. If this is for some reason unreasonable, a check function can be written by hand, but care must be taken to make sure it perfectly lines up with the mutation function.

source

fn satisfy_attempts(&self) -> usize

Make this many attempts to satisfy a constraint before giving up and panicking.

If you are combining highly contentious facts together and relying on randomness to find a solution, this limit may need to be higher. In general, you should try to write facts that don’t interfere with each other so that the constraint can be met on the first attempt, or perhaps the second or third. If necessary, this can be raised to lean more on random search.

source

fn satisfy(&mut self, g: &mut Generator<'a>, t: T) -> ContrafactResult<T>

Mutate a value such that it satisfies the constraint. If the constraint cannot be satisfied, panic.

source

fn build_fallible(self, g: &mut Generator<'a>) -> ContrafactResult<T>

Build a new value such that it satisfies the constraint

source

fn build(self, g: &mut Generator<'a>) -> T

Build a new value such that it satisfies the constraint, panicking on error

Implementations on Foreign Types§

source§

impl<'a, T, F1, F2> Fact<'a, T> for Either<F1, F2>where T: Target<'a>, F1: Fact<'a, T> + ?Sized, F2: Fact<'a, T> + ?Sized,

source§

fn mutate(&mut self, g: &mut Generator<'a>, t: T) -> Mutation<T>

source§

fn label(&self) -> String

source§

fn labeled(self, label: impl ToString) -> Self

Implementors§