Skip to content

Commit 09513fb

Browse files
unhappychoiceclaude
andcommitted
test: add screen snapshot test infrastructure
- Add screen_snapshot_test! macro for easy test creation - Support for key event simulation in tests - EmptyMockProvider for screens without data dependencies - Organize helpers in dedicated directory 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 7a898ba commit 09513fb

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

tests/integration/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod comment_processing_tests;
33
pub mod indent_treesitter_tests;
44
pub mod languages;
55
pub mod missing_ascii_art_test;
6+
pub mod screens;
67

78
use gittype::domain::models::languages::*;
89
use gittype::domain::models::{Challenge, CodeChunk, ExtractionOptions, Language, Languages};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[macro_use]
2+
mod test_helpers;
3+
4+
pub use test_helpers::*;
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use gittype::presentation::game::models::ScreenDataProvider;
2+
use gittype::Result;
3+
4+
/// Empty mock provider for screens that don't need data
5+
pub struct EmptyMockProvider;
6+
7+
impl ScreenDataProvider for EmptyMockProvider {
8+
fn provide(&self) -> Result<Box<dyn std::any::Any>> {
9+
Ok(Box::new(()))
10+
}
11+
}
12+
13+
/// Helper macro to create snapshot tests for screens using ratatui TestBackend
14+
#[macro_export]
15+
macro_rules! screen_snapshot_test {
16+
// Version with custom provider
17+
($test_name:ident, $screen_type:ty, $screen_init:expr, provider = $provider:expr) => {
18+
#[test]
19+
fn $test_name() {
20+
use gittype::presentation::game::Screen;
21+
use gittype::presentation::game::models::ScreenDataProvider;
22+
use ratatui::backend::TestBackend;
23+
use ratatui::Terminal;
24+
25+
let mut screen: $screen_type = $screen_init;
26+
27+
// Initialize screen with data from the provided mock provider
28+
let data = $provider.provide().unwrap();
29+
let _ = screen.init_with_data(data);
30+
31+
// Create a test backend with a reasonable terminal size
32+
let backend = TestBackend::new(120, 40);
33+
let mut terminal = Terminal::new(backend).unwrap();
34+
35+
terminal
36+
.draw(|frame| {
37+
screen.render_ratatui(frame).unwrap();
38+
})
39+
.unwrap();
40+
41+
// Get the rendered buffer as a string representation
42+
let buffer = terminal.backend().buffer();
43+
let mut output = String::new();
44+
for y in 0..buffer.area.height {
45+
for x in 0..buffer.area.width {
46+
let cell = &buffer[(x, y)];
47+
output.push_str(cell.symbol());
48+
}
49+
output.push('\n');
50+
}
51+
insta::assert_snapshot!(output);
52+
}
53+
};
54+
55+
// Version without custom provider (uses EmptyMockProvider)
56+
($test_name:ident, $screen_type:ty, $screen_init:expr) => {
57+
screen_snapshot_test!($test_name, $screen_type, $screen_init, provider = $crate::integration::screens::helpers::EmptyMockProvider);
58+
};
59+
60+
// Version with key events
61+
($test_name:ident, $screen_type:ty, $screen_init:expr, provider = $provider:expr, keys = [$($key:expr),*]) => {
62+
#[test]
63+
fn $test_name() {
64+
use gittype::presentation::game::Screen;
65+
use gittype::presentation::game::models::ScreenDataProvider;
66+
use ratatui::backend::TestBackend;
67+
use ratatui::Terminal;
68+
69+
let mut screen: $screen_type = $screen_init;
70+
71+
// Initialize screen with data from the provided mock provider
72+
let data = $provider.provide().unwrap();
73+
let _ = screen.init_with_data(data);
74+
75+
// Handle key events
76+
$(
77+
let _ = screen.handle_key_event($key);
78+
)*
79+
80+
// Create a test backend with a reasonable terminal size
81+
let backend = TestBackend::new(120, 40);
82+
let mut terminal = Terminal::new(backend).unwrap();
83+
84+
terminal
85+
.draw(|frame| {
86+
screen.render_ratatui(frame).unwrap();
87+
})
88+
.unwrap();
89+
90+
// Get the rendered buffer as a string representation
91+
let buffer = terminal.backend().buffer();
92+
let mut output = String::new();
93+
for y in 0..buffer.area.height {
94+
for x in 0..buffer.area.width {
95+
let cell = &buffer[(x, y)];
96+
output.push_str(cell.symbol());
97+
}
98+
output.push('\n');
99+
}
100+
insta::assert_snapshot!(output);
101+
}
102+
};
103+
104+
// Version with key events but without provider
105+
($test_name:ident, $screen_type:ty, $screen_init:expr, keys = [$($key:expr),*]) => {
106+
screen_snapshot_test!($test_name, $screen_type, $screen_init, provider = $crate::integration::screens::helpers::EmptyMockProvider, keys = [$($key),*]);
107+
};
108+
}

0 commit comments

Comments
 (0)