Skip to main content

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);
30lift1!([] Result<A, string::ParseError>; Result::Ok);
31
32// We assume that `MaybeOk` is canonical as it's the most likely Strategy
33// a user wants.
34
35arbitrary!([A: Arbitrary, B: Arbitrary] Result<A, B>,
36    MaybeOk<A::Strategy, B::Strategy>,
37    product_type![Probability, A::Parameters, B::Parameters];
38    args => {
39        let product_unpack![prob, a, b] = args;
40        let (p, a, b) = (prob, any_with::<A>(a), any_with::<B>(b));
41        maybe_ok_weighted(p, a, b)
42    }
43);
44
45impl<A: fmt::Debug, E: Arbitrary> functor::ArbitraryF1<A> for Result<A, E>
46where
47    E::Strategy: 'static,
48{
49    type Parameters = product_type![Probability, E::Parameters];
50
51    fn lift1_with<AS>(base: AS, args: Self::Parameters) -> BoxedStrategy<Self>
52    where
53        AS: Strategy<Value = A> + 'static,
54    {
55        let product_unpack![prob, e] = args;
56        let (p, a, e) = (prob, base, any_with::<E>(e));
57        maybe_ok_weighted(p, a, e).boxed()
58    }
59}
60
61impl<A: fmt::Debug, B: fmt::Debug> functor::ArbitraryF2<A, B> for Result<A, B> {
62    type Parameters = Probability;
63
64    fn lift2_with<AS, BS>(
65        fst: AS,
66        snd: BS,
67        args: Self::Parameters,
68    ) -> BoxedStrategy<Self>
69    where
70        AS: Strategy<Value = A> + 'static,
71        BS: Strategy<Value = B> + 'static,
72    {
73        maybe_ok_weighted(args, fst, snd).boxed()
74    }
75}
76
77arbitrary!([A: Arbitrary] IntoIter<A>,
78    SMapped<Result<A, ()>, Self>,
79    <Result<A, ()> as Arbitrary>::Parameters;
80    args => static_map(any_with::<Result<A, ()>>(args), Result::into_iter)
81);
82
83lift1!(['static] IntoIter<A>, Probability; base, args => {
84    maybe_ok_weighted(args, base, Just(())).prop_map(Result::into_iter)
85});
86
87#[cfg(test)]
88mod test {
89    no_panic_test!(
90        result    => Result<u8, u16>,
91        into_iter => IntoIter<u8>,
92        result_a_parse_error => Result<u8, ::std::string::ParseError>,
93        result_parse_error_a => Result<::std::string::ParseError, u8>
94    );
95}