Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix all_fruits_types_in_basket to fail if all fruit kinds are not included #1782

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions exercises/11_hashmaps/hashmaps2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use std::collections::HashMap;

#[derive(Hash, PartialEq, Eq)]
#[derive(Hash, PartialEq, Eq, Debug)]
enum Fruit {
Apple,
Banana,
Expand All @@ -27,6 +27,14 @@ enum Fruit {
Pineapple,
}

const FRUIT_KINDS: [Fruit; 5] = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be in the test module, and there's no reason to have this be an array when it could be a Vec<Fruit> instead

Fruit::Apple,
Fruit::Banana,
Fruit::Mango,
Fruit::Lychee,
Fruit::Pineapple,
];

fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
let fruit_kinds = vec![
Fruit::Apple,
Expand Down Expand Up @@ -81,12 +89,15 @@ mod tests {
let count = basket.values().sum::<u32>();
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);
}
}
Expand Down