pub struct Description { /* private fields */ }
Expand description

A structured description, either of a (composed) matcher or of an assertion failure.

One can compose blocks of text into a Description. Each one appears on a new line. For example:

let description = Description::new()
    .text("A block")
    .text("Another block");
verify_that!(description, displays_as(eq("A block\nAnother block")))

One can embed nested descriptions into a Description. The resulting nested description is then rendered with an additional level of indentation. For example:

let inner_description = Description::new()
    .text("A block")
    .text("Another block");
let outer_description = Description::new()
    .text("Header")
    .nested(inner_description);
verify_that!(outer_description, displays_as(eq("\
Header
  A block
  Another block")))

One can also enumerate or bullet list the elements of a Description:

let description = Description::new()
    .text("First item")
    .text("Second item")
    .bullet_list();
verify_that!(description, displays_as(eq("\
* First item
* Second item")))

One can construct a Description from a String or a string slice, an iterator thereof, or from an iterator over other Descriptions:

let single_element_description: Description =
    "A single block description".into();
let two_element_description: Description =
    ["First item", "Second item"].into_iter().collect();
let two_element_description_from_strings: Description =
    ["First item".to_string(), "Second item".to_string()].into_iter().collect();

No newline is added after the last element during rendering. This makes it easier to support single-line matcher descriptions and match explanations.

Implementations§

source§

impl Description

source

pub fn new() -> Self

Returns a new empty Description.

source

pub fn text(self, text: impl Into<Cow<'static, str>>) -> Self

Appends a block of text to this instance.

The block is indented uniformly when this instance is rendered.

source

pub fn nested(self, inner: Description) -> Self

Appends a nested Description to this instance.

The nested Description inner is indented uniformly at the next level of indentation when this instance is rendered.

source

pub fn collect(self, inner: impl IntoIterator<Item = Description>) -> Self

Appends all Description in the given sequence inner to this instance.

Each element is treated as a nested Description in the sense of Self::nested.

source

pub fn indent(self) -> Self

Indents the lines in elements of this description.

This operation will be performed lazily when self is displayed.

This will indent every line inside each element.

For example:

let description = std::iter::once("A B C\nD E F".to_string()).collect::<Description>();
verify_that!(description.indent(), displays_as(eq("  A B C\n  D E F")))
source

pub fn bullet_list(self) -> Self

Instructs this instance to render its elements as a bullet list.

Each element (from either Description::text or Description::nested) is rendered as a bullet point. If an element contains multiple lines, the following lines are aligned with the first one in the block.

For instance:

let description = Description::new()
    .text("First line\nsecond line")
    .bullet_list();
verify_that!(description, displays_as(eq("\
* First line
  second line")))
source

pub fn enumerate(self) -> Self

Instructs this instance to render its elements as an enumerated list.

Each element (from either Description::text or Description::nested) is rendered with its zero-based index. If an element contains multiple lines, the following lines are aligned with the first one in the block.

For instance:

let description = Description::new()
    .text("First line\nsecond line")
    .enumerate();
verify_that!(description, displays_as(eq("\
0. First line
   second line")))
source

pub fn len(&self) -> usize

Returns the length of elements.

source

pub fn is_empty(&self) -> bool

Returns whether the set of elements is empty.

Trait Implementations§

source§

impl Debug for Description

source§

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

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

impl Default for Description

source§

fn default() -> Description

Returns the “default value” for a type. Read more
source§

impl Display for Description

source§

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

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

impl<T: Into<Cow<'static, str>>> From<T> for Description

source§

fn from(value: T) -> Self

Converts to this type from the input type.
source§

impl FromIterator<Description> for Description

source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = Description>,

Creates a value from an iterator. Read more
source§

impl<ElementT: Into<Cow<'static, str>>> FromIterator<ElementT> for Description

source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = ElementT>,

Creates a value from an iterator. Read more

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<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> 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.