From 8bfe2ec71e4800b798d7c0d7b3224ff2530a1c79 Mon Sep 17 00:00:00 2001 From: Daniel Somerfield Date: Tue, 21 Nov 2023 14:02:26 -0800 Subject: [PATCH 1/2] Fix all_fruits_types_in_basket to fail if all fruit kinds are not included --- exercises/11_hashmaps/hashmaps2.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/exercises/11_hashmaps/hashmaps2.rs b/exercises/11_hashmaps/hashmaps2.rs index a5925690..ab918cd8 100644 --- a/exercises/11_hashmaps/hashmaps2.rs +++ b/exercises/11_hashmaps/hashmaps2.rs @@ -18,7 +18,7 @@ use std::collections::HashMap; -#[derive(Hash, PartialEq, Eq)] +#[derive(Hash, PartialEq, Eq, Debug)] enum Fruit { Apple, Banana, @@ -27,6 +27,14 @@ enum Fruit { Pineapple, } +const FRUIT_KINDS: [Fruit; 5] = [ + Fruit::Apple, + Fruit::Banana, + Fruit::Mango, + Fruit::Lychee, + Fruit::Pineapple, +]; + fn fruit_basket(basket: &mut HashMap) { let fruit_kinds = vec![ Fruit::Apple, @@ -81,12 +89,15 @@ mod tests { let count = basket.values().sum::(); assert!(count > 11); } - + #[test] fn all_fruit_types_in_basket() { let mut basket = get_fruit_basket(); fruit_basket(&mut basket); - for amount in basket.values() { + for fruit_kind in FRUIT_KINDS { + let amount = basket + .get(&fruit_kind) + .expect(format!("Fruit kind {:?} was not found in basket", fruit_kind).as_str()); assert_ne!(amount, &0); } } From 62afbb034f74dc170347d3eff5131e780930d639 Mon Sep 17 00:00:00 2001 From: Daniel Somerfield Date: Wed, 27 Mar 2024 20:37:19 -0700 Subject: [PATCH 2/2] Move test array to be in test module as vec --- exercises/11_hashmaps/hashmaps2.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/exercises/11_hashmaps/hashmaps2.rs b/exercises/11_hashmaps/hashmaps2.rs index ab918cd8..a860d7b2 100644 --- a/exercises/11_hashmaps/hashmaps2.rs +++ b/exercises/11_hashmaps/hashmaps2.rs @@ -27,14 +27,6 @@ enum Fruit { Pineapple, } -const FRUIT_KINDS: [Fruit; 5] = [ - Fruit::Apple, - Fruit::Banana, - Fruit::Mango, - Fruit::Lychee, - Fruit::Pineapple, -]; - fn fruit_basket(basket: &mut HashMap) { let fruit_kinds = vec![ Fruit::Apple, @@ -92,9 +84,17 @@ mod tests { #[test] fn all_fruit_types_in_basket() { + let fruit_kinds = vec![ + Fruit::Apple, + Fruit::Banana, + Fruit::Mango, + Fruit::Lychee, + Fruit::Pineapple, + ]; + let mut basket = get_fruit_basket(); fruit_basket(&mut basket); - for fruit_kind in FRUIT_KINDS { + for fruit_kind in fruit_kinds { let amount = basket .get(&fruit_kind) .expect(format!("Fruit kind {:?} was not found in basket", fruit_kind).as_str());