phuccaoca123 / mb-unit

Automatically exported from code.google.com/p/mb-unit
0 stars 0 forks source link

Convert all MbUnit Asserts to constraint-style to encourage reuse. #62

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Charlie Poole has a good idea using constraints in NUnit.  The Assert.That
syntax is pretty good.  More than that, I like the reuse that occurs among
constraints and the resulting reduction in code weight in the Assert classes.

I envision an automatic translation between C# 3.0 lambda expressions and
constraints so that we can largely reuse them there too.

After trying out several different alternatives for redesigning the
assertions to be more maintainable and consistent, I've settled on taking
NUnit's lead.  With some twists...

I want to make it easier to define these constraints and reduce the
boilerplate to a minimum.  Still working on that.

Original issue reported on code.google.com by jeff.br...@gmail.com on 20 Sep 2007 at 6:54

GoogleCodeExporter commented 9 years ago

Original comment by jeff.br...@gmail.com on 25 Sep 2007 at 2:18

GoogleCodeExporter commented 9 years ago

Original comment by jeff.br...@gmail.com on 30 Oct 2007 at 2:24

GoogleCodeExporter commented 9 years ago
A comment by Aaron Jensen on the ALT.Net mailing like:

I want better syntax. NUnit's new syntax is close, but missed the mark a bit. I 
want
this:

Assert.That(foo).IsEqualTo(bar);
Assert.That(foo).IsLessThan(bar);
Assert.That(fooList).Contains(bar);
Assert.That(fooList).IsEmpty();

Or we can use extension methods to move a parenthesis over:
Assert.That(foo.IsEqualTo(bar));

But that's not as safe and won't work in .NET 2.0. I prefer the first even 
though I
know it's kind of dirty to have a method that requires you to chain with little 
or no
way to check it (I suppose the runner could check for a "dangling" That()), but 
I
just love how that code reads. Also, once 3.5 rolls around I can add my own 
extension
methods to That:

Assert.That(foo).IsPrime();

You can also do better context sensitive things w/ this syntax like I showed in 
the
list code up there. Just define the methods like so: 

    IAsserter<T> That<T>(T subject);
    IFloatAsserter That(float subject);
    IListAsserter That(IList subject);
    IGenericListAsserter<T> That<T>(IList<T> subject);

And yea, I realize it'd be relatively easy to write a wrapper for this, but 
hey, why
not suggest it while you're asking :)

Aaron

Original comment by jeff.br...@gmail.com on 1 Nov 2007 at 7:37

GoogleCodeExporter commented 9 years ago
I wrote some quick code and did a mockup of this back in early August .. but an
extended version that supports assertion extension methods, for things like
consistent message formatting etc. It's fairly easy to implement using generics 
and
implied typing by the compilerg, like this ..

Assert.That(1).IsEqualTo(5);
Assert.That("string").IsEmpty.WithMessage("String is not empty");
Assert.That(new string[] {"a", "b", "c"} as
ICollection<string>).Contains("b").WithMessage("String list does not contain
value").ButDontFail();

I've attached the example code ..

Original comment by d...@davegriffiths.net on 1 Nov 2007 at 12:46

Attachments:

GoogleCodeExporter commented 9 years ago
Dru Sellers:

Do you have a snap shot of the JUnit Eclipse plugin?
In the GUI (MbUnit at least)

instead of:
Message:  Equal assertion failed: [[]]!=[[
]]

use:
Message:  Equal assertion failed: [[]]!=[[\n]]

Also the '[[' and ']]' are a lot of noise (chart junk) when whats wrong is near 
the
beginning or the end of the string.
I like in NUnit how it points to the bad piece as well.

'asteouatoneuhatoeua' 
---------------^

Original comment by jeff.br...@gmail.com on 1 Nov 2007 at 9:56

GoogleCodeExporter commented 9 years ago

Original comment by jeff.br...@gmail.com on 6 Jan 2008 at 7:17

GoogleCodeExporter commented 9 years ago

Original comment by jeff.br...@gmail.com on 6 Apr 2008 at 2:24

GoogleCodeExporter commented 9 years ago
Also Assert.With and Assert.Multiple.

Original comment by jeff.br...@gmail.com on 8 Apr 2008 at 7:17

GoogleCodeExporter commented 9 years ago
Won't do this after all.
It seems few people actually like NUnit's Assert.That syntax.

What's more, it's possible to compose assertions as if they were constraints if 
you
make them context-sensitive.  For example, we can establish a context within 
which we
record the result of the assertion without actually throwing an exception.  
This is
done and solves most of the problems without requiring yet another wacky 
constraint
syntax.

However, for .Net 3.5 we'll use lambdas to their fullest extent.  This has 
already
been implemented and tested.

Original comment by jeff.br...@gmail.com on 29 Jul 2008 at 2:25