Struct ratatui::backend::TestBackend

source ·
pub struct TestBackend { /* private fields */ }
Expand description

A Backend implementation used for integration testing that that renders to an in 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::TestBackend, prelude::*};

let mut backend = TestBackend::new(10, 2);
backend.clear()?;
backend.assert_buffer(&Buffer::with_lines(vec!["          "; 2]));

Implementations§

source§

impl TestBackend

source

pub fn new(width: u16, height: u16) -> Self

Creates a new TestBackend with the specified width and height.

source

pub const fn buffer(&self) -> &Buffer

Returns a reference to the internal buffer of the TestBackend.

source

pub fn resize(&mut self, width: u16, height: u16)

Resizes the TestBackend to the specified width and height.

source

pub fn assert_buffer(&self, expected: &Buffer)

Asserts that the TestBackend’s buffer is equal to the expected buffer. If the buffers are not equal, a panic occurs with a detailed error message showing the differences between the expected and actual buffers.

Trait Implementations§

source§

impl Backend for TestBackend

source§

fn append_lines(&mut self, n: 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<()>
where I: Iterator<Item = (u16, u16, &'a Cell)>,

Draw the given content to the terminal screen. Read more
source§

fn hide_cursor(&mut self) -> Result<()>

Hide the cursor on the terminal screen. Read more
source§

fn show_cursor(&mut self) -> Result<()>

Show the cursor on the terminal screen. Read more
source§

fn get_cursor(&mut self) -> Result<(u16, u16)>

Get the current cursor position on the terminal screen. Read more
source§

fn set_cursor(&mut self, x: u16, y: u16) -> Result<()>

Set the cursor position on the terminal screen to the given x and y coordinates. Read more
source§

fn clear(&mut self) -> Result<()>

Clears the whole terminal screen Read more
source§

fn clear_region(&mut self, clear_type: ClearType) -> Result<()>

Clears a specific region of the terminal specified by the ClearType parameter Read more
source§

fn size(&self) -> Result<Rect>

Get the size of the terminal screen in columns/rows as a Rect. Read more
source§

fn window_size(&mut self) -> Result<WindowSize>

Get the size of the terminal screen in columns/rows and pixels as a WindowSize. Read more
source§

fn flush(&mut self) -> Result<()>

Flush any buffered content to the terminal screen.
source§

impl Clone for TestBackend

source§

fn clone(&self) -> TestBackend

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for TestBackend

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for TestBackend

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for TestBackend

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the TestBackend for display by calling the buffer_view function on its internal buffer.

source§

impl Hash for TestBackend

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for TestBackend

source§

fn eq(&self, other: &TestBackend) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for TestBackend

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for TestBackend

source§

impl StructuralPartialEq for TestBackend

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToCompactString for T
where T: Display,

source§

fn to_compact_string(&self) -> CompactString

Converts the given value to a CompactString. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,