add happy path test

main
_ 2021-10-24 23:06:58 +00:00
parent 576cf0b4e5
commit 9ae1b73c47
1 changed files with 35 additions and 7 deletions

View File

@ -114,6 +114,9 @@ fn parse_input (s: &str) -> PlayerAction {
let s = s.to_lowercase (); 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" { if s == "quit" || s == "quit game" {
Quit Quit
} }
@ -124,22 +127,22 @@ fn parse_input (s: &str) -> PlayerAction {
RoomSpecific (LookAround) RoomSpecific (LookAround)
} }
else if let Some (rest) = s.strip_prefix ("look at the ") { 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 ") { 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 ") { 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 ") { 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 ") { 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 ") { else if let Some (rest) = s.strip_prefix ("use ") {
RoomSpecific (Use (parse_item_name (rest))) activate (rest)
} }
else if else if
s == "do nothing" || s == "do nothing" ||
@ -156,6 +159,8 @@ fn parse_input (s: &str) -> PlayerAction {
} }
fn parse_item_name (s: &str) -> ItemName { fn parse_item_name (s: &str) -> ItemName {
let s = s.trim ();
if s == "door" { if s == "door" {
ItemName::Door ItemName::Door
} }
@ -219,6 +224,7 @@ enum Response {
// These are just useful markers for the automated tests // These are just useful markers for the automated tests
JokeEnding, JokeEnding,
FailedDetectionCheck, FailedDetectionCheck,
PlayerVictory,
} }
#[derive (Clone, Default)] #[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.")); 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 => { ItemName::Note => {
require_detection! (self.room_1.detected_note); require_detection! (self.room_1.detected_note);
@ -437,6 +447,7 @@ mod test {
("look at the table", RoomSpecific (Look (ItemName::Table))), ("look at the table", RoomSpecific (Look (ItemName::Table))),
("look at table", RoomSpecific (Look (ItemName::Table))), ("look at table", RoomSpecific (Look (ItemName::Table))),
("look table", RoomSpecific (Look (ItemName::Table))), ("look table", RoomSpecific (Look (ItemName::Table))),
("look note ", RoomSpecific (Look (ItemName::Note))),
("LOOK TABLE", RoomSpecific (Look (ItemName::Table))), ("LOOK TABLE", RoomSpecific (Look (ItemName::Table))),
("wait", RoomSpecific (Wait)), ("wait", RoomSpecific (Wait)),
("hint", RoomSpecific (Hint)), ("hint", RoomSpecific (Hint)),
@ -468,4 +479,21 @@ mod test {
let responses = state.step ("look keypad"); let responses = state.step ("look keypad");
assert! (! responses.contains (&Response::FailedDetectionCheck)); 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));
}
} }