mirror of
https://github.com/ratatui-org/ratatui
synced 2025-02-16 22:18:51 +00:00
refactor(tests): rename test files and use the new TestBackend::assert_buffer
method
This commit is contained in:
parent
96c6b4efcb
commit
a00350ab54
7 changed files with 147 additions and 98 deletions
|
@ -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);
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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() {
|
|||
"│ │",
|
||||
"│ │",
|
||||
"└────────────────────────────┘",
|
||||
])
|
||||
]),
|
||||
);
|
||||
}
|
45
tests/widgets_tabs.rs
Normal file
45
tests/widgets_tabs.rs
Normal 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);
|
||||
}
|
Loading…
Add table
Reference in a new issue