Skip to main content

property

Macro property 

Source
macro_rules! property {
    (| $name:ident : $ty:ty | $body:block) => { ... };
    (| $name:ident : $ty:ty | $body:block using $strategy:expr) => { ... };
    (| $name:ident | $body:block using $strategy:expr) => { ... };
}
Expand description

Checks that a property holds for every generated input.

property! takes a closure with a typed binding and a block body that returns TestResult, runs it against generated values, and on failure produces a TestError naming the shrunk counterexample. It expands to an expression, so it is the body (or the tail) of an ordinary #[test] function:

use test_better_core::TestResult;
use test_better_matchers::{check, lt};
use test_better_property::property;

// In a real test this is `#[test] fn doubling_stays_in_range()`.
property!(|n: u8| {
    check!(u16::from(n) * 2).satisfies(lt(512u16))
})

ยงInferring vs. naming the strategy

With only a typed binding, the strategy is inferred from the type via any (the type must be proptest::arbitrary::Arbitrary). To generate from a specific strategy instead, add a trailing using clause:

use test_better_core::TestResult;
use test_better_matchers::{check, lt};
use test_better_property::property;

// `using` names the strategy explicitly; the binding need not be annotated.
property!(|n| {
    check!(n).satisfies(lt(100u32))
} using 0u32..100)