From b9330825b5d9c665b3e019b1872ee74e23d9b4a5 Mon Sep 17 00:00:00 2001 From: Hendrik Bilges Date: Tue, 29 Sep 2020 15:41:16 +0200 Subject: [PATCH 1/2] also using Result for the tested function --- exercises/poker/example.rs | 5 +++-- exercises/poker/tests/poker.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/exercises/poker/example.rs b/exercises/poker/example.rs index af9b024f6..f4ddff430 100644 --- a/exercises/poker/example.rs +++ b/exercises/poker/example.rs @@ -7,11 +7,11 @@ use counter::Counter; /// /// Note the type signature: this function should return _the same_ reference to /// the winning hand(s) as were passed in, not reconstructed strings which happen to be equal. -pub fn winning_hands<'a>(hands: &[&'a str]) -> Option> { +pub fn winning_hands<'a>(hands: &[&'a str]) -> Result, &'static str> { let mut hands = hands .into_iter() .map(|source| Hand::try_from(*source)) - .collect::, _>>().ok()?; + .collect::, _>>()?; hands.sort_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Less)); hands.last().map(|last| { hands @@ -21,6 +21,7 @@ pub fn winning_hands<'a>(hands: &[&'a str]) -> Option> { .map(|hand| hand.source) .collect() }) + .ok_or("No winning hand") } #[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Copy, Hash)] diff --git a/exercises/poker/tests/poker.rs b/exercises/poker/tests/poker.rs index b3d7b8ca4..6a6ac638d 100644 --- a/exercises/poker/tests/poker.rs +++ b/exercises/poker/tests/poker.rs @@ -26,6 +26,18 @@ fn test_single_hand_always_wins() { test(&["4S 5S 7H 8D JC"], &["4S 5S 7H 8D JC"]) } +#[test] +#[ignore] +fn test_empty_input() { + assert!(&winning_hands(&[""]).is_err()) +} + +#[test] +#[ignore] +fn test_not_enough_cards() { + assert!(&winning_hands(&["4S 5S 7H 8D"]).is_err()) +} + #[test] #[ignore] fn test_highest_card_of_all_hands_wins() { From b3fceede28a719e085bccfeb358b97647dfeec52 Mon Sep 17 00:00:00 2001 From: Hendrik Bilges Date: Tue, 29 Sep 2020 18:02:28 +0200 Subject: [PATCH 2/2] correcting function signature in lib.rs --- exercises/poker/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/poker/src/lib.rs b/exercises/poker/src/lib.rs index 3e91bafea..1fee8aaec 100644 --- a/exercises/poker/src/lib.rs +++ b/exercises/poker/src/lib.rs @@ -2,6 +2,6 @@ /// /// Note the type signature: this function should return _the same_ reference to /// the winning hand(s) as were passed in, not reconstructed strings which happen to be equal. -pub fn winning_hands<'a>(hands: &[&'a str]) -> Option> { +pub fn winning_hands<'a>(hands: &[&'a str]) -> Result, &'static str> { unimplemented!("Out of {:?}, which hand wins?", hands) }