Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More matchers #34

Open
nrxus opened this issue Mar 21, 2021 · 6 comments
Open

More matchers #34

nrxus opened this issue Mar 21, 2021 · 6 comments

Comments

@nrxus
Copy link
Owner

nrxus commented Mar 21, 2021

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 ArgMatcher though.

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
Copy link
Owner Author

nrxus commented Apr 9, 2021

custom closure was added: from_fn!: a209091

@mbuscemi
Copy link

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
Copy link
Owner Author

nrxus commented May 15, 2021

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 be faux::any(), or faux::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.

@mbuscemi
Copy link

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
Copy link
Owner Author

nrxus commented May 17, 2021

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

@nrxus
Copy link
Owner Author

nrxus commented Jun 17, 2021

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants