-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_engine.rs
More file actions
85 lines (75 loc) · 2.7 KB
/
Copy pathtest_engine.rs
File metadata and controls
85 lines (75 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use captured_write::CapturedWrite;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::{cell::RefCell, str};
use crate::{
engine::{Engine, UpdateResult, UpdateSignal},
state::GameState,
};
use raw_format_ansi::raw_format_ansi;
pub struct TestEngine {
writer_ref: RefCell<CapturedWrite>,
game_state: GameState,
}
impl TestEngine {
#[allow(unused_must_use)]
pub fn from_game_state(mut game_state: GameState) -> UpdateResult<Self> {
let writer = CapturedWrite::new();
let writer_box: RefCell<CapturedWrite> = RefCell::from(writer);
let mut engine = Engine::new(&writer_box);
engine.draw_scene(&mut game_state)?;
Ok(Self {
writer_ref: writer_box,
game_state,
})
}
pub fn get_current_formatted(&self) -> String {
let buffer = self.writer_ref.borrow().buffer.clone();
raw_format_ansi(&buffer)
}
pub fn expect(&self, expectation: &str) -> bool {
let expectation = expectation.trim_matches('\n');
let formatted = self.get_current_formatted();
let result = formatted.contains(expectation);
if !result {
println!("----------------\n{}\n----------------", formatted);
}
result
}
pub fn nexpect(&self, expectation: &str) -> bool {
let expectation = expectation.trim_matches('\n');
let formatted = self.get_current_formatted();
let result = !formatted.contains(expectation);
if !result {
println!("----------------\n{}\n----------------", formatted);
}
result
}
pub fn expect_full(&self, expectation: &str) -> String {
let expectation = expectation.trim_matches('\n');
let formatted = self.get_current_formatted();
let result = formatted == *expectation;
if !result {
println!("----------------\n{}\n----------------", formatted);
}
expectation.to_string()
}
#[allow(unused_must_use)]
pub fn keypress(&mut self, key_code: KeyCode) -> UpdateResult<UpdateSignal> {
self.writer_ref.borrow_mut().reset();
let mut engine = Engine::new(&self.writer_ref);
let update = engine.draw_scene(&mut self.game_state)?;
let signal = update(
KeyEvent::new(key_code, KeyModifiers::empty()),
&mut self.game_state,
)?;
self.writer_ref.borrow_mut().reset();
engine.draw_scene(&mut self.game_state)?;
Ok(signal)
}
pub fn charpress(&mut self, char: char) -> UpdateResult<UpdateSignal> {
self.keypress(KeyCode::Char(char))
}
pub fn enterpress(&mut self) -> UpdateResult<UpdateSignal> {
self.keypress(KeyCode::Enter)
}
}