Function faux::matcher::eq [−][src]
pub fn eq<Arg, Expected>(expected: Expected) -> impl ArgMatcher<Arg> where
Arg: Borrow<Expected>,
Expected: Debug + PartialEq,
Expand description
Returns an equality matcher.
Returns a matcher that compares Arg
using equality across
borrows.
Unlike eq_against
, it only allows equality matching for the
same type. This comes at the benefit of being able to match across
borrows.
Examples
Basic usage
use faux::matcher::{self, ArgMatcher};
let eq_four = matcher::eq(4);
assert!(eq_four.matches(&4));
assert!(!eq_four.matches(&5));
Matching across borrows
use std::rc::Rc;
use faux::matcher::{self, ArgMatcher};
// `Rc<T>` implements `Borrow<T>`
assert!(matcher::eq(5).matches(&Rc::new(5)));
// `&T` implements `Borrow<T>`
let ref_of_ref: &&i32 = &&5;
assert!(matcher::eq(5).matches(ref_of_ref));
Usage within when!
For convenience, faux::when!
defaults to the
eq
matcher. See the matcher
syntax for more
information.
ⓘ
// `_` means the `any` matcher
faux::when!(my_struct.some_method(2)).then_return(5);
// we can also call it manually within `when!`
faux::when!(my_struct.some_method(_ = faux::matcher::eq(2)))
.then_return(5);
// or call it manually outside `when!`
faux::when!(my_struct.some_method)
.with_args((matcher::eq(2),)).then_return(5);