Matches constraint

80 views
Skip to first unread message

Aaron Stambler

unread,
Feb 26, 2020, 2:05:22 PM2/26/20
to NUnit-Discuss
I find myself frequently using the Matches constraint.  However the current Constraints documentation (https://github.com/nunit/docs/wiki/Constraints) is silent with regard to Matches.  Is this just a gap in the documentation or is Matches not the way I should be doing things?

In the interest of full disclosure I may lean toward Matches because I had been using xUnit.net where most asserts take a lambda-type filter.  As far as I can tell in NUnit that is done with Matches.

eberhard

unread,
Feb 26, 2020, 3:22:25 PM2/26/20
to NUnit-Discuss

Did you see the docs for the RegexConstraint (https://github.com/nunit/docs/wiki/RegexConstraint)? That mentions Matches.

Charlie Poole

unread,
Feb 26, 2020, 4:15:04 PM2/26/20
to NUnit-Discuss
The use of "Matches" on the RegexConstraint is a remnant of an earlier
syntax and should be removed. The current syntax is "Does.Match(string
pattern)", which generates a RegexConstraint.

However, Aaron is talking about something else, the
PredicateConstraint, which allows matching an arbitrary predicate,
including lambdas. I once had great hopes for that approach as a way
to move us to a new syntax, but it never caught on. Currently, it
appears that PredicateConstraint still exists and Matches can be used
but only in the course of a constraint expression. That is, you can
use this...

Assert.That(123, Is.TypeOf<int>().And.Matches<int>((int x) => x > 100));

but you can't have the PredicateConstraint (easily) all by itself.

I'm not sure whether there was once more and it was removed or if the
full implementation was never done. I suggest creating an issue if you
would like to see something happen.

Charlie
> --
> You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discus...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nunit-discuss/d9d023b4-dd44-4795-87b0-20760bcbd863%40googlegroups.com.

Aaron Stambler

unread,
Feb 26, 2020, 5:22:58 PM2/26/20
to NUnit-Discuss
The issue I'd be inclined to create is to enhance the documentation to include Matches.

I'd start with documenting in the context of collections where I mostly (entirely?) use Matches:

Assert.That(someCollectionOfT, Has.Some.Matches<T>(eachT => eachT.SomeProperty == somOtherValue);

and

Assert.That(someCollectionOfT, Has.None.Matches<T>(...same idea);

Those work like a charm.  I would just document them.

If you agree I'm on a reasonable track I'll create an issue and even volunteer to write a first draft of the doc.

Aaron
> To unsubscribe from this group and stop receiving emails from it, send an email to nunit-...@googlegroups.com.

Charlie Poole

unread,
Feb 26, 2020, 6:58:01 PM2/26/20
to NUnit-Discuss
In terms of documentation, I think you've got it right with the
addition that I'd first create a page for PredicateConstraint itself,
which is completely undocumented.

But I do thin we need a way to use it outside of a fully formed
expression, which would be a code change as well. For example, we
might want "Does.Match()" to form a RegExConstraint if the argument is
a pattern or a PredicateConstraint if it's a Predicate or suitable
lambda. We do something like that already with "Is.Empty" which works
for an empty string or an empty collection.

I'll comment on the issue itself if you create one.

Charlie
> To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discus...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nunit-discuss/9f4489ff-5c8d-45b0-9fec-bcd8a0356632%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages