Skip to content

Commit

Permalink
Remove free functions of low utility.
Browse files Browse the repository at this point in the history
As agreed in the PR, but this was previously done in the bad merge
commit.
  • Loading branch information
AltSysrq committed Jan 14, 2018
1 parent 07e794d commit cbb3029
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 72 deletions.
16 changes: 7 additions & 9 deletions src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ where ValueFor<T> : Hash + Eq {
let min_size = size.start;
HashSetStrategy(statics::Filter::new(
statics::Map::new(vec(element, size), VecToHashSet),
reject("HashSet minimum size"),
"HashSet minimum size".into(),
MinSize(min_size)))
}

Expand Down Expand Up @@ -224,7 +224,7 @@ where ValueFor<T> : Ord {

BTreeSetStrategy(statics::Filter::new(
statics::Map::new(vec(element, size), VecToBTreeSet),
reject("BTreeSet minimum size"),
"BTreeSet minimum size".into(),
MinSize(min_size)))
}

Expand Down Expand Up @@ -275,7 +275,7 @@ where ValueFor<K> : Hash + Eq {
let min_size = size.start;
HashMapStrategy(statics::Filter::new(
statics::Map::new(vec((key, value), size), VecToHashMap),
reject("HashMap minimum size"),
"HashMap minimum size".into(),
MinSize(min_size)))
}

Expand Down Expand Up @@ -326,7 +326,7 @@ where ValueFor<K> : Ord {
let min_size = size.start;
BTreeMapStrategy(statics::Filter::new(
statics::Map::new(vec((key, value), size.clone()), VecToBTreeMap),
reject("BTreeMap minimum size"),
"BTreeMap minimum size".into(),
MinSize(min_size)))
}

Expand Down Expand Up @@ -467,11 +467,9 @@ mod test {
assert!(start.iter().map(|&v| v).collect::<BitSet>().len() >= 2);

let result = runner.run_one(case, |v| {
if v.iter().map(|&v| v).sum::<usize>() < 9 {
Ok(())
} else {
fail_case("greater than 8")
}
prop_assert!(v.iter().map(|&v| v).sum::<usize>() < 9,
"greater than 8");
Ok(())
});

match result {
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,9 @@ macro_rules! opaque_strategy_wrapper {
}
}

#[doc(hidden)]
#[macro_use] pub mod sugar;

pub mod test_runner;
pub mod strategy;
pub mod bool;
Expand All @@ -1511,7 +1514,4 @@ pub mod option;
pub mod result;
pub mod sample;

#[doc(hidden)]
#[macro_use] pub mod sugar;

pub mod prelude;
10 changes: 4 additions & 6 deletions src/strategy/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ mod test {
if a <= 10000 || b <= a {
Ok(())
} else {
fail_case("fail")
Err(TestCaseError::fail("fail"))
}
});

Expand Down Expand Up @@ -313,11 +313,9 @@ mod test {
});
let case = input.new_value(&mut runner).unwrap();
let _ = runner.run_one(case, |_| {
if pass.fetch_or(true, Ordering::SeqCst) {
Ok(())
} else {
fail_case("fail")
}
// Only the first run fails, all others succeed
prop_assert!(pass.fetch_or(true, Ordering::SeqCst));
Ok(())
});
}

