2023-03-17 16:03:49 +00:00
|
|
|
use ratatui::{
|
2020-05-10 13:44:30 +00:00
|
|
|
backend::TestBackend,
|
|
|
|
buffer::Buffer,
|
2021-05-22 14:55:24 +00:00
|
|
|
layout::{Alignment, Rect},
|
2020-07-11 17:08:28 +00:00
|
|
|
style::{Color, Style},
|
2020-05-10 13:44:30 +00:00
|
|
|
text::Span,
|
2023-06-19 08:24:36 +00:00
|
|
|
widgets::{
|
|
|
|
block::title::{Position, Title},
|
|
|
|
Block, Borders,
|
|
|
|
},
|
2020-05-10 13:44:30 +00:00
|
|
|
Terminal,
|
|
|
|
};
|
2018-11-04 19:16:10 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-05-21 18:42:34 +00:00
|
|
|
fn widgets_block_renders() {
|
2018-11-04 19:16:10 +00:00
|
|
|
let backend = TestBackend::new(10, 10);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
2024-02-07 23:24:14 +00:00
|
|
|
let block = Block::default()
|
|
|
|
.title(Span::styled("Title", Style::default().fg(Color::LightBlue)))
|
|
|
|
.borders(Borders::ALL);
|
2018-11-04 19:16:10 +00:00
|
|
|
terminal
|
2024-02-07 23:24:14 +00:00
|
|
|
.draw(|frame| frame.render_widget(block, Rect::new(0, 0, 8, 8)))
|
2018-12-07 15:17:33 +00:00
|
|
|
.unwrap();
|
2018-11-04 19:16:10 +00:00
|
|
|
let mut expected = Buffer::with_lines(vec![
|
|
|
|
"┌Title─┐ ",
|
|
|
|
"│ │ ",
|
|
|
|
"│ │ ",
|
|
|
|
"│ │ ",
|
|
|
|
"│ │ ",
|
|
|
|
"│ │ ",
|
|
|
|
"│ │ ",
|
|
|
|
"└──────┘ ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]);
|
|
|
|
for x in 1..=5 {
|
|
|
|
expected.get_mut(x, 0).set_fg(Color::LightBlue);
|
|
|
|
}
|
2020-05-21 18:17:55 +00:00
|
|
|
terminal.backend().assert_buffer(&expected);
|
2018-11-04 19:16:10 +00:00
|
|
|
}
|
2020-11-14 19:26:16 +00:00
|
|
|
|
2023-06-19 08:24:36 +00:00
|
|
|
#[test]
|
|
|
|
fn widgets_block_titles_overlap() {
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
2024-02-07 23:24:14 +00:00
|
|
|
#[track_caller]
|
|
|
|
fn test_case(block: Block, area: Rect, expected: Buffer) {
|
2023-06-19 08:24:36 +00:00
|
|
|
let backend = TestBackend::new(area.width, area.height);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
2024-02-07 23:24:14 +00:00
|
|
|
.draw(|frame| frame.render_widget(block, area))
|
2023-06-19 08:24:36 +00:00
|
|
|
.unwrap();
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
2024-02-07 23:24:14 +00:00
|
|
|
}
|
2023-06-19 08:24:36 +00:00
|
|
|
|
|
|
|
// Left overrides the center
|
|
|
|
test_case(
|
|
|
|
Block::default()
|
|
|
|
.title(Title::from("aaaaa").alignment(Alignment::Left))
|
|
|
|
.title(Title::from("bbb").alignment(Alignment::Center))
|
|
|
|
.title(Title::from("ccc").alignment(Alignment::Right)),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 10, 1),
|
2023-06-19 08:24:36 +00:00
|
|
|
Buffer::with_lines(vec!["aaaaab ccc"]),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Left alignment overrides the center alignment which overrides the right alignment
|
|
|
|
test_case(
|
|
|
|
Block::default()
|
|
|
|
.title(Title::from("aaaaa").alignment(Alignment::Left))
|
|
|
|
.title(Title::from("bbbbb").alignment(Alignment::Center))
|
|
|
|
.title(Title::from("ccccc").alignment(Alignment::Right)),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 11, 1),
|
2023-06-19 08:24:36 +00:00
|
|
|
Buffer::with_lines(vec!["aaaaabbbccc"]),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Multiple left alignment overrides the center alignment and the right alignment
|
|
|
|
test_case(
|
|
|
|
Block::default()
|
|
|
|
.title(Title::from("aaaaa").alignment(Alignment::Left))
|
|
|
|
.title(Title::from("aaaaa").alignment(Alignment::Left))
|
|
|
|
.title(Title::from("bbbbb").alignment(Alignment::Center))
|
|
|
|
.title(Title::from("ccccc").alignment(Alignment::Right)),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 11, 1),
|
2023-06-19 08:24:36 +00:00
|
|
|
Buffer::with_lines(vec!["aaaaabaaaaa"]),
|
|
|
|
);
|
|
|
|
|
|
|
|
// The right alignment doesn't override the center alignment, but pierces through it
|
|
|
|
test_case(
|
|
|
|
Block::default()
|
|
|
|
.title(Title::from("bbbbb").alignment(Alignment::Center))
|
|
|
|
.title(Title::from("ccccccccccc").alignment(Alignment::Right)),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 11, 1),
|
2023-06-19 08:24:36 +00:00
|
|
|
Buffer::with_lines(vec!["cccbbbbbccc"]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-14 19:26:16 +00:00
|
|
|
#[test]
|
|
|
|
fn widgets_block_renders_on_small_areas() {
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
2024-02-07 23:24:14 +00:00
|
|
|
#[track_caller]
|
|
|
|
fn test_case(block: Block, area: Rect, expected: Buffer) {
|
2020-11-14 19:26:16 +00:00
|
|
|
let backend = TestBackend::new(area.width, area.height);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
2024-02-07 23:24:14 +00:00
|
|
|
.draw(|frame| frame.render_widget(block, area))
|
2020-11-14 19:26:16 +00:00
|
|
|
.unwrap();
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
2024-02-07 23:24:14 +00:00
|
|
|
}
|
2020-11-14 19:26:16 +00:00
|
|
|
|
|
|
|
let one_cell_test_cases = [
|
|
|
|
(Borders::NONE, "T"),
|
|
|
|
(Borders::LEFT, "│"),
|
|
|
|
(Borders::TOP, "T"),
|
|
|
|
(Borders::RIGHT, "│"),
|
|
|
|
(Borders::BOTTOM, "T"),
|
|
|
|
(Borders::ALL, "┌"),
|
|
|
|
];
|
2024-03-02 09:06:53 +00:00
|
|
|
for (borders, symbol) in one_cell_test_cases {
|
2020-11-14 19:26:16 +00:00
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(borders),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 0, 0),
|
|
|
|
Buffer::empty(Rect::new(0, 0, 0, 0)),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(borders),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 1, 0),
|
|
|
|
Buffer::empty(Rect::new(0, 0, 1, 0)),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(borders),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 0, 1),
|
|
|
|
Buffer::empty(Rect::new(0, 0, 0, 1)),
|
2020-11-14 19:26:16 +00:00
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(borders),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 1, 1),
|
2020-11-14 19:26:16 +00:00
|
|
|
Buffer::with_lines(vec![symbol]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(Borders::LEFT),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 4, 1),
|
2020-11-14 19:26:16 +00:00
|
|
|
Buffer::with_lines(vec!["│Tes"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(Borders::RIGHT),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 4, 1),
|
2020-11-14 19:26:16 +00:00
|
|
|
Buffer::with_lines(vec!["Tes│"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(Borders::RIGHT),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 4, 1),
|
2020-11-14 19:26:16 +00:00
|
|
|
Buffer::with_lines(vec!["Tes│"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default()
|
|
|
|
.title("Test")
|
|
|
|
.borders(Borders::LEFT | Borders::RIGHT),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 4, 1),
|
2020-11-14 19:26:16 +00:00
|
|
|
Buffer::with_lines(vec!["│Te│"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(Borders::TOP),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 4, 1),
|
2020-11-14 19:26:16 +00:00
|
|
|
Buffer::with_lines(vec!["Test"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(Borders::TOP),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 5, 1),
|
2020-11-14 19:26:16 +00:00
|
|
|
Buffer::with_lines(vec!["Test─"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default()
|
|
|
|
.title("Test")
|
|
|
|
.borders(Borders::LEFT | Borders::TOP),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 5, 1),
|
2020-11-14 19:26:16 +00:00
|
|
|
Buffer::with_lines(vec!["┌Test"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default()
|
|
|
|
.title("Test")
|
|
|
|
.borders(Borders::LEFT | Borders::TOP),
|
2024-02-07 23:24:14 +00:00
|
|
|
Rect::new(0, 0, 6, 1),
|
2020-11-14 19:26:16 +00:00
|
|
|
Buffer::with_lines(vec!["┌Test─"]),
|
|
|
|
);
|
|
|
|
}
|
2021-05-22 14:55:24 +00:00
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2021-05-22 14:55:24 +00:00
|
|
|
#[test]
|
|
|
|
fn widgets_block_title_alignment() {
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
2024-02-07 23:24:14 +00:00
|
|
|
#[track_caller]
|
|
|
|
fn test_case(alignment: Alignment, borders: Borders, expected: Buffer) {
|
|
|
|
let backend = TestBackend::new(15, 3);
|
2021-05-22 14:55:24 +00:00
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
|
2023-07-17 10:27:58 +00:00
|
|
|
let block1 = Block::default()
|
2023-06-19 08:24:36 +00:00
|
|
|
.title(Title::from(Span::styled("Title", Style::default())).alignment(alignment))
|
2021-05-22 14:55:24 +00:00
|
|
|
.borders(borders);
|
|
|
|
|
2023-07-17 10:27:58 +00:00
|
|
|
let block2 = Block::default()
|
|
|
|
.title("Title")
|
|
|
|
.title_alignment(alignment)
|
|
|
|
.borders(borders);
|
|
|
|
|
2024-02-07 23:24:14 +00:00
|
|
|
let area = Rect::new(1, 0, 13, 3);
|
2021-05-22 14:55:24 +00:00
|
|
|
|
2023-07-17 10:27:58 +00:00
|
|
|
for block in [block1, block2] {
|
|
|
|
terminal
|
2024-02-07 23:24:14 +00:00
|
|
|
.draw(|frame| frame.render_widget(block, area))
|
2023-07-17 10:27:58 +00:00
|
|
|
.unwrap();
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
|
|
|
}
|
2024-02-07 23:24:14 +00:00
|
|
|
}
|
2021-05-22 14:55:24 +00:00
|
|
|
|
|
|
|
// title top-left with all borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Left,
|
|
|
|
Borders::ALL,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌Title──────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-left without top border
|
|
|
|
test_case(
|
|
|
|
Alignment::Left,
|
|
|
|
Borders::LEFT | Borders::BOTTOM | Borders::RIGHT,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" │Title │ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-left with no left border
|
|
|
|
test_case(
|
|
|
|
Alignment::Left,
|
|
|
|
Borders::TOP | Borders::RIGHT | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" Title───────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-left without right border
|
|
|
|
test_case(
|
|
|
|
Alignment::Left,
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌Title─────── ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-left without borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Left,
|
|
|
|
Borders::NONE,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" Title ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center with all borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Center,
|
|
|
|
Borders::ALL,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌───Title───┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center without top border
|
|
|
|
test_case(
|
|
|
|
Alignment::Center,
|
|
|
|
Borders::LEFT | Borders::BOTTOM | Borders::RIGHT,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" │ Title │ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center with no left border
|
|
|
|
test_case(
|
|
|
|
Alignment::Center,
|
|
|
|
Borders::TOP | Borders::RIGHT | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ───Title────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center without right border
|
|
|
|
test_case(
|
|
|
|
Alignment::Center,
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌───Title──── ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center without borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Center,
|
|
|
|
Borders::NONE,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" Title ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-right with all borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Right,
|
|
|
|
Borders::ALL,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌──────Title┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-right without top border
|
|
|
|
test_case(
|
|
|
|
Alignment::Right,
|
|
|
|
Borders::LEFT | Borders::BOTTOM | Borders::RIGHT,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" │ Title│ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-right with no left border
|
|
|
|
test_case(
|
|
|
|
Alignment::Right,
|
|
|
|
Borders::TOP | Borders::RIGHT | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ───────Title┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-right without right border
|
|
|
|
test_case(
|
|
|
|
Alignment::Right,
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌───────Title ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-right without borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Right,
|
|
|
|
Borders::NONE,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" Title ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]),
|
2021-05-22 14:55:24 +00:00
|
|
|
);
|
|
|
|
}
|
2023-04-13 20:24:31 +00:00
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2023-04-13 20:24:31 +00:00
|
|
|
#[test]
|
|
|
|
fn widgets_block_title_alignment_bottom() {
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
2024-02-07 23:24:14 +00:00
|
|
|
#[track_caller]
|
|
|
|
fn test_case(alignment: Alignment, borders: Borders, expected: Buffer) {
|
|
|
|
let backend = TestBackend::new(15, 3);
|
2023-04-13 20:24:31 +00:00
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
|
2024-02-07 23:24:14 +00:00
|
|
|
let title = Title::from(Span::styled("Title", Style::default()))
|
|
|
|
.alignment(alignment)
|
|
|
|
.position(Position::Bottom);
|
|
|
|
let block = Block::default().title(title).borders(borders);
|
|
|
|
let area = Rect::new(1, 0, 13, 3);
|
2023-04-13 20:24:31 +00:00
|
|
|
terminal
|
2024-02-07 23:24:14 +00:00
|
|
|
.draw(|frame| frame.render_widget(block, area))
|
2023-04-13 20:24:31 +00:00
|
|
|
.unwrap();
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
2024-02-07 23:24:14 +00:00
|
|
|
}
|
2023-04-13 20:24:31 +00:00
|
|
|
|
|
|
|
// title bottom-left with all borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Left,
|
|
|
|
Borders::ALL,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └Title──────┘ ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title bottom-left without bottom border
|
|
|
|
test_case(
|
|
|
|
Alignment::Left,
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::RIGHT,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" │Title │ ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title bottom-left with no left border
|
|
|
|
test_case(
|
|
|
|
Alignment::Left,
|
|
|
|
Borders::TOP | Borders::RIGHT | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ────────────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" Title───────┘ ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title bottom-left without right border
|
|
|
|
test_case(
|
|
|
|
Alignment::Left,
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌──────────── ",
|
|
|
|
" │ ",
|
|
|
|
" └Title─────── ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title bottom-left without borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Left,
|
|
|
|
Borders::NONE,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" Title ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center with all borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Center,
|
|
|
|
Borders::ALL,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───Title───┘ ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center without bottom border
|
|
|
|
test_case(
|
|
|
|
Alignment::Center,
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::RIGHT,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" │ Title │ ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center with no left border
|
|
|
|
test_case(
|
|
|
|
Alignment::Center,
|
|
|
|
Borders::TOP | Borders::RIGHT | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ────────────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ───Title────┘ ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center without right border
|
|
|
|
test_case(
|
|
|
|
Alignment::Center,
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌──────────── ",
|
|
|
|
" │ ",
|
|
|
|
" └───Title──── ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center without borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Center,
|
|
|
|
Borders::NONE,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" Title ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title bottom-right with all borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Right,
|
|
|
|
Borders::ALL,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └──────Title┘ ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title bottom-right without bottom border
|
|
|
|
test_case(
|
|
|
|
Alignment::Right,
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::RIGHT,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌───────────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" │ Title│ ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title bottom-right with no left border
|
|
|
|
test_case(
|
|
|
|
Alignment::Right,
|
|
|
|
Borders::TOP | Borders::RIGHT | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ────────────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ───────Title┘ ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title bottom-right without right border
|
|
|
|
test_case(
|
|
|
|
Alignment::Right,
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌──────────── ",
|
|
|
|
" │ ",
|
|
|
|
" └───────Title ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title bottom-right without borders
|
|
|
|
test_case(
|
|
|
|
Alignment::Right,
|
|
|
|
Borders::NONE,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" Title ",
|
|
|
|
]),
|
2023-04-13 20:24:31 +00:00
|
|
|
);
|
|
|
|
}
|
2023-06-19 08:24:36 +00:00
|
|
|
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::too_many_lines)]
|
2023-06-19 08:24:36 +00:00
|
|
|
#[test]
|
|
|
|
fn widgets_block_multiple_titles() {
|
2024-03-02 09:06:53 +00:00
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
2024-02-07 23:24:14 +00:00
|
|
|
#[track_caller]
|
|
|
|
fn test_case(title_a: Title, title_b: Title, borders: Borders, expected: Buffer) {
|
|
|
|
let backend = TestBackend::new(15, 3);
|
2023-06-19 08:24:36 +00:00
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
|
|
|
|
let block = Block::default()
|
|
|
|
.title(title_a)
|
|
|
|
.title(title_b)
|
|
|
|
.borders(borders);
|
|
|
|
|
2024-02-07 23:24:14 +00:00
|
|
|
let area = Rect::new(1, 0, 13, 3);
|
2023-06-19 08:24:36 +00:00
|
|
|
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
f.render_widget(block, area);
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
2024-02-07 23:24:14 +00:00
|
|
|
}
|
2023-06-19 08:24:36 +00:00
|
|
|
|
|
|
|
// title bottom-left with all borders
|
|
|
|
test_case(
|
|
|
|
Title::from("foo"),
|
|
|
|
Title::from("bar"),
|
|
|
|
Borders::ALL,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌foo─bar────┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-left without top border
|
|
|
|
test_case(
|
|
|
|
Title::from("foo"),
|
|
|
|
Title::from("bar"),
|
|
|
|
Borders::LEFT | Borders::BOTTOM | Borders::RIGHT,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" │foo bar │ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-left with no left border
|
|
|
|
test_case(
|
|
|
|
Title::from("foo"),
|
|
|
|
Title::from("bar"),
|
|
|
|
Borders::TOP | Borders::RIGHT | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" foo─bar─────┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-left without right border
|
|
|
|
test_case(
|
|
|
|
Title::from("foo"),
|
|
|
|
Title::from("bar"),
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌foo─bar───── ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-left without borders
|
|
|
|
test_case(
|
|
|
|
Title::from("foo"),
|
|
|
|
Title::from("bar"),
|
|
|
|
Borders::NONE,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" foo bar ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center with all borders
|
|
|
|
test_case(
|
|
|
|
Title::from("foo").alignment(Alignment::Center),
|
|
|
|
Title::from("bar").alignment(Alignment::Center),
|
|
|
|
Borders::ALL,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌──foo─bar──┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center without top border
|
|
|
|
test_case(
|
|
|
|
Title::from("foo").alignment(Alignment::Center),
|
|
|
|
Title::from("bar").alignment(Alignment::Center),
|
|
|
|
Borders::LEFT | Borders::BOTTOM | Borders::RIGHT,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" │ foo bar │ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center with no left border
|
|
|
|
test_case(
|
|
|
|
Title::from("foo").alignment(Alignment::Center),
|
|
|
|
Title::from("bar").alignment(Alignment::Center),
|
|
|
|
Borders::TOP | Borders::RIGHT | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ──foo─bar───┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center without right border
|
|
|
|
test_case(
|
|
|
|
Title::from("foo").alignment(Alignment::Center),
|
|
|
|
Title::from("bar").alignment(Alignment::Center),
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌──foo─bar─── ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title center without borders
|
|
|
|
test_case(
|
|
|
|
Title::from("foo").alignment(Alignment::Center),
|
|
|
|
Title::from("bar").alignment(Alignment::Center),
|
|
|
|
Borders::NONE,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" foo bar ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-right with all borders
|
|
|
|
test_case(
|
|
|
|
Title::from("foo").alignment(Alignment::Right),
|
|
|
|
Title::from("bar").alignment(Alignment::Right),
|
|
|
|
Borders::ALL,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌────foo─bar┐ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-right without top border
|
|
|
|
test_case(
|
|
|
|
Title::from("foo").alignment(Alignment::Right),
|
|
|
|
Title::from("bar").alignment(Alignment::Right),
|
|
|
|
Borders::LEFT | Borders::BOTTOM | Borders::RIGHT,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" │ foo bar│ ",
|
|
|
|
" │ │ ",
|
|
|
|
" └───────────┘ ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-right with no left border
|
|
|
|
test_case(
|
|
|
|
Title::from("foo").alignment(Alignment::Right),
|
|
|
|
Title::from("bar").alignment(Alignment::Right),
|
|
|
|
Borders::TOP | Borders::RIGHT | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ─────foo─bar┐ ",
|
|
|
|
" │ ",
|
|
|
|
" ────────────┘ ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-right without right border
|
|
|
|
test_case(
|
|
|
|
Title::from("foo").alignment(Alignment::Right),
|
|
|
|
Title::from("bar").alignment(Alignment::Right),
|
|
|
|
Borders::LEFT | Borders::TOP | Borders::BOTTOM,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" ┌─────foo─bar ",
|
|
|
|
" │ ",
|
|
|
|
" └──────────── ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// title top-right without borders
|
|
|
|
test_case(
|
|
|
|
Title::from("foo").alignment(Alignment::Right),
|
|
|
|
Title::from("bar").alignment(Alignment::Right),
|
|
|
|
Borders::NONE,
|
2024-02-07 23:24:14 +00:00
|
|
|
Buffer::with_lines(vec![
|
|
|
|
" foo bar ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
]),
|
2023-06-19 08:24:36 +00:00
|
|
|
);
|
|
|
|
}
|