refactor: Updated all model tests to use purpose-built assertions to improve readability and maintainability

This commit is contained in:
2025-12-09 14:29:35 -07:00
parent d4bea91186
commit b51e42b4b2
7 changed files with 140 additions and 147 deletions
+2
View File
@@ -58,6 +58,7 @@ pub trait Paginated {
}
#[derive(Default)]
#[cfg_attr(test, derive(Debug))]
pub struct ScrollableText {
pub items: Vec<String>,
pub offset: u16,
@@ -277,6 +278,7 @@ pub struct TabRoute {
pub config: Option<ServarrConfig>,
}
#[cfg_attr(test, derive(Debug))]
pub struct TabState {
pub tabs: Vec<TabRoute>,
pub index: usize,
+10 -11
View File
@@ -46,7 +46,7 @@ mod tests {
fn test_scrollable_text_is_empty() {
let scrollable_text = ScrollableText::default();
assert!(scrollable_text.is_empty());
assert_is_empty!(scrollable_text);
let test_text = "Test \nString";
let scrollable_text = ScrollableText::with_string(test_text.to_owned());
@@ -151,7 +151,7 @@ mod tests {
offset: AtomicUsize::new(test_text.len()),
};
assert!(horizontally_scrollable_text.to_string().is_empty());
assert_is_empty!(horizontally_scrollable_text.to_string());
}
#[test]
@@ -228,7 +228,7 @@ mod tests {
horizontally_scrollable_text.offset.load(Ordering::SeqCst),
2
);
assert!(horizontally_scrollable_text.to_string().is_empty());
assert_is_empty!(horizontally_scrollable_text.to_string());
}
#[test]
@@ -444,7 +444,7 @@ mod tests {
horizontally_scrollable_text.pop();
assert!(horizontally_scrollable_text.text.is_empty());
assert_is_empty!(horizontally_scrollable_text.text);
assert_eq!(
horizontally_scrollable_text.offset.load(Ordering::SeqCst),
0
@@ -452,7 +452,7 @@ mod tests {
horizontally_scrollable_text.pop();
assert!(horizontally_scrollable_text.text.is_empty());
assert_is_empty!(horizontally_scrollable_text.text);
assert_eq!(
horizontally_scrollable_text.offset.load(Ordering::SeqCst),
0
@@ -530,7 +530,7 @@ mod tests {
let active_config = tab_state.get_active_config();
assert!(active_config.is_some());
assert_some!(active_config);
assert_str_eq!(active_config.clone().unwrap().name.unwrap(), "Test");
}
@@ -541,7 +541,7 @@ mod tests {
let active_config = tab_state.get_active_config();
assert!(active_config.is_none());
assert_none!(active_config);
}
#[test]
@@ -630,8 +630,7 @@ mod tests {
let tab_help = tab_state.get_active_route_contextual_help();
assert!(tab_help.is_some());
assert_eq!(tab_help.unwrap(), second_tab_help.unwrap());
assert_some_eq_x!(tab_help, second_tab_help.unwrap());
}
#[test]
@@ -784,7 +783,7 @@ mod tests {
fn test_from_i64() {
let deserializer: I64Deserializer<ValueError> = 1i64.into_deserializer();
assert_eq!(from_i64(deserializer), Ok(1));
assert_ok_eq_x!(from_i64(deserializer), 1);
}
#[test]
@@ -801,7 +800,7 @@ mod tests {
fn test_from_f64() {
let deserializer: F64Deserializer<ValueError> = 1f64.into_deserializer();
assert_eq!(from_f64(deserializer), Ok(1.0));
assert_ok_eq_x!(from_f64(deserializer), 1.0);
}
#[test]
+1
View File
@@ -210,6 +210,7 @@ impl From<&SonarrData<'_>> for EditSeriesModal {
}
}
#[cfg_attr(test, derive(Debug))]
pub struct EpisodeDetailsModal {
pub episode_details: ScrollableText,
pub file_details: String,
+1
View File
@@ -7,6 +7,7 @@ use std::fmt::Debug;
mod stateful_list_tests;
#[derive(Default)]
#[cfg_attr(test, derive(Debug))]
pub struct StatefulList<T> {
pub state: ListState,
pub items: Vec<T>,
+19 -19
View File
@@ -2,25 +2,25 @@
mod tests {
use crate::models::Scrollable;
use crate::models::stateful_list::StatefulList;
use pretty_assertions::{assert_eq, assert_str_eq};
use pretty_assertions::assert_str_eq;
#[test]
fn test_stateful_list_scrolling_on_empty_list_performs_no_op() {
let mut stateful_list: StatefulList<String> = StatefulList::default();
assert_eq!(stateful_list.state.selected(), None);
assert_none!(stateful_list.state.selected());
stateful_list.scroll_up();
assert_eq!(stateful_list.state.selected(), None);
assert_none!(stateful_list.state.selected());
stateful_list.scroll_down();
assert_eq!(stateful_list.state.selected(), None);
assert_none!(stateful_list.state.selected());
stateful_list.scroll_to_top();
assert_eq!(stateful_list.state.selected(), None);
assert_none!(stateful_list.state.selected());
stateful_list.scroll_to_bottom();
}
@@ -29,31 +29,31 @@ mod tests {
fn test_stateful_list_scroll() {
let mut stateful_list = create_test_stateful_list();
assert_eq!(stateful_list.state.selected(), Some(0));
assert_some_eq_x!(stateful_list.state.selected(), 0);
stateful_list.scroll_down();
assert_eq!(stateful_list.state.selected(), Some(1));
assert_some_eq_x!(stateful_list.state.selected(), 1);
stateful_list.scroll_down();
assert_eq!(stateful_list.state.selected(), Some(0));
assert_some_eq_x!(stateful_list.state.selected(), 0);
stateful_list.scroll_up();
assert_eq!(stateful_list.state.selected(), Some(1));
assert_some_eq_x!(stateful_list.state.selected(), 1);
stateful_list.scroll_up();
assert_eq!(stateful_list.state.selected(), Some(0));
assert_some_eq_x!(stateful_list.state.selected(), 0);
stateful_list.scroll_to_bottom();
assert_eq!(stateful_list.state.selected(), Some(1));
assert_some_eq_x!(stateful_list.state.selected(), 1);
stateful_list.scroll_to_top();
assert_eq!(stateful_list.state.selected(), Some(0));
assert_some_eq_x!(stateful_list.state.selected(), 0);
}
#[test]
@@ -63,17 +63,17 @@ mod tests {
stateful_list.set_items(items_vec.clone());
assert_eq!(stateful_list.state.selected(), Some(0));
assert_some_eq_x!(stateful_list.state.selected(), 0);
stateful_list.state.select(Some(1));
stateful_list.set_items(items_vec.clone());
assert_eq!(stateful_list.state.selected(), Some(1));
assert_some_eq_x!(stateful_list.state.selected(), 1);
stateful_list.state.select(Some(3));
stateful_list.set_items(items_vec);
assert_eq!(stateful_list.state.selected(), Some(2));
assert_some_eq_x!(stateful_list.state.selected(), 2);
}
#[test]
@@ -91,15 +91,15 @@ mod tests {
fn test_stateful_list_scroll_up() {
let mut stateful_list = create_test_stateful_list();
assert_eq!(stateful_list.state.selected(), Some(0));
assert_some_eq_x!(stateful_list.state.selected(), 0);
stateful_list.scroll_up();
assert_eq!(stateful_list.state.selected(), Some(1));
assert_some_eq_x!(stateful_list.state.selected(), 1);
stateful_list.scroll_up();
assert_eq!(stateful_list.state.selected(), Some(0));
assert_some_eq_x!(stateful_list.state.selected(), 0);
}
#[test]
@@ -110,7 +110,7 @@ mod tests {
stateful_list = StatefulList::default();
assert!(stateful_list.is_empty());
assert_is_empty!(stateful_list);
}
fn create_test_stateful_list() -> StatefulList<&'static str> {
+1
View File
@@ -31,6 +31,7 @@ where
impl<T> Eq for SortOption<T> where T: Clone + PartialEq + Eq + Debug {}
#[derive(Default)]
#[cfg_attr(test, derive(Debug))]
pub struct StatefulTable<T>
where
T: Clone + PartialEq + Eq + Debug,
+106 -117
View File
@@ -10,19 +10,19 @@ mod tests {
fn test_stateful_table_scrolling_on_empty_table_performs_no_op() {
let mut stateful_table: StatefulTable<String> = StatefulTable::default();
assert_eq!(stateful_table.state.selected(), None);
assert_none!(stateful_table.state.selected());
stateful_table.scroll_up();
assert_eq!(stateful_table.state.selected(), None);
assert_none!(stateful_table.state.selected());
stateful_table.scroll_down();
assert_eq!(stateful_table.state.selected(), None);
assert_none!(stateful_table.state.selected());
stateful_table.scroll_to_top();
assert_eq!(stateful_table.state.selected(), None);
assert_none!(stateful_table.state.selected());
stateful_table.scroll_to_bottom();
}
@@ -35,46 +35,42 @@ mod tests {
..StatefulTable::default()
};
assert_eq!(
assert_none!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
None
.selected()
);
filtered_stateful_table.scroll_up();
assert_eq!(
assert_none!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
None
.selected()
);
filtered_stateful_table.scroll_down();
assert_eq!(
assert_none!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
None
.selected()
);
filtered_stateful_table.scroll_to_top();
assert_eq!(
assert_none!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
None
.selected()
);
filtered_stateful_table.scroll_to_bottom();
@@ -84,110 +80,110 @@ mod tests {
fn test_stateful_table_scroll() {
let mut stateful_table = create_test_stateful_table();
assert_eq!(stateful_table.state.selected(), Some(0));
assert_some_eq_x!(stateful_table.state.selected(), 0);
stateful_table.scroll_down();
assert_eq!(stateful_table.state.selected(), Some(1));
assert_some_eq_x!(stateful_table.state.selected(), 1);
stateful_table.scroll_down();
assert_eq!(stateful_table.state.selected(), Some(0));
assert_some_eq_x!(stateful_table.state.selected(), 0);
stateful_table.scroll_up();
assert_eq!(stateful_table.state.selected(), Some(1));
assert_some_eq_x!(stateful_table.state.selected(), 1);
stateful_table.scroll_up();
assert_eq!(stateful_table.state.selected(), Some(0));
assert_some_eq_x!(stateful_table.state.selected(), 0);
stateful_table.scroll_to_bottom();
assert_eq!(stateful_table.state.selected(), Some(1));
assert_some_eq_x!(stateful_table.state.selected(), 1);
stateful_table.scroll_to_top();
assert_eq!(stateful_table.state.selected(), Some(0));
assert_some_eq_x!(stateful_table.state.selected(), 0);
}
#[test]
fn test_stateful_table_filtered_items_scroll() {
let mut filtered_stateful_table = create_test_filtered_stateful_table();
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(0)
0
);
filtered_stateful_table.scroll_down();
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(1)
1
);
filtered_stateful_table.scroll_down();
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(0)
0
);
filtered_stateful_table.scroll_up();
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(1)
1
);
filtered_stateful_table.scroll_up();
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(0)
0
);
filtered_stateful_table.scroll_to_bottom();
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(1)
1
);
filtered_stateful_table.scroll_to_top();
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(0)
0
);
}
@@ -195,15 +191,15 @@ mod tests {
fn test_stateful_table_pagination_on_empty_table_performs_no_op() {
let mut stateful_table: StatefulTable<String> = StatefulTable::default();
assert_eq!(stateful_table.state.selected(), None);
assert_none!(stateful_table.state.selected());
stateful_table.page_down();
assert_eq!(stateful_table.state.selected(), None);
assert_none!(stateful_table.state.selected());
stateful_table.page_up();
assert_eq!(stateful_table.state.selected(), None);
assert_none!(stateful_table.state.selected());
}
#[test]
@@ -214,35 +210,32 @@ mod tests {
..StatefulTable::default()
};
assert_eq!(
assert_none!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
None
.selected()
);
filtered_stateful_table.page_down();
assert_eq!(
assert_none!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
None
.selected()
);
filtered_stateful_table.page_up();
assert_eq!(
assert_none!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
None
.selected()
);
}
@@ -260,53 +253,53 @@ mod tests {
.collect(),
);
assert_eq!(
assert_some_eq_x!(
stateful_table.filtered_state.as_ref().unwrap().selected(),
Some(0)
0
);
stateful_table.page_down();
assert_eq!(
assert_some_eq_x!(
stateful_table.filtered_state.as_ref().unwrap().selected(),
Some(20)
20
);
stateful_table.page_up();
assert_eq!(
assert_some_eq_x!(
stateful_table.filtered_state.as_ref().unwrap().selected(),
Some(0)
0
);
stateful_table.page_up();
assert_eq!(
assert_some_eq_x!(
stateful_table.filtered_state.as_ref().unwrap().selected(),
Some(stateful_table.filtered_items.as_ref().unwrap().len() - 21)
stateful_table.filtered_items.as_ref().unwrap().len() - 21
);
stateful_table.page_down();
assert_eq!(
assert_some_eq_x!(
stateful_table.filtered_state.as_ref().unwrap().selected(),
Some(0)
0
);
stateful_table.scroll_down();
stateful_table.page_up();
assert_eq!(
assert_some_eq_x!(
stateful_table.filtered_state.as_ref().unwrap().selected(),
Some(stateful_table.filtered_items.as_ref().unwrap().len() - 20)
stateful_table.filtered_items.as_ref().unwrap().len() - 20
);
stateful_table.scroll_down();
stateful_table.page_down();
assert_eq!(
assert_some_eq_x!(
stateful_table.filtered_state.as_ref().unwrap().selected(),
Some(2)
2
);
}
@@ -324,39 +317,39 @@ mod tests {
.collect(),
);
assert_eq!(stateful_table.state.selected(), Some(0));
assert_some_eq_x!(stateful_table.state.selected(), 0);
stateful_table.page_down();
assert_eq!(stateful_table.state.selected(), Some(20));
assert_some_eq_x!(stateful_table.state.selected(), 20);
stateful_table.page_up();
assert_eq!(stateful_table.state.selected(), Some(0));
assert_some_eq_x!(stateful_table.state.selected(), 0);
stateful_table.page_up();
assert_eq!(
assert_some_eq_x!(
stateful_table.state.selected(),
Some(stateful_table.items.len() - 21)
stateful_table.items.len() - 21
);
stateful_table.page_down();
assert_eq!(stateful_table.state.selected(), Some(0));
assert_some_eq_x!(stateful_table.state.selected(), 0);
stateful_table.scroll_down();
stateful_table.page_up();
assert_eq!(
assert_some_eq_x!(
stateful_table.state.selected(),
Some(stateful_table.items.len() - 20)
stateful_table.items.len() - 20
);
stateful_table.scroll_down();
stateful_table.page_down();
assert_eq!(stateful_table.state.selected(), Some(2));
assert_some_eq_x!(stateful_table.state.selected(), 2);
}
#[test]
@@ -366,17 +359,17 @@ mod tests {
stateful_table.set_items(items_vec.clone());
assert_eq!(stateful_table.state.selected(), Some(0));
assert_some_eq_x!(stateful_table.state.selected(), 0);
stateful_table.state.select(Some(1));
stateful_table.set_items(items_vec.clone());
assert_eq!(stateful_table.state.selected(), Some(1));
assert_some_eq_x!(stateful_table.state.selected(), 1);
stateful_table.state.select(Some(3));
stateful_table.set_items(items_vec);
assert_eq!(stateful_table.state.selected(), Some(2));
assert_some_eq_x!(stateful_table.state.selected(), 2);
}
#[test]
@@ -386,18 +379,15 @@ mod tests {
filtered_stateful_table.set_filtered_items(filtered_items_vec.clone());
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(0)
);
assert_eq!(
filtered_stateful_table.filtered_items,
Some(filtered_items_vec.clone())
0
);
assert_some_eq_x!(&filtered_stateful_table.filtered_items, &filtered_items_vec);
}
#[test]
@@ -602,50 +592,49 @@ mod tests {
fn test_stateful_table_select_index() {
let mut stateful_table = create_test_stateful_table();
assert_eq!(stateful_table.state.selected(), Some(0));
assert_some_eq_x!(stateful_table.state.selected(), 0);
stateful_table.select_index(Some(1));
assert_eq!(stateful_table.state.selected(), Some(1));
assert_some_eq_x!(stateful_table.state.selected(), 1);
stateful_table.select_index(None);
assert_eq!(stateful_table.state.selected(), None);
assert_none!(stateful_table.state.selected());
}
#[test]
fn test_filtered_stateful_table_select_index() {
let mut filtered_stateful_table = create_test_filtered_stateful_table();
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(0)
0
);
filtered_stateful_table.select_index(Some(1));
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(1)
1
);
filtered_stateful_table.select_index(None);
assert_eq!(
assert_none!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
None
.selected()
);
}
@@ -653,50 +642,50 @@ mod tests {
fn test_stateful_table_scroll_up() {
let mut stateful_table = create_test_stateful_table();
assert_eq!(stateful_table.state.selected(), Some(0));
assert_some_eq_x!(stateful_table.state.selected(), 0);
stateful_table.scroll_up();
assert_eq!(stateful_table.state.selected(), Some(1));
assert_some_eq_x!(stateful_table.state.selected(), 1);
stateful_table.scroll_up();
assert_eq!(stateful_table.state.selected(), Some(0));
assert_some_eq_x!(stateful_table.state.selected(), 0);
}
#[test]
fn test_filtered_stateful_table_scroll_up() {
let mut filtered_stateful_table = create_test_filtered_stateful_table();
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(0)
0
);
filtered_stateful_table.scroll_up();
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(1)
1
);
filtered_stateful_table.scroll_up();
assert_eq!(
assert_some_eq_x!(
filtered_stateful_table
.filtered_state
.as_ref()
.unwrap()
.selected(),
Some(0)
0
);
}
@@ -711,9 +700,9 @@ mod tests {
let has_matches = stateful_table.apply_filter(|&item| item);
assert_eq!(stateful_table.filter, None);
assert_eq!(stateful_table.filtered_items, Some(expected_items));
assert_eq!(stateful_table.filtered_state, Some(expected_state));
assert_none!(stateful_table.filter);
assert_some_eq_x!(&stateful_table.filtered_items, &expected_items);
assert_some_eq_x!(&stateful_table.filtered_state, &expected_state);
assert!(has_matches);
}
@@ -725,9 +714,9 @@ mod tests {
let has_matches = stateful_table.apply_filter(|&item| item);
assert_eq!(stateful_table.filter, None);
assert_eq!(stateful_table.filtered_items, None);
assert_eq!(stateful_table.filtered_state, None);
assert_none!(stateful_table.filter);
assert_none!(stateful_table.filtered_items);
assert_none!(stateful_table.filtered_state);
assert!(!has_matches);
}
@@ -736,9 +725,9 @@ mod tests {
let mut stateful_table = create_test_filtered_stateful_table();
stateful_table.reset_filter();
assert_eq!(stateful_table.filter, None);
assert_eq!(stateful_table.filtered_items, None);
assert_eq!(stateful_table.filtered_state, None);
assert_none!(stateful_table.filter);
assert_none!(stateful_table.filtered_items);
assert_none!(stateful_table.filtered_state);
}
#[test]
@@ -751,7 +740,7 @@ mod tests {
let has_match = stateful_table.apply_search(|&item| item);
assert_eq!(stateful_table.search, None);
assert_none!(stateful_table.search);
assert_eq!(stateful_table.state, expected_state);
assert!(has_match);
}
@@ -764,7 +753,7 @@ mod tests {
let has_match = stateful_table.apply_search(|&item| item);
assert_eq!(stateful_table.search, None);
assert_none!(stateful_table.search);
assert!(!has_match);
}
@@ -778,8 +767,8 @@ mod tests {
let has_match = stateful_table.apply_search(|&item| item);
assert_eq!(stateful_table.search, None);
assert_eq!(stateful_table.filtered_state, Some(expected_state));
assert_none!(stateful_table.search);
assert_some_eq_x!(&stateful_table.filtered_state, &expected_state);
assert!(has_match);
}
@@ -793,8 +782,8 @@ mod tests {
let has_match = stateful_table.apply_search(|&item| item);
assert_eq!(stateful_table.search, None);
assert_eq!(stateful_table.filtered_state, Some(expected_state));
assert_none!(stateful_table.search);
assert_some_eq_x!(&stateful_table.filtered_state, &expected_state);
assert!(!has_match);
}
@@ -804,7 +793,7 @@ mod tests {
stateful_table.search = Some("test".into());
stateful_table.reset_search();
assert_eq!(stateful_table.search, None);
assert_none!(stateful_table.search);
}
#[test]
@@ -815,7 +804,7 @@ mod tests {
stateful_table = StatefulTable::default();
assert!(stateful_table.is_empty());
assert_is_empty!(stateful_table);
}
fn create_test_stateful_table() -> StatefulTable<&'static str> {