test: add more tests

This commit is contained in:
Kristofers Solo 2025-07-01 22:05:52 +03:00
parent 8ecd9ec8d6
commit 9efeec3890
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
5 changed files with 138 additions and 1 deletions

View File

@ -5,7 +5,7 @@ use std::thread;
use std::time::{Duration, Instant};
/// Terminal events.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Event {
/// Terminal tick.
Tick,

View File

@ -14,3 +14,5 @@ pub mod tui;
pub mod handler;

12
tests/event.rs Normal file
View File

@ -0,0 +1,12 @@
use traxor::event::Event;
#[test]
fn test_event_from_key_code() {
// Test cases for various key codes
// Add more as needed based on your Event enum definition
let key_event = crossterm::event::KeyEvent::new(
crossterm::event::KeyCode::Char('q'),
crossterm::event::KeyModifiers::NONE,
);
assert_eq!(Event::Key(key_event), Event::Key(key_event));
}

109
tests/handler.rs Normal file
View File

@ -0,0 +1,109 @@
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use traxor::{app::action::Action, handler::get_action};
#[test]
fn test_get_action_quit() {
assert_eq!(get_action(KeyEvent::from(KeyCode::Esc)), Some(Action::Quit));
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('q'))),
Some(Action::Quit)
);
assert_eq!(
get_action(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)),
Some(Action::Quit)
);
assert_eq!(
get_action(KeyEvent::new(KeyCode::Char('C'), KeyModifiers::CONTROL)),
Some(Action::Quit)
);
}
#[test]
fn test_get_action_navigation() {
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('l'))),
Some(Action::NextTab)
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Right)),
Some(Action::NextTab)
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('h'))),
Some(Action::PrevTab)
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Left)),
Some(Action::PrevTab)
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('j'))),
Some(Action::NextTorrent)
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Down)),
Some(Action::NextTorrent)
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('k'))),
Some(Action::PrevTorrent)
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Up)),
Some(Action::PrevTorrent)
);
}
#[test]
fn test_get_action_switch_tab() {
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('1'))),
Some(Action::SwitchTab(0))
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('2'))),
Some(Action::SwitchTab(1))
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('3'))),
Some(Action::SwitchTab(2))
);
}
#[test]
fn test_get_action_torrent_actions() {
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('t'))),
Some(Action::ToggleTorrent)
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Enter)),
Some(Action::ToggleTorrent)
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Menu)),
Some(Action::ToggleTorrent)
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('a'))),
Some(Action::ToggleAll)
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('d'))),
Some(Action::Delete(false))
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('D'))),
Some(Action::Delete(true))
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::Char(' '))),
Some(Action::Select)
);
}
#[test]
fn test_get_action_unhandled() {
assert_eq!(get_action(KeyEvent::from(KeyCode::Char('x'))), None);
assert_eq!(get_action(KeyEvent::from(KeyCode::F(1))), None);
}

14
tests/tui.rs Normal file
View File

@ -0,0 +1,14 @@
use ratatui::backend::TestBackend;
use ratatui::Terminal;
use traxor::event::EventHandler;
use traxor::tui::Tui;
#[test]
fn test_tui_new() {
let backend = TestBackend::new(10, 10);
let terminal = Terminal::new(backend).unwrap();
let events = EventHandler::new(250); // Dummy tick_rate
let _tui = Tui::new(terminal, events);
// Add assertions for initial state of Tui if applicable
// For example, if Tui has a field that should be initialized to a specific value
}