pub struct Spec<'a, S, R> { /* private fields */ }Expand description
Data of an actual assertion.
It holds the data needed to execute an assertion such as the subject, the name of the subject or expression, an optional description of the current assertion and the location of the assertion in the source code respectively test code.
It also holds the concrete FailingStrategy on how to behave in case
an assertion fails.
In case of the CollectFailures failing strategy, the AssertFailures
are collected in this struct.
Implementations§
Source§impl<S, R> Spec<'_, S, R>
impl<S, R> Spec<'_, S, R>
Sourcepub fn expression(&self) -> Option<Expression<'_>>
pub fn expression(&self) -> Option<Expression<'_>>
Returns the expression (or subject name) if one has been set.
Sourcepub fn location(&self) -> Option<Location<'_>>
pub fn location(&self) -> Option<Location<'_>>
Returns the location in source code or test code if it has been set.
Sourcepub fn description(&self) -> Option<&str>
pub fn description(&self) -> Option<&str>
Returns the description or the assertion if it has been set.
Sourcepub const fn diff_format(&self) -> &DiffFormat
pub const fn diff_format(&self) -> &DiffFormat
Returns the diff format used with this assertion.
Sourcepub fn failing_strategy(&self) -> &R
pub fn failing_strategy(&self) -> &R
Returns the failing strategy that is used in case an assertion fails.
Sourcepub fn failures(&self) -> Vec<AssertFailure>
pub fn failures(&self) -> Vec<AssertFailure>
Returns the assertion failures that have been collected so far.
Sourcepub fn display_failures(&self) -> Vec<String>
pub fn display_failures(&self) -> Vec<String>
Returns the assertion failures collected so far as formatted text.
Source§impl<'a, S, R> Spec<'a, S, R>
impl<'a, S, R> Spec<'a, S, R>
Sourcepub const fn new(subject: S, failing_strategy: R) -> Self
pub const fn new(subject: S, failing_strategy: R) -> Self
Constructs a new Spec for the given subject and with the specified
failing strategy.
The diff format is set to “no highlighting”. Failure messages will not highlight differences between the actual and the expected value.
Sourcepub const fn named(self, subject_name: &'a str) -> Self
pub const fn named(self, subject_name: &'a str) -> Self
Sets the subject name or expression for this assertion.
Sourcepub const fn described_as(self, description: &'a str) -> Self
pub const fn described_as(self, description: &'a str) -> Self
Sets a custom description about what is being asserted.
Sourcepub const fn located_at(self, location: Location<'a>) -> Self
pub const fn located_at(self, location: Location<'a>) -> Self
Sets the location of the assertion in the source code respectively test code.
Sourcepub const fn with_diff_format(self, diff_format: DiffFormat) -> Self
pub const fn with_diff_format(self, diff_format: DiffFormat) -> Self
Sets the diff format that is used to highlight differences between the actual value and the expected value.
Note: This method must be called before an assertion method is called to have an effect on the failure message of the assertion as failure messages are formatted immediately when an assertion is executed.
Sourcepub fn with_configured_diff_format(self) -> Self
Available on crate feature colored only.
pub fn with_configured_diff_format(self) -> Self
colored only.Sets the diff format used to highlight differences between the actual value and the expected value according the configured mode.
The mode is configured via environment variables like described in the module colored.
Sourcepub fn extracting<F, U>(self, extractor: F) -> Spec<'a, U, R>where
F: FnOnce(S) -> U,
pub fn extracting<F, U>(self, extractor: F) -> Spec<'a, U, R>where
F: FnOnce(S) -> U,
Maps the current subject to some other value.
It takes a closure that maps the current subject to a new subject and
returns a new Spec with the value returned by the closure as the new
subject. The new subject may have a different type than the original
subject. All other data like expression, description and location are
taken over from this Spec into the returned Spec.
This function is useful when having a custom type and some specific property of this type shall be asserted only.
This is an alias function to the mapping() function.
Both functions do exactly the same. The idea is to provide different
names to be able to express the intent more clearly when used in
assertions.
§Example
use asserting::prelude::*;
struct MyStruct {
important_property: String,
other_property: f64,
}
let some_thing = MyStruct {
important_property: "imperdiet aliqua zzril eiusmod".into(),
other_property: 99.9,
};
assert_that!(some_thing).extracting(|s| s.important_property)
.is_equal_to("imperdiet aliqua zzril eiusmod");
Sourcepub fn mapping<F, U>(self, mapper: F) -> Spec<'a, U, R>where
F: FnOnce(S) -> U,
pub fn mapping<F, U>(self, mapper: F) -> Spec<'a, U, R>where
F: FnOnce(S) -> U,
Maps the current subject to some other value.
It takes a closure that maps the current subject to a new subject and
returns a new Spec with the value returned by the closure as the new
subject. The new subject may have a different type than the original
subject. All other data like expression, description and location are
taken over from this Spec into the returned Spec.
This function is useful if some type does not implement a trait that is required for an assertion.
Spec also provides the extracting() function,
which is an alias to this function. Both functions do exactly the same.
Choose that function of which its name expresses the intent more
clearly.
§Example
use asserting::prelude::*;
struct Point {
x: i64,
y: i64,
}
let target = Point { x: 12, y: -64 };
assert_that!(target).mapping(|s| (s.x, s.y)).is_equal_to((12, -64));The custom type Point does not implement the PartialEq trait nor
the Debug trait, which are both required for an is_equal_to
assertion. So we map the subject of the type Point to a tuple of its
fields.
Source§impl<S, R> Spec<'_, S, R>where
R: FailingStrategy,
impl<S, R> Spec<'_, S, R>where
R: FailingStrategy,
Sourcepub fn expecting(self, expectation: impl Expectation<S>) -> Self
pub fn expecting(self, expectation: impl Expectation<S>) -> Self
Asserts the given expectation.
In case the expectation is not meet, it does fail according the current
failing strategy of this Spec.
This method is called from the implementations of the assertion traits
defined in the assertions module. Implementations
of custom assertions will call this method with a proper expectation.
§Examples
use asserting::expectations::{IsEmpty, IsEqualTo};
use asserting::prelude::*;
assert_that!(7 * 6).expecting(IsEqualTo {expected: 42 });
assert_that!("").expecting(IsEmpty);Sourcepub fn satisfies<P>(self, predicate: P) -> Self
pub fn satisfies<P>(self, predicate: P) -> Self
Asserts whether the given predicate is meet.
This method takes a predicate function and calls it as an expectation.
In case the predicate function returns false, it does fail with a
generic failure message and according to the current failing strategy of
this Spec.
This method can be used to do simple custom assertions without
implementing an Expectation and an assertion trait.
§Examples
use asserting::prelude::*;
fn is_odd(value: &i32) -> bool {
value & 1 == 1
}
assert_that!(37).satisfies(is_odd);
let failures = verify_that!(22).satisfies(is_odd).display_failures();
assert_that!(failures).contains_exactly([
"assertion failed: expected 22 to satisfy the given predicate, but returned false\n"
]);To assert a predicate with a custom failure message instead of the
generic one, use the method
satisfies_with_message.
Sourcepub fn satisfies_with_message<P>(
self,
message: impl Into<String>,
predicate: P,
) -> Self
pub fn satisfies_with_message<P>( self, message: impl Into<String>, predicate: P, ) -> Self
Asserts whether the given predicate is meet.
This method takes a predicate function and calls it as an expectation.
In case the predicate function returns false, it does fail with the
provided failure message and according to the current failing strategy
of this Spec.
This method can be used to do simple custom assertions without
implementing an Expectation and an assertion trait.
§Examples
use asserting::prelude::*;
fn is_odd(value: &i32) -> bool {
value & 1 == 1
}
assert_that!(37).satisfies_with_message("expected my number to be odd", is_odd);
let failures = verify_that!(22)
.satisfies_with_message("expected my number to be odd", is_odd)
.display_failures();
assert_that!(failures).contains_exactly([
"assertion failed: expected my number to be odd\n"
]);To assert a predicate with a generic failure message instead of
providing one use the method
satisfies.
Source§impl<S> Spec<'_, S, CollectFailures>
impl<S> Spec<'_, S, CollectFailures>
Sourcepub fn soft_panic(&self)
pub fn soft_panic(&self)
Turns assertions into “soft assertions”.
It executes all specified assertions on a Spec and if at least one
assertion fails, it panics. The panic message contains the messages of
all assertions that have failed.
This method is only available on Specs with the
CollectFailures-FailingStrategy. That is any Spec contructed
by the macros verify_that! and verify_that_code! or by the
functions verify_that() and verify_that_code().
On a Spec with the PanicOnFail-FailingStrategy it would not
work as the very first failing assertion panics immediately, and later
assertions never get executed.
§Examples
Running the following two assertions in “soft” mode:
use asserting::prelude::*;
verify_that!("the answer to all important questions is 42")
.contains("unimportant")
.has_at_most_length(41)
.soft_panic();executes both assertions and prints the messages of both failing assertions in the panic message:
assertion failed: expected subject to contain "unimportant"
but was: "the answer to all important questions is 42"
expected: "unimportant"
assertion failed: expected subject has at most a length of 41
but was: 43
expected: <= 41To highlight differences in failure messages of soft assertions use
the with_configured_diff_format() method, like so:
use asserting::prelude::*;
verify_that!("the answer to all important questions is 42")
.with_configured_diff_format()
.contains("important")
.has_at_most_length(43)
.soft_panic();Trait Implementations§
Source§impl<R> AssertBoolean for Spec<'_, bool, R>where
R: FailingStrategy,
impl<R> AssertBoolean for Spec<'_, bool, R>where
R: FailingStrategy,
Source§impl<S, R> AssertCodePanics for Spec<'_, Code<S>, R>where
S: FnOnce(),
R: FailingStrategy,
impl<S, R> AssertCodePanics for Spec<'_, Code<S>, R>where
S: FnOnce(),
R: FailingStrategy,
Source§fn does_not_panic(self) -> Self
fn does_not_panic(self) -> Self
panic only.Source§fn panics(self) -> Self
fn panics(self) -> Self
panic only.Source§fn panics_with_message(self, message: impl Into<String>) -> Self
fn panics_with_message(self, message: impl Into<String>) -> Self
panic only.Source§impl<S, R> AssertEmptiness for Spec<'_, S, R>
impl<S, R> AssertEmptiness for Spec<'_, S, R>
Source§impl<S, E, R> AssertEquality<E> for Spec<'_, S, R>
impl<S, E, R> AssertEquality<E> for Spec<'_, S, R>
Source§fn is_equal_to(self, expected: E) -> Self
fn is_equal_to(self, expected: E) -> Self
Source§fn is_not_equal_to(self, expected: E) -> Self
fn is_not_equal_to(self, expected: E) -> Self
Source§impl<S, R> AssertHasCharCount<usize> for Spec<'_, S, R>
impl<S, R> AssertHasCharCount<usize> for Spec<'_, S, R>
Source§fn has_char_count(self, expected_char_count: usize) -> Self
fn has_char_count(self, expected_char_count: usize) -> Self
Source§fn has_char_count_in_range<U>(self, expected_range: U) -> Self
fn has_char_count_in_range<U>(self, expected_range: U) -> Self
Source§fn has_char_count_less_than(self, expected_char_count: usize) -> Self
fn has_char_count_less_than(self, expected_char_count: usize) -> Self
Source§fn has_char_count_greater_than(self, expected_char_count: usize) -> Self
fn has_char_count_greater_than(self, expected_char_count: usize) -> Self
Source§fn has_at_most_char_count(self, expected_char_count: usize) -> Self
fn has_at_most_char_count(self, expected_char_count: usize) -> Self
Source§fn has_at_least_char_count(self, expected_char_count: usize) -> Self
fn has_at_least_char_count(self, expected_char_count: usize) -> Self
Source§impl<T, E, X, R> AssertHasError<X> for Spec<'_, Result<T, E>, R>
impl<T, E, X, R> AssertHasError<X> for Spec<'_, Result<T, E>, R>
Source§impl<'a, T, E, X, R> AssertHasErrorMessage<'a, X, R> for Spec<'a, Result<T, E>, R>
impl<'a, T, E, X, R> AssertHasErrorMessage<'a, X, R> for Spec<'a, Result<T, E>, R>
Source§impl<S, R> AssertHasLength<usize> for Spec<'_, S, R>
impl<S, R> AssertHasLength<usize> for Spec<'_, S, R>
Source§fn has_length(self, expected_length: usize) -> Self
fn has_length(self, expected_length: usize) -> Self
Source§fn has_length_in_range<U>(self, expected_range: U) -> Self
fn has_length_in_range<U>(self, expected_range: U) -> Self
Source§fn has_length_less_than(self, expected_length: usize) -> Self
fn has_length_less_than(self, expected_length: usize) -> Self
Source§fn has_length_greater_than(self, expected_length: usize) -> Self
fn has_length_greater_than(self, expected_length: usize) -> Self
Source§fn has_at_most_length(self, expected_length: usize) -> Self
fn has_at_most_length(self, expected_length: usize) -> Self
Source§fn has_at_least_length(self, expected_length: usize) -> Self
fn has_at_least_length(self, expected_length: usize) -> Self
Source§impl<S, E, R> AssertHasValue<E> for Spec<'_, Option<S>, R>
impl<S, E, R> AssertHasValue<E> for Spec<'_, Option<S>, R>
Source§impl<T, E, X, R> AssertHasValue<X> for Spec<'_, Result<T, E>, R>
impl<T, E, X, R> AssertHasValue<X> for Spec<'_, Result<T, E>, R>
Source§impl<S, E, R> AssertInRange<E> for Spec<'_, S, R>
impl<S, E, R> AssertInRange<E> for Spec<'_, S, R>
Source§fn is_in_range<U>(self, range: U) -> Selfwhere
U: RangeBounds<E> + Debug,
fn is_in_range<U>(self, range: U) -> Selfwhere
U: RangeBounds<E> + Debug,
Source§fn is_not_in_range<U>(self, range: U) -> Selfwhere
U: RangeBounds<E> + Debug,
fn is_not_in_range<U>(self, range: U) -> Selfwhere
U: RangeBounds<E> + Debug,
Source§impl<S, R> AssertInfinity for Spec<'_, S, R>
impl<S, R> AssertInfinity for Spec<'_, S, R>
Source§impl<R> AssertIsCloseToWithDefaultMargin<f32> for Spec<'_, f32, R>where
R: FailingStrategy,
impl<R> AssertIsCloseToWithDefaultMargin<f32> for Spec<'_, f32, R>where
R: FailingStrategy,
Source§fn is_close_to(self, expected: f32) -> Self
fn is_close_to(self, expected: f32) -> Self
float-cmp only.Source§fn is_not_close_to(self, expected: f32) -> Self
fn is_not_close_to(self, expected: f32) -> Self
float-cmp only.Source§impl<R> AssertIsCloseToWithDefaultMargin<f64> for Spec<'_, f64, R>where
R: FailingStrategy,
impl<R> AssertIsCloseToWithDefaultMargin<f64> for Spec<'_, f64, R>where
R: FailingStrategy,
Source§fn is_close_to(self, expected: f64) -> Self
fn is_close_to(self, expected: f64) -> Self
float-cmp only.Source§fn is_not_close_to(self, expected: f64) -> Self
fn is_not_close_to(self, expected: f64) -> Self
float-cmp only.