Skip to content

Commit bd27265

Browse files
committed
test: write several tests
1 parent b397578 commit bd27265

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/config.rs

+9
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,13 @@ mod tests {
160160
assert!(result.is_err()); // Now we expect an error since the buffer write fails
161161
assert!(file.exists()); // File should still be created even though buffer write failed
162162
}
163+
164+
#[test]
165+
fn test_init_config_file_write_failure() -> io::Result<()> {
166+
let non_existent_dir = PathBuf::from("/non/existent/dir/envfetch.toml");
167+
let mut buffer = Vec::new();
168+
let result = init_config(non_existent_dir, &mut buffer);
169+
assert!(result.is_err()); // This should fail on fs::write
170+
Ok(())
171+
}
163172
}

src/interactive/tests.rs

+80
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,83 @@ fn test_event_non_key_press() {
537537
let result = mode.run(&mut terminal);
538538
assert!(result.is_ok());
539539
}
540+
541+
#[test]
542+
fn test_run_with_event_error() -> io::Result<()> {
543+
use crossterm::event::{Event, KeyEventState};
544+
let mut mode = InteractiveMode::default();
545+
546+
// Test different event types
547+
let events = vec![
548+
Event::Paste("test".to_string()),
549+
Event::Key(KeyEvent {
550+
code: KeyCode::Char('q'),
551+
modifiers: KeyModifiers::NONE,
552+
kind: KeyEventKind::Press,
553+
state: KeyEventState::NONE,
554+
}),
555+
Event::Key(KeyEvent {
556+
code: KeyCode::Char('q'),
557+
modifiers: KeyModifiers::CONTROL,
558+
kind: KeyEventKind::Press,
559+
state: KeyEventState::NONE,
560+
}),
561+
Event::Key(KeyEvent {
562+
code: KeyCode::Char('r'),
563+
modifiers: KeyModifiers::NONE,
564+
kind: KeyEventKind::Press,
565+
state: KeyEventState::NONE,
566+
}),
567+
Event::Key(KeyEvent {
568+
code: KeyCode::Char('r'),
569+
modifiers: KeyModifiers::CONTROL,
570+
kind: KeyEventKind::Press,
571+
state: KeyEventState::NONE,
572+
}),
573+
Event::Resize(20, 20),
574+
Event::FocusGained,
575+
Event::FocusLost,
576+
];
577+
578+
for event in events {
579+
match event {
580+
Event::Key(key_event) => mode.handle_key_event(key_event),
581+
_ => {}
582+
}
583+
}
584+
585+
Ok(())
586+
}
587+
588+
#[test]
589+
fn test_run_with_empty_terminal() -> io::Result<()> {
590+
use ratatui::backend::TestBackend;
591+
592+
let mut mode = InteractiveMode::default();
593+
let backend = TestBackend::new(0, 0);
594+
let mut terminal = Terminal::new(backend)?;
595+
596+
// Force exit condition
597+
mode.exit = true;
598+
599+
// This should test both the draw and event handling paths
600+
let result = mode.run(&mut terminal);
601+
assert!(result.is_ok());
602+
603+
Ok(())
604+
}
605+
606+
#[test]
607+
fn test_draw_with_generic_backend() -> io::Result<()> {
608+
let mut mode = InteractiveMode::default();
609+
let backend = TestBackend::new(10, 10);
610+
let mut terminal = Terminal::new(backend)?;
611+
612+
terminal.draw(|f| mode.draw(f))?;
613+
614+
// Try different frame sizes
615+
terminal.resize(Rect::new(0, 0, 5, 5))?;
616+
terminal.draw(|f| mode.draw(f))?;
617+
618+
Ok(())
619+
}

0 commit comments

Comments
 (0)