mirror of
https://github.com/kristoferssolo/traxor.git
synced 2025-10-21 20:10:35 +00:00
test: fix errors
This commit is contained in:
parent
df0101d1c5
commit
eff0fe20e4
@ -20,3 +20,27 @@ impl Display for FileSize {
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -20,3 +20,27 @@ impl Display for NetSpeed {
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -58,7 +58,7 @@ impl Unit {
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn value(&self) -> u64 {
|
||||
pub const fn as_raw(&self) -> u64 {
|
||||
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");
|
||||
}
|
||||
}
|
||||
@ -125,6 +125,9 @@ fn parse_keybind(key_str: &str) -> Result<KeyEvent, ParseKeybingError> {
|
||||
for raw in key_str.split('+') {
|
||||
let part = raw.trim();
|
||||
if part.is_empty() {
|
||||
if raw.contains(' ') {
|
||||
key_code = Some(KeyCode::Char(' '));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let low = part.to_lowercase();
|
||||
@ -176,8 +179,8 @@ fn parse_keybind(key_str: &str) -> Result<KeyEvent, ParseKeybingError> {
|
||||
}
|
||||
|
||||
// single‐character fallback
|
||||
_ if low.len() == 1 => {
|
||||
if let Some(ch) = low.trim().chars().next() {
|
||||
_ if part.len() == 1 => {
|
||||
if let Some(ch) = part.chars().next() {
|
||||
key_code = Some(KeyCode::Char(ch));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,86 +1,122 @@
|
||||
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]
|
||||
fn test_get_action_quit() {
|
||||
#[tokio::test]
|
||||
async fn test_get_action_quit() {
|
||||
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('q')), &app),
|
||||
get_action(KeyEvent::from(KeyCode::Char('q')), &mut app)
|
||||
.await
|
||||
.unwrap(),
|
||||
Some(Action::Quit)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_action_navigation() {
|
||||
#[tokio::test]
|
||||
async fn test_get_action_navigation() {
|
||||
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('l')), &app),
|
||||
get_action(KeyEvent::from(KeyCode::Char('l')), &mut app)
|
||||
.await
|
||||
.unwrap(),
|
||||
Some(Action::NextTab)
|
||||
);
|
||||
assert_eq!(
|
||||
get_action(KeyEvent::from(KeyCode::Char('h')), &app),
|
||||
get_action(KeyEvent::from(KeyCode::Char('h')), &mut app)
|
||||
.await
|
||||
.unwrap(),
|
||||
Some(Action::PrevTab)
|
||||
);
|
||||
assert_eq!(
|
||||
get_action(KeyEvent::from(KeyCode::Char('j')), &app),
|
||||
get_action(KeyEvent::from(KeyCode::Char('j')), &mut app)
|
||||
.await
|
||||
.unwrap(),
|
||||
Some(Action::NextTorrent)
|
||||
);
|
||||
assert_eq!(
|
||||
get_action(KeyEvent::from(KeyCode::Char('k')), &app),
|
||||
get_action(KeyEvent::from(KeyCode::Char('k')), &mut app)
|
||||
.await
|
||||
.unwrap(),
|
||||
Some(Action::PrevTorrent)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_action_switch_tab() {
|
||||
#[tokio::test]
|
||||
async fn test_get_action_switch_tab() {
|
||||
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('1')), &app),
|
||||
get_action(KeyEvent::from(KeyCode::Char('1')), &mut app)
|
||||
.await
|
||||
.unwrap(),
|
||||
Some(Action::SwitchTab(0))
|
||||
);
|
||||
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))
|
||||
);
|
||||
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))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_action_torrent_actions() {
|
||||
#[tokio::test]
|
||||
async fn test_get_action_torrent_actions() {
|
||||
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::Enter), &app),
|
||||
get_action(KeyEvent::from(KeyCode::Enter), &mut app)
|
||||
.await
|
||||
.unwrap(),
|
||||
Some(Action::ToggleTorrent)
|
||||
);
|
||||
assert_eq!(
|
||||
get_action(KeyEvent::from(KeyCode::Char('a')), &app),
|
||||
get_action(KeyEvent::from(KeyCode::Char('a')), &mut app)
|
||||
.await
|
||||
.unwrap(),
|
||||
Some(Action::ToggleAll)
|
||||
);
|
||||
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))
|
||||
);
|
||||
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))
|
||||
);
|
||||
assert_eq!(
|
||||
get_action(KeyEvent::from(KeyCode::Char(' ')), &app),
|
||||
get_action(KeyEvent::from(KeyCode::Char(' ')), &mut app)
|
||||
.await
|
||||
.unwrap(),
|
||||
Some(Action::Select)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_action_unhandled() {
|
||||
#[tokio::test]
|
||||
async fn test_get_action_unhandled() {
|
||||
let config = Config::load().unwrap();
|
||||
let app = App::new(config).unwrap();
|
||||
assert_eq!(get_action(KeyEvent::from(KeyCode::Char('x')), &app), None);
|
||||
assert_eq!(get_action(KeyEvent::from(KeyCode::F(1)), &app), None);
|
||||
let mut app = App::new(config).unwrap();
|
||||
assert_eq!(
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user