Struct AssertThat

Source
pub struct AssertThat<'t, T, M: Mode> { /* private fields */ }
Expand description

AssertThat is the core structure used for assertions. It allows developers to perform assertions on actual values in a fluent and expressive manner, supporting detailed messages as well as different modes of operation, such as panic or capture modes.

§Type Parameters

  • 't: The lifetime of the actual value being asserted.
  • T: The type of the actual value being asserted.
  • M: The assertion mode, implementing the Mode trait. Examples include Panic and Capture modes.

§Fields

  • parent: A reference to the parent assertion, if this is a derived assertion. Failures will propagate to the root assertion.
  • actual: The value being asserted against.
  • subject_name: An optional subject name for the assertion, allowing for more descriptive error messages.
  • detail_messages: A collection of additional messages that provide context for the assertion.
  • print_location: A boolean indicating whether the source code location of the assertion should be printed on failure.
  • number_of_assertions: Tracks the number of assertions made.
  • failures: A collection of failure messages for assertions in Capture mode.
  • mode: The mode used for this assertion, determining behavior on failure.

§Key Features

  • Fluent API: Chainable and composable methods for making expressive assertions.
  • Detail Messages: Add custom messages to provide context for failures.
  • Modes:
    • Panic Mode: The default mode where failures result in immediate panics.
    • Capture Mode: Collect failures instead of panicking, useful for batch processing scenarios.
  • Derived Assertions: Assertions derived from parent assertions, facilitating nested or mapped assertions.

§Notes

  • When using Capture mode, failures must be captured explicitly.
  • This struct is designed to handle both simple and complex assertion chaining scenarios.

Implementations§

Source§

impl<T, M: Mode> AssertThat<'_, T, M>

Source

pub fn with_detail_message(self, message: impl Into<String>) -> Self

Add a message to be displayed on assertion failure.

Source

pub fn with_conditional_detail_message<Message: Into<String>>( self, condition: impl Fn(&Self) -> bool, message_provider: impl Fn(&Self) -> Message, ) -> Self

Add a message to be displayed on assertion failure bound by the given condition.

Source

pub fn add_detail_message(&self, message: impl Into<String>)

Add a message to be displayed on assertion failure.

Use this variant instead of the with_ variants when not in a call-chain context, and you don’t want to call an ownership-taking function.

Source§

impl<T, M: Mode> AssertThat<'_, T, M>

Source

pub fn fail(&self, failure: impl Failure)

Source§

impl<T> AssertThat<'_, T, Capture>

Source§

impl<'t, T, M: Mode> AssertThat<'t, T, M>

Source

pub fn actual(&self) -> &T

Source

pub fn map<U>( self, mapper: impl FnOnce(Actual<'_, T>) -> Actual<'_, U>, ) -> AssertThat<'t, U, M>

Source

pub fn map_owned<U>( self, mapper: impl FnOnce(<T as ToOwned>::Owned) -> U, ) -> AssertThat<'t, U, M>
where T: ToOwned,

Source

pub async fn map_async<U: 't, Fut>( self, mapper: impl FnOnce(Actual<'_, T>) -> Fut, ) -> AssertThat<'t, U, M>
where Fut: Future<Output = U>,

Source

pub fn derive<'u, U: 'u>( &'t self, mapper: impl FnOnce(&'t T) -> U, ) -> AssertThat<'u, U, M>
where 't: 'u,

Source

pub async fn derive_async<'u, U: 'u, Fut: Future<Output = U>>( &'t self, mapper: impl FnOnce(&'t T) -> Fut, ) -> AssertThat<'u, U, M>
where 't: 'u,

Source

pub fn satisfies<U, F, A>(self, mapper: F, assertions: A) -> Self
where for<'a> F: FnOnce(&'a T) -> U, for<'a> A: FnOnce(AssertThat<'a, U, M>),

Source

pub fn satisfies_ref<U, F, A>(self, mapper: F, assertions: A) -> Self
where for<'a> F: FnOnce(&'a T) -> &'a U, for<'a> A: FnOnce(AssertThat<'a, &'a U, M>),

Source

pub fn with_subject_name(self, subject_name: impl Into<String>) -> Self

Gives the actual value contain in this assertion a descriptive name. This name will be part of panic messages when set.

