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§

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.

Make an assertion with the given matcher.

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"));

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"));

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"));

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));

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));

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())
    .to(equal("disco"))
    .into_inner();

Get the context value associated with this assertion.

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

Get the formatter for this assertion.

Get a mutable reference to the formatter for this assertion.

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();

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§

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.