Skip to content

Commit

Permalink
fix stack overflow caused by parsing again again; fix grammar issue w…
Browse files Browse the repository at this point in the history
…hen trying to open a non-container; improve tests
  • Loading branch information
Zaechus committed Jan 13, 2025
1 parent a3003d7 commit ababbd4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ impl Game {
// TODO: equip
fn parse(&mut self, action: &Action) -> Outcome {
match action {
Action::Again => self.parse(&self.last_command.action().clone()), // FIXME
Action::Again => self.parse_again(),
Action::Attack(noun, obj) => Outcome::Active(self.parse_attack(noun, obj)),
Action::Break(_) => Outcome::Active("You can't do that yet.".to_owned()),
Action::Clarify(message) => Outcome::Idle(message.to_owned()),
Expand All @@ -700,6 +700,14 @@ impl Game {
}
}

fn parse_again(&mut self) -> Outcome {
if let Action::Again = self.last_command.action() {
Outcome::Idle("Excuse me?".to_owned())
} else {
self.parse(&self.last_command.action().clone())
}
}

fn parse_attack(&mut self, noun: &str, obj: &str) -> String {
find!(self, "attack", noun, in_room, obj, holding, attack);

Expand Down
4 changes: 2 additions & 2 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Item {
Container::Closed => {
format!("The {} is already closed.", self.name())
}
_ => format!("You cannot do that to a {}.", self.name()),
_ => format!("You cannot do that to the {}.", self.name()),
}
}

Expand Down Expand Up @@ -218,7 +218,7 @@ impl Item {
"Opened.".to_owned()
}
}
_ => format!("You cannot do that to a {}.", self.name()),
_ => format!("You cannot do that to the {}.", self.name()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/world.ron
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
dest: "BRIG",
door: "BRIG DOOR",
),
"ROOT BEER BARRELS": ( // FIXME: english
"ROOT BEER BARRELS": (
desc: "You can detect the slight scent of root beer.",
names: ["barrels", "root beer", "crates"],
locations: ["HOLD 1", "HOLD 2"],
Expand Down
24 changes: 20 additions & 4 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ mod tests {
fn test() {
let mut game: Game = include_str!("world.ron").parse().unwrap();

let excuse_me = ["", "a", "and", "a and", "and a"];
for s in excuse_me {
for s in ["", "a", "and", "a and", "and a"] {
assert_eq!(game.ask(s), "Excuse me?");
}

Expand All @@ -19,13 +18,15 @@ mod tests {
fn look() {
let mut game: Game = include_str!("world.ron").parse().unwrap();

let expected = "Center Room\nYou are in the center room.\nThere is a box here.";
for s in [
"l", // alias
"look", // look command
"look around", // long form
] {
assert_eq!(game.ask(s), expected);
assert_eq!(
game.ask(s),
"Center Room\nYou are in the center room.\nThere is a box here."
);
}
}

Expand Down Expand Up @@ -101,6 +102,13 @@ mod tests {
assert_eq!(game.ask("put apple in apple"), "Impossible.");
assert_eq!(game.ask("put box in box"), "Impossible.");

// try to open/close non-container
assert_eq!(
game.ask("open the apple"),
"You cannot do that to the apple."
);
assert_eq!(game.ask("close apple"), "You cannot do that to the apple.");

// close
assert_eq!(game.ask("close box"), "Closed.");

Expand Down Expand Up @@ -162,4 +170,12 @@ mod tests {
"You hit the self with your dagger."
);
}

#[test]
fn again() {
let mut game: Game = include_str!("world.ron").parse().unwrap();

game.ask("take it");
game.ask("again"); // make sure nothing funny happens
}
}

0 comments on commit ababbd4

Please sign in to comment.