proptest-rs / proptest

Hypothesis-like property testing for Rust
Apache License 2.0
1.71k stars 159 forks source link

Provide a proper strategy for dates using chrono #109

Open svenstaro opened 5 years ago

svenstaro commented 5 years ago

Generating dates is not actually trivial and since this crate already has two examples to generate dates, how about an example with a strategy using chrono to actually generate real dates?

Limeth commented 3 months ago

I ran into this issue, and my current solution to generating dates is to use proptest-arbitrary-interop with chrono's arbitrary feature:

use proptest_arbitrary_interop::arb;

prop_compose! {
    fn my_strategy()(
        created_at in arb::<DateTime<FixedOffset>>(),
    ) -> ... { ... }
}

However, my particular use case required RFC3339 datetimes, so I ended up having to add a filter:

prop_compose! {
    fn my_strategy()(
        created_at in arb::<DateTime<FixedOffset>>().prop_filter("Ability to parse from RFC3339 format", |datetime| {
            DateTime::parse_from_rfc3339(&datetime.to_rfc3339()).is_ok()
        }),
    ) -> ... { ... }
}

I'd be interested in a filter-less solution.