Re: Writing custom constraints

"Brad Stiles" <[email protected]> Thu, 21 Aug 2008 20:56:31 -0400
Newsgroups gmane.comp.windows.dotnet.nunit.user
Message-ID <[email protected]>
The threading on this message will probably be messed up.  I apparently
missed your reply, and it's not in my inbox anywhere.  Hopefully, the
list will adjust and thread it properly, but if not, I at least used the
same subject.

> See http://nunit.org/index.php?p=customConstraints&r=2.4.8

> It's very basic but intellisense and experimentation should
> give you the rest.

I surmised as much, based on examining the source for a couple of
constraints, including the abstract base.  I think "elegant" describes
it.  Much simpler than I thought it would be.

> Note that this allows writing a custom constraint only. You
> could use it like Assert.That(x, new MyCustomConstraint());

Yes, that's all that's really needed right now.  I'm investigating
whether or not it's worth it to write custom constraints for some of our
custom structs.  I'm also toying with the idea of a constraint that
reflects over the members of simple structs, i.e. ones with all
primitive values, to compare.  We use a fair number of structs, which we
use as return values from various web services, and it'll save some time
to write one constraint, instead of one for each.  Haven't actually
*tried* it, but it *sounds* like it might work. :)

> 1. If you work exclusively in C# 3.0 use extension methods.

Thought of that too, but not all of our projects are C# 3.0.  'Course,
that doesn't really matter if I just say "in order to use this, at least
your unit test project must be C# 3.0," since it's our general practice
to put the tests in a separate project in the same solution.  MSBuild
seems to handle it OK.  Might be a good excuse to upgrade existing ones
when they need it, too. :)

> For 2.5, I'm working to make the syntax layer extensible
> just like the constraints.

I'm using the current 2.5 Alpha for this effort, in case there's
something in there I might be able to use...

Brad


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/