1 // ****************************************************************
\r
2 // This is free software licensed under the NUnit license. You
\r
3 // may obtain a copy of the license as well as information regarding
\r
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.
\r
5 // ****************************************************************
\r
8 using System.Collections;
\r
9 using System.ComponentModel;
\r
10 using NUnit.Framework.Constraints;
\r
12 namespace NUnit.Framework
\r
15 /// A set of Assert methods operationg on one or more collections
\r
17 public class CollectionAssert
\r
19 #region Equals and ReferenceEquals
\r
22 /// The Equals method throws an AssertionException. This is done
\r
23 /// to make sure there is no mistake by calling this function.
\r
25 /// <param name="a"></param>
\r
26 /// <param name="b"></param>
\r
27 [EditorBrowsable(EditorBrowsableState.Never)]
\r
28 public static new bool Equals(object a, object b)
\r
30 throw new AssertionException("Assert.Equals should not be used for Assertions");
\r
34 /// override the default ReferenceEquals to throw an AssertionException. This
\r
35 /// implementation makes sure there is no mistake in calling this function
\r
36 /// as part of Assert.
\r
38 /// <param name="a"></param>
\r
39 /// <param name="b"></param>
\r
40 public static new void ReferenceEquals(object a, object b)
\r
42 throw new AssertionException("Assert.ReferenceEquals should not be used for Assertions");
\r
47 #region AllItemsAreInstancesOfType
\r
49 /// Asserts that all items contained in collection are of the type specified by expectedType.
\r
51 /// <param name="collection">IEnumerable containing objects to be considered</param>
\r
52 /// <param name="expectedType">System.Type that all objects in collection must be instances of</param>
\r
53 public static void AllItemsAreInstancesOfType (IEnumerable collection, Type expectedType)
\r
55 AllItemsAreInstancesOfType(collection, expectedType, string.Empty, null);
\r
59 /// Asserts that all items contained in collection are of the type specified by expectedType.
\r
61 /// <param name="collection">IEnumerable containing objects to be considered</param>
\r
62 /// <param name="expectedType">System.Type that all objects in collection must be instances of</param>
\r
63 /// <param name="message">The message that will be displayed on failure</param>
\r
64 public static void AllItemsAreInstancesOfType (IEnumerable collection, Type expectedType, string message)
\r
66 AllItemsAreInstancesOfType(collection, expectedType, message, null);
\r
70 /// Asserts that all items contained in collection are of the type specified by expectedType.
\r
72 /// <param name="collection">IEnumerable containing objects to be considered</param>
\r
73 /// <param name="expectedType">System.Type that all objects in collection must be instances of</param>
\r
74 /// <param name="message">The message that will be displayed on failure</param>
\r
75 /// <param name="args">Arguments to be used in formatting the message</param>
\r
76 public static void AllItemsAreInstancesOfType (IEnumerable collection, Type expectedType, string message, params object[] args)
\r
78 Assert.That(collection, new AllItemsConstraint(new InstanceOfTypeConstraint(expectedType)), message, args);
\r
82 #region AllItemsAreNotNull
\r
85 /// Asserts that all items contained in collection are not equal to null.
\r
87 /// <param name="collection">IEnumerable containing objects to be considered</param>
\r
88 public static void AllItemsAreNotNull (IEnumerable collection)
\r
90 AllItemsAreNotNull(collection, string.Empty, null);
\r
94 /// Asserts that all items contained in collection are not equal to null.
\r
96 /// <param name="collection">IEnumerable containing objects to be considered</param>
\r
97 /// <param name="message">The message that will be displayed on failure</param>
\r
98 public static void AllItemsAreNotNull (IEnumerable collection, string message)
\r
100 AllItemsAreNotNull(collection, message, null);
\r
104 /// Asserts that all items contained in collection are not equal to null.
\r
106 /// <param name="collection">IEnumerable of objects to be considered</param>
\r
107 /// <param name="message">The message that will be displayed on failure</param>
\r
108 /// <param name="args">Arguments to be used in formatting the message</param>
\r
109 public static void AllItemsAreNotNull (IEnumerable collection, string message, params object[] args)
\r
111 Assert.That(collection, new AllItemsConstraint(new NotConstraint(new EqualConstraint(null))), message, args);
\r
115 #region AllItemsAreUnique
\r
118 /// Ensures that every object contained in collection exists within the collection
\r
119 /// once and only once.
\r
121 /// <param name="collection">IEnumerable of objects to be considered</param>
\r
122 public static void AllItemsAreUnique (IEnumerable collection)
\r
124 AllItemsAreUnique(collection, string.Empty, null);
\r
128 /// Ensures that every object contained in collection exists within the collection
\r
129 /// once and only once.
\r
131 /// <param name="collection">IEnumerable of objects to be considered</param>
\r
132 /// <param name="message">The message that will be displayed on failure</param>
\r
133 public static void AllItemsAreUnique (IEnumerable collection, string message)
\r
135 AllItemsAreUnique(collection, message, null);
\r
139 /// Ensures that every object contained in collection exists within the collection
\r
140 /// once and only once.
\r
142 /// <param name="collection">IEnumerable of objects to be considered</param>
\r
143 /// <param name="message">The message that will be displayed on failure</param>
\r
144 /// <param name="args">Arguments to be used in formatting the message</param>
\r
145 public static void AllItemsAreUnique (IEnumerable collection, string message, params object[] args)
\r
147 Assert.That(collection, new UniqueItemsConstraint(), message, args);
\r
154 /// Asserts that expected and actual are exactly equal. The collections must have the same count,
\r
155 /// and contain the exact same objects in the same order.
\r
157 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
158 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
159 public static void AreEqual (IEnumerable expected, IEnumerable actual)
\r
161 //AreEqual(expected, actual, null, string.Empty, null);
\r
162 Assert.That(actual, new EqualConstraint(expected));
\r
166 /// Asserts that expected and actual are exactly equal. The collections must have the same count,
\r
167 /// and contain the exact same objects in the same order.
\r
168 /// If comparer is not null then it will be used to compare the objects.
\r
170 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
171 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
172 /// <param name="comparer">The IComparer to use in comparing objects from each IEnumerable</param>
\r
173 public static void AreEqual (IEnumerable expected, IEnumerable actual, IComparer comparer)
\r
175 AreEqual(expected, actual, comparer, string.Empty, null);
\r
179 /// Asserts that expected and actual are exactly equal. The collections must have the same count,
\r
180 /// and contain the exact same objects in the same order.
\r
182 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
183 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
184 /// <param name="message">The message that will be displayed on failure</param>
\r
185 public static void AreEqual (IEnumerable expected, IEnumerable actual, string message)
\r
187 //AreEqual(expected, actual, null, message, null);
\r
188 Assert.That(actual, new EqualConstraint(expected), message);
\r
192 /// Asserts that expected and actual are exactly equal. The collections must have the same count,
\r
193 /// and contain the exact same objects in the same order.
\r
194 /// If comparer is not null then it will be used to compare the objects.
\r
196 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
197 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
198 /// <param name="comparer">The IComparer to use in comparing objects from each IEnumerable</param>
\r
199 /// <param name="message">The message that will be displayed on failure</param>
\r
200 public static void AreEqual (IEnumerable expected, IEnumerable actual, IComparer comparer, string message)
\r
202 AreEqual(expected, actual, comparer, message, null);
\r
206 /// Asserts that expected and actual are exactly equal. The collections must have the same count,
\r
207 /// and contain the exact same objects in the same order.
\r
209 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
210 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
211 /// <param name="message">The message that will be displayed on failure</param>
\r
212 /// <param name="args">Arguments to be used in formatting the message</param>
\r
213 public static void AreEqual (IEnumerable expected, IEnumerable actual, string message, params object[] args)
\r
215 //AreEqual(expected, actual, null, message, args);
\r
216 Assert.That(actual, new EqualConstraint(expected), message, args);
\r
220 /// Asserts that expected and actual are exactly equal. The collections must have the same count,
\r
221 /// and contain the exact same objects in the same order.
\r
222 /// If comparer is not null then it will be used to compare the objects.
\r
224 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
225 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
226 /// <param name="comparer">The IComparer to use in comparing objects from each IEnumerable</param>
\r
227 /// <param name="message">The message that will be displayed on failure</param>
\r
228 /// <param name="args">Arguments to be used in formatting the message</param>
\r
229 public static void AreEqual (IEnumerable expected, IEnumerable actual, IComparer comparer, string message, params object[] args)
\r
231 Assert.That(actual, new EqualConstraint(expected).Comparer(comparer), message, args);
\r
235 #region AreEquivalent
\r
238 /// Asserts that expected and actual are equivalent, containing the same objects but the match may be in any order.
\r
240 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
241 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
242 public static void AreEquivalent (IEnumerable expected, IEnumerable actual)
\r
244 AreEquivalent(expected, actual, string.Empty, null);
\r
248 /// Asserts that expected and actual are equivalent, containing the same objects but the match may be in any order.
\r
250 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
251 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
252 /// <param name="message">The message that will be displayed on failure</param>
\r
253 public static void AreEquivalent (IEnumerable expected, IEnumerable actual, string message)
\r
255 AreEquivalent(expected, actual, message, null);
\r
259 /// Asserts that expected and actual are equivalent, containing the same objects but the match may be in any order.
\r
261 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
262 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
263 /// <param name="message">The message that will be displayed on failure</param>
\r
264 /// <param name="args">Arguments to be used in formatting the message</param>
\r
265 public static void AreEquivalent (IEnumerable expected, IEnumerable actual, string message, params object[] args)
\r
267 Assert.That(actual, new CollectionEquivalentConstraint(expected), message, args);
\r
271 #region AreNotEqual
\r
274 /// Asserts that expected and actual are not exactly equal.
\r
276 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
277 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
278 public static void AreNotEqual (IEnumerable expected, IEnumerable actual)
\r
280 Assert.That(actual, new NotConstraint(new EqualConstraint(expected)));
\r
284 /// Asserts that expected and actual are not exactly equal.
\r
285 /// If comparer is not null then it will be used to compare the objects.
\r
287 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
288 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
289 /// <param name="comparer">The IComparer to use in comparing objects from each IEnumerable</param>
\r
290 public static void AreNotEqual (IEnumerable expected, IEnumerable actual, IComparer comparer)
\r
292 AreNotEqual(expected, actual, comparer, string.Empty, null);
\r
296 /// Asserts that expected and actual are not exactly equal.
\r
298 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
299 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
300 /// <param name="message">The message that will be displayed on failure</param>
\r
301 public static void AreNotEqual (IEnumerable expected, IEnumerable actual, string message)
\r
303 //AreNotEqual(expected, actual, null, message, null);
\r
304 //Assert.AreNotEqual( expected, actual, message );
\r
305 Assert.That(actual, new NotConstraint(new EqualConstraint(expected)), message);
\r
309 /// Asserts that expected and actual are not exactly equal.
\r
310 /// If comparer is not null then it will be used to compare the objects.
\r
312 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
313 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
314 /// <param name="comparer">The IComparer to use in comparing objects from each IEnumerable</param>
\r
315 /// <param name="message">The message that will be displayed on failure</param>
\r
316 public static void AreNotEqual (IEnumerable expected, IEnumerable actual, IComparer comparer, string message)
\r
318 AreNotEqual(expected, actual, comparer, message, null);
\r
322 /// Asserts that expected and actual are not exactly equal.
\r
324 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
325 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
326 /// <param name="message">The message that will be displayed on failure</param>
\r
327 /// <param name="args">Arguments to be used in formatting the message</param>
\r
328 public static void AreNotEqual (IEnumerable expected, IEnumerable actual, string message, params object[] args)
\r
330 //AreNotEqual(expected, actual, null, message, args);
\r
331 //Assert.AreNotEqual( expected, actual, message, args );
\r
332 Assert.That(actual, new NotConstraint(new EqualConstraint(expected)), message, args);
\r
336 /// Asserts that expected and actual are not exactly equal.
\r
337 /// If comparer is not null then it will be used to compare the objects.
\r
339 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
340 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
341 /// <param name="comparer">The IComparer to use in comparing objects from each IEnumerable</param>
\r
342 /// <param name="message">The message that will be displayed on failure</param>
\r
343 /// <param name="args">Arguments to be used in formatting the message</param>
\r
344 public static void AreNotEqual (IEnumerable expected, IEnumerable actual, IComparer comparer, string message, params object[] args)
\r
346 Assert.That(actual, new NotConstraint(new EqualConstraint(expected).Comparer(comparer)), message, args);
\r
350 #region AreNotEquivalent
\r
353 /// Asserts that expected and actual are not equivalent.
\r
355 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
356 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
357 public static void AreNotEquivalent (IEnumerable expected, IEnumerable actual)
\r
359 AreNotEquivalent(expected, actual, string.Empty, null);
\r
363 /// Asserts that expected and actual are not equivalent.
\r
365 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
366 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
367 /// <param name="message">The message that will be displayed on failure</param>
\r
368 public static void AreNotEquivalent (IEnumerable expected, IEnumerable actual, string message)
\r
370 AreNotEquivalent(expected, actual, message, null);
\r
374 /// Asserts that expected and actual are not equivalent.
\r
376 /// <param name="expected">The first IEnumerable of objects to be considered</param>
\r
377 /// <param name="actual">The second IEnumerable of objects to be considered</param>
\r
378 /// <param name="message">The message that will be displayed on failure</param>
\r
379 /// <param name="args">Arguments to be used in formatting the message</param>
\r
380 public static void AreNotEquivalent (IEnumerable expected, IEnumerable actual, string message, params object[] args)
\r
382 Assert.That(actual, new NotConstraint(new CollectionEquivalentConstraint(expected)), message, args);
\r
388 /// Asserts that collection contains actual as an item.
\r
390 /// <param name="collection">IEnumerable of objects to be considered</param>
\r
391 /// <param name="actual">Object to be found within collection</param>
\r
392 public static void Contains (IEnumerable collection, Object actual)
\r
394 Contains(collection, actual, string.Empty, null);
\r
398 /// Asserts that collection contains actual as an item.
\r
400 /// <param name="collection">IEnumerable of objects to be considered</param>
\r
401 /// <param name="actual">Object to be found within collection</param>
\r
402 /// <param name="message">The message that will be displayed on failure</param>
\r
403 public static void Contains (IEnumerable collection, Object actual, string message)
\r
405 Contains(collection, actual, message, null);
\r
409 /// Asserts that collection contains actual as an item.
\r
411 /// <param name="collection">IEnumerable of objects to be considered</param>
\r
412 /// <param name="actual">Object to be found within collection</param>
\r
413 /// <param name="message">The message that will be displayed on failure</param>
\r
414 /// <param name="args">Arguments to be used in formatting the message</param>
\r
415 public static void Contains (IEnumerable collection, Object actual, string message, params object[] args)
\r
417 Assert.That(collection, new CollectionContainsConstraint(actual), message, args);
\r
421 #region DoesNotContain
\r
424 /// Asserts that collection does not contain actual as an item.
\r
426 /// <param name="collection">IEnumerable of objects to be considered</param>
\r
427 /// <param name="actual">Object that cannot exist within collection</param>
\r
428 public static void DoesNotContain (IEnumerable collection, Object actual)
\r
430 DoesNotContain(collection, actual, string.Empty, null);
\r
434 /// Asserts that collection does not contain actual as an item.
\r
436 /// <param name="collection">IEnumerable of objects to be considered</param>
\r
437 /// <param name="actual">Object that cannot exist within collection</param>
\r
438 /// <param name="message">The message that will be displayed on failure</param>
\r
439 public static void DoesNotContain (IEnumerable collection, Object actual, string message)
\r
441 DoesNotContain(collection, actual, message, null);
\r
445 /// Asserts that collection does not contain actual as an item.
\r
447 /// <param name="collection">IEnumerable of objects to be considered</param>
\r
448 /// <param name="actual">Object that cannot exist within collection</param>
\r
449 /// <param name="message">The message that will be displayed on failure</param>
\r
450 /// <param name="args">Arguments to be used in formatting the message</param>
\r
451 public static void DoesNotContain (IEnumerable collection, Object actual, string message, params object[] args)
\r
453 Assert.That(collection, new NotConstraint( new CollectionContainsConstraint( actual ) ), message, args);
\r
457 #region IsNotSubsetOf
\r
460 /// Asserts that superset is not a subject of subset.
\r
462 /// <param name="subset">The IEnumerable superset to be considered</param>
\r
463 /// <param name="superset">The IEnumerable subset to be considered</param>
\r
464 public static void IsNotSubsetOf (IEnumerable subset, IEnumerable superset)
\r
466 IsNotSubsetOf(subset, superset, string.Empty, null);
\r
470 /// Asserts that superset is not a subject of subset.
\r
472 /// <param name="subset">The IEnumerable superset to be considered</param>
\r
473 /// <param name="superset">The IEnumerable subset to be considered</param>
\r
474 /// <param name="message">The message that will be displayed on failure</param>
\r
475 public static void IsNotSubsetOf (IEnumerable subset, IEnumerable superset, string message)
\r
477 IsNotSubsetOf(subset, superset, message, null);
\r
481 /// Asserts that superset is not a subject of subset.
\r
483 /// <param name="subset">The IEnumerable superset to be considered</param>
\r
484 /// <param name="superset">The IEnumerable subset to be considered</param>
\r
485 /// <param name="message">The message that will be displayed on failure</param>
\r
486 /// <param name="args">Arguments to be used in formatting the message</param>
\r
487 public static void IsNotSubsetOf (IEnumerable subset, IEnumerable superset, string message, params object[] args)
\r
489 Assert.That(subset, new NotConstraint(new CollectionSubsetConstraint(superset)), message, args);
\r
496 /// Asserts that superset is a subset of subset.
\r
498 /// <param name="subset">The IEnumerable superset to be considered</param>
\r
499 /// <param name="superset">The IEnumerable subset to be considered</param>
\r
500 public static void IsSubsetOf (IEnumerable subset, IEnumerable superset)
\r
502 IsSubsetOf(subset, superset, string.Empty, null);
\r
506 /// Asserts that superset is a subset of subset.
\r
508 /// <param name="subset">The IEnumerable superset to be considered</param>
\r
509 /// <param name="superset">The IEnumerable subset to be considered</param>
\r
510 /// <param name="message">The message that will be displayed on failure</param>
\r
511 public static void IsSubsetOf (IEnumerable subset, IEnumerable superset, string message)
\r
513 IsSubsetOf(subset, superset, message, null);
\r
517 /// Asserts that superset is a subset of subset.
\r
519 /// <param name="subset">The IEnumerable superset to be considered</param>
\r
520 /// <param name="superset">The IEnumerable subset to be considered</param>
\r
521 /// <param name="message">The message that will be displayed on failure</param>
\r
522 /// <param name="args">Arguments to be used in formatting the message</param>
\r
523 public static void IsSubsetOf (IEnumerable subset, IEnumerable superset, string message, params object[] args)
\r
525 Assert.That(subset, new CollectionSubsetConstraint(superset), message, args);
\r
531 /// Assert that an array, list or other collection is empty
\r
533 /// <param name="collection">An array, list or other collection implementing IEnumerable</param>
\r
534 /// <param name="message">The message to be displayed on failure</param>
\r
535 /// <param name="args">Arguments to be used in formatting the message</param>
\r
536 public static void IsEmpty(IEnumerable collection, string message, params object[] args)
\r
538 Assert.That(collection, new EmptyConstraint(), message, args);
\r
542 /// Assert that an array, list or other collection is empty
\r
544 /// <param name="collection">An array, list or other collection implementing IEnumerable</param>
\r
545 /// <param name="message">The message to be displayed on failure</param>
\r
546 public static void IsEmpty(IEnumerable collection, string message)
\r
548 IsEmpty(collection, message, null);
\r
552 /// Assert that an array,list or other collection is empty
\r
554 /// <param name="collection">An array, list or other collection implementing IEnumerable</param>
\r
555 public static void IsEmpty(IEnumerable collection)
\r
557 IsEmpty(collection, string.Empty, null);
\r
563 /// Assert that an array, list or other collection is empty
\r
565 /// <param name="collection">An array, list or other collection implementing IEnumerable</param>
\r
566 /// <param name="message">The message to be displayed on failure</param>
\r
567 /// <param name="args">Arguments to be used in formatting the message</param>
\r
568 public static void IsNotEmpty(IEnumerable collection, string message, params object[] args)
\r
570 Assert.That(collection, new NotConstraint(new EmptyConstraint()), message, args);
\r
574 /// Assert that an array, list or other collection is empty
\r
576 /// <param name="collection">An array, list or other collection implementing IEnumerable</param>
\r
577 /// <param name="message">The message to be displayed on failure</param>
\r
578 public static void IsNotEmpty(IEnumerable collection, string message)
\r
580 IsNotEmpty(collection, message, null);
\r
584 /// Assert that an array,list or other collection is empty
\r
586 /// <param name="collection">An array, list or other collection implementing IEnumerable</param>
\r
587 public static void IsNotEmpty(IEnumerable collection)
\r
589 IsNotEmpty(collection, string.Empty, null);
\r