✅ add happy path test
parent
576cf0b4e5
commit
9ae1b73c47
42
src/main.rs
42
src/main.rs
|
@ -114,6 +114,9 @@ fn parse_input (s: &str) -> PlayerAction {
|
|||
|
||||
let s = s.to_lowercase ();
|
||||
|
||||
let look = |rest| RoomSpecific (Look (parse_item_name (rest)));
|
||||
let activate = |rest| RoomSpecific (Use (parse_item_name (rest)));
|
||||
|
||||
if s == "quit" || s == "quit game" {
|
||||
Quit
|
||||
}
|
||||
|
@ -124,22 +127,22 @@ fn parse_input (s: &str) -> PlayerAction {
|
|||
RoomSpecific (LookAround)
|
||||
}
|
||||
else if let Some (rest) = s.strip_prefix ("look at the ") {
|
||||
RoomSpecific (Look (parse_item_name (rest)))
|
||||
look (rest)
|
||||
}
|
||||
else if let Some (rest) = s.strip_prefix ("look at ") {
|
||||
RoomSpecific (Look (parse_item_name (rest)))
|
||||
look (rest)
|
||||
}
|
||||
else if let Some (rest) = s.strip_prefix ("look ") {
|
||||
RoomSpecific (Look (parse_item_name (rest)))
|
||||
look (rest)
|
||||
}
|
||||
else if let Some (rest) = s.strip_prefix ("examine ") {
|
||||
RoomSpecific (Look (parse_item_name (rest)))
|
||||
look (rest)
|
||||
}
|
||||
else if let Some (rest) = s.strip_prefix ("use the ") {
|
||||
RoomSpecific (Use (parse_item_name (rest)))
|
||||
activate (rest)
|
||||
}
|
||||
else if let Some (rest) = s.strip_prefix ("use ") {
|
||||
RoomSpecific (Use (parse_item_name (rest)))
|
||||
activate (rest)
|
||||
}
|
||||
else if
|
||||
s == "do nothing" ||
|
||||
|
@ -156,6 +159,8 @@ fn parse_input (s: &str) -> PlayerAction {
|
|||
}
|
||||
|
||||
fn parse_item_name (s: &str) -> ItemName {
|
||||
let s = s.trim ();
|
||||
|
||||
if s == "door" {
|
||||
ItemName::Door
|
||||
}
|
||||
|
@ -219,6 +224,7 @@ enum Response {
|
|||
// These are just useful markers for the automated tests
|
||||
JokeEnding,
|
||||
FailedDetectionCheck,
|
||||
PlayerVictory,
|
||||
}
|
||||
|
||||
#[derive (Clone, Default)]
|
||||
|
@ -346,7 +352,11 @@ impl State {
|
|||
return just (line_response ("You can't USE the KEYPAD, you don't know the code for it. You would normally try guessing, but it would take the programmer all day to implement that level of interaction."));
|
||||
}
|
||||
|
||||
panic! ("You USE the code on the KEYPAD. The door opens, and the game immediately crashes.");
|
||||
vec! [
|
||||
line_response ("You USE the code on the KEYPAD. The door opens, and the game immediately crashes."),
|
||||
Response::PlayerVictory,
|
||||
Response::Quit,
|
||||
]
|
||||
},
|
||||
ItemName::Note => {
|
||||
require_detection! (self.room_1.detected_note);
|
||||
|
@ -437,6 +447,7 @@ mod test {
|
|||
("look at the table", RoomSpecific (Look (ItemName::Table))),
|
||||
("look at table", RoomSpecific (Look (ItemName::Table))),
|
||||
("look table", RoomSpecific (Look (ItemName::Table))),
|
||||
("look note ", RoomSpecific (Look (ItemName::Note))),
|
||||
("LOOK TABLE", RoomSpecific (Look (ItemName::Table))),
|
||||
("wait", RoomSpecific (Wait)),
|
||||
("hint", RoomSpecific (Hint)),
|
||||
|
@ -468,4 +479,21 @@ mod test {
|
|||
let responses = state.step ("look keypad");
|
||||
assert! (! responses.contains (&Response::FailedDetectionCheck));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn happy_path () {
|
||||
let mut state = State::default ();
|
||||
|
||||
for line in [
|
||||
"look table",
|
||||
"look note",
|
||||
"look door",
|
||||
] {
|
||||
let responses = state.step (line);
|
||||
assert! (! responses.contains (&Response::PlayerVictory));
|
||||
}
|
||||
|
||||
let responses = state.step ("use keypad");
|
||||
assert! (responses.contains (&Response::PlayerVictory));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue