pub trait ArgMatcher<Arg: ?Sized>: Display {
// Required method
fn matches(&self, argument: &Arg) -> bool;
// Provided method
fn into_ref_matcher(self) -> RefMatcher<Self>
where Self: Sized { ... }
}Expand description
Matcher for single argument of a method.
Implementors provide an expectation to match an argument against.
faux provides some simple matchers: any(), eq(), and
eq_against(). Additionally, faux also provides two macros:
pattern! for pattern matching and
from_fn! to provide a custom function.
You may define your own matcher for special use cases. The
fmt::Display implementation is used by InvocationMatcher
to display the expectation when any arguments failed to match.
§Examples
use std::fmt::{self, Formatter};
use faux::ArgMatcher;
struct HasLength(usize);
// displayed as the expectation when any argument fails to match
// when used by an `InvocationMatcher`
impl fmt::Display for HasLength {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "_.len() == {}", self.0)
}
}
impl <T> ArgMatcher<&[T]> for HasLength {
// matches takes a reference to the argument
// the argument is &[T] so it takes &&[T]
fn matches(&self, argument: &&[T]) -> bool {
argument.len() == self.0
}
}
let has_three_length = HasLength(3);
let vec = vec![56, 78, 12, 43, 23];
assert!(has_three_length.matches(&&vec[..3]));
assert!(!has_three_length.matches(&&vec[1..]));
Required Methods§
Provided Methods§
Sourcefn into_ref_matcher(self) -> RefMatcher<Self>where
Self: Sized,
fn into_ref_matcher(self) -> RefMatcher<Self>where
Self: Sized,
Converts the Argmatcher<Arg> into an ArgMatcher<&Arg> to
test against the reference of the argument.