test: fix errors

This commit is contained in:
Kristofers Solo 2025-07-14 14:08:00 +03:00
parent df0101d1c5
commit eff0fe20e4
Signed by: kristoferssolo
GPG Key ID: 8687F2D3EEE6F0ED
5 changed files with 153 additions and 34 deletions

View File

@ -20,3 +20,27 @@ impl Display for FileSize {
write!(f, "{}", UnitDisplay::new(&self.0, UNITS)) write!(f, "{}", UnitDisplay::new(&self.0, UNITS))
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let file_size = FileSize::new(1024);
assert_eq!(file_size.0.as_raw(), 1024);
}
#[test]
fn test_display() {
let file_size = FileSize::new(1024);
assert_eq!(file_size.to_string(), "1.00 KB");
let file_size = FileSize::new(1024 * 1024);
assert_eq!(file_size.to_string(), "1.00 MB");
let file_size = FileSize::new(1024 * 1024 * 1024);
assert_eq!(file_size.to_string(), "1.00 GB");
}
}

View File

@ -20,3 +20,27 @@ impl Display for NetSpeed {
write!(f, "{}", UnitDisplay::new(&self.0, UNITS)) write!(f, "{}", UnitDisplay::new(&self.0, UNITS))
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let net_speed = NetSpeed::new(1024);
assert_eq!(net_speed.0.as_raw(), 1024);
}
#[test]
fn test_display() {
let net_speed = NetSpeed::new(1024);
assert_eq!(net_speed.to_string(), "1.00 KB/s");
let net_speed = NetSpeed::new(1024 * 1024);
assert_eq!(net_speed.to_string(), "1.00 MB/s");
let net_speed = NetSpeed::new(1024 * 1024 * 1024);
assert_eq!(net_speed.to_string(), "1.00 GB/s");
}
}

View File

@ -58,7 +58,7 @@ impl Unit {
#[inline] #[inline]
#[must_use] #[must_use]
pub const fn value(&self) -> u64 { pub const fn as_raw(&self) -> u64 {
self.0 self.0
} }
} }
@ -126,3 +126,35 @@ macro_rules! impl_unit_newtype {
} }
}; };
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_unit_from_u64() {
let unit = Unit::from(1024u64);
assert_eq!(unit.as_raw(), 1024);
}
#[test]
fn test_unit_from_raw() {
let unit = Unit::from_raw(1024);
assert_eq!(unit.as_raw(), 1024);
}
#[test]
fn test_unit_display() {
let unit = Unit::from_raw(1024);
let display = UnitDisplay::new(&unit, &["B", "KB", "MB"]);
assert_eq!(display.to_string(), "1.00 KB");
let unit = Unit::from_raw(1024 * 1024);
let display = UnitDisplay::new(&unit, &["B", "KB", "MB"]);
assert_eq!(display.to_string(), "1.00 MB");
let unit = Unit::from_raw(512);
let display = UnitDisplay::new(&unit, &["B", "KB", "MB"]);
assert_eq!(display.to_string(), "512 B");
}
}

View File

