Skip to content

Commit

Permalink
Merge #324
Browse files Browse the repository at this point in the history
324: Add unit tests for exactly_one and move ExactlyOneError::new to pub(crate) r=bluss a=Xaeroxe

Follow up to #310 

Ready for review

Co-authored-by: Jacob Kiesel <kieseljake@gmail.com>
  • Loading branch information
bors[bot] and Xaeroxe committed Dec 13, 2018
2 parents d68008d + b150ee0 commit 2a092df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/exactly_one_err.rs
Expand Up @@ -24,7 +24,7 @@ where
I: Iterator,
{
/// Creates a new `ExactlyOneErr` iterator.
pub fn new(first_two: (Option<I::Item>, Option<I::Item>), inner: I) -> Self {
pub(crate) fn new(first_two: (Option<I::Item>, Option<I::Item>), inner: I) -> Self {
Self { first_two, inner }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -2056,7 +2056,7 @@ pub trait Itertools : Iterator {
/// assert_eq!((0..10).filter(|&x| x == 2).exactly_one().unwrap(), 2);
/// assert!((0..10).filter(|&x| x > 1 && x < 4).exactly_one().unwrap_err().eq(2..4));
/// assert!((0..10).filter(|&x| x > 1 && x < 5).exactly_one().unwrap_err().eq(2..5));
/// assert!((0..10).filter(|&x| false).exactly_one().unwrap_err().eq(0..0));
/// assert!((0..10).filter(|&_| false).exactly_one().unwrap_err().eq(0..0));
/// ```
fn exactly_one(mut self) -> Result<Self::Item, ExactlyOneError<Self>>
where
Expand Down
10 changes: 10 additions & 0 deletions tests/quick.rs
Expand Up @@ -1015,3 +1015,13 @@ quickcheck! {
TestResult::from_bool(actual == expected)
}
}

quickcheck! {
fn exactly_one_i32(a: Vec<i32>) -> TestResult {
let ret = a.iter().cloned().exactly_one();
match a.len() {
1 => TestResult::from_bool(ret.unwrap() == a[0]),
_ => TestResult::from_bool(ret.unwrap_err().eq(a.iter().cloned())),
}
}
}
8 changes: 8 additions & 0 deletions tests/test_core.rs
Expand Up @@ -244,3 +244,11 @@ fn tree_fold1() {
assert_eq!((0..i).tree_fold1(|x, y| x + y), (0..i).fold1(|x, y| x + y));
}
}

#[test]
fn exactly_one() {
assert_eq!((0..10).filter(|&x| x == 2).exactly_one().unwrap(), 2);
assert!((0..10).filter(|&x| x > 1 && x < 4).exactly_one().unwrap_err().eq(2..4));
assert!((0..10).filter(|&x| x > 1 && x < 5).exactly_one().unwrap_err().eq(2..5));
assert!((0..10).filter(|&_| false).exactly_one().unwrap_err().eq(0..0));
}

0 comments on commit 2a092df

Please sign in to comment.