Skip to content

Commit

Permalink
test(buffer): allow with_lines to accept Vec<Into<Line>> (#494)
Browse files Browse the repository at this point in the history
This allows writing unit tests without having to call set_style on the
expected buffer.

E.g.:
```rust
use crate::style::Stylize;
let mut buf = Buffer::empty(Rect::new(0, 0, 10, 10));
buf.set_string(0, 0, "foo", Style::new().red());
buf.set_string(0, 1, "bar", Style::new().blue());
assert_eq!(buf, Buffer::with_lines(vec!["foo".red(), "bar".blue()]));
```

Inspired by #493 (comment)
  • Loading branch information
joshka authored Sep 12, 2023
1 parent 42f8169 commit 94af2a2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 69 deletions.
29 changes: 15 additions & 14 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,16 @@ impl Buffer {
}

/// Returns a Buffer containing the given lines
pub fn with_lines<S>(lines: Vec<S>) -> Buffer
pub fn with_lines<'a, S>(lines: Vec<S>) -> Buffer
where
S: AsRef<str>,
S: Into<Line<'a>>,
{
let lines = lines.into_iter().map(Into::into).collect::<Vec<_>>();
let height = lines.len() as u16;
let width = lines
.iter()
.map(|i| i.as_ref().width() as u16)
.max()
.unwrap_or_default();
let mut buffer = Buffer::empty(Rect {
x: 0,
y: 0,
width,
height,
});
let width = lines.iter().map(Line::width).max().unwrap_or_default() as u16;
let mut buffer = Buffer::empty(Rect::new(0, 0, width, height));
for (y, line) in lines.iter().enumerate() {
buffer.set_string(0, y as u16, line, Style::default());
buffer.set_line(0, y as u16, line, width);
}
buffer
}
Expand Down Expand Up @@ -1066,4 +1058,13 @@ mod tests {
let skipped: Vec<bool> = one.content().iter().map(|c| c.skip).collect();
assert_eq!(skipped, vec![true, true, false, false, false, false]);
}

#[test]
fn with_lines_accepts_into_lines() {
use crate::style::Stylize;
let mut buf = Buffer::empty(Rect::new(0, 0, 3, 2));
buf.set_string(0, 0, "foo", Style::new().red());
buf.set_string(0, 1, "bar", Style::new().blue());
assert_eq!(buf, Buffer::with_lines(vec!["foo".red(), "bar".blue()]));
}
}
4 changes: 3 additions & 1 deletion src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,7 @@ mod tests {
use pretty_assertions::assert_eq;

use crate::{
assert_buffer_eq,
prelude::{Constraint::*, *},
widgets::{Paragraph, Widget},
};
Expand All @@ -1078,7 +1079,8 @@ mod tests {
let s: String = c.to_string().repeat(area.width as usize);
Paragraph::new(s).render(layout[i], &mut buffer);
}
assert_eq!(buffer.content, Buffer::with_lines(vec![expected]).content);
let expected = Buffer::with_lines(vec![expected]);
assert_buffer_eq!(buffer, expected);
}

