## Collection Constraints (NUnit 2.4 / 2.5)

Collection constraints perform tests that are specific to collections. The following collection constraints are provided. Before NUnit 2.4.6, these constraints only operated on true Collections. Beginning with 2.4.6, they can work with any object that implements IEnumerable.

Beginning with NUnit 2.4.2, use of an improper argument type caused tests to fail. Later releases give an error rather than a failure, so that negated constraints will not appear to succeed.

For example, both of the following statements give an error in later releases, but the second would have succeeded in earlier versions of NUnit.

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

    Assert.That( 5, Is.SubsetOf( iarray ) );  // Fails in early releases
    Assert.That( 5, Is.Not.SubsetOf( iarray ) ); /

### AllItemsConstraint

#### Action

Applies a constraint to each item in a collection, succeeding only if all of them succeed.

#### Constructor

    AllItemsConstraint(Constraint itemConstraint)

#### Syntax

    Is.All...
    Has.All...

#### Examples of Use

    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.InstanceOf() );
    Assert.That( iarray, Is.All.GreaterThan(0) );
    Assert.That( iarray, Has.All.GreaterThan(0) );

### SomeItemsConstraint

#### Action

Applies a constraint to each item in a collection, succeeding if at least one of them succeeds.

#### Constructor

    SomeItemsConstraint(Constraint itemConstraint)

#### Syntax

    Has.Some...

#### Examples of Use

    int[] iarray = new int[] { 1, 2, 3 };
    string[] sarray = new string[] { "a", "b", "c" };

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

### NoItemConstraint

#### Action

Applies a constraint to each item in a collection, succeeding only if all of them fail.

#### Constructor

    NoItemConstraint(Constraint itemConstraint)

#### Syntax

    Has.None...
    Has.No...

#### Examples of Use

    int[] iarray = new int[] { 1, 2, 3 };
    string[] sarray = new string[] { "a", "b", "c" };

    Assert.That( iarray, Has.None.Null );
    Assert.That( iarray, Has.No.Null );
    Assert.That( sarray, Has.None.EqualTo("d") );
    Assert.That( iarray, Has.None.LessThan(0) );

### UniqueItemsConstraint

#### Action

Tests that a collection contains only unique items.

#### Constructor

    UniqueItemsConstraint()

#### Syntax

    Is.Unique

#### Example of Use

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

    Assert.That( sarray, Is.Unique );

#### Notes

1. ??

### CollectionContainsConstraint

#### Action

Tests that a collection contains an object.

#### Constructor

    CollectionContainsConstraint( object )

#### Syntax

    Has.Member( object )
    Contains.Item( object )

#### Modifiers

    ...Using(IComparer comparer)
    ...Using(IComparer<T> comparer)
    ...Using(Comparison<T> comparer)

#### Examples of Use

    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, Contains.Item("c") );
    Assert.That( sarray, Has.No.Member("x") );

#### Notes

1. For references, Has.Member uses object equality to find a member in a collection. To check for an object equal to an item the collection, use Has.Some.EqualTo(...).

### CollectionEquivalentConstraint

#### Action

Tests that two collections are equivalent - that they contain the same items, in any order.

#### Constructor

    CollectionEquivalentConstraint( IEnumerable other )

#### Syntax

    Is.EquivalentTo( IEnumerable other )

#### Examples of Use

    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 ) );

#### Notes

1. To compare collections for equality, use Is.EqualTo().

### CollectionSubsetConstraint

#### Action

Tests that one collection is a subset of another.

#### Constructor

    CollectionSubsetConstraint( ICollection )

#### Syntax

    Is.SubsetOf( IEnumerable )

#### Example of Use

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

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

### CollectionOrderedConstraint (NUnit 2.5)

#### Action

Tests that a collection is ordered.

#### Constructor

    CollectionOrderedConstraint()

#### Syntax

    Is.Ordered

#### Modifiers

    ...Descending
    ...By(string propertyName)
    ...Using(IComparer comparer)
    ...Using(IComparer<T> comparer)
    ...Using(Comparison<T> comparer)

#### Examples of Use

    int[] iarray = new int[] { 1, 2, 3 };
    string[] sarray = new string[] { "c", "b", "a" };
    string[] sarray2 = new string[] ( "a", "aa", "aaa" );

    Assert.That( iarray, Is.Ordered );
    Assert.That( sarray, Is.Ordered.Descending );
    Assert.That( sarray2, Is.Ordered.By("Length");

#### Notes

1. Modifiers may be combined and may appear in any order. If the same modifier is used more than once, the result is undefined.
2. The syntax of Is.Ordered has changed from earlier betas.