Struct xpct::core::Assertion

source ·
pub struct Assertion<In, AssertFmt>where
    AssertFmt: AssertionFormat,{ /* private fields */ }
Expand description

An assertion, the starting point in a chain of matchers.

This is the value returned by expect!. You can use the to and to_not methods to use matchers, chaining the output of each into the input of the next.

Implementations§

source§

impl<In, AssertFmt> Assertion<In, AssertFmt>where AssertFmt: AssertionFormat + Default,

source

pub fn new(value: In, ctx: AssertFmt::Context) -> Self

Create a new Assertion.

This accepts the “actual” value to match against and a context value which is passed to its formatter.

Typically you’ll want to use the expect! macro instead, because it does nice things like capture the file name, line number, and the stringified expression that was passed to it.

However, if you want to use a custom AssertionFormat, then creating an assertion this way allows you to do it.

source§

impl<In, AssertFmt> Assertion<In, AssertFmt>where AssertFmt: AssertionFormat,

source

pub fn to<Out>( self, matcher: impl DynTransformMatch<In = In, PosOut = Out> ) -> Assertion<Out, AssertFmt>

Make an assertion with the given matcher.

source

pub fn to_not<Out>( self, matcher: impl DynTransformMatch<In = In, NegOut = Out> ) -> Assertion<Out, AssertFmt>

Same as to, but negated.

This does the same thing as the not matcher.

This tests that the given matcher does not succeed.

Examples
use xpct::{expect, equal};

expect!("foo").to_not(equal("bar"));
source

pub fn map<Out>(self, func: impl FnOnce(In) -> Out) -> Assertion<Out, AssertFmt>

Infallibly map the input value by applying a function to it.

This does the same thing as the map matcher.

Examples
use std::convert::Infallible;
use xpct::{expect, equal};

fn do_stuff() -> Result<String, Infallible> {
    Ok(String::from("foobar"))
}

expect!(do_stuff())
    .map(Result::unwrap)
    .to(equal("foobar"));
source

pub fn try_map<Out>( self, func: impl FnOnce(In) -> Result<Out> ) -> Assertion<Out, AssertFmt>

Fallibly map the input value by applying a function to it.

This does the same thing as the try_map matcher.

Examples
use xpct::{expect, equal};

expect!(vec![0x43, 0x75, 0x6e, 0x6f])
    .try_map(|bytes| Ok(String::from_utf8(bytes)?))
    .to(equal("Cuno"));
source

pub fn into<Out>(self) -> Assertion<Out, AssertFmt>where Out: From<In>,

Infallibly convert the input value via From/Into.

This does the same thing as the into matcher.

Examples
use xpct::{expect, equal};

expect!(41u32)
    .into::<u64>()
    .to(equal(41u64));
source

pub fn try_into<Out>(self) -> Assertion<Out, AssertFmt>where Out: TryFrom<In>, <Out as TryFrom<In>>::Error: Error + Send + Sync + 'static,

Fallibly convert the input value via TryFrom/TryInto.

This does the same thing as the try_into matcher.

Examples
use xpct::{expect, equal};

expect!(41u64)
    .try_into::<u32>()
    .to(equal(41u32));
source

pub fn into_inner(self) -> In

Consume this assertion and return the value passed to expect!.

If the value has been transformed by any matchers like be_ok or be_some, this returns the final value.

Examples
use xpct::{expect, be_some, equal};

let result: &str = expect!(Some("disco"))
    .to(be_some())
    .into_inner();
source

pub fn ctx(&self) -> &AssertFmt::Context

Get the context value associated with this assertion.

source

pub fn ctx_mut(&mut self) -> &mut AssertFmt::Context

Get a mutable reference to the context value associated with this assertion.

source

pub fn fmt(&self) -> &AssertFmt

Get the formatter for this assertion.

source

pub fn fmt_mut(&mut self) -> &mut AssertFmt

Get a mutable reference to the formatter for this assertion.

source§

impl<In, AssertFmt> Assertion<In, AssertFmt>where In: IntoIterator, AssertFmt: AssertionFormat,

source

pub fn iter_map<'a, Out>( self, func: impl Fn(In::Item) -> Out + 'a ) -> Assertion<IterMap<'a, In::Item, Out, In::IntoIter>, AssertFmt>

Infallibly map each value of an iterator by applying a function to it.

This does the same thing as the iter_map matcher.

Examples

This fails to compile if we try to pass items by reference.

use xpct::{be_some, every, expect};

let items = vec![Some("foo"), Some("bar")];

let output: Vec<&str> = expect!(&items)
    .to(every(be_some))
    .into_inner();

To fix that, we need to call Option::as_deref on each value.

use xpct::{be_some, every, expect};

let items = vec![Some("foo"), Some("bar")];

let output: Vec<&str> = expect!(&items)
    .iter_map(Option::as_deref)
    .to(every(be_some))
    .into_inner();
source

pub fn iter_try_map<'a, Out>( self, func: impl Fn(In::Item) -> Result<Out> + 'a ) -> Assertion<Vec<Out>, AssertFmt>

Fallibly map each value of an iterator by applying a function to it.

This does the same thing as the iter_try_map matcher.

Examples
use xpct::{expect, consist_of};

let small_integers: [u64; 2] = [41, 57];

expect!(small_integers)
    .iter_try_map(|value| Ok(u32::try_from(value)?))
    .to(consist_of([41u32, 57u32]));

Trait Implementations§

source§

impl<In: Debug, AssertFmt> Debug for Assertion<In, AssertFmt>where AssertFmt: AssertionFormat + Debug, AssertFmt::Context: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<In, AssertFmt> RefUnwindSafe for Assertion<In, AssertFmt>where AssertFmt: RefUnwindSafe, In: RefUnwindSafe, <AssertFmt as AssertionFormat>::Context: RefUnwindSafe,

§

impl<In, AssertFmt> Send for Assertion<In, AssertFmt>where AssertFmt: Send, In: Send, <AssertFmt as AssertionFormat>::Context: Send,

§

impl<In, AssertFmt> Sync for Assertion<In, AssertFmt>where AssertFmt: Sync, In: Sync, <AssertFmt as AssertionFormat>::Context: Sync,

§

impl<In, AssertFmt> Unpin for Assertion<In, AssertFmt>where AssertFmt: Unpin, In: Unpin, <AssertFmt as AssertionFormat>::Context: Unpin,

§

impl<In, AssertFmt> UnwindSafe for Assertion<In, AssertFmt>where AssertFmt: UnwindSafe, In: UnwindSafe, <AssertFmt as AssertionFormat>::Context: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.