Skip to content

Commit ababbd4

Browse files
committed
fix stack overflow caused by parsing again again; fix grammar issue when trying to open a non-container; improve tests
1 parent a3003d7 commit ababbd4

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

src/game.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ impl Game {
673673
// TODO: equip
674674
fn parse(&mut self, action: &Action) -> Outcome {
675675
match action {
676-
Action::Again => self.parse(&self.last_command.action().clone()), // FIXME
676+
Action::Again => self.parse_again(),
677677
Action::Attack(noun, obj) => Outcome::Active(self.parse_attack(noun, obj)),
678678
Action::Break(_) => Outcome::Active("You can't do that yet.".to_owned()),
679679
Action::Clarify(message) => Outcome::Idle(message.to_owned()),
@@ -700,6 +700,14 @@ impl Game {
700700
}
701701
}
702702

703+
fn parse_again(&mut self) -> Outcome {
704+
if let Action::Again = self.last_command.action() {
705+
Outcome::Idle("Excuse me?".to_owned())
706+
} else {
707+
self.parse(&self.last_command.action().clone())
708+
}
709+
}
710+
703711
fn parse_attack(&mut self, noun: &str, obj: &str) -> String {
704712
find!(self, "attack", noun, in_room, obj, holding, attack);
705713

src/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Item {
8787
Container::Closed => {
8888
format!("The {} is already closed.", self.name())
8989
}
90-
_ => format!("You cannot do that to a {}.", self.name()),
90+
_ => format!("You cannot do that to the {}.", self.name()),
9191
}
9292
}
9393

@@ -218,7 +218,7 @@ impl Item {
218218
"Opened.".to_owned()
219219
}
220220
}
221-
_ => format!("You cannot do that to a {}.", self.name()),
221+
_ => format!("You cannot do that to the {}.", self.name()),
222222
}
223223
}
224224

src/world.ron

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
dest: "BRIG",
4141
door: "BRIG DOOR",
4242
),
43-
"ROOT BEER BARRELS": ( // FIXME: english
43+
"ROOT BEER BARRELS": (
4444
desc: "You can detect the slight scent of root beer.",
4545
names: ["barrels", "root beer", "crates"],
4646
locations: ["HOLD 1", "HOLD 2"],

tests/test.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ mod tests {
66
fn test() {
77
let mut game: Game = include_str!("world.ron").parse().unwrap();
88

9-
let excuse_me = ["", "a", "and", "a and", "and a"];
10-
for s in excuse_me {
9+
for s in ["", "a", "and", "a and", "and a"] {
1110
assert_eq!(game.ask(s), "Excuse me?");
1211
}
1312

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

22-
let expected = "Center Room\nYou are in the center room.\nThere is a box here.";
2321
for s in [
2422
"l", // alias
2523
"look", // look command
2624
"look around", // long form
2725
] {
28-
assert_eq!(game.ask(s), expected);
26+
assert_eq!(
27+
game.ask(s),
28+
"Center Room\nYou are in the center room.\nThere is a box here."
29+
);
2930
}
3031
}
3132

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

105+
// try to open/close non-container
106+
assert_eq!(
107+
game.ask("open the apple"),
108+
"You cannot do that to the apple."
109+
);
110+
assert_eq!(game.ask("close apple"), "You cannot do that to the apple.");
111+
104112
// close
105113
assert_eq!(game.ask("close box"), "Closed.");
106114

@@ -162,4 +170,12 @@ mod tests {
162170
"You hit the self with your dagger."
163171
);
164172
}
173+
174+
#[test]
175+
fn again() {
176+
let mut game: Game = include_str!("world.ron").parse().unwrap();
177+
178+
game.ask("take it");
179+
game.ask("again"); // make sure nothing funny happens
180+
}
165181
}

0 commit comments

Comments
 (0)