Enginerds

05/01/2009

Assert.That("NUnit", Has.Some.Matches("Fluent Interface Pattern")

Filed under: .NET arendus,Bookmarking — Mart Leet @ 16:40

Equal Constraint

To implement an equality assertion:

Assert.That(1 + 1, Is.EqualTo(2));

To implement an inequality assertion:

Assert.That(1 + 1, Is.Not.EqualTo(3));

To implement an equality assertion for floating point numbers and allow for a range of tolerance:

Assert.That(2.5000 + 2.5001, Is.EqualTo(5).Within(.0001));

To implement an equality assertion for strings in a case-insensitive manner:

Assert.That( "Hello", Is.EqualTo( "hello" ).IgnoreCase );

NOTE: In this section, I introduced the Is.Not "syntax helper". It, in effect, returns the opposite of its partner Is and it’s usable in almost all the places that Is…well… is.
Also, some of you may have noticed that the "syntax helpers" in the last three examples (e.g.: Is.EqualTo(5).Within(.0001) and Is.EqualTo( "hello" ).IgnoreCase) displayed a syntax of "chained" methods/properties. This syntax is popularly known under the name of Fluent Interfaces.


SameAs Constraint

To implement a "same referenced object" assertion:

Object o1 = new Object();

Object o2 = o1;

Assert.That(o1, Is.SameAs(o2));

Condition Constraints

To implement boolean assertions:

Assert.That(true, Is.True);

Assert.That(false, Is.False);

To implement "Not A Number" assertions:

Assert.That(Double.NaN, Is.NaN);

Assert.That(1, Is.Not.Nan);

To implement assertion for empty Strings or Collections:

Assert.That("", Is.Empty);

Assert.That(new ArrayList(), Is.Empty);

To implement assertion for Collections containing only unique items:

ArrayList al = new ArrayList();

al.Add(1);

al.Add(2);

Assert.That(al, Is.Unique);

Comparison Constraints

To implement comparison assertions:

Assert.That(7, Is.GreaterThan(3));

Assert.That(7, Is.GreaterThanOrEqualTo(3));

Assert.That(7, Is.AtLeast(3));

Assert.That(7, Is.GreaterThanOrEqualTo(7));

Assert.That(7, Is.AtLeast(7));

Assert.That(3, Is.LessThan(7));

Assert.That(3, Is.LessThanOrEqualTo(7));

Assert.That(3, Is.AtMost(7));

Assert.That(3, Is.LessThanOrEqualTo(3));

Assert.That(3, Is.AtMost(3));

Note: Is.AtLeast() is a synonym for Is.GreaterThanOrEqualTo() and Is.AtMost() is a synonym for Is.LessThanOrEqualTo().


Type Constraints

To implement an "exact type" assertion:

Hashtable ht = new Hashtable();

Assert.That(ht, Is.TypeOf(typeof(Hashtable)));

Assert.That(ht, Is.Not.TypeOf(typeof(IDictionary))); //not exact type

To implement an "instance of a type" assertion:

Hashtable ht = new Hashtable();

Assert.That(ht, Is.InstanceOfType(typeof(IDictionary)));

Assert.That(ht, Is.Not.InstanceOfType(typeof(String)));

To implement an "assignable from a type" assertion:

Hashtable ht = new Hashtable();

Assert.That(ht, Is.AssignableFrom(typeof(System.Configuration.SettingsContext))); //subclass

Assert.That(ht, Is.Not.AssignableFrom(typeof(IDictionary)));

String Constraints

To implement string assertions:

string phrase = "Make your tests fail before passing!";

Assert.That( phrase, Text.Contains( "tests fail" ) );

Assert.That( phrase, Text.Contains( "make" ).IgnoreCase );

Assert.That( phrase, Text.StartsWith( "Make" ) );

Assert.That( phrase, Text.DoesNotStartWith( "Break" ) );

Assert.That( phrase, Text.EndsWith( "!" ) );

Assert.That( phrase, Text.EndsWith( "PASSING!" ).IgnoreCase );

Assert.That( phrase, Text.Matches( "Make.*tests.*pass" ) );

Assert.That( phrase, Text.DoesNotMatch( "your.*passing.*tests" ) );

Collection Constraints

To implement a collection assertion where each item must pass the specified constraint:

int[] iarray = new int[] { 1, 2, 3 };

string[] sarray = new string[] { "a", "b", "c" };

Assert.That(iarray, Is.All.Not.Null);

Assert.That(sarray, Is.All.InstanceOfType(typeof(string)));

Assert.That(iarray, Is.All.GreaterThan(0));

To implement a collection assertion where at least 1 item must pass the specified constraint:

int[] iarray = new int[] { 1, 2, 3 };

string[] sarray = new string[] { "a", "b", "c" };

Assert.That(sarray, Has.Some.Not.Null);

Assert.That(sarray, Has.Some.InstanceOfType(typeof(string)));

Assert.That(iarray, Has.Some.GreaterThan(2));

To implement a collection assertion where each item must fail the specified constraint:

int[] iarray = new int[] { 1, 2, 3 };

string[] sarray = new string[] { "a", "b", "c" };

Assert.That(sarray, Has.None.Null);

Assert.That(sarray, Has.None.InstanceOfType(typeof(double)));

Assert.That(iarray, Has.None.GreaterThan(5));

NOTE: List.Contains() has been replaced by Has.Member() in version 2.4.2.
To implement a "unique items only" collection assertion:

int[] iarray = new int[] { 1, 2, 3 };

string[] sarray = new string[] { "a", "b", "c" };

Assert.That(sarray, Is.Unique);

To implement a "must contain this item" collection assertion:

int[] iarray = new int[] { 1, 2, 3 };

string[] sarray = new string[] { "a", "b", "c" };

Assert.That(iarray, Has.Member(3));

Assert.That(sarray, Has.Member("b"));

Assert.That(sarray, Has.No.Member("x"));

NOTE: Has.Member() uses object equality to find an object in a collection. To check for an object equal to an item the collection, use Has.Some.EqualTo().
To implement an "equivalent" collection assertion:

int[] iarray = new int[] { 1, 2, 3 };

string[] sarray = new string[] { "a", "b", "c" };

Assert.That(new string[] { "c", "a", "b" }, Is.EquivalentTo(sarray));

Assert.That(new int[] { 1, 2, 2 }, Is.Not.EquivalentTo(iarray));

NOTE: 2 collections are equivalent if they contain the same items, in any order. To compare collections for equality, use Is.EqualTo()
To implement an "is a subset of" collection assertion:

int[] iarray = new int[] { 1, 2, 3 };

string[] sarray = new string[] { "a", "b", "c" };

Assert.That(new int[] { 1, 3 }, Is.SubsetOf(iarray));

29/10/2008

C# Kollane raamat

Filed under: .NET arendus — kris @ 18:51

 

image Kuivõrd ma olen Rob Milesi tulihingeline fänn, siis viitaksin ära temalt ilmunud raamatu:

Ma pole seda raamatut lugenud ja ilmselt sisukorra järgi on see suhteliselt ABC123 tüüpi. Ometi soovitan, just kasutada abimaterjalina, sest viis kuidas Rob esitlustel näiteid toob, on lihtsalt esmaklassiline.

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.