Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a wrapper for disabling shrinking for an Arbitrary #293

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -1124,6 +1124,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(gen: &mut Gen) -> Self {
Self{inner: Arbitrary::arbitrary(gen)}
}
}

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

#[cfg(test)]
mod test {
use std::collections::{
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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, NoShrink, Gen};
pub use crate::tester::{quickcheck, QuickCheck, TestResult, Testable};

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