#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

#293 in Testing

Download history 414/week @ 2024-07-22 701/week @ 2024-07-29 621/week @ 2024-08-05 428/week @ 2024-08-12 583/week @ 2024-08-19 670/week @ 2024-08-26 487/week @ 2024-09-02 572/week @ 2024-09-09 589/week @ 2024-09-16 799/week @ 2024-09-23 1459/week @ 2024-09-30 1042/week @ 2024-10-07 489/week @ 2024-10-14 558/week @ 2024-10-21 746/week @ 2024-10-28 508/week @ 2024-11-04

2,342 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.4MB
~46K SLoC