test joke ending

main
_ 2021-10-24 22:16:14 +00:00
parent 6630002a34
commit d40e6a83f7
1 changed files with 19 additions and 8 deletions

View File

@ -185,6 +185,7 @@ struct StateRoom1 {
detected_note: bool, detected_note: bool,
} }
#[derive (PartialEq)]
enum Response { enum Response {
Print (String), Print (String),
PrintMany (Vec <&'static str>), PrintMany (Vec <&'static str>),
@ -199,12 +200,12 @@ struct State {
} }
impl State { impl State {
fn room_1 (&mut self, input: String) -> Result <Vec <Response>> { fn room_1 (&mut self, input: &str) -> Vec <Response> {
use Response::*; use Response::*;
let action = parse_input (&input); let action = parse_input (input);
Ok (match action { match action {
PlayerAction::Quit => { PlayerAction::Quit => {
vec! [ vec! [
line_response ("Bye."), line_response ("Bye."),
@ -240,16 +241,16 @@ impl State {
}, },
ItemName::Keypad => { ItemName::Keypad => {
if ! self.room_1.detected_keypad { if ! self.room_1.detected_keypad {
return Ok (just (undetected_item ())); return just (undetected_item ());
} }
just (line_response ("The DOOR is locked by an electronic KEYPAD. A soft amber power light indicates that the KEYPAD is likely functional. The KEYPAD buttons are the digits 0-9, Enter, and Clear. Experience tells you that the key code is likely 4 or 5 digits long.")) just (line_response ("The DOOR is locked by an electronic KEYPAD. A soft amber power light indicates that the KEYPAD is likely functional. The KEYPAD buttons are the digits 0-9, Enter, and Clear. Experience tells you that the key code is likely 4 or 5 digits long."))
}, },
ItemName::Note => { ItemName::Note => {
if ! self.room_1.detected_note { if ! self.room_1.detected_note {
return Ok (vec! [ return vec! [
undetected_item (), undetected_item (),
]); ];
} }
just (Response::PrintMany (vec! [ just (Response::PrintMany (vec! [
@ -314,7 +315,7 @@ impl State {
}, },
} }
}, },
}) }
} }
} }
@ -343,7 +344,7 @@ fn game () -> Result <()> {
'main_loop: loop { 'main_loop: loop {
let input = read_input ()?; let input = read_input ()?;
let responses = state.room_1 (input)?; let responses = state.room_1 (&input);
for response in responses.into_iter () { for response in responses.into_iter () {
match response { match response {
@ -395,6 +396,16 @@ mod test {
#[test] #[test]
fn joke_ending () { fn joke_ending () {
use super::{
Response,
State,
};
let mut state = State::default ();
let responses = state.room_1 ("use emergency exit");
assert! (responses.contains (&Response::Quit));
assert! (responses.contains (&Response::JokeEnding));
} }
} }