#constraints #composable #fixtures

contrafact

A trait for highly composable constraints ("facts") which can be used both to verify data and to generate arbitrary data within those constraints

10 releases

0.2.0-rc.1 Jun 30, 2023
0.2.0-dev.3 Jun 10, 2023
0.1.1 Jun 8, 2023
0.1.0-dev.1 May 18, 2021

#233 in Testing

Download history 383/week @ 2023-12-15 186/week @ 2023-12-22 121/week @ 2023-12-29 415/week @ 2024-01-05 604/week @ 2024-01-12 490/week @ 2024-01-19 777/week @ 2024-01-26 804/week @ 2024-02-02 310/week @ 2024-02-09 513/week @ 2024-02-16 404/week @ 2024-02-23 659/week @ 2024-03-01 648/week @ 2024-03-08 675/week @ 2024-03-15 629/week @ 2024-03-22 401/week @ 2024-03-29

2,440 downloads per month
Used in 13 crates (6 directly)

MIT license

66KB
1.5K SLoC

contrafact-rs

CI Docs

A trait for highly composable constraints ("facts") which can be used both to verify data and to generate arbitrary data within those constraints

See the rust docs for more info

TODO:

  • make Arbitrary a feature, generalize data generation
  • add lens-rs support

lib.rs:

A trait for highly composable constraints ("facts") which can be used both to verify data and to generate arbitrary data satisfying those constraints.

This crate is mainly intended for use in writing tests, and in particular for generating meaningful fixture data. By defining composable, reusable constraints, they can be mixed and matched to handle the specific use cases of your tests. By defining what you need from a fixture rather than simply writing the fixture you need, the hope is that you save yourself duplicated effort over time.

Example

The following example defines a simple struct S with two fields, and a simple Fact (constraint) about S which says that S::x must always equal 1. This Fact, like all Facts, can be used both to verify that an instance of S meets the constraint, or to generate new instances of S which meet the constraint.

use contrafact::{Fact, facts::{eq, lens1}};
use arbitrary::{Arbitrary, Unstructured};

#[derive(Debug, Clone, PartialEq, Arbitrary)]
struct S {
    x: u32,
    y: u32,
}

let mut fact = lens1("S::x", |s: &mut S| &mut s.x, eq(1));

assert!(fact.clone().check(&S {x: 1, y: 333}).is_ok());
assert!(fact.clone().check(&S {x: 2, y: 333}).is_err());

// NB: don't actually construct a Generator this way! See the docs for [[`Generator`]].
let mut g = contrafact::utils::random_generator();
let a = fact.build(&mut g);
assert_eq!(a.x, 1);

Things to know

The above example composes together existing Facts provided by this crate. You can also define your own facts by hand by implementing the Fact trait. TODO: example of this.

contrafact leans heavily on the arbitrary crate for generating arbitrary data. Get to know this library, because you will need to implement Arbitrary for any type you wish to write a Fact about.

Facts can be used to check if a constraint is matched via [Fact::check()] or check_seq, and also to build new values via Fact::build and build_seq. Building values requires the use of arbitrary::Unstructured.

Facts can also be stateful, such that the constraint changes while checking or building a sequence. TODO: example of stateful fact.

Facts can be easily "horizontally" composed together through the facts! macro, which boxes each Fact and lumps them together as trait objects, applying each fact one after the other.

Facts can be "vertically" composed together through the lens and prism combinators, which allow you to lift a Fact about one type into a Fact about another type.

See the Functions documentation for more examples and detailed instructions about each Fact defined by this crate.

Dependencies

~1.5–2.3MB
~46K SLoC