proptest/arbitrary/_core/
option.rs

1//-
2// Copyright 2017, 2018 The proptest developers
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! Arbitrary implementations for `std::option`.
11
12use crate::std_facade::string;
13use core::ops::RangeInclusive;
14use core::option as opt;
15
16use crate::arbitrary::*;
17use crate::option::{weighted, OptionStrategy, Probability};
18use crate::strategy::statics::static_map;
19use crate::strategy::*;
20
21arbitrary!(Probability, MapInto<RangeInclusive<f64>, Self>;
22    (0.0..=1.0).prop_map_into()
23);
24
25// These are Option<AnUninhabitedType> impls:
26
27arbitrary!(Option<string::ParseError>; None);
28#[cfg(feature = "unstable")]
29arbitrary!(Option<!>; None);
30
31arbitrary!([A: Arbitrary] opt::Option<A>, OptionStrategy<A::Strategy>,
32    product_type![Probability, A::Parameters];
33    args => {
34        let product_unpack![prob, a] = args;
35        weighted(prob, any_with::<A>(a))
36    }
37);
38
39lift1!([] Option<A>, Probability; base, prob => weighted(prob, base));
40
41arbitrary!([A: Arbitrary] opt::IntoIter<A>, SMapped<Option<A>, Self>,
42    <Option<A> as Arbitrary>::Parameters;
43    args => static_map(any_with::<Option<A>>(args), Option::into_iter));
44
45lift1!(['static] opt::IntoIter<A>, Probability;
46    base, prob => weighted(prob, base).prop_map(Option::into_iter)
47);
48
49#[cfg(test)]
50mod test {
51    no_panic_test!(
52        probability => Probability,
53        option      => Option<u8>,
54        option_iter => opt::IntoIter<u8>,
55        option_parse_error => Option<string::ParseError>
56    );
57}