1 // ***********************************************************************
2 // Copyright (c) 2007 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 using System.Collections;
32 namespace NUnit.Framework.Constraints
35 /// CollectionConstraint is the abstract base class for
36 /// constraints that operate on collections.
38 public abstract class CollectionConstraint : Constraint
41 /// Construct an empty CollectionConstraint
43 protected CollectionConstraint() { }
46 /// Construct a CollectionConstraint
48 /// <param name="arg"></param>
49 protected CollectionConstraint(object arg) : base(arg) { }
52 /// Determines whether the specified enumerable is empty.
54 /// <param name="enumerable">The enumerable.</param>
56 /// <c>true</c> if the specified enumerable is empty; otherwise, <c>false</c>.
58 protected static bool IsEmpty(IEnumerable enumerable)
60 ICollection collection = enumerable as ICollection;
61 if (collection != null)
62 return collection.Count == 0;
64 foreach (object o in enumerable)
71 /// Test whether the constraint is satisfied by a given value
73 /// <param name="actual">The value to be tested</param>
74 /// <returns>True for success, false for failure</returns>
75 public override ConstraintResult ApplyTo<TActual>(TActual actual)
77 // TODO: Use an error result if actual is not IEnumerable
78 IEnumerable enumerable = actual as IEnumerable;
79 if (enumerable == null)
80 throw new ArgumentException("The actual value must be an IEnumerable", "actual");
81 //return new ConstraintResult(this, actual, "The actual value must be an IEnumerable");
83 return new ConstraintResult(this, actual, Matches(enumerable));
87 /// Protected method to be implemented by derived classes
89 /// <param name="collection"></param>
90 /// <returns></returns>
91 protected abstract bool Matches(IEnumerable collection);