Skip to main content

AssertElements

Trait AssertElements 

Source
pub trait AssertElements<'a, I>
where I: IntoIterator,
{ type Output; // Required methods fn each_element<A, B>(self, assert: A) -> Self::Output where A: Fn(Spec<'a, <I as IntoIterator>::Item, DebugRepresentation, CollectFailures>) -> B, B: GetFailures; fn any_element<A, B>(self, assert: A) -> Self::Output where A: Fn(Spec<'a, <I as IntoIterator>::Item, DebugRepresentation, CollectFailures>) -> B, B: GetFailures; }
Expand description

Execute assertions on every element of a collection or iterator.

Iterators over the elements of a collection or an iterator and executes one or multiple assertions on each of those elements. The failure report contains detailed information for each element for which one or multiple assertions failed.

Required Associated Types§

Source

type Output

A spec-like type that is returned by the methods of this trait.

Required Methods§

Source

fn each_element<A, B>(self, assert: A) -> Self::Output

Iterates over the elements of a collection or an iterator and executes the given assertions for each of those elements. If all elements are asserted successfully, the whole assertion succeeds.

It iterates over all elements of the collection or iterator and collects the failure messages for those elements where the assertion fails. In other words, it does not stop iterating when the assertion for one element fails.

The failure messages contain the position of the element within the collection or iterator. The position is 0-based. So a failure message for the first element contains [0], the second [1], and so on.

§Example

The following assertion:

ⓘ
use asserting::prelude::*;

let numbers = [2, 4, 6, 8, 10];

assert_that!(numbers).each_element(|e|
    e.is_greater_than(2)
        .is_at_most(7)
);

will print:

expected numbers [0] to be greater than 2
   but was: 2
  expected: > 2

expected numbers [3] to be at most 7
   but was: 8
  expected: <= 7

expected numbers [4] to be at most 7
   but was: 10
  expected: <= 7
Source

fn any_element<A, B>(self, assert: A) -> Self::Output

Iterates over the elements of a collection or an iterator and executes the given assertions for each of those elements. If the assertion of any element is successful, the iteration stops and the whole assertion succeeds.

If the assertion fails for all elements, the failures of the assertion for all elements are collected.

The failure messages contain the position of the element within the collection or iterator. The position is 0-based. So a failure message for the first element contains [0], the second [1], and so on.

§Example

The following assertion:

ⓘ
use asserting::prelude::*;

let digit_names = ["one", "two", "three"];

assert_that!(digit_names).any_element(|e|
    e.contains('x')
);

will print:

expected digit_names [0] to contain 'x'
   but was: "one"
  expected: 'x'

expected digit_names [1] to contain 'x'
   but was: "two"
  expected: 'x'

expected digit_names [2] to contain 'x'
   but was: "three"
  expected: 'x'

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<'a, I, D, R> AssertElements<'a, I> for Spec<'a, I, D, R>
where I: IntoIterator,

Source§

impl<'a, O, I, D> AssertElements<'a, I> for DerivedSpec<'a, O, I, D>
where I: 'a + IntoIterator, O: DoFail + GetLocation<'a>,