retroglyph_core/testing/
recording.rs1use crate::event::Event;
5use alloc::vec::Vec;
6use core::time::Duration;
7
8#[derive(Debug, Clone, PartialEq)]
23#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24pub struct InputRecording {
25 width: u16,
26 height: u16,
27 events: Vec<(Duration, Event)>,
28}
29
30impl InputRecording {
31 #[must_use]
33 pub const fn new(width: u16, height: u16) -> Self {
34 Self {
35 width,
36 height,
37 events: Vec::new(),
38 }
39 }
40
41 #[must_use]
43 pub const fn width(&self) -> u16 {
44 self.width
45 }
46
47 #[must_use]
49 pub const fn height(&self) -> u16 {
50 self.height
51 }
52
53 pub fn push(&mut self, delay: Duration, event: Event) {
56 self.events.push((delay, event));
57 }
58
59 #[must_use]
62 pub fn events(&self) -> impl ExactSizeIterator<Item = &(Duration, Event)> {
63 self.events.iter()
64 }
65
66 #[must_use]
68 pub const fn len(&self) -> usize {
69 self.events.len()
70 }
71
72 #[must_use]
74 pub const fn is_empty(&self) -> bool {
75 self.events.is_empty()
76 }
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82 use crate::event::{KeyCode, KeyEvent, KeyModifiers};
83
84 #[test]
85 fn new_recording_is_empty() {
86 let recording = InputRecording::new(80, 24);
87 assert_eq!(recording.width(), 80);
88 assert_eq!(recording.height(), 24);
89 assert!(recording.is_empty());
90 assert_eq!(recording.len(), 0);
91 }
92
93 #[test]
94 fn push_appends_in_order() {
95 let mut recording = InputRecording::new(4, 2);
96 let a = Event::Key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE));
97 let b = Event::Key(KeyEvent::new(KeyCode::Char('b'), KeyModifiers::NONE));
98 recording.push(Duration::ZERO, a.clone());
99 recording.push(Duration::from_millis(50), b.clone());
100
101 let events: Vec<_> = recording.events().collect();
102 assert_eq!(
103 events,
104 vec![&(Duration::ZERO, a), &(Duration::from_millis(50), b)]
105 );
106 assert_eq!(recording.len(), 2);
107 assert!(!recording.is_empty());
108 }
109
110 #[cfg(feature = "serde")]
111 #[test]
112 fn round_trips_through_serde_json() {
113 let mut recording = InputRecording::new(80, 24);
114 recording.push(
115 Duration::ZERO,
116 Event::Key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE)),
117 );
118 recording.push(
119 Duration::from_millis(250),
120 Event::Key(KeyEvent::new(KeyCode::Enter, KeyModifiers::SHIFT)),
121 );
122
123 let json = serde_json::to_string(&recording).expect("serialize");
124 let round_tripped: InputRecording = serde_json::from_str(&json).expect("deserialize");
125 assert_eq!(round_tripped, recording);
126 }
127}