From 61e3ab5f99d08a82da9ed99603faf5af0ced6d01 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Fri, 19 Jan 2024 10:59:10 -0500 Subject: [PATCH] =?UTF-8?q?test(Scrollbar):=20backport=20scrollbar=20tests?= =?UTF-8?q?=20and=20make=20the=20tests=20pass=20=F0=9F=9A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/widgets/scrollbar.rs | 402 +++++++++++++++++++-------------------- 1 file changed, 201 insertions(+), 201 deletions(-) diff --git a/src/widgets/scrollbar.rs b/src/widgets/scrollbar.rs index af9abf93..c8a78725 100644 --- a/src/widgets/scrollbar.rs +++ b/src/widgets/scrollbar.rs @@ -1133,8 +1133,8 @@ mod tests { } #[rstest] - #[case("█═", 0, 2, "position_0")] - #[case("═█", 1, 2, "position_1")] + #[case("██", 0, 2, "position_0")] + #[case("██", 1, 2, "position_1")] fn render_scrollbar_simplest( #[case] expected: &str, #[case] position: usize, @@ -1160,16 +1160,16 @@ mod tests { } #[rstest] - #[case("#####-----", 0, 10, "position_0")] - #[case("-#####----", 1, 10, "position_1")] - #[case("-#####----", 2, 10, "position_2")] - #[case("--#####---", 3, 10, "position_3")] - #[case("--#####---", 4, 10, "position_4")] - #[case("---#####--", 5, 10, "position_5")] - #[case("---#####--", 6, 10, "position_6")] - #[case("----#####-", 7, 10, "position_7")] - #[case("----#####-", 8, 10, "position_8")] - #[case("-----#####", 9, 10, "position_9")] + #[case("##########", 0, 10, "position_0")] + #[case("##########", 1, 10, "position_1")] + #[case("##########", 2, 10, "position_2")] + #[case("##########", 3, 10, "position_3")] + #[case("##########", 4, 10, "position_4")] + #[case("##########", 5, 10, "position_5")] + #[case("##########", 6, 10, "position_6")] + #[case("##########", 7, 10, "position_7")] + #[case("##########", 8, 10, "position_8")] + #[case("##########", 9, 10, "position_9")] fn render_scrollbar_simple( #[case] expected: &str, #[case] position: usize, @@ -1253,8 +1253,8 @@ mod tests { } #[rstest] - #[case("#########-", 0, 2, "position_0")] - #[case("-#########", 1, 2, "position_1")] + #[case("##########", 0, 2, "position_0")] + #[case("##########", 1, 2, "position_1")] fn render_scrollbar_almost_fullbar( #[case] expected: &str, #[case] position: usize, @@ -1281,191 +1281,191 @@ mod tests { ); } - // #[rstest] - // #[case("█████═════", 0, 10, "position_0")] - // #[case("═█████════", 1, 10, "position_1")] - // #[case("═█████════", 2, 10, "position_2")] - // #[case("══█████═══", 3, 10, "position_3")] - // #[case("══█████═══", 4, 10, "position_4")] - // #[case("═══█████══", 5, 10, "position_5")] - // #[case("═══█████══", 6, 10, "position_6")] - // #[case("════█████═", 7, 10, "position_7")] - // #[case("════█████═", 8, 10, "position_8")] - // #[case("═════█████", 9, 10, "position_9")] - // #[case("═════█████", 100, 10, "position_out_of_bounds")] - // fn render_scrollbar_without_symbols( - // #[case] expected: &str, - // #[case] position: usize, - // #[case] content_length: usize, - // #[case] assertion_message: &str, - // ) { - // let size = expected.width() as u16; - // let mut buffer = Buffer::empty(Rect::new(0, 0, size, 1)); - // let mut state = ScrollbarState::default() - // .position(position) - // .content_length(content_length); - // Scrollbar::default() - // .orientation(ScrollbarOrientation::HorizontalBottom) - // .begin_symbol(None) - // .end_symbol(None) - // .render(buffer.area, &mut buffer, &mut state); - // assert_eq!( - // buffer, - // Buffer::with_lines(vec![expected]), - // "{}", - // assertion_message - // ); - // } - // - // #[rstest] - // #[case("<####---->", 0, 10, "position_0")] - // #[case("<#####--->", 1, 10, "position_1")] - // #[case("<-####--->", 2, 10, "position_2")] - // #[case("<-####--->", 3, 10, "position_3")] - // #[case("<--####-->", 4, 10, "position_4")] - // #[case("<--####-->", 5, 10, "position_5")] - // #[case("<---####->", 6, 10, "position_6")] - // #[case("<---####->", 7, 10, "position_7")] - // #[case("<---#####>", 8, 10, "position_8")] - // #[case("<----####>", 9, 10, "position_9")] - // #[case("<----####>", 10, 10, "position_one_out_of_bounds")] - // #[case("<----####>", 15, 10, "position_few_out_of_bounds")] - // #[case("<----####>", 500, 10, "position_very_many_out_of_bounds")] - // fn render_scrollbar_with_symbols( - // #[case] expected: &str, - // #[case] position: usize, - // #[case] content_length: usize, - // #[case] assertion_message: &str, - // ) { - // let size = expected.width() as u16; - // let mut buffer = Buffer::empty(Rect::new(0, 0, size, 1)); - // let mut state = ScrollbarState::default() - // .position(position) - // .content_length(content_length); - // Scrollbar::default() - // .orientation(ScrollbarOrientation::HorizontalTop) - // .begin_symbol(Some("<")) - // .end_symbol(Some(">")) - // .track_symbol(Some("-")) - // .thumb_symbol("#") - // .render(buffer.area, &mut buffer, &mut state); - // assert_eq!( - // buffer, - // Buffer::with_lines(vec![expected]), - // "{}", - // assertion_message, - // ); - // } - // - // #[rstest] - // #[case("█████═════", 0, 10, "position_0")] - // #[case("═█████════", 1, 10, "position_1")] - // #[case("═█████════", 2, 10, "position_2")] - // #[case("══█████═══", 3, 10, "position_3")] - // #[case("══█████═══", 4, 10, "position_4")] - // #[case("═══█████══", 5, 10, "position_5")] - // #[case("═══█████══", 6, 10, "position_6")] - // #[case("════█████═", 7, 10, "position_7")] - // #[case("════█████═", 8, 10, "position_8")] - // #[case("═════█████", 9, 10, "position_9")] - // #[case("═════█████", 100, 10, "position_out_of_bounds")] - // fn render_scrollbar_twoline_horizontal( - // #[case] expected: &str, - // #[case] position: usize, - // #[case] content_length: usize, - // #[case] assertion_message: &str, - // ) { - // let size = expected.width() as u16; - // let mut buffer = Buffer::empty(Rect::new(0, 0, size, 2)); - // let mut state = ScrollbarState::default() - // .position(position) - // .content_length(content_length); - // Scrollbar::default() - // .orientation(ScrollbarOrientation::HorizontalBottom) - // .begin_symbol(None) - // .end_symbol(None) - // .render(buffer.area, &mut buffer, &mut state); - // let empty_string: String = " ".repeat(size as usize); - // assert_eq!( - // buffer, - // Buffer::with_lines(vec![&empty_string, expected]), - // "{}", - // assertion_message - // ); - // - // let mut buffer = Buffer::empty(Rect::new(0, 0, size, 2)); - // let mut state = ScrollbarState::default() - // .position(position) - // .content_length(content_length); - // Scrollbar::default() - // .orientation(ScrollbarOrientation::HorizontalTop) - // .begin_symbol(None) - // .end_symbol(None) - // .render(buffer.area, &mut buffer, &mut state); - // let empty_string: String = " ".repeat(size as usize); - // assert_eq!( - // buffer, - // Buffer::with_lines(vec![expected, &empty_string]), - // "{}", - // assertion_message - // ); - // } - // - // #[rstest] - // #[case("<####---->", 0, 10, "position_0")] - // #[case("<#####--->", 1, 10, "position_1")] - // #[case("<-####--->", 2, 10, "position_2")] - // #[case("<-####--->", 3, 10, "position_3")] - // #[case("<--####-->", 4, 10, "position_4")] - // #[case("<--####-->", 5, 10, "position_5")] - // #[case("<---####->", 6, 10, "position_6")] - // #[case("<---####->", 7, 10, "position_7")] - // #[case("<---#####>", 8, 10, "position_8")] - // #[case("<----####>", 9, 10, "position_9")] - // #[case("<----####>", 10, 10, "position_one_out_of_bounds")] - // fn render_scrollbar_twoline_vertical( - // #[case] expected: &str, - // #[case] position: usize, - // #[case] content_length: usize, - // #[case] assertion_message: &str, - // ) { - // let size = expected.width() as u16; - // let mut buffer = Buffer::empty(Rect::new(0, 0, 2, size)); - // let mut state = ScrollbarState::default() - // .position(position) - // .content_length(content_length); - // Scrollbar::default() - // .orientation(ScrollbarOrientation::VerticalRight) - // .begin_symbol(Some("<")) - // .end_symbol(Some(">")) - // .track_symbol(Some("-")) - // .thumb_symbol("#") - // .render(buffer.area, &mut buffer, &mut state); - // let empty_string: String = " ".repeat(size as usize); - // let bar = empty_string - // .chars() - // .zip(expected.chars()) - // .map(|(a, b)| format!("{a}{b}")) - // .collect_vec(); - // assert_eq!(buffer, Buffer::with_lines(bar), "{}", assertion_message); - // - // let mut buffer = Buffer::empty(Rect::new(0, 0, 2, size)); - // let mut state = ScrollbarState::default() - // .position(position) - // .content_length(content_length); - // Scrollbar::default() - // .orientation(ScrollbarOrientation::VerticalLeft) - // .begin_symbol(Some("<")) - // .end_symbol(Some(">")) - // .track_symbol(Some("-")) - // .thumb_symbol("#") - // .render(buffer.area, &mut buffer, &mut state); - // let empty_string: String = " ".repeat(size as usize); - // let bar = expected - // .chars() - // .zip(empty_string.chars()) - // .map(|(a, b)| format!("{a}{b}")) - // .collect_vec(); - // assert_eq!(buffer, Buffer::with_lines(bar), "{}", assertion_message); - // } + #[rstest] + #[case("██████████", 0, 10, "position_0")] + #[case("██████████", 1, 10, "position_1")] + #[case("██████████", 2, 10, "position_2")] + #[case("██████████", 3, 10, "position_3")] + #[case("██████████", 4, 10, "position_4")] + #[case("██████████", 5, 10, "position_5")] + #[case("██████████", 6, 10, "position_6")] + #[case("██████████", 7, 10, "position_7")] + #[case("██████████", 8, 10, "position_8")] + #[case("██████████", 9, 10, "position_9")] + #[case("██████████", 100, 10, "position_out_of_bounds")] + fn render_scrollbar_without_symbols( + #[case] expected: &str, + #[case] position: usize, + #[case] content_length: usize, + #[case] assertion_message: &str, + ) { + let size = expected.width() as u16; + let mut buffer = Buffer::empty(Rect::new(0, 0, size, 1)); + let mut state = ScrollbarState::default() + .position(position) + .content_length(content_length); + Scrollbar::default() + .orientation(ScrollbarOrientation::HorizontalBottom) + .begin_symbol(None) + .end_symbol(None) + .render(buffer.area, &mut buffer, &mut state); + assert_eq!( + buffer, + Buffer::with_lines(vec![expected]), + "{}", + assertion_message + ); + } + + #[rstest] + #[case("<######-->", 0, 10, "position_0")] + #[case("<######-->", 1, 10, "position_1")] + #[case("<######-->", 2, 10, "position_2")] + #[case("<-######->", 3, 10, "position_3")] + #[case("<-######->", 4, 10, "position_4")] + #[case("<-######->", 5, 10, "position_5")] + #[case("<-######->", 6, 10, "position_6")] + #[case("<-######->", 7, 10, "position_7")] + #[case("<--######>", 8, 10, "position_8")] + #[case("<--######>", 9, 10, "position_9")] + #[case("<--######>", 10, 10, "position_one_out_of_bounds")] + #[case("<--######>", 15, 10, "position_few_out_of_bounds")] + #[case("<--######>", 500, 10, "position_very_many_out_of_bounds")] + fn render_scrollbar_with_symbols( + #[case] expected: &str, + #[case] position: usize, + #[case] content_length: usize, + #[case] assertion_message: &str, + ) { + let size = expected.width() as u16; + let mut buffer = Buffer::empty(Rect::new(0, 0, size, 1)); + let mut state = ScrollbarState::default() + .position(position) + .content_length(content_length); + Scrollbar::default() + .orientation(ScrollbarOrientation::HorizontalTop) + .begin_symbol(Some("<")) + .end_symbol(Some(">")) + .track_symbol(Some("-")) + .thumb_symbol("#") + .render(buffer.area, &mut buffer, &mut state); + assert_eq!( + buffer, + Buffer::with_lines(vec![expected]), + "{}", + assertion_message, + ); + } + + #[rstest] + #[case("██████████", 0, 10, "position_0")] + #[case("██████████", 1, 10, "position_1")] + #[case("██████████", 2, 10, "position_2")] + #[case("██████████", 3, 10, "position_3")] + #[case("██████████", 4, 10, "position_4")] + #[case("██████████", 5, 10, "position_5")] + #[case("██████████", 6, 10, "position_6")] + #[case("██████████", 7, 10, "position_7")] + #[case("██████████", 8, 10, "position_8")] + #[case("██████████", 9, 10, "position_9")] + #[case("██████████", 100, 10, "position_out_of_bounds")] + fn render_scrollbar_twoline_horizontal( + #[case] expected: &str, + #[case] position: usize, + #[case] content_length: usize, + #[case] assertion_message: &str, + ) { + let size = expected.width() as u16; + let mut buffer = Buffer::empty(Rect::new(0, 0, size, 2)); + let mut state = ScrollbarState::default() + .position(position) + .content_length(content_length); + Scrollbar::default() + .orientation(ScrollbarOrientation::HorizontalBottom) + .begin_symbol(None) + .end_symbol(None) + .render(buffer.area, &mut buffer, &mut state); + let empty_string: String = " ".repeat(size as usize); + assert_eq!( + buffer, + Buffer::with_lines(vec![&empty_string, expected]), + "{}", + assertion_message + ); + + let mut buffer = Buffer::empty(Rect::new(0, 0, size, 2)); + let mut state = ScrollbarState::default() + .position(position) + .content_length(content_length); + Scrollbar::default() + .orientation(ScrollbarOrientation::HorizontalTop) + .begin_symbol(None) + .end_symbol(None) + .render(buffer.area, &mut buffer, &mut state); + let empty_string: String = " ".repeat(size as usize); + assert_eq!( + buffer, + Buffer::with_lines(vec![expected, &empty_string]), + "{}", + assertion_message + ); + } + + #[rstest] + #[case("<######-->", 0, 10, "position_0")] + #[case("<######-->", 1, 10, "position_1")] + #[case("<######-->", 2, 10, "position_2")] + #[case("<-######->", 3, 10, "position_3")] + #[case("<-######->", 4, 10, "position_4")] + #[case("<-######->", 5, 10, "position_5")] + #[case("<-######->", 6, 10, "position_6")] + #[case("<-######->", 7, 10, "position_7")] + #[case("<--######>", 8, 10, "position_8")] + #[case("<--######>", 9, 10, "position_9")] + #[case("<--######>", 10, 10, "position_one_out_of_bounds")] + fn render_scrollbar_twoline_vertical( + #[case] expected: &str, + #[case] position: usize, + #[case] content_length: usize, + #[case] assertion_message: &str, + ) { + let size = expected.width() as u16; + let mut buffer = Buffer::empty(Rect::new(0, 0, 2, size)); + let mut state = ScrollbarState::default() + .position(position) + .content_length(content_length); + Scrollbar::default() + .orientation(ScrollbarOrientation::VerticalRight) + .begin_symbol(Some("<")) + .end_symbol(Some(">")) + .track_symbol(Some("-")) + .thumb_symbol("#") + .render(buffer.area, &mut buffer, &mut state); + let empty_string: String = " ".repeat(size as usize); + let bar = empty_string + .chars() + .zip(expected.chars()) + .map(|(a, b)| format!("{a}{b}")) + .collect_vec(); + assert_eq!(buffer, Buffer::with_lines(bar), "{}", assertion_message); + + let mut buffer = Buffer::empty(Rect::new(0, 0, 2, size)); + let mut state = ScrollbarState::default() + .position(position) + .content_length(content_length); + Scrollbar::default() + .orientation(ScrollbarOrientation::VerticalLeft) + .begin_symbol(Some("<")) + .end_symbol(Some(">")) + .track_symbol(Some("-")) + .thumb_symbol("#") + .render(buffer.area, &mut buffer, &mut state); + let empty_string: String = " ".repeat(size as usize); + let bar = expected + .chars() + .zip(empty_string.chars()) + .map(|(a, b)| format!("{a}{b}")) + .collect_vec(); + assert_eq!(buffer, Buffer::with_lines(bar), "{}", assertion_message); + } }