### 2 releases

0.1.0-dev.1 | May 18, 2021 |
---|---|

0.1.0-dev.0 | May 17, 2021 |

#**403** in Testing

**1,441** downloads per month

Used in **10** crates
(5 directly)

**MIT**license

45KB

1K
SLoC

# contrafact-rs

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

###
`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

with two fields, and a simple
Fact (constraint) about `S`

which says that `S`

must always equal `S ::`x

`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`,` eq`,` lens`}``;`
`use` `arbitrary``::``{`Arbitrary`,` Unstructured`}``;`
`#``[``derive``(``Debug``,` Clone`,` PartialEq`,` Arbitrary`)``]`
`struct` `S` `{`
`x``:` `u32`,
`y``:` `u32`,
`}`
`let` `mut` fact `=` `lens``(``"`S::x`"``,` `|``s``:` `&``mut` S`|` `&``mut` s`.`x`,` `eq``(``"`must be 1`"``,` `&``1``)``)``;`
`assert!``(`fact`.``check``(``&`S `{`x`:` `1``,` y`:` `333``}``)``.``is_ok``(``)``)``;`
`assert!``(`fact`.``check``(``&`S `{`x`:` `2``,` y`:` `333``}``)``.``is_err``(``)``)``;`
`let` `mut` u `=` `Unstructured``::`new`(``&``[``0``;` `9999``]``)``;` `//` NB: don't actually construct Unstructured this way!
`let` a `=` fact`.``build``(``&``mut` u`)``;`
`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

trait. `Fact`*TODO: example of this.*

leans heavily on the `contrafact`

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 `Arbitrary`

about.`Fact`

Facts can be used to check if a constraint is matched via [

] or [`Fact ::`check

`(`

`)`

`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 [

] macro, which
boxes each Fact and lumps them together as trait objects, applying each fact one after the other.`facts!`

Facts can be "vertically" composed together through the [

] and [`lens`

]
combinators, which allow you to lift a Fact about one type into a Fact about another type.`prism`

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

#### Dependencies

~1.4–2MB

~41K SLoC