From 9ae1b73c4763a1c5331d69b18bac85f84e31a82d Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 24 Oct 2021 23:06:58 +0000 Subject: [PATCH] :white_check_mark: add happy path test --- src/main.rs | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 55ac026..a4bcd25 100644 --- a/src/main.rs +++ b/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)); + } }