diff options
Diffstat (limited to 'tests/client_core.rs')
| -rw-r--r-- | tests/client_core.rs | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/tests/client_core.rs b/tests/client_core.rs index 53a024c..722cf0d 100644 --- a/tests/client_core.rs +++ b/tests/client_core.rs | |||
| @@ -6,7 +6,8 @@ | |||
| 6 | 6 | ||
| 7 | use std::time::Duration; | 7 | use std::time::Duration; |
| 8 | use zagan_bet::blackjack::BlackjackState; | 8 | use zagan_bet::blackjack::BlackjackState; |
| 9 | use zagan_bet::client::core::{ClientCore, Effect, InteractiveCommand, Key}; | 9 | use zagan_bet::client::core::{ClientCore, Effect, InteractiveCommand}; |
| 10 | use zagan_bet::client::input::{game_input, Editor, GameInput, Input, Key}; | ||
| 10 | use zagan_bet::client::games::response::{ | 11 | use zagan_bet::client::games::response::{ |
| 11 | parse_blackjack_response, parse_crash_response, parse_spin_response, | 12 | parse_blackjack_response, parse_crash_response, parse_spin_response, |
| 12 | }; | 13 | }; |
| @@ -66,10 +67,6 @@ impl Screen { | |||
| 66 | self.lines.extend(lines.iter().cloned()); | 67 | self.lines.extend(lines.iter().cloned()); |
| 67 | self.region_len = lines.len(); | 68 | self.region_len = lines.len(); |
| 68 | } | 69 | } |
| 69 | Effect::SetPrompt { text, cursor_col } => { | ||
| 70 | self.prompt = text.clone(); | ||
| 71 | self.prompt_cursor = *cursor_col; | ||
| 72 | } | ||
| 73 | Effect::SendLine(line) => sent.push(line.clone()), | 70 | Effect::SendLine(line) => sent.push(line.clone()), |
| 74 | Effect::Interactive(command) => interactive.push(command.clone()), | 71 | Effect::Interactive(command) => interactive.push(command.clone()), |
| 75 | Effect::Quit | Effect::Disconnect | Effect::ListHosts | Effect::ForgetHost(_) => {} | 72 | Effect::Quit | Effect::Disconnect | Effect::ListHosts | Effect::ForgetHost(_) => {} |
| @@ -87,6 +84,7 @@ impl Screen { | |||
| 87 | 84 | ||
| 88 | struct Harness { | 85 | struct Harness { |
| 89 | core: ClientCore, | 86 | core: ClientCore, |
| 87 | editor: Editor, | ||
| 90 | screen: Screen, | 88 | screen: Screen, |
| 91 | sent: Vec<String>, | 89 | sent: Vec<String>, |
| 92 | interactive: Vec<InteractiveCommand>, | 90 | interactive: Vec<InteractiveCommand>, |
| @@ -99,6 +97,7 @@ impl Harness { | |||
| 99 | core.set_logged_in(Some("alice".to_string())); | 97 | core.set_logged_in(Some("alice".to_string())); |
| 100 | Self { | 98 | Self { |
| 101 | core, | 99 | core, |
| 100 | editor: Editor::new(), | ||
| 102 | screen: Screen::default(), | 101 | screen: Screen::default(), |
| 103 | sent: Vec::new(), | 102 | sent: Vec::new(), |
| 104 | interactive: Vec::new(), | 103 | interactive: Vec::new(), |
| @@ -111,11 +110,30 @@ impl Harness { | |||
| 111 | } | 110 | } |
| 112 | 111 | ||
| 113 | fn key(&mut self, key: Key) { | 112 | fn key(&mut self, key: Key) { |
| 114 | let effects = self.core.handle_key(key); | 113 | let effects = if self.core.is_in_game() { |
| 114 | match game_input(self.core.current_game_type().unwrap_or(""), key) { | ||
| 115 | GameInput::Quit => vec![Effect::Quit], | ||
| 116 | GameInput::Command(command) => self.core.command(command), | ||
| 117 | GameInput::None => Vec::new(), | ||
| 118 | } | ||
| 119 | } else { | ||
| 120 | match self.editor.handle(key) { | ||
| 121 | Input::Quit => vec![Effect::Quit], | ||
| 122 | Input::Submit(line) => self.core.submit_line(&line), | ||
| 123 | Input::Print(lines) => vec![Effect::Print(lines)], | ||
| 124 | Input::Prompt | Input::None => Vec::new(), | ||
| 125 | } | ||
| 126 | }; | ||
| 115 | self.apply(effects); | 127 | self.apply(effects); |
| 116 | if let Some(prompt) = self.core.prompt_effect() { | 128 | self.apply_prompt(); |
| 117 | self.apply(vec![prompt]); | 129 | } |
| 130 | |||
| 131 | fn apply_prompt(&mut self) { | ||
| 132 | if self.core.is_in_game() { | ||
| 133 | return; | ||
| 118 | } | 134 | } |
| 135 | self.screen.prompt = self.editor.input().to_string(); | ||
| 136 | self.screen.prompt_cursor = self.editor.cursor_column(); | ||
| 119 | } | 137 | } |
| 120 | 138 | ||
| 121 | fn line(&mut self, line: &str) { | 139 | fn line(&mut self, line: &str) { |
| @@ -128,9 +146,7 @@ impl Harness { | |||
| 128 | fn reply(&mut self, reply: &str) { | 146 | fn reply(&mut self, reply: &str) { |
| 129 | let effects = self.core.handle_reply(reply); | 147 | let effects = self.core.handle_reply(reply); |
| 130 | self.apply(effects); | 148 | self.apply(effects); |
| 131 | if let Some(prompt) = self.core.prompt_effect() { | 149 | self.apply_prompt(); |
| 132 | self.apply(vec![prompt]); | ||
| 133 | } | ||
| 134 | } | 150 | } |
| 135 | 151 | ||
| 136 | fn board(&self) -> &[String] { | 152 | fn board(&self) -> &[String] { |
| @@ -469,16 +485,15 @@ fn play_requires_a_connection_and_a_login() { | |||
| 469 | 485 | ||
| 470 | #[test] | 486 | #[test] |
| 471 | fn ctrl_c_quits_from_the_prompt() { | 487 | fn ctrl_c_quits_from_the_prompt() { |
| 472 | let mut core = ClientCore::new(); | 488 | let mut editor = Editor::new(); |
| 473 | assert_eq!(core.handle_key(Key::CtrlC), vec![Effect::Quit]); | 489 | assert_eq!(editor.handle(Key::CtrlC), Input::Quit); |
| 474 | } | 490 | } |
| 475 | 491 | ||
| 476 | #[test] | 492 | #[test] |
| 477 | fn ctrl_c_quits_from_a_game() { | 493 | fn ctrl_c_quits_from_a_game() { |
| 478 | let mut h = Harness::new(); | 494 | assert_eq!(game_input("slots", Key::CtrlC), GameInput::Quit); |
| 479 | h.enter_slots("50"); | 495 | assert_eq!(game_input("roulette", Key::CtrlC), GameInput::Quit); |
| 480 | 496 | assert_eq!(game_input("blackjack", Key::CtrlC), GameInput::Quit); | |
| 481 | assert_eq!(h.core.handle_key(Key::CtrlC), vec![Effect::Quit]); | ||
| 482 | } | 497 | } |
| 483 | 498 | ||
| 484 | #[test] | 499 | #[test] |
