refactor(tests): rename test files and use the new TestBackend::assert_buffer method

This commit is contained in:
Florian Dehau 2020-05-21 20:17:55 +02:00
parent 96c6b4efcb
commit a00350ab54
7 changed files with 147 additions and 98 deletions

View file

@ -41,5 +41,5 @@ fn it_draws_a_block() {
for x in 1..=5 { for x in 1..=5 {
expected.get_mut(x, 0).set_fg(Color::LightBlue); expected.get_mut(x, 0).set_fg(Color::LightBlue);
} }
assert_eq!(&expected, terminal.backend().buffer()); terminal.backend().assert_buffer(&expected);
} }

View file

@ -1,8 +1,10 @@
use tui::backend::TestBackend; use tui::{
use tui::buffer::Buffer; backend::TestBackend,
use tui::layout::{Constraint, Direction, Layout}; buffer::Buffer,
use tui::widgets::{Block, Borders, Gauge}; layout::{Constraint, Direction, Layout},
use tui::Terminal; widgets::{Block, Borders, Gauge},
Terminal,
};
#[test] #[test]
fn gauge_render() { fn gauge_render() {
@ -38,5 +40,5 @@ fn gauge_render() {
" ", " ",
" ", " ",
]); ]);
assert_eq!(&expected, terminal.backend().buffer()); terminal.backend().assert_buffer(&expected);
} }

View file

@ -1,8 +1,10 @@
use tui::backend::TestBackend; use tui::{
use tui::buffer::Buffer; backend::TestBackend,
use tui::layout::Alignment; buffer::Buffer,
use tui::widgets::{Block, Borders, Paragraph, Text}; layout::Alignment,
use tui::Terminal; widgets::{Block, Borders, Paragraph, Text},
Terminal,
};
const SAMPLE_STRING: &str = "The library is based on the principle of immediate rendering with \ 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 \ 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] #[test]
fn paragraph_render_wrap() { fn paragraph_render_wrap() {
let render = |alignment| { let test_case = |alignment, expected| {
let backend = TestBackend::new(20, 10); let backend = TestBackend::new(20, 10);
let mut terminal = Terminal::new(backend).unwrap(); let mut terminal = Terminal::new(backend).unwrap();
@ -26,11 +28,11 @@ fn paragraph_render_wrap() {
f.render_widget(paragraph, size); f.render_widget(paragraph, size);
}) })
.unwrap(); .unwrap();
terminal.backend().buffer().clone() terminal.backend().assert_buffer(&expected);
}; };
assert_eq!( test_case(
render(Alignment::Left), Alignment::Left,
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌──────────────────┐", "┌──────────────────┐",
"│The library is │", "│The library is │",
@ -42,10 +44,10 @@ fn paragraph_render_wrap() {
"│buffers. This │", "│buffers. This │",
"│means that at each│", "│means that at each│",
"└──────────────────┘", "└──────────────────┘",
]) ]),
); );
assert_eq!( test_case(
render(Alignment::Right), Alignment::Right,
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌──────────────────┐", "┌──────────────────┐",
"│ The library is│", "│ The library is│",
@ -57,10 +59,10 @@ fn paragraph_render_wrap() {
"│ buffers. This│", "│ buffers. This│",
"│means that at each│", "│means that at each│",
"└──────────────────┘", "└──────────────────┘",
]) ]),
); );
assert_eq!( test_case(
render(Alignment::Center), Alignment::Center,
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌──────────────────┐", "┌──────────────────┐",
"│ The library is │", "│ The library is │",
@ -72,7 +74,7 @@ fn paragraph_render_wrap() {
"│ buffers. This │", "│ buffers. This │",
"│means that at each│", "│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] #[test]
@ -135,5 +137,5 @@ fn paragraph_render_mixed_width() {
"│、 │", "│、 │",
"└────────┘", "└────────┘",
]); ]);
assert_eq!(&expected, terminal.backend().buffer()); terminal.backend().assert_buffer(&expected);
} }

View file

