refactor: refactor some tests (#718)

Small PR to quickly refactor some recent tests to avoid too much duplication.
This commit is contained in:
Clement Tsang 2022-04-29 16:02:34 -04:00 committed by GitHub
parent cddee9d923
commit a516104dec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 127 deletions

View File

@ -977,55 +977,53 @@ mod test {
#[test] #[test]
fn test_scroll_update_position() { fn test_scroll_update_position() {
fn check_scroll_update(
scroll: &mut AppScrollWidgetState, change: i64, max: usize, ret: Option<usize>,
new_position: usize,
) {
assert_eq!(scroll.update_position(change, max), ret);
assert_eq!(scroll.current_scroll_position, new_position);
}
let mut scroll = AppScrollWidgetState { let mut scroll = AppScrollWidgetState {
current_scroll_position: 5, current_scroll_position: 5,
scroll_bar: 0, scroll_bar: 0,
scroll_direction: ScrollDirection::Down, scroll_direction: ScrollDirection::Down,
table_state: Default::default(), table_state: Default::default(),
}; };
let s = &mut scroll;
// Update by 0. Should not change. // Update by 0. Should not change.
assert_eq!(scroll.update_position(0, 15), None); check_scroll_update(s, 0, 15, None, 5);
assert_eq!(scroll.current_scroll_position, 5);
// Update by 5. Should increment to index 10. // Update by 5. Should increment to index 10.
assert_eq!(scroll.update_position(5, 15), Some(10)); check_scroll_update(s, 5, 15, Some(10), 10);
assert_eq!(scroll.current_scroll_position, 10);
// Update by 5. Should not change. // Update by 5. Should not change.
assert_eq!(scroll.update_position(5, 15), None); check_scroll_update(s, 5, 15, None, 10);
assert_eq!(scroll.current_scroll_position, 10);
// Update by 4. Should increment to index 14 (supposed max). // Update by 4. Should increment to index 14 (supposed max).
assert_eq!(scroll.update_position(4, 15), Some(14)); check_scroll_update(s, 4, 15, Some(14), 14);
assert_eq!(scroll.current_scroll_position, 14);
// Update by 1. Should do nothing. // Update by 1. Should do nothing.
assert_eq!(scroll.update_position(1, 15), None); check_scroll_update(s, 1, 15, None, 14);
assert_eq!(scroll.current_scroll_position, 14);
// Update by -15. Should do nothing. // Update by -15. Should do nothing.
assert_eq!(scroll.update_position(-15, 15), None); check_scroll_update(s, -15, 15, None, 14);
assert_eq!(scroll.current_scroll_position, 14);
// Update by -14. Should land on position 0. // Update by -14. Should land on position 0.
assert_eq!(scroll.update_position(-14, 15), Some(0)); check_scroll_update(s, -14, 15, Some(0), 0);
assert_eq!(scroll.current_scroll_position, 0);
// Update by -1. Should do nothing. // Update by -1. Should do nothing.
assert_eq!(scroll.update_position(-1, 15), None); check_scroll_update(s, -15, 15, None, 0);
assert_eq!(scroll.current_scroll_position, 0);
// Update by 0. Should do nothing. // Update by 0. Should do nothing.
assert_eq!(scroll.update_position(0, 15), None); check_scroll_update(s, 0, 15, None, 0);
assert_eq!(scroll.current_scroll_position, 0);
// Update by 15. Should do nothing. // Update by 15. Should do nothing.
assert_eq!(scroll.update_position(15, 15), None); check_scroll_update(s, 15, 15, None, 0);
assert_eq!(scroll.current_scroll_position, 0);
// Update by 15 but with a larger bound. Should increment to 15. // Update by 15 but with a larger bound. Should increment to 15.
assert_eq!(scroll.update_position(15, 16), Some(15)); check_scroll_update(s, 15, 16, Some(15), 15);
assert_eq!(scroll.current_scroll_position, 15);
} }
} }

View File

