Skip to main content

AssertOrderedElements

Trait AssertOrderedElements 

Source
pub trait AssertOrderedElements {
    type SingleElement;
    type MultipleElements;

    // Required methods
    fn first_element(self) -> Self::SingleElement;
    fn last_element(self) -> Self::SingleElement;
    fn nth_element(self, n: usize) -> Self::SingleElement;
    fn elements_at(
        self,
        indices: impl IntoIterator<Item = usize>,
    ) -> Self::MultipleElements;
}
Expand description

Extract one or multiple elements of a collection or an iterator that yields its elements in a defined order to call assertions on the extracted elements.

Extraction is used to target the assertions on specific elements of a collection or an iterator, such as the first or last element.

See also the AssertOrderedElementsRef trait for extracting multiple elements from the same ordered collection or iterator for individual assertions.

§Examples

use asserting::prelude::*;

let subject = ["one", "two", "three", "four", "five"];

assert_that!(subject).first_element().is_equal_to("one");
assert_that!(subject).last_element().is_equal_to("five");
assert_that!(subject).nth_element(3).is_equal_to("four");
assert_that!(subject)
    .elements_at([0, 2, 4])
    .contains_exactly(["one", "three", "five"]);

Required Associated Types§

Source

type SingleElement

A spec-like type that contains a single element as the subject that is extracted from the iterator.

Usually this is a Spec<'a, T, R>.

Source

type MultipleElements

A spec-like type that contains multiple or all elements of an iterator as the subject.

Usually this is a Spec<'a, Vec<T>, R>.

Required Methods§

Source

fn first_element(self) -> Self::SingleElement

Verify that a collection or an iterator contains at least one element and return a Spec for the first element.

§Examples
use asserting::prelude::*;

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

assert_that!(subject).first_element().is_equal_to("one");
Source

fn last_element(self) -> Self::SingleElement

Verify that a collection or an iterator contains at least one element and return a Spec for the last element.

§Examples
use asserting::prelude::*;

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

assert_that!(subject).last_element().is_equal_to("three");
Source

fn nth_element(self, n: usize) -> Self::SingleElement

Verify that a collection or an iterator contains at least n + 1 elements and return a Spec for the nth element.

The index n is zero-based (similar to the nth method of iterators).

§Examples
use asserting::prelude::*;

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

assert_that!(subject).nth_element(0).is_equal_to("one");
assert_that!(subject).nth_element(1).is_equal_to("two");
assert_that!(subject).nth_element(2).is_equal_to("three");
Source

fn elements_at( self, indices: impl IntoIterator<Item = usize>, ) -> Self::MultipleElements

Pick the elements of a collection or an iterator at the given positions and return a Spec only containing the selected elements.

§Examples
use asserting::prelude::*;

let subject = ["one", "two", "three", "four", "five"];

assert_that!(subject)
    .elements_at([0, 2, 4])
    .contains_exactly(["one", "three", "five"]);

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl<'a, O, S, T> AssertOrderedElements for DerivedSpec<'a, O, S>

Source§

impl<'a, S, T, R> AssertOrderedElements for Spec<'a, S, R>

Source§

type SingleElement = Spec<'a, T, R>

Source§

type MultipleElements = Spec<'a, Vec<T>, R>