@ -125,6 +125,9 @@ fn parse_keybind(key_str: &str) -> Result<KeyEvent, ParseKeybingError> {
for raw in key_str.split('+') { for raw in key_str.split('+') {
let part = raw.trim(); let part = raw.trim();
if part.is_empty() { if part.is_empty() {
if raw.contains(' ') {
key_code = Some(KeyCode::Char(' '));
}
continue; continue;
} }
let low = part.to_lowercase(); let low = part.to_lowercase();
@ -176,8 +179,8 @@ fn parse_keybind(key_str: &str) -> Result<KeyEvent, ParseKeybingError> {
} }
// singlecharacter fallback // singlecharacter fallback
_ if low.len() == 1 => { _ if part.len() == 1 => {
if let Some(ch) = low.trim().chars().next() { if let Some(ch) = part.chars().next() {
key_code = Some(KeyCode::Char(ch)); key_code = Some(KeyCode::Char(ch));
} }
} }

View File

@ -1,86 +1,122 @@
use crossterm::event::{KeyCode, KeyEvent}; use crossterm::event::{KeyCode, KeyEvent};
use traxor::{app::action::Action, app::App, config::Config, handler::get_action}; use traxor::{app::App, app::action::Action, config::Config, handler::get_action};
#[test] #[tokio::test]
fn test_get_action_quit() { async fn test_get_action_quit() {
let config = Config::load().unwrap(); let config = Config::load().unwrap();
let app = App::new(config).unwrap(); let mut app = App::new(config).unwrap();
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('q')), &app), get_action(KeyEvent::from(KeyCode::Char('q')), &mut app)
.await
.unwrap(),
Some(Action::Quit) Some(Action::Quit)
); );
} }
#[test] #[tokio::test]
fn test_get_action_navigation() { async fn test_get_action_navigation() {
let config = Config::load().unwrap(); let config = Config::load().unwrap();
let app = App::new(config).unwrap(); let mut app = App::new(config).unwrap();
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('l')), &app), get_action(KeyEvent::from(KeyCode::Char('l')), &mut app)
.await
.unwrap(),
Some(Action::NextTab) Some(Action::NextTab)
); );
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('h')), &app), get_action(KeyEvent::from(KeyCode::Char('h')), &mut app)
.await
.unwrap(),
Some(Action::PrevTab) Some(Action::PrevTab)
); );
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('j')), &app), get_action(KeyEvent::from(KeyCode::Char('j')), &mut app)
.await
.unwrap(),
Some(Action::NextTorrent) Some(Action::NextTorrent)
); );
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('k')), &app), get_action(KeyEvent::from(KeyCode::Char('k')), &mut app)
.await
.unwrap(),
Some(Action::PrevTorrent) Some(Action::PrevTorrent)
); );
} }
#[test] #[tokio::test]
fn test_get_action_switch_tab() { async fn test_get_action_switch_tab() {
let config = Config::load().unwrap(); let config = Config::load().unwrap();
let app = App::new(config).unwrap(); let mut app = App::new(config).unwrap();
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('1')), &app), get_action(KeyEvent::from(KeyCode::Char('1')), &mut app)
.await
.unwrap(),
Some(Action::SwitchTab(0)) Some(Action::SwitchTab(0))
); );
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('2')), &app), get_action(KeyEvent::from(KeyCode::Char('2')), &mut app)
.await
.unwrap(),
Some(Action::SwitchTab(1)) Some(Action::SwitchTab(1))
); );
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('3')), &app), get_action(KeyEvent::from(KeyCode::Char('3')), &mut app)
.await
.unwrap(),
Some(Action::SwitchTab(2)) Some(Action::SwitchTab(2))
); );
} }
#[test] #[tokio::test]
fn test_get_action_torrent_actions() { async fn test_get_action_torrent_actions() {
let config = Config::load().unwrap(); let config = Config::load().unwrap();
let app = App::new(config).unwrap(); let mut app = App::new(config).unwrap();
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Enter), &app), get_action(KeyEvent::from(KeyCode::Enter), &mut app)
.await
.unwrap(),
Some(Action::ToggleTorrent) Some(Action::ToggleTorrent)
); );
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('a')), &app), get_action(KeyEvent::from(KeyCode::Char('a')), &mut app)
.await
.unwrap(),
Some(Action::ToggleAll) Some(Action::ToggleAll)
); );
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('d')), &app), get_action(KeyEvent::from(KeyCode::Char('d')), &mut app)
.await
.unwrap(),
Some(Action::Delete(false)) Some(Action::Delete(false))
); );
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char('D')), &app), get_action(KeyEvent::from(KeyCode::Char('D')), &mut app)
.await
.unwrap(),
Some(Action::Delete(true)) Some(Action::Delete(true))
); );
assert_eq!( assert_eq!(
get_action(KeyEvent::from(KeyCode::Char(' ')), &app), get_action(KeyEvent::from(KeyCode::Char(' ')), &mut app)
.await
.unwrap(),
Some(Action::Select) Some(Action::Select)
); );
} }
#[test] #[tokio::test]
fn test_get_action_unhandled() { async fn test_get_action_unhandled() {
let config = Config::load().unwrap(); let config = Config::load().unwrap();
let app = App::new(config).unwrap(); let mut app = App::new(config).unwrap();
assert_eq!(get_action(KeyEvent::from(KeyCode::Char('x')), &app), None); assert_eq!(
assert_eq!(get_action(KeyEvent::from(KeyCode::F(1)), &app), None); get_action(KeyEvent::from(KeyCode::Char('x')), &mut app)
.await
.unwrap(),
None
);
assert_eq!(
get_action(KeyEvent::from(KeyCode::F(1)), &mut app)
.await
.unwrap(),
None
);
} }