// **************************************************************** // This is free software licensed under the NUnit license. You // may obtain a copy of the license as well as information regarding // copyright ownership at http://nunit.org/?p=license&r=2.4. // **************************************************************** using System; using System.Collections; using System.ComponentModel; using NUnit.Framework.Constraints; namespace NUnit.Framework { /// /// A set of Assert methods operationg on one or more collections /// public class CollectionAssert { #region Equals and ReferenceEquals /// /// The Equals method throws an AssertionException. This is done /// to make sure there is no mistake by calling this function. /// /// /// [EditorBrowsable(EditorBrowsableState.Never)] public static new bool Equals(object a, object b) { throw new AssertionException("Assert.Equals should not be used for Assertions"); } /// /// override the default ReferenceEquals to throw an AssertionException. This /// implementation makes sure there is no mistake in calling this function /// as part of Assert. /// /// /// public static new void ReferenceEquals(object a, object b) { throw new AssertionException("Assert.ReferenceEquals should not be used for Assertions"); } #endregion #region AllItemsAreInstancesOfType /// /// Asserts that all items contained in collection are of the type specified by expectedType. /// /// IEnumerable containing objects to be considered /// System.Type that all objects in collection must be instances of public static void AllItemsAreInstancesOfType (IEnumerable collection, Type expectedType) { AllItemsAreInstancesOfType(collection, expectedType, string.Empty, null); } /// /// Asserts that all items contained in collection are of the type specified by expectedType. /// /// IEnumerable containing objects to be considered /// System.Type that all objects in collection must be instances of /// The message that will be displayed on failure public static void AllItemsAreInstancesOfType (IEnumerable collection, Type expectedType, string message) { AllItemsAreInstancesOfType(collection, expectedType, message, null); } /// /// Asserts that all items contained in collection are of the type specified by expectedType. /// /// IEnumerable containing objects to be considered /// System.Type that all objects in collection must be instances of /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void AllItemsAreInstancesOfType (IEnumerable collection, Type expectedType, string message, params object[] args) { Assert.That(collection, new AllItemsConstraint(new InstanceOfTypeConstraint(expectedType)), message, args); } #endregion #region AllItemsAreNotNull /// /// Asserts that all items contained in collection are not equal to null. /// /// IEnumerable containing objects to be considered public static void AllItemsAreNotNull (IEnumerable collection) { AllItemsAreNotNull(collection, string.Empty, null); } /// /// Asserts that all items contained in collection are not equal to null. /// /// IEnumerable containing objects to be considered /// The message that will be displayed on failure public static void AllItemsAreNotNull (IEnumerable collection, string message) { AllItemsAreNotNull(collection, message, null); } /// /// Asserts that all items contained in collection are not equal to null. /// /// IEnumerable of objects to be considered /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void AllItemsAreNotNull (IEnumerable collection, string message, params object[] args) { Assert.That(collection, new AllItemsConstraint(new NotConstraint(new EqualConstraint(null))), message, args); } #endregion #region AllItemsAreUnique /// /// Ensures that every object contained in collection exists within the collection /// once and only once. /// /// IEnumerable of objects to be considered public static void AllItemsAreUnique (IEnumerable collection) { AllItemsAreUnique(collection, string.Empty, null); } /// /// Ensures that every object contained in collection exists within the collection /// once and only once. /// /// IEnumerable of objects to be considered /// The message that will be displayed on failure public static void AllItemsAreUnique (IEnumerable collection, string message) { AllItemsAreUnique(collection, message, null); } /// /// Ensures that every object contained in collection exists within the collection /// once and only once. /// /// IEnumerable of objects to be considered /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void AllItemsAreUnique (IEnumerable collection, string message, params object[] args) { Assert.That(collection, new UniqueItemsConstraint(), message, args); } #endregion #region AreEqual /// /// Asserts that expected and actual are exactly equal. The collections must have the same count, /// and contain the exact same objects in the same order. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered public static void AreEqual (IEnumerable expected, IEnumerable actual) { //AreEqual(expected, actual, null, string.Empty, null); Assert.That(actual, new EqualConstraint(expected)); } /// /// Asserts that expected and actual are exactly equal. The collections must have the same count, /// and contain the exact same objects in the same order. /// If comparer is not null then it will be used to compare the objects. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The IComparer to use in comparing objects from each IEnumerable public static void AreEqual (IEnumerable expected, IEnumerable actual, IComparer comparer) { AreEqual(expected, actual, comparer, string.Empty, null); } /// /// Asserts that expected and actual are exactly equal. The collections must have the same count, /// and contain the exact same objects in the same order. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The message that will be displayed on failure public static void AreEqual (IEnumerable expected, IEnumerable actual, string message) { //AreEqual(expected, actual, null, message, null); Assert.That(actual, new EqualConstraint(expected), message); } /// /// Asserts that expected and actual are exactly equal. The collections must have the same count, /// and contain the exact same objects in the same order. /// If comparer is not null then it will be used to compare the objects. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The IComparer to use in comparing objects from each IEnumerable /// The message that will be displayed on failure public static void AreEqual (IEnumerable expected, IEnumerable actual, IComparer comparer, string message) { AreEqual(expected, actual, comparer, message, null); } /// /// Asserts that expected and actual are exactly equal. The collections must have the same count, /// and contain the exact same objects in the same order. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void AreEqual (IEnumerable expected, IEnumerable actual, string message, params object[] args) { //AreEqual(expected, actual, null, message, args); Assert.That(actual, new EqualConstraint(expected), message, args); } /// /// Asserts that expected and actual are exactly equal. The collections must have the same count, /// and contain the exact same objects in the same order. /// If comparer is not null then it will be used to compare the objects. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The IComparer to use in comparing objects from each IEnumerable /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void AreEqual (IEnumerable expected, IEnumerable actual, IComparer comparer, string message, params object[] args) { Assert.That(actual, new EqualConstraint(expected).Comparer(comparer), message, args); } #endregion #region AreEquivalent /// /// Asserts that expected and actual are equivalent, containing the same objects but the match may be in any order. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered public static void AreEquivalent (IEnumerable expected, IEnumerable actual) { AreEquivalent(expected, actual, string.Empty, null); } /// /// Asserts that expected and actual are equivalent, containing the same objects but the match may be in any order. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The message that will be displayed on failure public static void AreEquivalent (IEnumerable expected, IEnumerable actual, string message) { AreEquivalent(expected, actual, message, null); } /// /// Asserts that expected and actual are equivalent, containing the same objects but the match may be in any order. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void AreEquivalent (IEnumerable expected, IEnumerable actual, string message, params object[] args) { Assert.That(actual, new CollectionEquivalentConstraint(expected), message, args); } #endregion #region AreNotEqual /// /// Asserts that expected and actual are not exactly equal. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered public static void AreNotEqual (IEnumerable expected, IEnumerable actual) { Assert.That(actual, new NotConstraint(new EqualConstraint(expected))); } /// /// Asserts that expected and actual are not exactly equal. /// If comparer is not null then it will be used to compare the objects. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The IComparer to use in comparing objects from each IEnumerable public static void AreNotEqual (IEnumerable expected, IEnumerable actual, IComparer comparer) { AreNotEqual(expected, actual, comparer, string.Empty, null); } /// /// Asserts that expected and actual are not exactly equal. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The message that will be displayed on failure public static void AreNotEqual (IEnumerable expected, IEnumerable actual, string message) { //AreNotEqual(expected, actual, null, message, null); //Assert.AreNotEqual( expected, actual, message ); Assert.That(actual, new NotConstraint(new EqualConstraint(expected)), message); } /// /// Asserts that expected and actual are not exactly equal. /// If comparer is not null then it will be used to compare the objects. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The IComparer to use in comparing objects from each IEnumerable /// The message that will be displayed on failure public static void AreNotEqual (IEnumerable expected, IEnumerable actual, IComparer comparer, string message) { AreNotEqual(expected, actual, comparer, message, null); } /// /// Asserts that expected and actual are not exactly equal. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void AreNotEqual (IEnumerable expected, IEnumerable actual, string message, params object[] args) { //AreNotEqual(expected, actual, null, message, args); //Assert.AreNotEqual( expected, actual, message, args ); Assert.That(actual, new NotConstraint(new EqualConstraint(expected)), message, args); } /// /// Asserts that expected and actual are not exactly equal. /// If comparer is not null then it will be used to compare the objects. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The IComparer to use in comparing objects from each IEnumerable /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void AreNotEqual (IEnumerable expected, IEnumerable actual, IComparer comparer, string message, params object[] args) { Assert.That(actual, new NotConstraint(new EqualConstraint(expected).Comparer(comparer)), message, args); } #endregion #region AreNotEquivalent /// /// Asserts that expected and actual are not equivalent. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered public static void AreNotEquivalent (IEnumerable expected, IEnumerable actual) { AreNotEquivalent(expected, actual, string.Empty, null); } /// /// Asserts that expected and actual are not equivalent. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The message that will be displayed on failure public static void AreNotEquivalent (IEnumerable expected, IEnumerable actual, string message) { AreNotEquivalent(expected, actual, message, null); } /// /// Asserts that expected and actual are not equivalent. /// /// The first IEnumerable of objects to be considered /// The second IEnumerable of objects to be considered /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void AreNotEquivalent (IEnumerable expected, IEnumerable actual, string message, params object[] args) { Assert.That(actual, new NotConstraint(new CollectionEquivalentConstraint(expected)), message, args); } #endregion #region Contains /// /// Asserts that collection contains actual as an item. /// /// IEnumerable of objects to be considered /// Object to be found within collection public static void Contains (IEnumerable collection, Object actual) { Contains(collection, actual, string.Empty, null); } /// /// Asserts that collection contains actual as an item. /// /// IEnumerable of objects to be considered /// Object to be found within collection /// The message that will be displayed on failure public static void Contains (IEnumerable collection, Object actual, string message) { Contains(collection, actual, message, null); } /// /// Asserts that collection contains actual as an item. /// /// IEnumerable of objects to be considered /// Object to be found within collection /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void Contains (IEnumerable collection, Object actual, string message, params object[] args) { Assert.That(collection, new CollectionContainsConstraint(actual), message, args); } #endregion #region DoesNotContain /// /// Asserts that collection does not contain actual as an item. /// /// IEnumerable of objects to be considered /// Object that cannot exist within collection public static void DoesNotContain (IEnumerable collection, Object actual) { DoesNotContain(collection, actual, string.Empty, null); } /// /// Asserts that collection does not contain actual as an item. /// /// IEnumerable of objects to be considered /// Object that cannot exist within collection /// The message that will be displayed on failure public static void DoesNotContain (IEnumerable collection, Object actual, string message) { DoesNotContain(collection, actual, message, null); } /// /// Asserts that collection does not contain actual as an item. /// /// IEnumerable of objects to be considered /// Object that cannot exist within collection /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void DoesNotContain (IEnumerable collection, Object actual, string message, params object[] args) { Assert.That(collection, new NotConstraint( new CollectionContainsConstraint( actual ) ), message, args); } #endregion #region IsNotSubsetOf /// /// Asserts that superset is not a subject of subset. /// /// The IEnumerable superset to be considered /// The IEnumerable subset to be considered public static void IsNotSubsetOf (IEnumerable subset, IEnumerable superset) { IsNotSubsetOf(subset, superset, string.Empty, null); } /// /// Asserts that superset is not a subject of subset. /// /// The IEnumerable superset to be considered /// The IEnumerable subset to be considered /// The message that will be displayed on failure public static void IsNotSubsetOf (IEnumerable subset, IEnumerable superset, string message) { IsNotSubsetOf(subset, superset, message, null); } /// /// Asserts that superset is not a subject of subset. /// /// The IEnumerable superset to be considered /// The IEnumerable subset to be considered /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void IsNotSubsetOf (IEnumerable subset, IEnumerable superset, string message, params object[] args) { Assert.That(subset, new NotConstraint(new CollectionSubsetConstraint(superset)), message, args); } #endregion #region IsSubsetOf /// /// Asserts that superset is a subset of subset. /// /// The IEnumerable superset to be considered /// The IEnumerable subset to be considered public static void IsSubsetOf (IEnumerable subset, IEnumerable superset) { IsSubsetOf(subset, superset, string.Empty, null); } /// /// Asserts that superset is a subset of subset. /// /// The IEnumerable superset to be considered /// The IEnumerable subset to be considered /// The message that will be displayed on failure public static void IsSubsetOf (IEnumerable subset, IEnumerable superset, string message) { IsSubsetOf(subset, superset, message, null); } /// /// Asserts that superset is a subset of subset. /// /// The IEnumerable superset to be considered /// The IEnumerable subset to be considered /// The message that will be displayed on failure /// Arguments to be used in formatting the message public static void IsSubsetOf (IEnumerable subset, IEnumerable superset, string message, params object[] args) { Assert.That(subset, new CollectionSubsetConstraint(superset), message, args); } #endregion #region IsEmpty /// /// Assert that an array, list or other collection is empty /// /// An array, list or other collection implementing IEnumerable /// The message to be displayed on failure /// Arguments to be used in formatting the message public static void IsEmpty(IEnumerable collection, string message, params object[] args) { Assert.That(collection, new EmptyConstraint(), message, args); } /// /// Assert that an array, list or other collection is empty /// /// An array, list or other collection implementing IEnumerable /// The message to be displayed on failure public static void IsEmpty(IEnumerable collection, string message) { IsEmpty(collection, message, null); } /// /// Assert that an array,list or other collection is empty /// /// An array, list or other collection implementing IEnumerable public static void IsEmpty(IEnumerable collection) { IsEmpty(collection, string.Empty, null); } #endregion #region IsNotEmpty /// /// Assert that an array, list or other collection is empty /// /// An array, list or other collection implementing IEnumerable /// The message to be displayed on failure /// Arguments to be used in formatting the message public static void IsNotEmpty(IEnumerable collection, string message, params object[] args) { Assert.That(collection, new NotConstraint(new EmptyConstraint()), message, args); } /// /// Assert that an array, list or other collection is empty /// /// An array, list or other collection implementing IEnumerable /// The message to be displayed on failure public static void IsNotEmpty(IEnumerable collection, string message) { IsNotEmpty(collection, message, null); } /// /// Assert that an array,list or other collection is empty /// /// An array, list or other collection implementing IEnumerable public static void IsNotEmpty(IEnumerable collection) { IsNotEmpty(collection, string.Empty, null); } #endregion } }