proptest/arbitrary/
macros.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#![cfg_attr(not(feature = "std"), allow(unused_macros))]
11
12//==============================================================================
13// Macros for quick implementing:
14//==============================================================================
15
16macro_rules! arbitrary {
17    ([$($bounds : tt)*] $typ: ty, $strat: ty, $params: ty;
18        $args: ident => $logic: expr) => {
19        impl<$($bounds)*> $crate::arbitrary::Arbitrary for $typ {
20            type Parameters = $params;
21            type Strategy = $strat;
22            fn arbitrary_with($args: Self::Parameters) -> Self::Strategy {
23                $logic
24            }
25        }
26    };
27    ([$($bounds : tt)*] $typ: ty, $strat: ty; $logic: expr) => {
28        arbitrary!([$($bounds)*] $typ, $strat, (); _args => $logic);
29    };
30    ([$($bounds : tt)*] $typ: ty; $logic: expr) => {
31        arbitrary!([$($bounds)*] $typ,
32            $crate::strategy::Just<Self>, ();
33            _args => $crate::strategy::Just($logic)
34        );
35    };
36    ($typ: ty, $strat: ty, $params: ty; $args: ident => $logic: expr) => {
37        arbitrary!([] $typ, $strat, $params; $args => $logic);
38    };
39    ($typ: ty, $strat: ty; $logic: expr) => {
40        arbitrary!([] $typ, $strat; $logic);
41    };
42    ($strat: ty; $logic: expr) => {
43        arbitrary!([] $strat; $logic);
44    };
45    ($($typ: ident),*) => {
46        $(arbitrary!($typ, $typ::Any; $typ::ANY);)*
47    };
48}
49
50macro_rules! wrap_ctor {
51    ($wrap: ident) => {
52        wrap_ctor!([] $wrap);
53    };
54    ($wrap: ident, $maker: expr) => {
55        wrap_ctor!([] $wrap, $maker);
56    };
57    ([$($bound : tt)*] $wrap: ident) => {
58        wrap_ctor!([$($bound)*] $wrap, $wrap::new);
59    };
60    ([$($bound : tt)*] $wrap: ident, $maker: expr) => {
61        arbitrary!([A: $crate::arbitrary::Arbitrary + $($bound)*] $wrap<A>,
62            $crate::arbitrary::SMapped<A, Self>, A::Parameters;
63            args => $crate::strategy::statics::static_map(
64                $crate::arbitrary::any_with::<A>(args), $maker));
65
66        lift1!([$($bound)*] $wrap<A>; $maker);
67    };
68}
69
70macro_rules! wrap_from {
71    ($wrap: ident) => {
72        wrap_from!([] $wrap);
73    };
74    ([$($bound : tt)*] $wrap: ident) => {
75        arbitrary!([A: $crate::arbitrary::Arbitrary + $($bound)*] $wrap<A>,
76            $crate::strategy::MapInto<A::Strategy, Self>, A::Parameters;
77            args => $crate::strategy::Strategy::prop_map_into(
78                $crate::arbitrary::any_with::<A>(args)));
79
80        lift1!([$($bound)*] $wrap<A>);
81    };
82}
83
84macro_rules! lazy_just {
85    ($($self: ty, $fun: expr);+) => {
86        $(
87            arbitrary!($self, $crate::strategy::LazyJust<Self, fn() -> Self>;
88                $crate::strategy::LazyJust::new($fun));
89        )+
90    };
91}
92
93//==============================================================================
94// Macros for testing:
95//==============================================================================
96
97/// We are mostly interested in ensuring that generating input from our
98/// strategies is able to construct a value, therefore ensuring that
99/// no panic occurs is mostly sufficient. Shrinking for strategies that
100/// use special shrinking methods can be handled separately.
101#[cfg(test)]
102macro_rules! no_panic_test {
103    ($($module: ident => $self: ty),+) => {
104        $(
105            mod $module {
106                #[allow(unused_imports)]
107                use super::super::*;
108                proptest! {
109                    #[test]
110                    fn no_panic(_ in $crate::arbitrary::any::<$self>()) {}
111                }
112            }
113        )+
114    };
115}