Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,49 @@ impl Arbitrary for SystemTime {
}
}

/// Wrapper for disabling shrinking for an Arbitrary
///
/// This type allows generating values via a given `Arbitrary` implementation
/// for a test for which we don't want to shrink input values.
///
/// # Example
///
/// ```rust
/// use quickcheck::{QuickCheck, NoShrink};
///
/// fn prop_sane_shrinker() {
/// // Yielding the original value will result in endless recursion
/// fn shrinker_no_self(value: NoShrink<u16>) -> bool {
/// use quickcheck::Arbitrary;
/// !value.as_ref().shrink().any(|v| v == *value.as_ref())
/// }
/// QuickCheck::new().quickcheck(shrinker_no_self as fn(NoShrink<u16>) -> bool);
/// }
/// ```
#[derive(Clone, Debug)]
pub struct NoShrink<A: Arbitrary> {
inner: A,
}

impl<A: Arbitrary> NoShrink<A> {
/// Unwrap the inner value
pub fn into_inner(self) -> A {
self.inner
}
}

impl<A: Arbitrary> Arbitrary for NoShrink<A> {
fn arbitrary(rnd: &mut Gen) -> Self {
Self { inner: Arbitrary::arbitrary(rnd) }
}
}

impl<A: Arbitrary> AsRef<A> for NoShrink<A> {
fn as_ref(&self) -> &A {
&self.inner
}
}

#[cfg(test)]
mod test {
use std::collections::{
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ new kind of witness being generated. These sorts of changes may happen in
semver compatible releases.
*/

pub use crate::arbitrary::{empty_shrinker, single_shrinker, Arbitrary, Gen};
pub use crate::arbitrary::{
empty_shrinker, single_shrinker, Arbitrary, Gen, NoShrink,
};
pub use crate::tester::{quickcheck, QuickCheck, TestResult, Testable};

/// A macro for writing quickcheck tests.
Expand Down