Make starting fireworks more fun :)

This commit is contained in:
Remo Senekowitsch 2026-04-13 18:51:38 +02:00
parent 7c1d8ebf49
commit 346753b673
No known key found for this signature in database
2 changed files with 14 additions and 28 deletions

View File

@ -22,13 +22,7 @@ fn add_rockets(fireworks: &mut Fireworks, rockets: usize) {
// TODO: Turn this function into a method on `Fireworks`. // TODO: Turn this function into a method on `Fireworks`.
fn start(fireworks: Fireworks) -> String { fn start(fireworks: Fireworks) -> String {
if fireworks.rockets < 5 { "🚀".repeat(fireworks.rockets)
String::from("small")
} else if fireworks.rockets < 20 {
String::from("medium")
} else {
String::from("big")
}
} }
fn main() { fn main() {
@ -41,18 +35,17 @@ mod tests {
#[test] #[test]
fn start_some_fireworks() { fn start_some_fireworks() {
let f = Fireworks::new();
assert_eq!(f.start(), "");
let mut f = Fireworks::new(); let mut f = Fireworks::new();
f.add_rockets(3); f.add_rockets(3);
assert_eq!(f.start(), "small"); assert_eq!(f.start(), "🚀🚀🚀");
let mut f = Fireworks::new(); let mut f = Fireworks::new();
f.add_rockets(15); f.add_rockets(7);
assert_eq!(f.start(), "medium");
let mut f = Fireworks::new();
f.add_rockets(100);
// We don't use method syntax in the last test to ensure the `start` // We don't use method syntax in the last test to ensure the `start`
// function takes ownership of the fireworks. // function takes ownership of the fireworks.
assert_eq!(Fireworks::start(f), "big"); assert_eq!(Fireworks::start(f), "🚀🚀🚀🚀🚀🚀🚀");
} }
} }

View File

@ -15,13 +15,7 @@ impl Fireworks {
} }
fn start(self) -> String { fn start(self) -> String {
if self.rockets < 5 { "🚀".repeat(self.rockets)
String::from("small")
} else if self.rockets < 20 {
String::from("medium")
} else {
String::from("big")
}
} }
} }
@ -35,18 +29,17 @@ mod tests {
#[test] #[test]
fn start_some_fireworks() { fn start_some_fireworks() {
let f = Fireworks::new();
assert_eq!(f.start(), "");
let mut f = Fireworks::new(); let mut f = Fireworks::new();
f.add_rockets(3); f.add_rockets(3);
assert_eq!(f.start(), "small"); assert_eq!(f.start(), "🚀🚀🚀");
let mut f = Fireworks::new(); let mut f = Fireworks::new();
f.add_rockets(15); f.add_rockets(7);
assert_eq!(f.start(), "medium");
let mut f = Fireworks::new();
f.add_rockets(100);
// We don't use method syntax in the last test to ensure the `start` // We don't use method syntax in the last test to ensure the `start`
// function takes ownership of the fireworks. // function takes ownership of the fireworks.
assert_eq!(Fireworks::start(f), "big"); assert_eq!(Fireworks::start(f), "🚀🚀🚀🚀🚀🚀🚀");
} }
} }