Skip to main content

Assert

Struct Assert 

Source
pub struct Assert<T> { /* private fields */ }
Expand description

Entry point for the Assert DSL.

Assert provides a fluent API for assertions. An Assert holds the actual value that can be used in assertion statements.

Assert::that(3).is(3);

It takes ownership of self and returns ownership back so that assertions can be chained in fluent manner.

Like in the following example:

Assert::that("foo")
    .is("foo")
    .is_not("bar");

Implementations§

Source§

impl<T> Assert<T>
where T: Debug,

Source

pub fn is<R>(self, expected: R) -> Self
where T: PartialEq<R>, R: Debug,

Assert that self is equal to the expected value.

Assert::that(2).is(2);
Assert::that(String::from("2")).is(String::from("2"));
Assert::that(String::from("2")).is("2");
Assert::that(2).is(3);
Source

pub fn is_not<R>(self, other: R) -> Self
where T: PartialEq<R>, R: Debug,

Assert that self is not equal to the other value.

Assert::that(2).is_not(3);
Assert::that(2).is_not(2);
Source

pub fn is_gt<R>(self, other: R) -> Self
where T: PartialOrd<R>, R: Debug,

Assert that self is greater than the other value.

Assert::that(3).is_gt(2);
Assert::that(3).is_gt(3);
Assert::that(3).is_gt(4);
Source

pub fn is_ge<R>(self, other: R) -> Self
where T: PartialOrd<R>, R: Debug,

Assert that self is greater than or equal to the other value.

Assert::that(3).is_ge(3);
Assert::that(3).is_ge(2);
Assert::that(3).is_ge(4);
Source

pub fn is_lt<R>(self, other: R) -> Self
where T: PartialOrd<R>, R: Debug,

Assert that self is less than the other value.

Assert::that(3).is_lt(4);
Assert::that(3).is_lt(2);
Assert::that(3).is_lt(3);
Source

pub fn is_le<R>(self, other: R) -> Self
where T: PartialOrd<R>, R: Debug,

Assert that self is less than or equal to the other value.

Assert::that(3).is_le(3);
Assert::that(3).is_le(4);
Assert::that(3).is_le(2);
Source

pub fn satisfies(self, predicate: impl FnOnce(&T) -> bool) -> Self

Assert that the actual value satisfies the given predicate.

Assert::that(4).satisfies(|v| v % 2 == 0);
Assert::that(3).satisfies(|v| v % 2 == 0);
Source§

impl<T> Assert<Option<T>>
where T: PartialEq + Debug,

Source

pub fn is_some(self, expected: T) -> Self

Assert that actual is equal to Some expected value.

Assert::that(Some(2)).is_some(2);
Assert::that(None::<i32>).is_some(2);
Source

pub fn is_none(self) -> Self

Assert that actual is equal to None.

Assert::that(None::<i32>).is_none();
Assert::that(Some(2)).is_none();
Source

pub fn unwrap(self) -> Assert<T>

Unwrap the Option value, panic for None.

Assert::that(Some(2)).unwrap().is(2);
Assert::that(None::<i32>).unwrap();
Source§

impl<T, E> Assert<Result<T, E>>
where T: Debug, E: Debug,

Source

pub fn is_ok(self) -> Self

Assert that actual is Ok.

let result: Result<i32, i32> = Ok(2);
Assert::that(result).is_ok();
let result: Result<i32, i32> = Err(2);
Assert::that(result).is_ok();
Source

pub fn is_err(self) -> Self

Assert that actual is Err.

let result: Result<i32, i32> = Err(2);
Assert::that(result).is_err();
let result: Result<i32, i32> = Ok(2);
Assert::that(result).is_err();
Source

pub fn unwrap(self) -> Assert<T>

Unwrap the Ok value, panic for Err.

let result: Result<i32, i32> = Ok(2);
Assert::that(result).unwrap().is(2);
let result: Result<i32, i32> = Err(2);
Assert::that(result).unwrap();
Source

pub fn unwrap_err(self) -> Assert<E>

Unwrap the Err value, panic for Ok.

let result: Result<i32, i32> = Err(2);
Assert::that(result).unwrap_err().is(2);
let result: Result<i32, i32> = Ok(2);
Assert::that(result).unwrap_err();
Source§

impl Assert<String>

DSL for String.

Source

pub fn starts_with(self, prefix: &str) -> Self

Assert that the actual string starts with the given prefix.

Assert::that(String::from("hello world")).starts_with("hello");
Assert::that(String::from("hello world")).starts_with("world");
Source

pub fn ends_with(self, suffix: &str) -> Self

Assert that the actual string ends with the given suffix.

Assert::that(String::from("hello world")).ends_with("world");
Assert::that(String::from("hello world")).ends_with("hello");
Source

pub fn contains(self, pattern: &str) -> Self

Assert that the actual string contains the given pattern.

Assert::that(String::from("hello world")).contains("lo wo");
Assert::that(String::from("hello world")).contains("xyz");
Source§

impl<T> Assert<Vec<T>>
where T: PartialEq + Debug,

DSL for Vec.

Source

pub fn contains(self, expected: &T) -> Self

Assert that the actual vector contains a specific expected value.

Assert::that(vec![1, 2, 3]).contains(&2);
Source

pub fn get(self, index: usize) -> Assert<Option<T>>

Returns an Assert for a value from the Vec.

Assert::that(vec!['a', 'b', 'c']).get(1).is_some('b');
Assert::that(vec!['a', 'b', 'c']).get(5).is_none();
Source

pub fn is_empty(self) -> Self

Assert that the actual vector is empty.

Assert::that(Vec::<i32>::new()).is_empty();
Assert::that(vec![1, 2, 3]).is_empty();
Source

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

Assert that the actual vector has the given length.

Assert::that(vec![1, 2, 3]).has_length(3);
Assert::that(vec![1, 2, 3]).has_length(2);
Source§

impl<T> Assert<T>

Source

pub fn that(actual: T) -> Self

Create an Assert instance for the actual value.

All assertions start with Assert::that(actual).

Source

pub fn map<R>(self, f: impl FnOnce(T) -> R) -> Assert<R>

Maps the actual value using lambda f.

This method is intended to map the actual value in order to apply further assertions.

Assert::that("3")
    .map(|v| v.parse::<i32>().unwrap())
    .is(3);
Assert::that(2).map(|v| v + 2).is(3);

Auto Trait Implementations§

§

impl<T> Freeze for Assert<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Assert<T>
where T: RefUnwindSafe,

§

impl<T> Send for Assert<T>
where T: Send,

§

impl<T> Sync for Assert<T>
where T: Sync,

§

impl<T> Unpin for Assert<T>
where T: Unpin,

§

impl<T> UnwindSafe for Assert<T>
where T: UnwindSafe,

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