Skip to content

Commit 2a092df

Browse files
bors[bot]Xaeroxe
andcommitted
Merge #324
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 <[email protected]>
2 parents d68008d + b150ee0 commit 2a092df

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/exactly_one_err.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ where
2424
I: Iterator,
2525
{
2626
/// Creates a new `ExactlyOneErr` iterator.
27-
pub fn new(first_two: (Option<I::Item>, Option<I::Item>), inner: I) -> Self {
27+
pub(crate) fn new(first_two: (Option<I::Item>, Option<I::Item>), inner: I) -> Self {
2828
Self { first_two, inner }
2929
}
3030
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ pub trait Itertools : Iterator {
20562056
/// assert_eq!((0..10).filter(|&x| x == 2).exactly_one().unwrap(), 2);
20572057
/// assert!((0..10).filter(|&x| x > 1 && x < 4).exactly_one().unwrap_err().eq(2..4));
20582058
/// assert!((0..10).filter(|&x| x > 1 && x < 5).exactly_one().unwrap_err().eq(2..5));
2059-
/// assert!((0..10).filter(|&x| false).exactly_one().unwrap_err().eq(0..0));
2059+
/// assert!((0..10).filter(|&_| false).exactly_one().unwrap_err().eq(0..0));
20602060
/// ```
20612061
fn exactly_one(mut self) -> Result<Self::Item, ExactlyOneError<Self>>
20622062
where

tests/quick.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,3 +1015,13 @@ quickcheck! {
10151015
TestResult::from_bool(actual == expected)
10161016
}
10171017
}
1018+
1019+
quickcheck! {
1020+
fn exactly_one_i32(a: Vec<i32>) -> TestResult {
1021+
let ret = a.iter().cloned().exactly_one();
1022+
match a.len() {
1023+
1 => TestResult::from_bool(ret.unwrap() == a[0]),
1024+
_ => TestResult::from_bool(ret.unwrap_err().eq(a.iter().cloned())),
1025+
}
1026+
}
1027+
}

tests/test_core.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,11 @@ fn tree_fold1() {
244244
assert_eq!((0..i).tree_fold1(|x, y| x + y), (0..i).fold1(|x, y| x + y));
245245
}
246246
}
247+
248+
#[test]
249+
fn exactly_one() {
250+
assert_eq!((0..10).filter(|&x| x == 2).exactly_one().unwrap(), 2);
251+
assert!((0..10).filter(|&x| x > 1 && x < 4).exactly_one().unwrap_err().eq(2..4));
252+
assert!((0..10).filter(|&x| x > 1 && x < 5).exactly_one().unwrap_err().eq(2..5));
253+
assert!((0..10).filter(|&_| false).exactly_one().unwrap_err().eq(0..0));
254+
}

0 commit comments

Comments
 (0)