PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.ComponentModel.Composition/Tests/UnitTestFramework/System/UnitTesting/EnumerableAssert.cs

https://bitbucket.org/danipen/mono
C# | 204 lines | 151 code | 41 blank | 12 comment | 9 complexity | 63ff8eeb033d4561a582864d46d0ee46 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Collections;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using Microsoft.Internal.Collections;
  10. namespace System.UnitTesting
  11. {
  12. public static class EnumerableAssert
  13. {
  14. public static void IsTrueForAll<T>(IEnumerable<T> source, Predicate<T> predicate)
  15. {
  16. IsTrueForAll(source, predicate, "IsTrueForAll Failed");
  17. }
  18. public static void IsTrueForAll<T>(IEnumerable<T> source, Predicate<T> predicate, string message)
  19. {
  20. Assert.IsNotNull(source, "Source should not be null!");
  21. foreach (T t in source)
  22. {
  23. Assert.IsTrue(predicate(t), message);
  24. }
  25. }
  26. // Needed to prevent strings from matching to the plain IEnumerable overload
  27. public static void AreEqual(IEnumerable actual, params string[] expected)
  28. {
  29. AreEqual((IEnumerable)expected, (IEnumerable)actual);
  30. }
  31. public static void AreEqual(IEnumerable actual, params object[] expected)
  32. {
  33. AreEqual((IEnumerable)expected, (IEnumerable)actual);
  34. }
  35. public static void AreEqual<T>(IEnumerable<T> actual, params T[] expected)
  36. {
  37. AreEqual<T>((IEnumerable<T>)expected, (IEnumerable<T>)actual);
  38. }
  39. public static void AreEqual(IEnumerable expected, IEnumerable actual)
  40. {
  41. Assert.AreEqual(expected.Count(), actual.Count(), "Enumerable should contain the correct number of items");
  42. List<object> actualList = actual.ToList();
  43. foreach (object value in expected)
  44. {
  45. bool removed = actualList.Remove(value);
  46. Assert.IsTrue(removed, "Enumerable does not contain value {0}.", value);
  47. }
  48. Assert.AreEqual(0, actualList.Count, "Enumerable contains extra values.");
  49. }
  50. public static void AreEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual)
  51. {
  52. // First, test the IEnumerable implementation
  53. AreEqual((IEnumerable)expected, (IEnumerable)actual);
  54. // Second, test the IEnumerable<T> implementation
  55. Assert.AreEqual(expected.Count(), actual.Count(), "Enumerable should contain the correct number of items");
  56. List<T> actualList = actual.ToList();
  57. foreach (T value in expected)
  58. {
  59. bool removed = actualList.Remove(value);
  60. Assert.IsTrue(removed, "Enumerable does not contain value {0}.", value);
  61. }
  62. Assert.AreEqual(0, actualList.Count, "Enumerable contains extra values.");
  63. }
  64. // Needed to prevent strings from matching to the plain IEnumerable overload
  65. public static void AreSequenceEqual(IEnumerable actual, params string[] expected)
  66. {
  67. AreEqual((IEnumerable)expected, (IEnumerable)actual);
  68. }
  69. public static void AreSequenceEqual(IEnumerable actual, params object[] expected)
  70. {
  71. AreEqual((IEnumerable)expected, (IEnumerable)actual);
  72. }
  73. public static void AreSequenceEqual(IEnumerable expected, IEnumerable actual)
  74. {
  75. AreSequenceEqual(expected, actual, (Action<int, object, object>)null);
  76. }
  77. public static void AreSequenceEqual(IEnumerable expected, IEnumerable actual, Action<int, object, object> comparer)
  78. {
  79. if (comparer == null)
  80. {
  81. comparer = (i, left, right) => Assert.AreEqual(left, right, "Enumerable at index {0} should have same value", i);
  82. }
  83. int expectedCount = expected.Count();
  84. Assert.AreEqual(expectedCount, actual.Count(), "Enumerable should contain the correct number of items");
  85. IEnumerator actualEnumerator = actual.GetEnumerator();
  86. IEnumerator expectedEnumerator = expected.GetEnumerator();
  87. int index = 0;
  88. while (index < expectedCount)
  89. {
  90. Assert.IsTrue(actualEnumerator.MoveNext());
  91. Assert.IsTrue(expectedEnumerator.MoveNext());
  92. comparer(index, expectedEnumerator.Current, actualEnumerator.Current);
  93. index++;
  94. }
  95. }
  96. public static void AreSequenceEqual<T>(IEnumerable<T> actual, params T[] expected)
  97. {
  98. AreSequenceEqual<T>((IEnumerable<T>)expected, (IEnumerable<T>)actual);
  99. }
  100. public static void AreSequenceEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual)
  101. {
  102. AreSequenceEqual<T>(expected, actual, (Action<int, T, T>)null);
  103. }
  104. public static void AreSequenceEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, Action<int, T, T> comparer)
  105. {
  106. if (comparer == null)
  107. {
  108. comparer = (i, left, right) => Assert.AreEqual(left, right, "Enumerable at index {0} should have same value", i);
  109. }
  110. // First, test the IEnumerable implementation
  111. AreSequenceEqual((IEnumerable)expected, (IEnumerable)actual, (Action<int, object, object>)((currentIndex, left, right) => comparer(currentIndex, (T)left, (T)right)));
  112. // Second, test the IEnumerable<T> implementation
  113. int expectedCount = expected.Count();
  114. IEnumerator<T> actualEnumerator = actual.GetEnumerator();
  115. IEnumerator<T> expectedEnumerator = expected.GetEnumerator();
  116. int index = 0;
  117. while (index < expectedCount)
  118. {
  119. Assert.IsTrue(actualEnumerator.MoveNext());
  120. Assert.IsTrue(expectedEnumerator.MoveNext());
  121. comparer(index, expectedEnumerator.Current, actualEnumerator.Current);
  122. index++;
  123. }
  124. }
  125. public static void AreSequenceSame<T>(IEnumerable<T> expected, IEnumerable<T> actual)
  126. {
  127. AreSequenceEqual<T>(expected, actual, (index, left, right) =>
  128. {
  129. Assert.AreSame(left, right, "Enumerable at index {0} should have same value", index);
  130. });
  131. }
  132. public static void AreEqual<TKey, TValue>(IDictionary<TKey, TValue> expected, IDictionary<TKey, TValue> actual)
  133. {
  134. Assert.AreEqual(expected.Count, actual.Count, "Dictionaries are different : first has '{0} elements, whereas second has '{1}", expected.Count, actual.Count);
  135. foreach (KeyValuePair<TKey, TValue> kvp in expected)
  136. {
  137. TValue firstValue = kvp.Value;
  138. TValue secondValue = default(TValue);
  139. if (!actual.TryGetValue(kvp.Key, out secondValue))
  140. {
  141. Assert.Fail("Dictionaries are different : There is no item with key '{0}' in the second dictionary", kvp.Key);
  142. }
  143. if ((firstValue is IDictionary<TKey, TValue>) && (secondValue is IDictionary<TKey, TValue>))
  144. {
  145. AreEqual((IDictionary<TKey, TValue>)firstValue, (IDictionary<TKey, TValue>)secondValue);
  146. continue;
  147. }
  148. Assert.AreEqual(kvp.Value, secondValue, "Dictionaries are different : values for key '{0}' are different - '{1}' vs '{2}'", kvp.Key, firstValue, secondValue);
  149. }
  150. }
  151. /// <summary>
  152. /// Verifies that the specified enumerable is empty.
  153. /// </summary>
  154. public static void IsEmpty(IEnumerable source)
  155. {
  156. IsEmpty(source, null);
  157. }
  158. public static void IsEmpty(IEnumerable source, string message)
  159. {
  160. Assert.AreEqual(0, source.Count(), message);
  161. }
  162. }
  163. }