#[test]
Expand Down
62 changes: 30 additions & 32 deletions src/widgets/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,17 +750,17 @@ mod tests {
fn test_list_style() {
let items = list_items(vec!["Item 0", "Item 1", "Item 2"]);
let list = List::new(items).style(Style::default().fg(Color::Red));
let buffer = render_widget(list, 10, 5);

let mut expected = Buffer::with_lines(vec![
"Item 0 ",
"Item 1 ",
"Item 2 ",
" ",
" ",
]);
expected.set_style(buffer.area, Style::default().fg(Color::Red));
assert_buffer_eq!(buffer, expected);
assert_buffer_eq!(
render_widget(list, 10, 5),
Buffer::with_lines(vec![
"Item 0 ".red(),
"Item 1 ".red(),
"Item 2 ".red(),
" ".red(),
" ".red(),
])
);
}

#[test]
Expand All @@ -772,17 +772,16 @@ mod tests {
let mut state = ListState::default();
state.select(Some(1));

let buffer = render_stateful_widget(list, &mut state, 10, 5);

let mut expected = Buffer::with_lines(vec![
" Item 0 ",
">>Item 1 ",
" Item 2 ",
" ",
" ",
]);
expected.set_style(Rect::new(0, 1, 10, 1), Style::default().fg(Color::Yellow));
assert_buffer_eq!(buffer, expected);
assert_buffer_eq!(
render_stateful_widget(list, &mut state, 10, 5),
Buffer::with_lines(vec![
" Item 0 ".into(),
">>Item 1 ".yellow(),
" Item 2 ".into(),
" ".into(),
" ".into(),
])
);
}

#[test]
Expand Down Expand Up @@ -923,17 +922,16 @@ mod tests {
let mut state = ListState::default();
state.select(Some(0));

let buffer = render_stateful_widget(list, &mut state, 10, 5);

let mut expected = Buffer::with_lines(vec![
">>Item 0 ",
">>Line 2 ",
" Item 1 ",
" Item 2 ",
" ",
]);
expected.set_style(Rect::new(0, 0, 10, 2), Style::default().fg(Color::Yellow));
assert_buffer_eq!(buffer, expected);
assert_buffer_eq!(
render_stateful_widget(list, &mut state, 10, 5),
Buffer::with_lines(vec![
">>Item 0 ".yellow(),
">>Line 2 ".yellow(),
" Item 1 ".into(),
" Item 2 ".into(),
" ".into(),
])
);
}

#[test]
Expand Down
48 changes: 29 additions & 19 deletions src/widgets/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ mod tests {
let tabs = Tabs::new(vec!["Tab1", "Tab2", "Tab3", "Tab4"]);
assert_buffer_eq!(
render(tabs, Rect::new(0, 0, 30, 1)),
Buffer::with_lines(vec![" Tab1 │ Tab2 │ Tab3 │ Tab4 ",])
Buffer::with_lines(vec![" Tab1 │ Tab2 │ Tab3 │ Tab4 "])
);
}

Expand All @@ -201,9 +201,10 @@ mod tests {
fn render_style() {
let tabs =
Tabs::new(vec!["Tab1", "Tab2", "Tab3", "Tab4"]).style(Style::default().fg(Color::Red));
let mut expected = Buffer::with_lines(vec![" Tab1 │ Tab2 │ Tab3 │ Tab4 "]);
expected.set_style(Rect::new(0, 0, 30, 1), Style::default().fg(Color::Red));
assert_buffer_eq!(render(tabs, Rect::new(0, 0, 30, 1)), expected);
assert_buffer_eq!(
render(tabs, Rect::new(0, 0, 30, 1)),
Buffer::with_lines(vec![" Tab1 │ Tab2 │ Tab3 │ Tab4 ".red()])
);
}

#[test]
Expand All @@ -212,34 +213,39 @@ mod tests {
.highlight_style(Style::new().reversed());

// first tab selected
let mut expected = Buffer::with_lines(vec![" Tab1 │ Tab2 │ Tab3 │ Tab4 "]);
expected.set_style(Rect::new(1, 0, 4, 1), Style::new().reversed());
assert_buffer_eq!(
render(tabs.clone().select(0), Rect::new(0, 0, 30, 1)),
expected
Buffer::with_lines(vec![Line::from(vec![
" ".into(),
"Tab1".reversed(),
" │ Tab2 │ Tab3 │ Tab4 ".into(),
])])
);

// second tab selected
let mut expected = Buffer::with_lines(vec![" Tab1 │ Tab2 │ Tab3 │ Tab4 "]);
expected.set_style(Rect::new(8, 0, 4, 1), Style::new().reversed());
assert_buffer_eq!(
render(tabs.clone().select(1), Rect::new(0, 0, 30, 1)),
expected
Buffer::with_lines(vec![Line::from(vec![
" Tab1 │ ".into(),
"Tab2".reversed(),
" │ Tab3 │ Tab4 ".into(),
])])
);

// last tab selected
let mut expected = Buffer::with_lines(vec![" Tab1 │ Tab2 │ Tab3 │ Tab4 "]);
expected.set_style(Rect::new(22, 0, 4, 1), Style::new().reversed());
assert_buffer_eq!(
render(tabs.clone().select(3), Rect::new(0, 0, 30, 1)),
expected
Buffer::with_lines(vec![Line::from(vec![
" Tab1 │ Tab2 │ Tab3 │ ".into(),
"Tab4".reversed(),
" ".into(),
])])
);

// out of bounds selects no tab
let expected = Buffer::with_lines(vec![" Tab1 │ Tab2 │ Tab3 │ Tab4 "]);
assert_buffer_eq!(
render(tabs.clone().select(4), Rect::new(0, 0, 30, 1)),
expected
Buffer::with_lines(vec![" Tab1 │ Tab2 │ Tab3 │ Tab4 "])
);
}

Expand All @@ -249,10 +255,14 @@ mod tests {
.style(Style::new().red())
.highlight_style(Style::new().reversed())
.select(0);
let mut expected = Buffer::with_lines(vec![" Tab1 │ Tab2 │ Tab3 │ Tab4 "]);
expected.set_style(Rect::new(0, 0, 30, 1), Style::new().red());
expected.set_style(Rect::new(1, 0, 4, 1), Style::new().reversed());
assert_buffer_eq!(render(tabs, Rect::new(0, 0, 30, 1)), expected);
assert_buffer_eq!(
render(tabs, Rect::new(0, 0, 30, 1)),
Buffer::with_lines(vec![Line::from(vec![
" ".red(),
"Tab1".red().reversed(),
" │ Tab2 │ Tab3 │ Tab4 ".red(),
])])
);
}

#[test]
Expand Down
7 changes: 4 additions & 3 deletions tests/widgets_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ratatui::{
layout::{Alignment, Rect},
style::{Color, Style},
symbols,
text::Span,
text::{self, Span},
widgets::{Axis, Block, Borders, Chart, Dataset, GraphType::Line},
Terminal,
};
Expand All @@ -13,9 +13,9 @@ fn create_labels<'a>(labels: &'a [&'a str]) -> Vec<Span<'a>> {
labels.iter().map(|l| Span::from(*l)).collect()
}

fn axis_test_case<S>(width: u16, height: u16, x_axis: Axis, y_axis: Axis, lines: Vec<S>)
fn axis_test_case<'a, S>(width: u16, height: u16, x_axis: Axis, y_axis: Axis, lines: Vec<S>)
where
S: AsRef<str>,
S: Into<text::Line<'a>>,
{
let backend = TestBackend::new(width, height);
let mut terminal = Terminal::new(backend).unwrap();
Expand All @@ -25,6 +25,7 @@ where
f.render_widget(chart, f.size());
})
.unwrap();
let lines = lines.into_iter().map(|l| l.into()).collect();
let expected = Buffer::with_lines(lines);
terminal.backend().assert_buffer(&expected);
}
Expand Down

0 comments on commit 94af2a2

Please sign in to comment.