pub struct TestBackend { /* private fields */ }
Expand description
A Backend
implementation used for integration testing that renders to an memory buffer.
Note: that although many of the integration and unit tests in ratatui are written using this backend, it is preferable to write unit tests for widgets directly against the buffer rather than using this backend. This backend is intended for integration tests that test the entire terminal UI.
§Example
use ratatui::backend::{Backend, TestBackend};
let mut backend = TestBackend::new(10, 2);
backend.clear()?;
backend.assert_buffer_lines([" "; 2]);
Implementations§
Source§impl TestBackend
impl TestBackend
Sourcepub fn new(width: u16, height: u16) -> Self
pub fn new(width: u16, height: u16) -> Self
Creates a new TestBackend
with the specified width and height.
Sourcepub fn with_lines<'line, Lines>(lines: Lines) -> Self
pub fn with_lines<'line, Lines>(lines: Lines) -> Self
Creates a new TestBackend
with the specified lines as the initial screen state.
The backend’s screen size is determined from the initial lines.
Sourcepub const fn buffer(&self) -> &Buffer
pub const fn buffer(&self) -> &Buffer
Returns a reference to the internal buffer of the TestBackend
.
Sourcepub const fn scrollback(&self) -> &Buffer
pub const fn scrollback(&self) -> &Buffer
Returns a reference to the internal scrollback buffer of the TestBackend
.
The scrollback buffer represents the part of the screen that is currently hidden from view, but that could be accessed by scrolling back in the terminal’s history. This would normally be done using the terminal’s scrollbar or an equivalent keyboard shortcut.
The scrollback buffer starts out empty. Lines are appended when they scroll off the top of
the main buffer. This happens when lines are appended to the bottom of the main buffer
using Backend::append_lines
.
The scrollback buffer has a maximum height of u16::MAX
. If lines are appended to the
bottom of the scrollback buffer when it is at its maximum height, a corresponding number of
lines will be removed from the top.
Sourcepub fn resize(&mut self, width: u16, height: u16)
pub fn resize(&mut self, width: u16, height: u16)
Resizes the TestBackend
to the specified width and height.
Sourcepub fn assert_buffer(&self, expected: &Buffer)
pub fn assert_buffer(&self, expected: &Buffer)
Asserts that the TestBackend
’s buffer is equal to the expected buffer.
This is a shortcut for assert_eq!(self.buffer(), &expected)
.
§Panics
When they are not equal, a panic occurs with a detailed error message showing the differences between the expected and actual buffers.
Sourcepub fn assert_scrollback(&self, expected: &Buffer)
pub fn assert_scrollback(&self, expected: &Buffer)
Asserts that the TestBackend
’s scrollback buffer is equal to the expected buffer.
This is a shortcut for assert_eq!(self.scrollback(), &expected)
.
§Panics
When they are not equal, a panic occurs with a detailed error message showing the differences between the expected and actual buffers.
Sourcepub fn assert_scrollback_empty(&self)
pub fn assert_scrollback_empty(&self)
Asserts that the TestBackend
’s scrollback buffer is empty.
§Panics
When the scrollback buffer is not equal, a panic occurs with a detailed error message showing the differences between the expected and actual buffers.
Sourcepub fn assert_buffer_lines<'line, Lines>(&self, expected: Lines)
pub fn assert_buffer_lines<'line, Lines>(&self, expected: Lines)
Asserts that the TestBackend
’s buffer is equal to the expected lines.
This is a shortcut for assert_eq!(self.buffer(), &Buffer::with_lines(expected))
.
§Panics
When they are not equal, a panic occurs with a detailed error message showing the differences between the expected and actual buffers.
Sourcepub fn assert_scrollback_lines<'line, Lines>(&self, expected: Lines)
pub fn assert_scrollback_lines<'line, Lines>(&self, expected: Lines)
Asserts that the TestBackend
’s scrollback buffer is equal to the expected lines.
This is a shortcut for assert_eq!(self.scrollback(), &Buffer::with_lines(expected))
.
§Panics
When they are not equal, a panic occurs with a detailed error message showing the differences between the expected and actual buffers.
Sourcepub fn assert_cursor_position<P: Into<Position>>(&mut self, position: P)
pub fn assert_cursor_position<P: Into<Position>>(&mut self, position: P)
Asserts that the TestBackend
’s cursor position is equal to the expected one.
This is a shortcut for assert_eq!(self.get_cursor_position().unwrap(), expected)
.
§Panics
When they are not equal, a panic occurs with a detailed error message showing the differences between the expected and actual position.
Trait Implementations§
Source§impl Backend for TestBackend
impl Backend for TestBackend
Source§fn append_lines(&mut self, line_count: u16) -> Result<()>
fn append_lines(&mut self, line_count: u16) -> Result<()>
Inserts n line breaks at the current cursor position.
After the insertion, the cursor x position will be incremented by 1 (unless it’s already at the end of line). This is a common behaviour of terminals in raw mode.
If the number of lines to append is fewer than the number of lines in the buffer after the cursor y position then the cursor is moved down by n rows.
If the number of lines to append is greater than the number of lines in the buffer after the cursor y position then that number of empty lines (at most the buffer’s height in this case but this limit is instead replaced with scrolling in most backend implementations) will be added after the current position and the cursor will be moved to the last row.
Source§fn draw<'a, I>(&mut self, content: I) -> Result<()>
fn draw<'a, I>(&mut self, content: I) -> Result<()>
Source§fn get_cursor_position(&mut self) -> Result<Position>
fn get_cursor_position(&mut self) -> Result<Position>
Source§fn set_cursor_position<P: Into<Position>>(&mut self, position: P) -> Result<()>
fn set_cursor_position<P: Into<Position>>(&mut self, position: P) -> Result<()>
Source§fn window_size(&mut self) -> Result<WindowSize>
fn window_size(&mut self) -> Result<WindowSize>
WindowSize
. Read moreSource§impl Clone for TestBackend
impl Clone for TestBackend
Source§fn clone(&self) -> TestBackend
fn clone(&self) -> TestBackend
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for TestBackend
impl Debug for TestBackend
Source§impl Display for TestBackend
impl Display for TestBackend
Source§impl Hash for TestBackend
impl Hash for TestBackend
Source§impl PartialEq for TestBackend
impl PartialEq for TestBackend
impl Eq for TestBackend
impl StructuralPartialEq for TestBackend
Auto Trait Implementations§
impl Freeze for TestBackend
impl RefUnwindSafe for TestBackend
impl Send for TestBackend
impl Sync for TestBackend
impl Unpin for TestBackend
impl UnwindSafe for TestBackend
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()
Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString
. Read more