quickcheck/
lib.rs

1/*!
2This crate is a port of
3[Haskell's QuickCheck](https://hackage.haskell.org/package/QuickCheck).
4
5For detailed examples, please see the
6[README](https://github.com/BurntSushi/quickcheck).
7
8# Compatibility
9
10In general, this crate considers the `Arbitrary` implementations provided as
11implementation details. Strategies may or may not change over time, which may
12cause new test failures, presumably due to the discovery of new bugs due to a
13new kind of witness being generated. These sorts of changes may happen in
14semver compatible releases.
15*/
16
17pub use crate::arbitrary::{empty_shrinker, single_shrinker, Arbitrary, Gen};
18pub use crate::tester::{quickcheck, QuickCheck, TestResult, Testable};
19
20/// A macro for writing quickcheck tests.
21///
22/// This macro takes as input one or more property functions to test, and
23/// produces a proper `#[test]` function for each property. If the property
24/// fails, the behavior is as if `quickcheck` were called on the property
25/// (i.e., it panics and fails the test).
26///
27/// Note that this macro doesn't support `mut` or patterns in parameters.
28///
29/// # Example
30///
31/// ```rust
32/// # #[macro_use] extern crate quickcheck; fn main() {
33/// quickcheck! {
34///     fn prop_reverse_reverse(xs: Vec<usize>) -> bool {
35///         let rev: Vec<_> = xs.clone().into_iter().rev().collect();
36///         let revrev: Vec<_> = rev.into_iter().rev().collect();
37///         xs == revrev
38///     }
39/// };
40/// # }
41/// ```
42#[macro_export]
43macro_rules! quickcheck {
44    (@as_items $($i:item)*) => ($($i)*);
45    {
46        $(
47            $(#[$m:meta])*
48            fn $fn_name:ident($($arg_name:ident : $arg_ty:ty),*) -> $ret:ty {
49                $($code:tt)*
50            }
51        )*
52    } => (
53        $crate::quickcheck! {
54            @as_items
55            $(
56                #[test]
57                $(#[$m])*
58                fn $fn_name() {
59                    fn prop($($arg_name: $arg_ty),*) -> $ret {
60                        $($code)*
61                    }
62                    $crate::quickcheck(prop as fn($($arg_ty),*) -> $ret);
63                }
64            )*
65        }
66    )
67}
68
69#[cfg(feature = "use_logging")]
70fn env_logger_init() -> Result<(), log::SetLoggerError> {
71    env_logger::try_init()
72}
73#[cfg(feature = "use_logging")]
74macro_rules! info {
75    ($($tt:tt)*) => {
76        log::info!($($tt)*)
77    };
78}
79
80#[cfg(not(feature = "use_logging"))]
81fn env_logger_init() {}
82#[cfg(not(feature = "use_logging"))]
83macro_rules! info {
84    ($($_ignore:tt)*) => {
85        ()
86    };
87}
88
89mod arbitrary;
90mod tester;
91
92#[cfg(test)]
93mod tests;