nrxus / faux

Struct mocking library for Rust
https://nrxus.github.io/faux/
MIT License
411 stars 14 forks source link

More matchers #34

Open nrxus opened 3 years ago

nrxus commented 3 years ago

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:

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

nrxus commented 3 years ago

custom closure was added: from_fn!: https://github.com/nrxus/faux/commit/a209091037a58cb10b9452148a3bfb7a10340a74

mbuscemi commented 3 years ago

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?

nrxus commented 3 years ago

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:

Let me know how could I have worded or organized the docs better to make this information more accessible.

mbuscemi commented 3 years ago

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.

nrxus commented 3 years ago

Good call, I'll be explicit about the when syntax on each matcher function, or at least link to it. Thanks!

nrxus commented 3 years ago

@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