Source

pub fn with_capture(self) -> AssertThat<'t, T, Capture>

Control whether the location is shown on assertion failure.

It can be helpful to call .with_location(false) when you want to test the panic message for exact equality and do not want to be bothered by constantly differing line and column numbers fo the assert-location.

Source

pub fn with_location(self, value: bool) -> Self

Control whether the location (file, line and column) is shown on assertion failure.

It can be helpful to call .with_location(false) when you want to test a panic message for exact equality and do not want to be bothered by constantly differing line and column numbers for the assert-location.

Trait Implementations§

Source§

impl<T: Debug, const N: usize, M: Mode> ArrayAssertions<T> for AssertThat<'_, [T; N], M>

Assertions for generic arrays.

Source§

fn contains_exactly<E: AsRef<[T]>>(self, expected: E) -> Self
where T: PartialEq,

Source§

fn contains_exactly_in_any_order<E: AsRef<[T]>>(self, expected: E) -> Self
where T: PartialEq,

Source§

impl<M: Mode> BoolAssertions for AssertThat<'_, bool, M>

Source§

fn is_true(self) -> Self

Source§

fn is_false(self) -> Self

Source§

impl<'t, M: Mode> BoxAssertions<'t, M> for AssertThat<'t, Box<dyn Any>, M>

Source§

fn has_type<E: 'static>(self) -> AssertThat<'t, E, M>

If this fails in capturing mode, a panic is raised!
Source§

fn has_type_ref<E: 'static>(&'t self) -> AssertThat<'t, &'t E, M>

If this fails in capturing mode, a panic is raised!
Source§

impl<M: Mode> CharAssertions for AssertThat<'_, char, M>

Source§

fn is_equal_to_ignoring_ascii_case(self, expected: char) -> Self

Source§

fn is_lowercase(self) -> Self

Source§

fn is_uppercase(self) -> Self

Source§

fn is_ascii_lowercase(self) -> Self

Source§

fn is_ascii_uppercase(self) -> Self

Source§

impl<M: Mode> CommandAssertions for AssertThat<'_, Command, M>

Source§

fn has_arg(self, expected: impl AsRef<OsStr>) -> Self

Source§

impl<T, M: Mode> ConditionAssertions<T> for AssertThat<'_, T, M>

Source§

fn is<C: Condition<T>>(self, condition: C) -> Self

Source§

fn has<C: Condition<T>>(self, condition: C) -> Self

Source§

impl<T: Debug, M: Mode> DebugAssertions for AssertThat<'_, T, M>

Source§

fn has_debug_string(self, expected: impl AsRef<str>) -> Self

Test that actual has the expected Debug representation.
Source§

fn has_debug_value(self, expected: impl Debug) -> Self

Test that actual and expected have the same Debug representation.
Source§

impl<T: Display, M: Mode> DisplayAssertions for AssertThat<'_, T, M>

Source§

fn has_display_value(self, expected: impl Display) -> Self

Source§

impl<'t, R, F: FnOnce() -> R, M: Mode> FnOnceAssertions<'t, R, M> for AssertThat<'t, F, M>

Source§

fn panics(self) -> AssertThat<'t, PanicValue, M>

Source§

fn does_not_panic(self) -> AssertThat<'t, R, M>
where R: Debug,

Source§

impl<K, V, M: Mode> HashMapAssertions<K, V> for AssertThat<'_, HashMap<K, V>, M>

Source§

fn contains_key(self, expected: impl Borrow<K>) -> Self
where K: Eq + Hash + Debug, V: Debug,

Source§

fn does_not_contain_key(self, not_expected: impl Borrow<K>) -> Self
where K: Eq + Hash + Debug, V: Debug,

Source§

fn contains_value<E>(self, expected: E) -> Self
where K: Debug, V: AssertrPartialEq<E> + Debug, E: Debug,

Source§

fn contains_entry<E>(self, key: impl Borrow<K>, value: impl Borrow<E>) -> Self
where K: Eq + Hash + Debug, V: AssertrPartialEq<E> + Debug, E: Debug,

Source§

impl<T, I, M: Mode> IntoIteratorAssertions<T> for AssertThat<'_, I, M>
where T: Debug, for<'any> &'any I: IntoIterator<Item = &'any T>,

Source§

