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
impl Description
Sourcepub fn new() -> Self
pub fn new() -> Self
Returns a new empty Description.
Sourcepub fn text(self, text: impl Into<Cow<'static, str>>) -> Self
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.
Sourcepub fn nested(self, inner: Description) -> Self
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.
Sourcepub fn collect(self, inner: impl IntoIterator<Item = Description>) -> Self
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.
Sourcepub fn indent(self) -> Self
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")))Sourcepub fn bullet_list(self) -> Self
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")))Sourcepub fn enumerate(self) -> Self
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")))