pub struct Assertion<In, AssertFmt>where
AssertFmt: AssertionFormat,{ /* private fields */ }
Expand description
Implementations§
source§impl<In, AssertFmt> Assertion<In, AssertFmt>where
AssertFmt: AssertionFormat + Default,
impl<In, AssertFmt> Assertion<In, AssertFmt>where
AssertFmt: AssertionFormat + Default,
sourcepub fn new(value: In, ctx: AssertFmt::Context) -> Self
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,
impl<In, AssertFmt> Assertion<In, AssertFmt>where
AssertFmt: AssertionFormat,
sourcepub fn to<Out>(
self,
matcher: impl DynMatch<In = In, PosOut = Out>
) -> Assertion<Out, AssertFmt>
pub fn to<Out>(
self,
matcher: impl DynMatch<In = In, PosOut = Out>
) -> Assertion<Out, AssertFmt>
Make an assertion with the given matcher
.
sourcepub fn to_not<Out>(
self,
matcher: impl DynMatch<In = In, NegOut = Out>
) -> Assertion<Out, AssertFmt>
pub fn to_not<Out>(
self,
matcher: impl DynMatch<In = In, NegOut = Out>
) -> Assertion<Out, AssertFmt>
sourcepub fn map<Out>(self, func: impl FnOnce(In) -> Out) -> Assertion<Out, AssertFmt>
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"));
sourcepub fn try_into<Out>(self) -> Assertion<Out, AssertFmt>where
Out: TryFrom<In>,
<Out as TryFrom<In>>::Error: Error + Send + Sync + 'static,
pub fn try_into<Out>(self) -> Assertion<Out, AssertFmt>where
Out: TryFrom<In>,
<Out as TryFrom<In>>::Error: Error + Send + Sync + 'static,
sourcepub fn into_inner(self) -> In
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())
.to(equal("disco"))
.into_inner();
source§impl<In, AssertFmt> Assertion<In, AssertFmt>where
In: IntoIterator,
AssertFmt: AssertionFormat,
impl<In, AssertFmt> Assertion<In, AssertFmt>where
In: IntoIterator,
AssertFmt: AssertionFormat,
sourcepub fn iter_map<'a, Out>(
self,
func: impl Fn(In::Item) -> Out + 'a
) -> Assertion<IterMap<'a, In::Item, Out, In::IntoIter>, AssertFmt>
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();
sourcepub fn iter_try_map<'a, Out>(
self,
func: impl Fn(In::Item) -> Result<Out> + 'a
) -> Assertion<Vec<Out>, AssertFmt>
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]));