@ -6,7 +6,7 @@ use tui::Terminal;
#[test] #[test]
fn table_column_spacing() { fn table_column_spacing() {
let render = |column_spacing| { let test_case = |column_spacing, expected| {
let backend = TestBackend::new(30, 10); let backend = TestBackend::new(30, 10);
let mut terminal = Terminal::new(backend).unwrap(); let mut terminal = Terminal::new(backend).unwrap();
@ -33,12 +33,12 @@ fn table_column_spacing() {
f.render_widget(table, size); f.render_widget(table, size);
}) })
.unwrap(); .unwrap();
terminal.backend().buffer().clone() terminal.backend().assert_buffer(&expected);
}; };
// no space between columns // no space between columns
assert_eq!( test_case(
render(0), 0,
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│Head1Head2Head3 │", "│Head1Head2Head3 │",
@ -50,12 +50,12 @@ fn table_column_spacing() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
// one space between columns // one space between columns
assert_eq!( test_case(
render(1), 1,
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│Head1 Head2 Head3 │", "│Head1 Head2 Head3 │",
@ -67,12 +67,12 @@ fn table_column_spacing() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
// enough space to just not hide the third column // enough space to just not hide the third column
assert_eq!( test_case(
render(6), 6,
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│Head1 Head2 Head3 │", "│Head1 Head2 Head3 │",
@ -84,12 +84,12 @@ fn table_column_spacing() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
// enough space to hide part of the third column // enough space to hide part of the third column
assert_eq!( test_case(
render(7), 7,
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│Head1 Head2 Head│", "│Head1 Head2 Head│",
@ -101,13 +101,13 @@ fn table_column_spacing() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
} }
#[test] #[test]
fn table_widths() { fn table_widths() {
let render = |widths| { let test_case = |widths, expected| {
let backend = TestBackend::new(30, 10); let backend = TestBackend::new(30, 10);
let mut terminal = Terminal::new(backend).unwrap(); let mut terminal = Terminal::new(backend).unwrap();
@ -129,16 +129,16 @@ fn table_widths() {
f.render_widget(table, size); f.render_widget(table, size);
}) })
.unwrap(); .unwrap();
terminal.backend().buffer().clone() terminal.backend().assert_buffer(&expected);
}; };
// columns of zero width show nothing // columns of zero width show nothing
assert_eq!( test_case(
render(&[ &[
Constraint::Length(0), Constraint::Length(0),
Constraint::Length(0), Constraint::Length(0),
Constraint::Length(0) Constraint::Length(0),
]), ],
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│ │", "│ │",
@ -150,16 +150,16 @@ fn table_widths() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
// columns of 1 width trim // columns of 1 width trim
assert_eq!( test_case(
render(&[ &[
Constraint::Length(1), Constraint::Length(1),
Constraint::Length(1), Constraint::Length(1),
Constraint::Length(1) Constraint::Length(1),
]), ],
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│H H H │", "│H H H │",
@ -171,16 +171,16 @@ fn table_widths() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
// columns of large width just before pushing a column off // columns of large width just before pushing a column off
assert_eq!( test_case(
render(&[ &[
Constraint::Length(8), Constraint::Length(8),
Constraint::Length(8), Constraint::Length(8),
Constraint::Length(8) Constraint::Length(8),
]), ],
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│Head1 Head2 Head3 │", "│Head1 Head2 Head3 │",
@ -192,13 +192,13 @@ fn table_widths() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
} }
#[test] #[test]
fn table_percentage_widths() { fn table_percentage_widths() {
let render = |widths| { let test_case = |widths, expected| {
let backend = TestBackend::new(30, 10); let backend = TestBackend::new(30, 10);
let mut terminal = Terminal::new(backend).unwrap(); let mut terminal = Terminal::new(backend).unwrap();
@ -221,16 +221,16 @@ fn table_percentage_widths() {
f.render_widget(table, size); f.render_widget(table, size);
}) })
.unwrap(); .unwrap();
terminal.backend().buffer().clone() terminal.backend().assert_buffer(&expected);
}; };
// columns of zero width show nothing // columns of zero width show nothing
assert_eq!( test_case(
render(&[ &[
Constraint::Percentage(0), Constraint::Percentage(0),
Constraint::Percentage(0), Constraint::Percentage(0),
Constraint::Percentage(0) Constraint::Percentage(0),
]), ],
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│ │", "│ │",
@ -242,16 +242,16 @@ fn table_percentage_widths() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
// columns of not enough width trims the data // columns of not enough width trims the data
assert_eq!( test_case(
render(&[ &[
Constraint::Percentage(10), Constraint::Percentage(10),
Constraint::Percentage(10), Constraint::Percentage(10),
Constraint::Percentage(10) Constraint::Percentage(10),
]), ],
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│HeaHeaHea │", "│HeaHeaHea │",
@ -263,16 +263,16 @@ fn table_percentage_widths() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
// columns of large width just before pushing a column off // columns of large width just before pushing a column off
assert_eq!( test_case(
render(&[ &[
Constraint::Percentage(30), Constraint::Percentage(30),
Constraint::Percentage(30), Constraint::Percentage(30),
Constraint::Percentage(30) Constraint::Percentage(30),
]), ],
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│Head1 Head2 Head3 │", "│Head1 Head2 Head3 │",
@ -284,12 +284,12 @@ fn table_percentage_widths() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
// percentages summing to 100 should give equal widths // percentages summing to 100 should give equal widths
assert_eq!( test_case(
render(&[Constraint::Percentage(50), Constraint::Percentage(50)]), &[Constraint::Percentage(50), Constraint::Percentage(50)],
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│Head1 Head2 │", "│Head1 Head2 │",
@ -301,13 +301,13 @@ fn table_percentage_widths() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
} }
#[test] #[test]
fn table_mixed_widths() { fn table_mixed_widths() {
let render = |widths| { let test_case = |widths, expected| {
let backend = TestBackend::new(30, 10); let backend = TestBackend::new(30, 10);
let mut terminal = Terminal::new(backend).unwrap(); let mut terminal = Terminal::new(backend).unwrap();
@ -329,16 +329,16 @@ fn table_mixed_widths() {
f.render_widget(table, size); f.render_widget(table, size);
}) })
.unwrap(); .unwrap();
terminal.backend().buffer().clone() terminal.backend().assert_buffer(&expected);
}; };
// columns of zero width show nothing // columns of zero width show nothing
assert_eq!( test_case(
render(&[ &[
Constraint::Percentage(0), Constraint::Percentage(0),
Constraint::Length(0), Constraint::Length(0),
Constraint::Percentage(0) Constraint::Percentage(0),
]), ],
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│ │", "│ │",
@ -350,16 +350,16 @@ fn table_mixed_widths() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
// columns of not enough width trims the data // columns of not enough width trims the data
assert_eq!( test_case(
render(&[ &[
Constraint::Percentage(10), Constraint::Percentage(10),
Constraint::Length(20), Constraint::Length(20),
Constraint::Percentage(10) Constraint::Percentage(10),
]), ],
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│Hea Head2 Hea│", "│Hea Head2 Hea│",
@ -371,16 +371,16 @@ fn table_mixed_widths() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
// columns of large width just before pushing a column off // columns of large width just before pushing a column off
assert_eq!( test_case(
render(&[ &[
Constraint::Percentage(30), Constraint::Percentage(30),
Constraint::Length(10), Constraint::Length(10),
Constraint::Percentage(30) Constraint::Percentage(30),
]), ],
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│Head1 Head2 Head3 │", "│Head1 Head2 Head3 │",
@ -392,16 +392,16 @@ fn table_mixed_widths() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
// columns of large size (>100% total) hide the last column // columns of large size (>100% total) hide the last column
assert_eq!( test_case(
render(&[ &[
Constraint::Percentage(60), Constraint::Percentage(60),
Constraint::Length(10), Constraint::Length(10),
Constraint::Percentage(60) Constraint::Percentage(60),
]), ],
Buffer::with_lines(vec![ Buffer::with_lines(vec![
"┌────────────────────────────┐", "┌────────────────────────────┐",
"│Head1 Head2 │", "│Head1 Head2 │",
@ -413,6 +413,6 @@ fn table_mixed_widths() {
"│ │", "│ │",
"│ │", "│ │",
"└────────────────────────────┘", "└────────────────────────────┘",
]) ]),
); );
} }

45
tests/widgets_tabs.rs Normal file
View file

@ -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);
}