Expand Down
21 changes: 1 addition & 20 deletions src/strategy/rejection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,6 @@ use std::borrow::Cow;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Rejection(Cow<'static, str>);

/// Constructs and returns a [`Rejection`] based on the input type.
/// See the `From<T> for Rejection` implementations for details.
///
/// # Example
///
/// ```rust
/// fn main() {
/// use proptest::strategy::reject;
/// let reason = format!("The value {:?} was too much!", 100);
/// let reject = reject(reason);
/// println!("{:?}", reject);
/// }
/// ```
///
/// [`Rejection`]: enum.Rejection.html
pub fn reject<S: Into<Rejection>>(reason: S) -> Rejection {
reason.into()
}

impl From<&'static str> for Rejection {
fn from(s: &'static str) -> Self {
Rejection(s.into())
Expand Down Expand Up @@ -73,4 +54,4 @@ impl fmt::Display for Rejection {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self.as_ref(), f)
}
}
}
10 changes: 4 additions & 6 deletions src/strategy/unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,9 @@ mod test {
for _ in 0..256 {
let mut runner = TestRunner::default();
let case = input.new_value(&mut runner).unwrap();
let result = runner.run_one(case, |&v| if v < 15 {
let result = runner.run_one(case, |&v| {
prop_assert!(v < 15);
Ok(())
} else {
fail_case("fail")
});

match result {
Expand Down Expand Up @@ -410,10 +409,9 @@ mod test {
for _ in 0..256 {
let mut runner = TestRunner::default();
let case = input.new_value(&mut runner).unwrap();
let result = runner.run_one(case, |&v| if v < 15 {
let result = runner.run_one(case, |&v| {
prop_assert!(v < 15);
Ok(())
} else {
fail_case("fail")
});

match result {
Expand Down
12 changes: 7 additions & 5 deletions src/sugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ macro_rules! prop_assume {

($expr:expr, $fmt:tt $(, $fmt_arg:expr),*) => {
if !$expr {
return $crate::test_runner::reject_case(
format!(concat!("{}:{}:{}: ", $fmt),
file!(), line!(), column!()
$(, $fmt_arg)*));
return ::std::result::Result::Err(
$crate::test_runner::TestCaseError::reject(
format!(concat!("{}:{}:{}: ", $fmt),
file!(), line!(), column!()
$(, $fmt_arg)*)));
}
};
}
Expand Down Expand Up @@ -518,7 +519,8 @@ macro_rules! prop_assert {
if !$cond {
let message = format!($($fmt)*);
let message = format!("{} at {}:{}", message, file!(), line!());
return $crate::test_runner::fail_case(message);
return ::std::result::Result::Err(
$crate::test_runner::TestCaseError::fail(message));
}
};
}
Expand Down
39 changes: 16 additions & 23 deletions src/test_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,6 @@ impl TestCaseError {
}
}

/// Short-hand for `Err(TestCaseError::reject(..))`.
pub fn reject_case<R: Into<Rejection>>(reason: R) -> TestCaseResult {
Err(TestCaseError::reject(reason))
}

/// Short-hand for `Err(TestCaseError::fail(..))`.
pub fn fail_case<R: Into<Rejection>>(reason: R) -> TestCaseResult {
Err(TestCaseError::fail(reason))
}

impl fmt::Display for TestCaseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand Down Expand Up @@ -576,11 +566,11 @@ where
{
match panic::catch_unwind(AssertUnwindSafe(|| test(&case))) {
Ok(r) => r,
Err(what) => fail_case(
what.downcast::<&'static str>().map(|s| reject(*s))
.or_else(|what| what.downcast::<String>().map(|b| reject(*b)))
.or_else(|what| what.downcast::<Box<str>>().map(|b| reject(*b)))
.unwrap_or_else(|_| reject("<unknown panic value>"))),
Err(what) => Err(TestCaseError::Fail(
what.downcast::<&'static str>().map(|s| (*s).into())
.or_else(|what| what.downcast::<String>().map(|b| (*b).into()))
.or_else(|what| what.downcast::<Box<str>>().map(|b| (*b).into()))
.unwrap_or_else(|_| "<unknown panic value>".into()))),
}
}

Expand Down Expand Up @@ -829,7 +819,7 @@ impl TestRunner {
R: Into<Rejection>
{
if self.local_rejects >= self.config.max_local_rejects {
Err(reject("Too many local rejects"))
Err("Too many local rejects".into())
} else {
self.local_rejects += 1;
Self::insert_or_increment(&mut self.local_reject_detail,
Expand All @@ -842,7 +832,7 @@ impl TestRunner {
/// return `Ok` if the caller should keep going or `Err` to abort.
fn reject_global<T>(&mut self, whence: Rejection) -> Result<(),TestError<T>> {
if self.global_rejects >= self.config.max_global_rejects {
Err(TestError::Abort(reject("Too many global rejects")))
Err(TestError::Abort("Too many global rejects".into()))
} else {
self.global_rejects += 1;
Self::insert_or_increment(&mut self.global_reject_detail, whence);
Expand Down Expand Up @@ -889,7 +879,7 @@ mod test {
let runs = Cell::new(0);
let result = runner.run(&(0u32..), |_| {
runs.set(runs.get() + 1);
reject_case("reject")
Err(TestCaseError::reject("reject"))
});
match result {
Err(TestError::Abort(_)) => (),
Expand All @@ -911,11 +901,14 @@ mod test {
failure_persistence: FailurePersistence::Off,
.. Config::default()
});
let result = runner.run(&(0u32..10u32), |&v| if v < 5 {
Ok(())
} else {
fail_case("not less than 5")
});
let result = runner.run(
&(0u32..10u32), |&v| {
if v < 5 {
Ok(())
} else {
Err(TestCaseError::fail("not less than 5"))
}
});

assert_eq!(Err(TestError::Fail("not less than 5".into(), 5)), result);
}
Expand Down

0 comments on commit cbb3029

Please sign in to comment.