2020-04-14 16:02:48 +00:00
|
|
|
use tui::{
|
|
|
|
backend::TestBackend,
|
|
|
|
buffer::Buffer,
|
2020-04-16 14:06:50 +00:00
|
|
|
layout::Rect,
|
2020-07-11 17:08:28 +00:00
|
|
|
style::{Color, Style},
|
2020-04-16 14:06:50 +00:00
|
|
|
symbols,
|
2020-05-10 13:44:30 +00:00
|
|
|
widgets::{Block, Borders, List, ListItem, ListState},
|
2020-04-14 16:02:48 +00:00
|
|
|
Terminal,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
2020-05-21 18:42:34 +00:00
|
|
|
fn widgets_list_should_highlight_the_selected_item() {
|
2020-04-14 16:02:48 +00:00
|
|
|
let backend = TestBackend::new(10, 3);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
let mut state = ListState::default();
|
|
|
|
state.select(Some(1));
|
|
|
|
terminal
|
2020-06-15 20:57:23 +00:00
|
|
|
.draw(|f| {
|
2020-04-14 16:02:48 +00:00
|
|
|
let size = f.size();
|
|
|
|
let items = vec![
|
2020-05-10 13:44:30 +00:00
|
|
|
ListItem::new("Item 1"),
|
|
|
|
ListItem::new("Item 2"),
|
|
|
|
ListItem::new("Item 3"),
|
2020-04-14 16:02:48 +00:00
|
|
|
];
|
2020-05-10 13:44:30 +00:00
|
|
|
let list = List::new(items)
|
2020-07-11 17:08:28 +00:00
|
|
|
.highlight_style(Style::default().bg(Color::Yellow))
|
2020-04-14 16:02:48 +00:00
|
|
|
.highlight_symbol(">> ");
|
|
|
|
f.render_stateful_widget(list, size, &mut state);
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let mut expected = Buffer::with_lines(vec![" Item 1 ", ">> Item 2 ", " Item 3 "]);
|
2020-07-11 17:08:28 +00:00
|
|
|
for x in 0..10 {
|
2020-04-14 16:02:48 +00:00
|
|
|
expected.get_mut(x, 1).set_bg(Color::Yellow);
|
|
|
|
}
|
2020-05-21 18:42:34 +00:00
|
|
|
terminal.backend().assert_buffer(&expected);
|
2020-04-14 16:02:48 +00:00
|
|
|
}
|
2020-04-16 14:06:50 +00:00
|
|
|
|
|
|
|
#[test]
|
2020-05-21 18:42:34 +00:00
|
|
|
fn widgets_list_should_truncate_items() {
|
2020-04-16 14:06:50 +00:00
|
|
|
let backend = TestBackend::new(10, 2);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
|
|
|
|
struct TruncateTestCase<'a> {
|
|
|
|
selected: Option<usize>,
|
2020-05-10 13:44:30 +00:00
|
|
|
items: Vec<ListItem<'a>>,
|
2020-04-16 14:06:50 +00:00
|
|
|
expected: Buffer,
|
|
|
|
}
|
|
|
|
|
|
|
|
let cases = vec![
|
2020-05-21 18:42:34 +00:00
|
|
|
// An item is selected
|
2020-04-16 14:06:50 +00:00
|
|
|
TruncateTestCase {
|
|
|
|
selected: Some(0),
|
2020-05-10 13:44:30 +00:00
|
|
|
items: vec![
|
|
|
|
ListItem::new("A very long line"),
|
|
|
|
ListItem::new("A very long line"),
|
|
|
|
],
|
2020-04-16 14:06:50 +00:00
|
|
|
expected: Buffer::with_lines(vec![
|
|
|
|
format!(">> A ve{} ", symbols::line::VERTICAL),
|
|
|
|
format!(" A ve{} ", symbols::line::VERTICAL),
|
|
|
|
]),
|
|
|
|
},
|
2020-05-21 18:42:34 +00:00
|
|
|
// No item is selected
|
2020-04-16 14:06:50 +00:00
|
|
|
TruncateTestCase {
|
|
|
|
selected: None,
|
2020-05-10 13:44:30 +00:00
|
|
|
items: vec![
|
|
|
|
ListItem::new("A very long line"),
|
|
|
|
ListItem::new("A very long line"),
|
|
|
|
],
|
2020-04-16 14:06:50 +00:00
|
|
|
expected: Buffer::with_lines(vec![
|
|
|
|
format!("A very {} ", symbols::line::VERTICAL),
|
|
|
|
format!("A very {} ", symbols::line::VERTICAL),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
];
|
2020-05-10 13:44:30 +00:00
|
|
|
for case in cases {
|
2020-04-16 14:06:50 +00:00
|
|
|
let mut state = ListState::default();
|
|
|
|
state.select(case.selected);
|
|
|
|
terminal
|
2020-06-15 20:57:23 +00:00
|
|
|
.draw(|f| {
|
2020-05-10 13:44:30 +00:00
|
|
|
let list = List::new(case.items.clone())
|
2020-04-16 14:06:50 +00:00
|
|
|
.block(Block::default().borders(Borders::RIGHT))
|
|
|
|
.highlight_symbol(">> ");
|
|
|
|
f.render_stateful_widget(list, Rect::new(0, 0, 8, 2), &mut state);
|
|
|
|
})
|
|
|
|
.unwrap();
|
2020-05-21 18:42:34 +00:00
|
|
|
terminal.backend().assert_buffer(&case.expected);
|
2020-04-16 14:06:50 +00:00
|
|
|
}
|
|
|
|
}
|