-
Notifications
You must be signed in to change notification settings - Fork 161
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
)* | ||
}; | ||
|
@@ -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) => { | ||
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. | ||
|
@@ -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)] | ||
|
@@ -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); | ||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given this...