From a00350ab54a79842b6f381613be316a05361d379 Mon Sep 17 00:00:00 2001 From: Florian Dehau Date: Thu, 21 May 2020 20:17:55 +0200 Subject: [PATCH] refactor(tests): rename test files and use the new `TestBackend::assert_buffer` method --- tests/{block.rs => widgets_block.rs} | 2 +- tests/{chart.rs => widgets_chart.rs} | 0 tests/{gauge.rs => widgets_gauge.rs} | 14 +- tests/{list.rs => widgets_list.rs} | 0 tests/{paragraph.rs => widgets_paragraph.rs} | 38 ++--- tests/{table.rs => widgets_table.rs} | 146 +++++++++---------- tests/widgets_tabs.rs | 45 ++++++ 7 files changed, 147 insertions(+), 98 deletions(-) rename tests/{block.rs => widgets_block.rs} (95%) rename tests/{chart.rs => widgets_chart.rs} (100%) rename tests/{gauge.rs => widgets_gauge.rs} (88%) rename tests/{list.rs => widgets_list.rs} (100%) rename tests/{paragraph.rs => widgets_paragraph.rs} (89%) rename tests/{table.rs => widgets_table.rs} (91%) create mode 100644 tests/widgets_tabs.rs diff --git a/tests/block.rs b/tests/widgets_block.rs similarity index 95% rename from tests/block.rs rename to tests/widgets_block.rs index 6138314c..27f1554f 100644 --- a/tests/block.rs +++ b/tests/widgets_block.rs @@ -41,5 +41,5 @@ fn it_draws_a_block() { for x in 1..=5 { expected.get_mut(x, 0).set_fg(Color::LightBlue); } - assert_eq!(&expected, terminal.backend().buffer()); + terminal.backend().assert_buffer(&expected); } diff --git a/tests/chart.rs b/tests/widgets_chart.rs similarity index 100% rename from tests/chart.rs rename to tests/widgets_chart.rs diff --git a/tests/gauge.rs b/tests/widgets_gauge.rs similarity index 88% rename from tests/gauge.rs rename to tests/widgets_gauge.rs index 82df65f1..25b8607f 100644 --- a/tests/gauge.rs +++ b/tests/widgets_gauge.rs @@ -1,8 +1,10 @@ -use tui::backend::TestBackend; -use tui::buffer::Buffer; -use tui::layout::{Constraint, Direction, Layout}; -use tui::widgets::{Block, Borders, Gauge}; -use tui::Terminal; +use tui::{ + backend::TestBackend, + buffer::Buffer, + layout::{Constraint, Direction, Layout}, + widgets::{Block, Borders, Gauge}, + Terminal, +}; #[test] fn gauge_render() { @@ -38,5 +40,5 @@ fn gauge_render() { " ", " ", ]); - assert_eq!(&expected, terminal.backend().buffer()); + terminal.backend().assert_buffer(&expected); } diff --git a/tests/list.rs b/tests/widgets_list.rs similarity index 100% rename from tests/list.rs rename to tests/widgets_list.rs diff --git a/tests/paragraph.rs b/tests/widgets_paragraph.rs similarity index 89% rename from tests/paragraph.rs rename to tests/widgets_paragraph.rs index 788a8226..59726b0c 100644 --- a/tests/paragraph.rs +++ b/tests/widgets_paragraph.rs @@ -1,8 +1,10 @@ -use tui::backend::TestBackend; -use tui::buffer::Buffer; -use tui::layout::Alignment; -use tui::widgets::{Block, Borders, Paragraph, Text}; -use tui::Terminal; +use tui::{ + backend::TestBackend, + buffer::Buffer, + layout::Alignment, + widgets::{Block, Borders, Paragraph, Text}, + Terminal, +}; const SAMPLE_STRING: &str = "The library is based on the principle of immediate rendering with \ intermediate buffers. This means that at each new frame you should build all widgets that are \ @@ -11,7 +13,7 @@ const SAMPLE_STRING: &str = "The library is based on the principle of immediate #[test] fn paragraph_render_wrap() { - let render = |alignment| { + let test_case = |alignment, expected| { let backend = TestBackend::new(20, 10); let mut terminal = Terminal::new(backend).unwrap(); @@ -26,11 +28,11 @@ fn paragraph_render_wrap() { f.render_widget(paragraph, size); }) .unwrap(); - terminal.backend().buffer().clone() + terminal.backend().assert_buffer(&expected); }; - assert_eq!( - render(Alignment::Left), + test_case( + Alignment::Left, Buffer::with_lines(vec![ "┌──────────────────┐", "│The library is │", @@ -42,10 +44,10 @@ fn paragraph_render_wrap() { "│buffers. This │", "│means that at each│", "└──────────────────┘", - ]) + ]), ); - assert_eq!( - render(Alignment::Right), + test_case( + Alignment::Right, Buffer::with_lines(vec![ "┌──────────────────┐", "│ The library is│", @@ -57,10 +59,10 @@ fn paragraph_render_wrap() { "│ buffers. This│", "│means that at each│", "└──────────────────┘", - ]) + ]), ); - assert_eq!( - render(Alignment::Center), + test_case( + Alignment::Center, Buffer::with_lines(vec![ "┌──────────────────┐", "│ The library is │", @@ -72,7 +74,7 @@ fn paragraph_render_wrap() { "│ buffers. This │", "│means that at each│", "└──────────────────┘", - ]) + ]), ); } @@ -105,7 +107,7 @@ fn paragraph_render_double_width() { "│を行う場│", "└────────┘", ]); - assert_eq!(&expected, terminal.backend().buffer()); + terminal.backend().assert_buffer(&expected); } #[test] @@ -135,5 +137,5 @@ fn paragraph_render_mixed_width() { "│、 │", "└────────┘", ]); - assert_eq!(&expected, terminal.backend().buffer()); + terminal.backend().assert_buffer(&expected); } diff --git a/tests/table.rs b/tests/widgets_table.rs similarity index 91% rename from tests/table.rs rename to tests/widgets_table.rs index abb498c2..add7aa0a 100644 --- a/tests/table.rs +++ b/tests/widgets_table.rs @@ -6,7 +6,7 @@ use tui::Terminal; #[test] fn table_column_spacing() { - let render = |column_spacing| { + let test_case = |column_spacing, expected| { let backend = TestBackend::new(30, 10); let mut terminal = Terminal::new(backend).unwrap(); @@ -33,12 +33,12 @@ fn table_column_spacing() { f.render_widget(table, size); }) .unwrap(); - terminal.backend().buffer().clone() + terminal.backend().assert_buffer(&expected); }; // no space between columns - assert_eq!( - render(0), + test_case( + 0, Buffer::with_lines(vec![ "┌────────────────────────────┐", "│Head1Head2Head3 │", @@ -50,12 +50,12 @@ fn table_column_spacing() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); // one space between columns - assert_eq!( - render(1), + test_case( + 1, Buffer::with_lines(vec![ "┌────────────────────────────┐", "│Head1 Head2 Head3 │", @@ -67,12 +67,12 @@ fn table_column_spacing() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); // enough space to just not hide the third column - assert_eq!( - render(6), + test_case( + 6, Buffer::with_lines(vec![ "┌────────────────────────────┐", "│Head1 Head2 Head3 │", @@ -84,12 +84,12 @@ fn table_column_spacing() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); // enough space to hide part of the third column - assert_eq!( - render(7), + test_case( + 7, Buffer::with_lines(vec![ "┌────────────────────────────┐", "│Head1 Head2 Head│", @@ -101,13 +101,13 @@ fn table_column_spacing() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); } #[test] fn table_widths() { - let render = |widths| { + let test_case = |widths, expected| { let backend = TestBackend::new(30, 10); let mut terminal = Terminal::new(backend).unwrap(); @@ -129,16 +129,16 @@ fn table_widths() { f.render_widget(table, size); }) .unwrap(); - terminal.backend().buffer().clone() + terminal.backend().assert_buffer(&expected); }; // columns of zero width show nothing - assert_eq!( - render(&[ + test_case( + &[ Constraint::Length(0), Constraint::Length(0), - Constraint::Length(0) - ]), + Constraint::Length(0), + ], Buffer::with_lines(vec![ "┌────────────────────────────┐", "│ │", @@ -150,16 +150,16 @@ fn table_widths() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); // columns of 1 width trim - assert_eq!( - render(&[ + test_case( + &[ Constraint::Length(1), Constraint::Length(1), - Constraint::Length(1) - ]), + Constraint::Length(1), + ], Buffer::with_lines(vec![ "┌────────────────────────────┐", "│H H H │", @@ -171,16 +171,16 @@ fn table_widths() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); // columns of large width just before pushing a column off - assert_eq!( - render(&[ + test_case( + &[ Constraint::Length(8), Constraint::Length(8), - Constraint::Length(8) - ]), + Constraint::Length(8), + ], Buffer::with_lines(vec![ "┌────────────────────────────┐", "│Head1 Head2 Head3 │", @@ -192,13 +192,13 @@ fn table_widths() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); } #[test] fn table_percentage_widths() { - let render = |widths| { + let test_case = |widths, expected| { let backend = TestBackend::new(30, 10); let mut terminal = Terminal::new(backend).unwrap(); @@ -221,16 +221,16 @@ fn table_percentage_widths() { f.render_widget(table, size); }) .unwrap(); - terminal.backend().buffer().clone() + terminal.backend().assert_buffer(&expected); }; // columns of zero width show nothing - assert_eq!( - render(&[ + test_case( + &[ Constraint::Percentage(0), Constraint::Percentage(0), - Constraint::Percentage(0) - ]), + Constraint::Percentage(0), + ], Buffer::with_lines(vec![ "┌────────────────────────────┐", "│ │", @@ -242,16 +242,16 @@ fn table_percentage_widths() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); // columns of not enough width trims the data - assert_eq!( - render(&[ + test_case( + &[ Constraint::Percentage(10), Constraint::Percentage(10), - Constraint::Percentage(10) - ]), + Constraint::Percentage(10), + ], Buffer::with_lines(vec![ "┌────────────────────────────┐", "│HeaHeaHea │", @@ -263,16 +263,16 @@ fn table_percentage_widths() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); // columns of large width just before pushing a column off - assert_eq!( - render(&[ + test_case( + &[ Constraint::Percentage(30), Constraint::Percentage(30), - Constraint::Percentage(30) - ]), + Constraint::Percentage(30), + ], Buffer::with_lines(vec![ "┌────────────────────────────┐", "│Head1 Head2 Head3 │", @@ -284,12 +284,12 @@ fn table_percentage_widths() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); // percentages summing to 100 should give equal widths - assert_eq!( - render(&[Constraint::Percentage(50), Constraint::Percentage(50)]), + test_case( + &[Constraint::Percentage(50), Constraint::Percentage(50)], Buffer::with_lines(vec![ "┌────────────────────────────┐", "│Head1 Head2 │", @@ -301,13 +301,13 @@ fn table_percentage_widths() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); } #[test] fn table_mixed_widths() { - let render = |widths| { + let test_case = |widths, expected| { let backend = TestBackend::new(30, 10); let mut terminal = Terminal::new(backend).unwrap(); @@ -329,16 +329,16 @@ fn table_mixed_widths() { f.render_widget(table, size); }) .unwrap(); - terminal.backend().buffer().clone() + terminal.backend().assert_buffer(&expected); }; // columns of zero width show nothing - assert_eq!( - render(&[ + test_case( + &[ Constraint::Percentage(0), Constraint::Length(0), - Constraint::Percentage(0) - ]), + Constraint::Percentage(0), + ], Buffer::with_lines(vec![ "┌────────────────────────────┐", "│ │", @@ -350,16 +350,16 @@ fn table_mixed_widths() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); // columns of not enough width trims the data - assert_eq!( - render(&[ + test_case( + &[ Constraint::Percentage(10), Constraint::Length(20), - Constraint::Percentage(10) - ]), + Constraint::Percentage(10), + ], Buffer::with_lines(vec![ "┌────────────────────────────┐", "│Hea Head2 Hea│", @@ -371,16 +371,16 @@ fn table_mixed_widths() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); // columns of large width just before pushing a column off - assert_eq!( - render(&[ + test_case( + &[ Constraint::Percentage(30), Constraint::Length(10), - Constraint::Percentage(30) - ]), + Constraint::Percentage(30), + ], Buffer::with_lines(vec![ "┌────────────────────────────┐", "│Head1 Head2 Head3 │", @@ -392,16 +392,16 @@ fn table_mixed_widths() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); // columns of large size (>100% total) hide the last column - assert_eq!( - render(&[ + test_case( + &[ Constraint::Percentage(60), Constraint::Length(10), - Constraint::Percentage(60) - ]), + Constraint::Percentage(60), + ], Buffer::with_lines(vec![ "┌────────────────────────────┐", "│Head1 Head2 │", @@ -413,6 +413,6 @@ fn table_mixed_widths() { "│ │", "│ │", "└────────────────────────────┘", - ]) + ]), ); } diff --git a/tests/widgets_tabs.rs b/tests/widgets_tabs.rs new file mode 100644 index 00000000..87ef8a2e --- /dev/null +++ b/tests/widgets_tabs.rs @@ -0,0 +1,45 @@ +use tui::{backend::TestBackend, buffer::Buffer, layout::Rect, symbols, widgets::Tabs, Terminal}; + +#[test] +fn tabs_should_not_panic_on_narrow_areas() { + let backend = TestBackend::new(1, 1); + let mut terminal = Terminal::new(backend).unwrap(); + terminal + .draw(|mut f| { + let tabs = Tabs::default().titles(&["Tab1", "Tab2"]); + f.render_widget( + tabs, + Rect { + x: 0, + y: 0, + width: 1, + height: 1, + }, + ); + }) + .unwrap(); + let expected = Buffer::with_lines(vec![" "]); + terminal.backend().assert_buffer(&expected); +} + +#[test] +fn tabs_should_truncate_the_last_item() { + let backend = TestBackend::new(10, 1); + let mut terminal = Terminal::new(backend).unwrap(); + terminal + .draw(|mut f| { + let tabs = Tabs::default().titles(&["Tab1", "Tab2"]); + f.render_widget( + tabs, + Rect { + x: 0, + y: 0, + width: 9, + height: 1, + }, + ); + }) + .unwrap(); + let expected = Buffer::with_lines(vec![format!(" Tab1 {} Ta", symbols::line::VERTICAL)]); + terminal.backend().assert_buffer(&expected); +}