2020-05-10 13:44:30 +00:00
|
|
|
use tui::{
|
|
|
|
backend::TestBackend,
|
|
|
|
buffer::Buffer,
|
|
|
|
layout::Rect,
|
2020-07-11 17:08:28 +00:00
|
|
|
style::{Color, Style},
|
2020-05-10 13:44:30 +00:00
|
|
|
text::Span,
|
|
|
|
widgets::{Block, Borders},
|
|
|
|
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();
|
|
|
|
terminal
|
2020-06-15 20:57:23 +00:00
|
|
|
.draw(|f| {
|
2019-12-15 20:38:18 +00:00
|
|
|
let block = Block::default()
|
2020-07-11 17:08:28 +00:00
|
|
|
.title(Span::styled("Title", Style::default().fg(Color::LightBlue)))
|
2020-05-10 13:44:30 +00:00
|
|
|
.borders(Borders::ALL);
|
2019-12-15 20:38:18 +00:00
|
|
|
f.render_widget(
|
|
|
|
block,
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 8,
|
|
|
|
height: 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
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn widgets_block_renders_on_small_areas() {
|
|
|
|
let test_case = |block, area: Rect, expected| {
|
|
|
|
let backend = TestBackend::new(area.width, area.height);
|
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
terminal
|
|
|
|
.draw(|f| {
|
|
|
|
f.render_widget(block, area);
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
terminal.backend().assert_buffer(&expected);
|
|
|
|
};
|
|
|
|
|
|
|
|
let one_cell_test_cases = [
|
|
|
|
(Borders::NONE, "T"),
|
|
|
|
(Borders::LEFT, "│"),
|
|
|
|
(Borders::TOP, "T"),
|
|
|
|
(Borders::RIGHT, "│"),
|
|
|
|
(Borders::BOTTOM, "T"),
|
|
|
|
(Borders::ALL, "┌"),
|
|
|
|
];
|
|
|
|
for (borders, symbol) in one_cell_test_cases.iter().cloned() {
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(borders),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
},
|
|
|
|
Buffer::empty(Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(borders),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 1,
|
|
|
|
height: 0,
|
|
|
|
},
|
|
|
|
Buffer::empty(Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 1,
|
|
|
|
height: 0,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(borders),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 0,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
Buffer::empty(Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 0,
|
|
|
|
height: 1,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(borders),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 1,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
Buffer::with_lines(vec![symbol]),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(Borders::LEFT),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 4,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
Buffer::with_lines(vec!["│Tes"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(Borders::RIGHT),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 4,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
Buffer::with_lines(vec!["Tes│"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(Borders::RIGHT),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 4,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
Buffer::with_lines(vec!["Tes│"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default()
|
|
|
|
.title("Test")
|
|
|
|
.borders(Borders::LEFT | Borders::RIGHT),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 4,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
Buffer::with_lines(vec!["│Te│"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(Borders::TOP),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 4,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
Buffer::with_lines(vec!["Test"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default().title("Test").borders(Borders::TOP),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 5,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
Buffer::with_lines(vec!["Test─"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default()
|
|
|
|
.title("Test")
|
|
|
|
.borders(Borders::LEFT | Borders::TOP),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 5,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
Buffer::with_lines(vec!["┌Test"]),
|
|
|
|
);
|
|
|
|
test_case(
|
|
|
|
Block::default()
|
|
|
|
.title("Test")
|
|
|
|
.borders(Borders::LEFT | Borders::TOP),
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 6,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
Buffer::with_lines(vec!["┌Test─"]),
|
|
|
|
);
|
|
|
|
}
|