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 DynTransformMatch<In = In, PosOut = Out>
) -> Assertion<Out, AssertFmt>
pub fn to<Out>( self, matcher: impl DynTransformMatch<In = In, PosOut = Out> ) -> Assertion<Out, AssertFmt>
Make an assertion with the given matcher
.
sourcepub fn to_not<Out>(
self,
matcher: impl DynTransformMatch<In = In, NegOut = Out>
) -> Assertion<Out, AssertFmt>
pub fn to_not<Out>( self, matcher: impl DynTransformMatch<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_map<Out>(
self,
func: impl FnOnce(In) -> Result<Out>
) -> Assertion<Out, AssertFmt>
pub fn try_map<Out>( self, func: impl FnOnce(In) -> Result<Out> ) -> Assertion<Out, AssertFmt>
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
sourcepub fn ctx_mut(&mut self) -> &mut AssertFmt::Context
pub fn ctx_mut(&mut self) -> &mut AssertFmt::Context
Get a mutable reference to the context value associated with this assertion.
sourcepub fn fmt(&self) -> &AssertFmt
pub fn fmt(&self) -> &AssertFmt
Get the formatter for this assertion.
sourcepub fn fmt_mut(&mut self) -> &mut AssertFmt
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,
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]));