fn into_iter_contains<E>(self, expected: E) -> Self
where E: Debug, T: Debug + AssertrPartialEq<E>,

Source§

fn into_iter_contains_exactly<E>(self, expected: impl AsRef<[E]>) -> Self
where E: Debug, T: PartialEq<E> + AssertrPartialEq<E> + Debug,

Source§

fn into_iter_iterator_is_empty(self) -> Self

Source§

impl<I, T, M: Mode> IterableConditionAssertions<T, I> for AssertThat<'_, I, M>
where for<'any> &'any I: IntoIterator<Item = &'any T>,

Source§

fn are<C: Condition<T>>(self, condition: C) -> Self

Source§

fn have<C: Condition<T>>(self, condition: C) -> Self

Source§

impl<'t, T, I, M: Mode> IteratorAssertions<'t, T, M> for AssertThat<'t, I, M>
where T: Debug, I: Iterator<Item = T>,

Source§

fn contains<'u, E>(self, expected: E) -> AssertThat<'u, (), M>
where E: Debug, T: Debug + AssertrPartialEq<E>, 't: 'u,

This is a terminal assertion, as it must consume the underlying iterator.
Source§

fn contains_exactly<'u, E>( self, expected: impl AsRef<[E]>, ) -> AssertThat<'u, (), M>
where E: Debug, T: PartialEq<E> + AssertrPartialEq<E> + Debug, 't: 'u,

This is a terminal assertion, as it must consume the underlying iterator.
Source§

impl<T: HasLength + Debug, M: Mode> LengthAssertions for AssertThat<'_, T, M>

Source§

fn is_empty(self) -> Self

Source§

fn is_not_empty(self) -> Self

Source§

fn has_length(self, expected: usize) -> Self

Source§

impl<T, M: Mode> MemAssertions for AssertThat<'_, Type<T>, M>

Source§

fn needs_drop(self) -> Self

Source§

impl<T: Debug, M: Mode> MutexAssertions for AssertThat<'_, Mutex<T>, M>

Source§

fn is_locked(self) -> Self

Asserts that this mutex is locked. Note that implementations may try to acquire the lock in order to check its state.
Source§

fn is_not_locked(self) -> Self

Asserts that this mutex is not locked. Note that implementations may try to acquire the lock in order to check its state.
Source§

fn is_free(self) -> Self
where Self: Sized,

Asserts that this mutex is not locked. Note that implementations may try to acquire the lock in order to check its state. Read more
Source§

impl<T: Num + Debug, M: Mode> NumAssertions<T> for AssertThat<'_, T, M>

Source§

fn is_zero(self) -> Self

Fails if actual is not equal to the additive identity.
Source§

fn is_additive_identity(self) -> Self

Source§

fn is_one(self) -> Self

Fails if actual is not equal to the multiplicative identity.
Source§

fn is_multiplicative_identity(self) -> Self

Source§

fn is_negative(self) -> Self
where T: Signed,

Source§

fn is_positive(self) -> Self
where T: Signed,

Source§

fn is_close_to(self, expected: T, allowed_deviation: T) -> Self
where T: PartialOrd + Clone,

Fails if actual is not in the range [expected - allowed_deviation, expected + allowed_deviation].
Source§

fn is_nan(self) -> Self
where T: Float,

Source§

fn is_finite(self) -> Self
where T: Float,

Source§

fn is_infinite(self) -> Self
where T: Float,

Source§

impl<'t, T, M: Mode> OptionAssertions<'t, T, M> for AssertThat<'t, Option<T>, M>

Source§

fn is_some(self) -> AssertThat<'t, T, M>
where T: Debug,

Test if this option is of the Some variant. This is a terminal operation on the contained Option, as there is nothing meaningful to do with the option if its variant was ensured. This allows you to chain additional expectations on the contained success value.
Source§

fn is_none(self) -> AssertThat<'t, (), M>
where T: Debug,

Test if this option is of the None variant. This is a terminal operation on the contained Option, as there is nothing meaningful to do with the option after its variant was ensured.
Source§

impl<'t, M: Mode> PanicValueAssertions<'t, M> for AssertThat<'t, PanicValue, M>

Source§

fn has_type<E: 'static>(self) -> AssertThat<'t, E, M>

