You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tui-rs/tests/widgets_paragraph.rs

200 lines
6.9 KiB
Rust

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

use tui::{
backend::TestBackend,
buffer::Buffer,
layout::Alignment,
text::{Spans, Text},
widgets::{Block, Borders, Paragraph, Wrap},
Terminal,
};
const SAMPLE_STRING: &str = "The library is based on the principle of immediate rendering with \
intermediate buffers. This means that at each new frame you should build all widgets that are \
supposed to be part of the UI. While providing a great flexibility for rich and \
interactive UI, this may introduce overhead for highly dynamic content.";
#[test]
fn widgets_paragraph_can_wrap_its_content() {
let test_case = |alignment, expected| {
let backend = TestBackend::new(20, 10);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| {
let size = f.size();
let text = vec![Spans::from(SAMPLE_STRING)];
let paragraph = Paragraph::new(text)
.block(Block::default().borders(Borders::ALL))
.alignment(alignment)
.wrap(Wrap::default());
f.render_widget(paragraph, size);
})
.unwrap();
terminal.backend().assert_buffer(&expected);
};
test_case(
Alignment::Left,
Buffer::with_lines(vec![
"",
"The library is ",
"based on the ",
"principle of ",
"immediate ",
"rendering with ",
"intermediate ",
"buffers. This ",
"means that at each",
"",
]),
);
test_case(
Alignment::Right,
Buffer::with_lines(vec![
"",
" The library is",
" based on the",
" principle of",
" immediate",
" rendering with",
" intermediate",
" buffers. This",
"means that at each",
"",
]),
);
test_case(
Alignment::Center,
Buffer::with_lines(vec![
"",
" The library is ",
" based on the ",
" principle of ",
" immediate ",
" rendering with ",
" intermediate ",
" buffers. This ",
"means that at each",
"",
]),
);
}
#[test]
fn widgets_paragraph_renders_double_width_graphemes() {
let backend = TestBackend::new(10, 10);
let mut terminal = Terminal::new(backend).unwrap();
let s = "";
terminal
.draw(|f| {
let size = f.size();
let text = vec![Spans::from(s)];
let paragraph = Paragraph::new(text)
.block(Block::default().borders(Borders::ALL))
.wrap(Wrap::default());
f.render_widget(paragraph, size);
})
.unwrap();
let expected = Buffer::with_lines(vec![
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
]);
terminal.backend().assert_buffer(&expected);
}
#[test]
fn widgets_paragraph_renders_mixed_width_graphemes() {
let backend = TestBackend::new(10, 7);
let mut terminal = Terminal::new(backend).unwrap();
let s = "a";
terminal
.draw(|f| {
let size = f.size();
let text = vec![Spans::from(s)];
let paragraph = Paragraph::new(text)
.block(Block::default().borders(Borders::ALL))
.wrap(Wrap::default());
f.render_widget(paragraph, size);
})
.unwrap();
let expected = Buffer::with_lines(vec![
// The internal width is 8 so only 4 slots for double-width characters.
"",
"a ", // Here we have 1 latin character so only 3 double-width ones can fit.
"",
"",
"",
" ",
"",
]);
terminal.backend().assert_buffer(&expected);
}
#[test]
fn widgets_paragraph_can_scroll_horizontally() {
let test_case = |alignment, scroll, expected| {
let backend = TestBackend::new(20, 10);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| {
let size = f.size();
let text = Text::from(
"\nParagraph can scroll horizontally!\nShort line",
);
let paragraph = Paragraph::new(text)
.block(Block::default().borders(Borders::ALL))
.alignment(alignment)
.scroll(scroll);
f.render_widget(paragraph, size);
})
.unwrap();
terminal.backend().assert_buffer(&expected);
};
test_case(
Alignment::Left,
(0, 7),
Buffer::with_lines(vec![
"",
"",
"ph can scroll hori",
"ine ",
" ",
" ",
" ",
" ",
" ",
"",
]),
);
// only support Alignment::Left
test_case(
Alignment::Right,
(0, 7),
Buffer::with_lines(vec![
"",
"",
"Paragraph can scro",
" Short line",
" ",
" ",
" ",
" ",
" ",
"",
]),
);
}