Macro faux::from_fn

source ·
macro_rules! from_fn {
    ($matcher:expr) => { ... };
}
Expand description

Returns an ArgMatcher that succeeds if the provided closure returns true.

The returned Argmatcher implements fmt::Display using the string representation of the closure, so it is only recommended for use with simple closures. For complex argument matching, implement your own ArgMatcher to make the expectation message more specific and less verbose.

Examples

use faux::{from_fn, matcher::ArgMatcher};

let contains_hello = from_fn!(|message: &str| message.contains("hello"));
assert!(contains_hello.matches("hello world"));
assert!(!contains_hello.matches("bye world"));
println!("{}", contains_hello); // '|message: &str| message.contains("hello")'

Usage within when!

faux::when! does not have a special syntax for this matcher. See the matcher syntax for more information.

// we can call it within `when!`
faux::when!(my_struct.some_method(_ = faux::from_fn!(|_: &i32| true)))
    .then_return(5);

// or call outside `when!`
faux::when!(my_struct.some_method)
    .with_args((faux::from_fn!(|_: &i32| true),)).then_return(5);