Skip to content

Commit

Permalink
also implement with_arbitrary
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekleog committed Jan 22, 2023
1 parent 5d2cc57 commit 55323d9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion bolero-generator/src/arbitrary.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::{Driver, ValueGenerator};
use arbitrary::{Arbitrary, Unstructured};
use arbitrary::Unstructured;
use core::marker::PhantomData;

pub use arbitrary::Arbitrary;

pub struct ArbitraryGenerator<T>(PhantomData<T>);

impl<T> ValueGenerator for ArbitraryGenerator<T>
Expand Down
1 change: 1 addition & 0 deletions bolero/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ readme = "../README.md"
default = ["std"]
std = ["alloc", "bolero-generator/std"]
alloc = ["bolero-generator/alloc"]
arbitrary = ["bolero-generator/arbitrary"]

[dependencies]
bolero-engine = { version = "0.8", path = "../bolero-engine" }
Expand Down
31 changes: 31 additions & 0 deletions bolero/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,28 @@ impl<G, Engine, InputOwnership> TestTarget<G, Engine, InputOwnership> {
}
}

/// Set the generator for the `TestTarget` as being `arbitrary`-based
///
/// Calling `with_arbitrar::<Type>()` will generate random values of `Type`
/// to be tested. `Type` is required to implement [`arbitrary::Arbitrary`]
/// in order to use this method.
///
/// This mode is used for testing an implementation that requires
/// structured input.
#[cfg(feature = "arbitrary")]
pub fn with_arbitrary<T: Debug + for<'a> bolero_generator::arbitrary::Arbitrary<'a>>(
self,
) -> TestTarget<bolero_generator::arbitrary::ArbitraryGenerator<T>, Engine, InputOwnership>
{
TestTarget {
driver_mode: self.driver_mode,
shrink_time: self.shrink_time,
generator: generator::gen_arbitrary(),
engine: self.engine,
input_ownership: self.input_ownership,
}
}

/// Set the amount of time that will be spent shrinking an input on failure
///
/// Engines can optionally shrink inputs on failures to make it easier to debug
Expand Down Expand Up @@ -475,6 +497,15 @@ fn type_generator_test() {
});
}

#[cfg(feature = "arbitrary")]
#[test]
#[should_panic]
fn arbitrary_generator_test() {
check!().with_arbitrary().for_each(|input: &u8| {
assert!(input < &128);
});
}

#[test]
#[should_panic]
fn type_generator_cloned_test() {
Expand Down

0 comments on commit 55323d9

Please sign in to comment.