If this fails in capturing mode, a panic is raised!

Source§

fn has_type_ref<E: 'static>(&'t self) -> AssertThat<'t, &'t E, M>

NOTE: If this fails in capturing mode, a panic is raised!
Source§

impl<T, M: Mode> PartialEqAssertions<T> for AssertThat<'_, T, M>

Source§

fn is_equal_to<E>(self, expected: E) -> Self
where T: AssertrPartialEq<E> + Debug, E: Debug,

Source§

fn is_not_equal_to<E>(self, expected: E) -> Self
where T: AssertrPartialEq<E> + Debug, E: Debug,

Source§

impl<T: Debug, M: Mode> PartialOrdAssertions<T> for AssertThat<'_, T, M>

Source§

fn is_less_than<E>(self, expected: impl Borrow<E>) -> Self
where E: Debug, T: PartialOrd<E>,

Source§

fn is_greater_than<E>(self, expected: impl Borrow<E>) -> Self
where E: Debug, T: PartialOrd<E>,

Source§

fn is_less_or_equal_to<E>(self, expected: impl Borrow<E>) -> Self
where E: Debug, T: PartialOrd<E>,

Source§

fn is_greater_or_equal_to<E>(self, expected: impl Borrow<E>) -> Self
where E: Debug, T: PartialOrd<E>,

Source§

impl<M: Mode> PathAssertions for AssertThat<'_, &Path, M>

Source§

fn exists(self) -> Self

Source§

fn does_not_exist(self) -> Self

Source§

fn is_a_file(self) -> Self

Source§

fn is_a_directory(self) -> Self

Source§

fn has_a_root(self) -> Self

Source§

fn is_relative(self) -> Self

Source§

fn has_file_name(self, expected: impl AsRef<OsStr>) -> Self

Source§

fn has_file_stem(self, expected: impl AsRef<OsStr>) -> Self

Source§

fn has_extension(self, expected: impl AsRef<OsStr>) -> Self

Source§

impl<M: Mode> PathAssertions for AssertThat<'_, PathBuf, M>

Source§

fn exists(self) -> Self

Source§

fn does_not_exist(self) -> Self

Source§

fn is_a_file(self) -> Self

Source§

fn is_a_directory(self) -> Self

Source§

fn has_a_root(self) -> Self

Source§

fn is_relative(self) -> Self

Source§

fn has_file_name(self, expected: impl AsRef<OsStr>) -> Self

Source§

fn has_file_stem(self, expected: impl AsRef<OsStr>) -> Self

Source§

fn has_extension(self, expected: impl AsRef<OsStr>) -> Self

Source§

impl<'t, T: Debug, M: Mode> PollAssertions<'t, T, M> for AssertThat<'t, Poll<T>, M>

Source§

fn is_pending(self) -> Self

Source§

fn is_ready(self) -> AssertThat<'t, T, M>

Source§

impl<B, M: Mode> RangeAssertions<B> for AssertThat<'_, B, M>

Source§

fn is_in_range(self, expected: impl RangeBounds<B>) -> Self
where B: PartialOrd + Debug,

Source§

fn is_not_in_range(self, expected: impl RangeBounds<B>) -> Self
where B: PartialOrd + Debug,

Source§

fn is_outside_of_range(self, expected: impl RangeBounds<B>) -> Self
where Self: Sized, B: PartialOrd + Debug,

Source§

impl<B, R: RangeBounds<B>, M: Mode> RangeBoundAssertions<B, R> for AssertThat<'_, R, M>

Source§

fn contains_element(&self, expected: B)
where R: Debug, B: PartialOrd + Debug,

Source§

fn does_not_contain_element(&self, expected: B)
where R: Debug, B: PartialOrd + Debug,

Source§

impl<T: Debug, M: Mode> RefCellAssertions for AssertThat<'_, RefCell<T>, M>

Source§

fn is_borrowed(self) -> Self

Check that the RefCell is immutably or mutably borrowed.
Source§

fn is_mutably_borrowed(self) -> Self

Check that the RefCell is mutably borrowed.
Source§

fn is_not_mutably_borrowed(self) -> Self

Check that the RefCell is not mutably borrowed, wither by being not borrowed at all, or only borrowed immutably.
Source§