@ -226,137 +226,58 @@ mod test {
#[test] #[test]
fn test_get_start_position() { fn test_get_start_position() {
use crate::app::ScrollDirection; use crate::app::ScrollDirection::{self, Down, Up};
fn test(
bar: usize, num: usize, direction: ScrollDirection, selected: usize, force: bool,
expected_posn: usize, expected_bar: usize,
) {
let mut bar = bar;
assert_eq!(
get_start_position(num, &direction, &mut bar, selected, force),
expected_posn
);
assert_eq!(bar, expected_bar);
}
// Scrolling down from start // Scrolling down from start
{ test(0, 10, Down, 0, false, 0, 0);
let mut bar = 0;
assert_eq!(
get_start_position(10, &ScrollDirection::Down, &mut bar, 0, false),
0
);
assert_eq!(bar, 0);
}
// Simple scrolling down // Simple scrolling down
{ test(0, 10, Down, 1, false, 0, 0);
let mut bar = 0;
assert_eq!(
get_start_position(10, &ScrollDirection::Down, &mut bar, 1, false),
0
);
assert_eq!(bar, 0);
}
// Scrolling down from the middle high up // Scrolling down from the middle high up
{ test(0, 10, Down, 5, false, 0, 0);
let mut bar = 0;
assert_eq!(
get_start_position(10, &ScrollDirection::Down, &mut bar, 5, false),
0
);
assert_eq!(bar, 0);
}
// Scrolling down into boundary // Scrolling down into boundary
{ test(0, 10, Down, 11, false, 1, 1);
let mut bar = 0;
assert_eq!(
get_start_position(10, &ScrollDirection::Down, &mut bar, 11, false),
1
);
assert_eq!(bar, 1);
}
// Scrolling down from the with non-zero bar // Scrolling down from the with non-zero bar
{ test(5, 10, Down, 15, false, 5, 5);
let mut bar = 5;
assert_eq!(
get_start_position(10, &ScrollDirection::Down, &mut bar, 15, false),
5
);
assert_eq!(bar, 5);
}
// Force redraw scrolling down (e.g. resize) // Force redraw scrolling down (e.g. resize)
{ test(5, 15, Down, 15, true, 0, 0);
let mut bar = 5;
assert_eq!(
get_start_position(15, &ScrollDirection::Down, &mut bar, 15, true),
0
);
assert_eq!(bar, 0);
}
// Test jumping down // Test jumping down
{ test(1, 10, Down, 20, true, 10, 10);
let mut bar = 1;
assert_eq!(
get_start_position(10, &ScrollDirection::Down, &mut bar, 20, true),
10
);
assert_eq!(bar, 10);
}
// Scrolling up from bottom // Scrolling up from bottom
{ test(10, 10, Up, 20, false, 10, 10);
let mut bar = 10;
assert_eq!(
get_start_position(10, &ScrollDirection::Up, &mut bar, 20, false),
10
);
assert_eq!(bar, 10);
}
// Simple scrolling up // Simple scrolling up
{ test(10, 10, Up, 19, false, 10, 10);
let mut bar = 10;
assert_eq!(
get_start_position(10, &ScrollDirection::Up, &mut bar, 19, false),
10
);
assert_eq!(bar, 10);
}
// Scrolling up from the middle // Scrolling up from the middle
{ test(10, 10, Up, 10, false, 10, 10);
let mut bar = 10;
assert_eq!(
get_start_position(10, &ScrollDirection::Up, &mut bar, 10, false),
10
);
assert_eq!(bar, 10);
}
// Scrolling up into boundary // Scrolling up into boundary
{ test(10, 10, Up, 9, false, 9, 9);
let mut bar = 10;
assert_eq!(
get_start_position(10, &ScrollDirection::Up, &mut bar, 9, false),
9
);
assert_eq!(bar, 9);
}
// Force redraw scrolling up (e.g. resize) // Force redraw scrolling up (e.g. resize)
{ test(5, 10, Up, 15, true, 5, 5);
let mut bar = 5;
assert_eq!(
get_start_position(10, &ScrollDirection::Up, &mut bar, 15, true),
5
);
assert_eq!(bar, 5);
}
// Test jumping up // Test jumping up
{ test(10, 10, Up, 0, false, 0, 0);
let mut bar = 10;
assert_eq!(
get_start_position(10, &ScrollDirection::Up, &mut bar, 0, false),
0
);
assert_eq!(bar, 0);
}
} }
#[test] #[test]