faux
faux copied to clipboard
More matchers
The only implemented matchers at the moment are:
Any: matches any argument
Eq matches based on Arg: PartialEq
EqAgainst matches based on Arg: PartialEq<Against>
We could implement other matchers so faux users don't have to handwrite their own matchers.
Quick ideas:
<<=>>=!||&&- contains
- ~custom closure. Unsure if this is all that much better than a just implementing
ArgMatcherthough.~
We can first decide what matchers to create and then see if when! can come up with an easy to use syntax for it.
cc: @muscovite
custom closure was added: from_fn!: https://github.com/nrxus/faux/commit/a209091037a58cb10b9452148a3bfb7a10340a74
It's unclear to me from the docs how to use custom matcher, particularly the any() matcher. I have the following method:
db_service.table.insert(itemA: &String, itemB: &String, itemC: &String)
I care about matching A, but not B or C (which are the results of hashing and random number generation respectively). I have tried this:
when!(db_servier.table.insert(itemA, matcher::any(), matcher::any())).then_return(Result::default());
But this generates multiple errors:
`impl ArgMatcher<_>` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
no implementation for `impl ArgMatcher<_> == impl ArgMatcher<_>`
the trait `Borrow<impl ArgMatcher<_>>` is not implemented for `&std::string::String`
If these matchers cannot be inlined inside when!, how should they be implemented?
Ah! I tried explaining that in the docs but it looks like it was either confusing or hard to find.
For your specific use case, to ignore arguments you can use _.
In the general case, argument matchers are either:
- Equality match: pass the argument to equal against
- Any match: pass
_ - Anything else:
_ = {matcher}. The "matcher" here is any expression that returns a matcher so it could befaux::any(), orfaux::pattern(/* some pattern to match against */), etc.
Let me know how could I have worded or organized the docs better to make this information more accessible.
Ah. I was scouring the Matcher page and all its structs, traits, and functions. I think a mention on any one of them that the when macro encodes them differently would be enough, with a link to when's documentation would suffice.
Good call, I'll be explicit about the when syntax on each matcher function, or at least link to it. Thanks!
@mbuscemi I have updated the docs to hopefully make it more clear for the future: https://docs.rs/faux/0.1.3/faux/matcher/fn.any.html