impl<'t, M: Mode, T, E> ResultAssertions<'t, M, T, E> for AssertThat<'t, Result<T, E>, M>

Source§

fn is_ok(self) -> AssertThat<'t, T, M>
where T: Debug, E: Debug,

This is a terminal operation on the contained Result, as there is little meaningful to do with the result if its variant was ensured. This allows you to chain additional expectations on the contained success value.

Source§

fn is_err(self) -> AssertThat<'t, E, M>
where T: Debug, E: Debug,

This is a terminal operation on the contained Result, as there is little meaningful to do with the result if its variant was ensured. This allows you to chain additional expectations on the contained error value.

Source§

fn is_ok_satisfying<A>(self, assertions: A) -> Self
where T: Debug, E: Debug, A: for<'a> FnOnce(AssertThat<'a, &'a T, M>),

Source§

fn is_err_satisfying<A>(self, assertions: A) -> Self
where T: Debug, E: Debug, A: for<'a> FnOnce(AssertThat<'a, &'a E, M>),

Source§

impl<'t, T, M: Mode> SliceAssertions<'t, T> for AssertThat<'t, &[T], M>

Source§

fn contains<E>(self, expected: E) -> Self
where E: Debug, T: Debug + AssertrPartialEq<E>,

Source§

fn contains_exactly<E, EE>(self, expected: EE) -> Self
where E: Debug + 't, EE: AsRef<[E]>, T: AssertrPartialEq<E> + Debug,

Test that the subject contains exactly the expected elements. Order is important. Lengths must be identical. Read more
Source§

fn contains_exactly_in_any_order<E: AsRef<[T]>>(self, expected: E) -> Self
where T: PartialEq + Debug,

Source§

fn contains_exactly_matching_in_any_order<P>( self, expected: impl AsRef<[P]>, ) -> Self
where T: Debug, P: Fn(&T) -> bool,

[P] - Predicate
Source§

impl<M: Mode> StrSliceAssertions for AssertThat<'_, &str, M>

Source§

fn is_blank(self) -> Self

Tests whether this string is empty or only containing whitespace characters. ‘Whitespace’ is defined according to the terms of the Unicode Derived Core Property White_Space.
Source§

fn is_blank_ascii(self) -> Self

Tests whether this string is empty or only containing ascii-whitespace characters.
Source§

fn contains(self, expected: impl AsRef<str>) -> Self

Source§

fn starts_with(self, expected: impl AsRef<str>) -> Self

Source§

fn ends_with(self, expected: impl AsRef<str>) -> Self

Source§

impl<M: Mode> StringAssertions for AssertThat<'_, String, M>

Source§

fn contains(self, expected: impl AsRef<str>) -> Self

Source§

fn starts_with(self, expected: impl AsRef<str>) -> Self

Source§

fn ends_with(self, expected: impl AsRef<str>) -> Self

Source§

impl<'t, T: Debug, M: Mode> VecAssertions<'t, T> for AssertThat<'t, Vec<T>, M>

Source§

fn contains<E>(self, expected: E) -> Self
where E: Debug, T: AssertrPartialEq<E> + Debug,

Source§

fn contains_exactly<E>(self, expected: impl AsRef<[E]>) -> Self
where E: Debug + 't, T: AssertrPartialEq<E> + Debug,

Source§

fn contains_exactly_matching_in_any_order<P>( self, expected: impl AsRef<[P]>, ) -> Self
where P: Fn(&T) -> bool,

[P] - Predicate
Source§

impl<T, M: Mode> RefUnwindSafe for AssertThat<'_, T, M>

Source§

impl<T, M: Mode> UnwindSafe for AssertThat<'_, T, M>

Auto Trait Implementations§

§

impl<'t, T, M> !Freeze for AssertThat<'t, T, M>

§

impl<'t, T, M> !Send for AssertThat<'t, T, M>

§

impl<'t, T, M> !Sync for AssertThat<'t, T, M>

§

impl<'t, T, M> Unpin for AssertThat<'t, T, M>
where T: Unpin, M: Unpin,

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> AssertingThat for T

Source§

fn assert_that<'t, U>(self, map: impl Fn(T) -> U) -> AssertThat<'t, U, Panic>

Source§

fn assert_that_it<'t>(self) -> AssertThat<'t, T, Panic>

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.