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

Permit specifying property tests as closures #62

Merged
merged 4 commits into from
Jun 27, 2018
Merged
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
91 changes: 74 additions & 17 deletions src/sugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,10 @@ macro_rules! proptest {
$(
$(#[$meta])*
fn $test_name() {
let mut config = $config.clone_with_source_file(file!());
let mut config = $config.clone();
config.test_name = Some(
concat!(module_path!(), "::", stringify!($test_name)));
let mut runner = $crate::test_runner::TestRunner::new(config);
let names = proptest_helper!(@_WRAPSTR ($($parm),*));
match runner.run(
&$crate::strategy::Strategy::prop_map(
proptest_helper!(@_WRAP ($($strategy)*)),
|values| $crate::sugar::NamedArguments(names, values)),
|$crate::sugar::NamedArguments(
_, proptest_helper!(@_WRAPPAT ($($parm),*)))|
{
$body;
Ok(())
})
{
Ok(_) => (),
Err(e) => panic!("{}\n{}", e, runner),
}
proptest_helper!(@_BODY config ($($parm in $strategy),+) $body);
}
)*
};
Expand All @@ -111,6 +96,22 @@ macro_rules! proptest {
$($(#[$meta])*
fn $test_name($($parm in $strategy),+) $body)*
} };

(|($($parm:pat in $strategy:expr),+)| $body:block) => {
|| {
let mut config = $crate::test_runner::Config::default();
proptest_helper!(@_NO_FORK config);
proptest_helper!(@_BODY config ($($parm in $strategy),+) $body);
}
};

(move |($($parm:pat in $strategy:expr),+)| $body:block) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this...

move || {
let mut config = $crate::test_runner::Config::default();
proptest_helper!(@_NO_FORK config);
proptest_helper!(@_BODY config ($($parm in $strategy),+) $body);
}
};
}

/// Rejects the test input if assumptions are not met.
Expand Down Expand Up @@ -674,6 +675,34 @@ macro_rules! proptest_helper {
(@_WRAPSTR ($a:pat, $($rest:pat),*)) => {
(stringify!($a), proptest_helper!(@_WRAPSTR ($($rest),*)))
};
// make sure that configuration does not have options set that would cause it to fork.
(@_NO_FORK $config:ident) => {{
#[cfg(feature = "fork")]
{ $config.fork = false; }
#[cfg(feature = "timeout")]
{ $config.timeout = 0; }
assert!(!$config.fork(), "configuration should not be forking");
}};
// build a property testing block that when executed, executes the full property test.
(@_BODY $config:ident ($($parm:pat in $strategy:expr),+) $body:block) => {{
$config.source_file = Some(file!());
let mut runner = $crate::test_runner::TestRunner::new($config);
let names = proptest_helper!(@_WRAPSTR ($($parm),*));
match runner.run(
&$crate::strategy::Strategy::prop_map(
proptest_helper!(@_WRAP ($($strategy)*)),
|values| $crate::sugar::NamedArguments(names, values)),
|$crate::sugar::NamedArguments(
_, proptest_helper!(@_WRAPPAT ($($parm),*)))|
{
$body;
Ok(())
})
{
Ok(_) => (),
Err(e) => panic!("{}\n{}", e, runner),
}
}};
}

#[doc(hidden)]
Expand Down Expand Up @@ -1071,3 +1100,31 @@ mod ownership_tests {
}
}
}

#[cfg(test)]
mod closure_tests {
#[test]
fn test_simple() {
let x = 42;

let _ = proptest! {
|(y in 0..100)| {
assert!(x != y);
}
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... add a test that ensures that it keeps working if we make changes :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed. This one slipped my mind!

}

#[test]
fn test_move() {
let foo = Foo;

let _ = proptest! {
move |(x in 1..100, y in 0..100)| {
assert!(x != y, "foo: {:?}", foo);
}
};

#[derive(Debug)]
struct Foo;
}
}