AssertElements

Trait AssertElements 

Source
pub trait AssertElements<'a, T, R> {
    // Required methods
    fn single_element(self) -> Spec<'a, T, R>;
    fn filtered_on<C>(self, condition: C) -> Spec<'a, Vec<T>, R>
       where C: FnMut(&T) -> bool;
    fn any_satisfies<P>(self, predicate: P) -> Spec<'a, Vec<T>, R>
       where P: FnMut(&T) -> bool;
    fn all_satisfy<P>(self, predicate: P) -> Spec<'a, Vec<T>, R>
       where P: FnMut(&T) -> bool;
    fn none_satisfies<P>(self, predicate: P) -> Spec<'a, Vec<T>, R>
       where P: FnMut(&T) -> bool;
}
Expand description

Filter assertions for elements of a collection or an iterator.

Filtering is used to target the assertions on specific elements of a collection or an iterator, such as a single element or elements matching a condition.

§Examples

Filtering elements:

use asserting::prelude::*;

let subject = ["single"];
assert_that!(subject).single_element().is_equal_to("single");

let subject = [1, 2, 3, 4, 5];
assert_that!(subject).filtered_on(|e| e & 1 == 0).contains_exactly_in_any_order([2, 4]);

Some elements satisfy a predicate:

use asserting::prelude::*;

let subject = [1, 41, 43, 42, 5];
assert_that!(subject).any_satisfies(|e| *e == 42);

let subject = [43, 44, 45, 46, 47];
assert_that!(subject).all_satisfy(|e| *e > 42);

let subject = [42, 43, 44, 45, 46];
assert_that!(subject).none_satisfies(|e| *e < 42);

Required Methods§

Source

fn single_element(self) -> Spec<'a, T, R>

Verify that the iterator contains exactly one element and return a Spec for that single element.

§Examples
use asserting::prelude::*;

let subject = ["single"];

assert_that!(subject).single_element().is_equal_to("single");
Source

fn filtered_on<C>(self, condition: C) -> Spec<'a, Vec<T>, R>
where C: FnMut(&T) -> bool,

Filter the elements of a collection or an iterator on a condition and return a Spec only containing the elements that match the condition.

§Examples
use asserting::prelude::*;

let subject = [1, 2, 3, 4, 5];
assert_that!(subject)
    .filtered_on(|e| e & 1 == 0)
    .contains_exactly_in_any_order([2, 4]);

let subject = ["one", "two", "three", "four"];
assert_that!(subject)
    .filtered_on(|e| e.len() == 5)
    .single_element()
    .is_equal_to("three");
Source

fn any_satisfies<P>(self, predicate: P) -> Spec<'a, Vec<T>, R>
where P: FnMut(&T) -> bool,

Verify that any element of a collection or an iterator satisfies a given predicate.

§Examples
use asserting::prelude::*;

let subject = [1, 41, 43, 42, 5];
assert_that!(subject).any_satisfies(|e| *e == 42);
Source

fn all_satisfy<P>(self, predicate: P) -> Spec<'a, Vec<T>, R>
where P: FnMut(&T) -> bool,

Verify that all elements of a collection or an iterator satisfy a given predicate.

§Examples
use asserting::prelude::*;

let subject = [43, 44, 45, 46, 47];
assert_that!(subject).all_satisfy(|e| *e > 42);
Source

fn none_satisfies<P>(self, predicate: P) -> Spec<'a, Vec<T>, R>
where P: FnMut(&T) -> bool,

Verify that none of the elements of a collection or an iterator satisfies a given predicate.

§Examples
use asserting::prelude::*;

let subject = [42, 43, 44, 45, 46];
assert_that!(subject).none_satisfies(|e| *e < 42);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'a, S, T, R> AssertElements<'a, T, R> for Spec<'a, S, R>
where S: IntoIterator<Item = T>, T: Debug, R: FailingStrategy,