proptest/arbitrary/_core/
result.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::result`.
11
12use crate::std_facade::string;
13use core::fmt;
14use core::result::IntoIter;
15
16use crate::arbitrary::*;
17use crate::result::*;
18use crate::strategy::statics::static_map;
19use crate::strategy::*;
20
21// These are Result with uninhabited type in some variant:
22arbitrary!([A: Arbitrary] Result<A, string::ParseError>,
23    SMapped<A, Self>, A::Parameters;
24    args => static_map(any_with::<A>(args), Result::Ok)
25);
26arbitrary!([A: Arbitrary] Result<string::ParseError, A>,
27    SMapped<A, Self>, A::Parameters;
28    args => static_map(any_with::<A>(args), Result::Err)
29);
30#[cfg(feature = "unstable")]
31arbitrary!([A: Arbitrary] Result<A, !>,
32    SMapped<A, Self>, A::Parameters;
33    args => static_map(any_with::<A>(args), Result::Ok)
34);
35#[cfg(feature = "unstable")]
36arbitrary!([A: Arbitrary] Result<!, A>,
37    SMapped<A, Self>, A::Parameters;
38    args => static_map(any_with::<A>(args), Result::Err)
39);
40
41lift1!([] Result<A, string::ParseError>; Result::Ok);
42#[cfg(feature = "unstable")]
43lift1!([] Result<A, !>; Result::Ok);
44
45// We assume that `MaybeOk` is canonical as it's the most likely Strategy
46// a user wants.
47
48arbitrary!([A: Arbitrary, B: Arbitrary] Result<A, B>,
49    MaybeOk<A::Strategy, B::Strategy>,
50    product_type![Probability, A::Parameters, B::Parameters];
51    args => {
52        let product_unpack![prob, a, b] = args;
53        let (p, a, b) = (prob, any_with::<A>(a), any_with::<B>(b));
54        maybe_ok_weighted(p, a, b)
55    }
56);
57
58impl<A: fmt::Debug, E: Arbitrary> functor::ArbitraryF1<A> for Result<A, E>
59where
60    E::Strategy: 'static,
61{
62    type Parameters = product_type![Probability, E::Parameters];
63
64    fn lift1_with<AS>(base: AS, args: Self::Parameters) -> BoxedStrategy<Self>
65    where
66        AS: Strategy<Value = A> + 'static,
67    {
68        let product_unpack![prob, e] = args;
69        let (p, a, e) = (prob, base, any_with::<E>(e));
70        maybe_ok_weighted(p, a, e).boxed()
71    }
72}
73
74impl<A: fmt::Debug, B: fmt::Debug> functor::ArbitraryF2<A, B> for Result<A, B> {
75    type Parameters = Probability;
76
77    fn lift2_with<AS, BS>(
78        fst: AS,
79        snd: BS,
80        args: Self::Parameters,
81    ) -> BoxedStrategy<Self>
82    where
83        AS: Strategy<Value = A> + 'static,
84        BS: Strategy<Value = B> + 'static,
85    {
86        maybe_ok_weighted(args, fst, snd).boxed()
87    }
88}
89
90arbitrary!([A: Arbitrary] IntoIter<A>,
91    SMapped<Result<A, ()>, Self>,
92    <Result<A, ()> as Arbitrary>::Parameters;
93    args => static_map(any_with::<Result<A, ()>>(args), Result::into_iter)
94);
95
96lift1!(['static] IntoIter<A>, Probability; base, args => {
97    maybe_ok_weighted(args, base, Just(())).prop_map(Result::into_iter)
98});
99
100#[cfg(test)]
101mod test {
102    no_panic_test!(
103        result    => Result<u8, u16>,
104        into_iter => IntoIter<u8>,
105        result_a_parse_error => Result<u8, ::std::string::ParseError>,
106        result_parse_error_a => Result<::std::string::ParseError, u8>
107    );
108}