fix(title): remove default alignment and position (#323)

* fix(title): remove default alignment and position

* test(block): add test cases for alignment

* test(block): extend the unit tests for block title alignment
This commit is contained in:
Orhun Parmaksız 2023-07-17 12:27:58 +02:00 committed by GitHub
parent 33f3212cbf
commit 1ff85535c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 9 deletions

View file

@ -50,8 +50,8 @@ impl<'a> Default for Title<'a> {
fn default() -> Self {
Self {
content: Line::from(""),
alignment: Some(Alignment::Left),
position: Some(Position::Top),
alignment: None,
position: None,
}
}
}

View file

@ -515,6 +515,7 @@ impl<'a> Styled for Block<'a> {
mod tests {
use super::*;
use crate::{
assert_buffer_eq,
layout::Rect,
style::{Color, Modifier, Stylize},
};
@ -887,4 +888,38 @@ mod tests {
.remove_modifier(Modifier::DIM)
)
}
#[test]
fn title_alignment() {
let tests = vec![
(Alignment::Left, "test "),
(Alignment::Center, " test "),
(Alignment::Right, " test"),
];
for (alignment, expected) in tests {
let mut buffer = Buffer::empty(Rect::new(0, 0, 8, 1));
Block::default()
.title("test")
.title_alignment(alignment)
.render(buffer.area, &mut buffer);
assert_buffer_eq!(buffer, Buffer::with_lines(vec![expected]));
}
}
#[test]
fn title_alignment_overrides_block_title_alignment() {
let tests = vec![
(Alignment::Right, Alignment::Left, "test "),
(Alignment::Left, Alignment::Center, " test "),
(Alignment::Center, Alignment::Right, " test"),
];
for (block_title_alignment, alignment, expected) in tests {
let mut buffer = Buffer::empty(Rect::new(0, 0, 8, 1));
Block::default()
.title(Title::from("test").alignment(alignment))
.title_alignment(block_title_alignment)
.render(buffer.area, &mut buffer);
assert_buffer_eq!(buffer, Buffer::with_lines(vec![expected]));
}
}
}

View file

@ -295,10 +295,15 @@ fn widgets_block_title_alignment() {
let backend = TestBackend::new(15, 2);
let mut terminal = Terminal::new(backend).unwrap();
let block = Block::default()
let block1 = Block::default()
.title(Title::from(Span::styled("Title", Style::default())).alignment(alignment))
.borders(borders);
let block2 = Block::default()
.title("Title")
.title_alignment(alignment)
.borders(borders);
let area = Rect {
x: 1,
y: 0,
@ -306,13 +311,15 @@ fn widgets_block_title_alignment() {
height: 2,
};
terminal
.draw(|f| {
f.render_widget(block, area);
})
.unwrap();
for block in [block1, block2] {
terminal
.draw(|f| {
f.render_widget(block, area);
})
.unwrap();
terminal.backend().assert_buffer(&expected);
terminal.backend().assert_buffer(&expected);
}
};
// title top-left with all borders