ReactiveAssert.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Reactive.Disposables;
  8. using System.Reactive.Linq;
  9. #if WINDOWS8
  10. using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
  11. #else
  12. using Microsoft.VisualStudio.TestTools.UnitTesting;
  13. #endif
  14. namespace Microsoft.Reactive.Testing
  15. {
  16. /// <summary>
  17. /// Helper class to write asserts in unit tests for applications and libraries built using Reactive Extensions.
  18. /// </summary>
  19. public static class ReactiveAssert
  20. {
  21. static string Message<T>(IEnumerable<T> actual, IEnumerable<T> expected)
  22. {
  23. var sb = new StringBuilder();
  24. sb.AppendLine();
  25. sb.Append("Expected: [");
  26. sb.Append(string.Join(", ", expected.Select(x => x.ToString()).ToArray()));
  27. sb.Append("]");
  28. sb.AppendLine();
  29. sb.Append("Actual..: [");
  30. sb.Append(string.Join(", ", actual.Select(x => x.ToString()).ToArray()));
  31. sb.Append("]");
  32. sb.AppendLine();
  33. return sb.ToString();
  34. }
  35. /// <summary>
  36. /// Asserts that both enumerable sequences have equal length and equal elements.
  37. /// </summary>
  38. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  39. /// <param name="expected">Expected sequence.</param>
  40. /// <param name="actual">Actual sequence to compare against the expected one.</param>
  41. /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
  42. public static void AreElementsEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual)
  43. {
  44. if (expected == null)
  45. throw new ArgumentNullException("expected");
  46. if (actual == null)
  47. throw new ArgumentNullException("actual");
  48. if (!expected.SequenceEqual(actual))
  49. Assert.Fail(Message(actual, expected));
  50. }
  51. /// <summary>
  52. /// Asserts that both enumerable sequences have equal length and equal elements.
  53. /// </summary>
  54. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  55. /// <param name="expected">Expected sequence.</param>
  56. /// <param name="actual">Actual sequence to compare against the expected one.</param>
  57. /// <param name="message">Error message for assert failure.</param>
  58. /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
  59. public static void AreElementsEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, string message)
  60. {
  61. if (expected == null)
  62. throw new ArgumentNullException("expected");
  63. if (actual == null)
  64. throw new ArgumentNullException("actual");
  65. if (!expected.SequenceEqual(actual))
  66. Assert.Fail(message);
  67. }
  68. /// <summary>
  69. /// Asserts that both observable sequences have equal length and equal notifications.
  70. /// </summary>
  71. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  72. /// <param name="expected">Expected sequence.</param>
  73. /// <param name="actual">Actual sequence to compare against the expected one.</param>
  74. /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
  75. public static void AreElementsEqual<T>(IObservable<T> expected, IObservable<T> actual)
  76. {
  77. if (expected == null)
  78. throw new ArgumentNullException("expected");
  79. if (actual == null)
  80. throw new ArgumentNullException("actual");
  81. AreElementsEqual(expected.Materialize().ToEnumerable(), actual.Materialize().ToEnumerable());
  82. }
  83. /// <summary>
  84. /// Asserts that both observable sequences have equal length and equal elements.
  85. /// </summary>
  86. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  87. /// <param name="expected">Expected sequence.</param>
  88. /// <param name="actual">Actual sequence to compare against the expected one.</param>
  89. /// <param name="message">Error message for assert failure.</param>
  90. /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
  91. public static void AreElementsEqual<T>(IObservable<T> expected, IObservable<T> actual, string message)
  92. {
  93. if (expected == null)
  94. throw new ArgumentNullException("expected");
  95. if (actual == null)
  96. throw new ArgumentNullException("actual");
  97. AreElementsEqual(expected.Materialize().ToEnumerable(), actual.Materialize().ToEnumerable(), message);
  98. }
  99. /// <summary>
  100. /// Asserts that the given action throws an exception of the type specified in the generic parameter, or a subtype thereof.
  101. /// </summary>
  102. /// <typeparam name="TException">Type of the exception to check for.</typeparam>
  103. /// <param name="action">Action to run.</param>
  104. /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
  105. public static void Throws<TException>(Action action) where TException : Exception
  106. {
  107. if (action == null)
  108. throw new ArgumentNullException("action");
  109. var failed = false;
  110. try
  111. {
  112. action();
  113. failed = true;
  114. }
  115. catch (TException)
  116. {
  117. }
  118. catch (Exception ex)
  119. {
  120. Assert.Fail(string.Format(CultureInfo.CurrentCulture, "Expected {0} threw {1}.\r\n\r\nStack trace:\r\n{2}", typeof(TException).Name, ex.GetType().Name, ex.StackTrace));
  121. }
  122. if (failed)
  123. Assert.Fail(string.Format(CultureInfo.CurrentCulture, "Expected {0}.", typeof(TException).Name));
  124. }
  125. /// <summary>
  126. /// Asserts that the given action throws an exception of the type specified in the generic parameter, or a subtype thereof.
  127. /// </summary>
  128. /// <typeparam name="TException">Type of the exception to check for.</typeparam>
  129. /// <param name="action">Action to run.</param>
  130. /// <param name="message">Error message for assert failure.</param>
  131. /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
  132. public static void Throws<TException>(Action action, string message) where TException : Exception
  133. {
  134. if (action == null)
  135. throw new ArgumentNullException("action");
  136. var failed = false;
  137. try
  138. {
  139. action();
  140. failed = true;
  141. }
  142. catch (TException)
  143. {
  144. }
  145. catch
  146. {
  147. Assert.Fail(message);
  148. }
  149. if (failed)
  150. Assert.Fail(message);
  151. }
  152. /// <summary>
  153. /// Asserts that the given action throws the specified exception.
  154. /// </summary>
  155. /// <typeparam name="TException">Type of the exception to check for.</typeparam>
  156. /// <param name="exception">Exception to assert being thrown.</param>
  157. /// <param name="action">Action to run.</param>
  158. /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
  159. public static void Throws<TException>(TException exception, Action action) where TException : Exception
  160. {
  161. if (action == null)
  162. throw new ArgumentNullException("action");
  163. var failed = false;
  164. try
  165. {
  166. action();
  167. failed = true;
  168. }
  169. catch (TException ex)
  170. {
  171. Assert.AreSame(exception, ex);
  172. }
  173. catch (Exception ex)
  174. {
  175. Assert.Fail(string.Format(CultureInfo.CurrentCulture, "Expected {0} threw {1}.\r\n\r\nStack trace:\r\n{2}", typeof(TException).Name, ex.GetType().Name, ex.StackTrace));
  176. }
  177. if (failed)
  178. Assert.Fail(string.Format(CultureInfo.CurrentCulture, "Expected {0}.", typeof(TException).Name));
  179. }
  180. /// <summary>
  181. /// Asserts that the given action throws the specified exception.
  182. /// </summary>
  183. /// <typeparam name="TException">Type of the exception to check for.</typeparam>
  184. /// <param name="exception">Exception to assert being thrown.</param>
  185. /// <param name="action">Action to run.</param>
  186. /// <param name="message">Error message for assert failure.</param>
  187. /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
  188. public static void Throws<TException>(TException exception, Action action, string message) where TException : Exception
  189. {
  190. if (action == null)
  191. throw new ArgumentNullException("action");
  192. var failed = false;
  193. try
  194. {
  195. action();
  196. failed = true;
  197. }
  198. catch (TException ex)
  199. {
  200. Assert.AreSame(exception, ex);
  201. }
  202. catch
  203. {
  204. Assert.Fail(message);
  205. }
  206. if (failed)
  207. Assert.Fail(message);
  208. }
  209. /// <summary>
  210. /// Asserts that both enumerable sequences have equal length and equal elements.
  211. /// </summary>
  212. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  213. /// <param name="actual">Actual sequence to compare against the expected one.</param>
  214. /// <param name="expected">Expected sequence.</param>
  215. /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
  216. public static void AssertEqual<T>(this IEnumerable<T> actual, IEnumerable<T> expected)
  217. {
  218. if (actual == null)
  219. throw new ArgumentNullException("actual");
  220. if (expected == null)
  221. throw new ArgumentNullException("expected");
  222. ReactiveAssert.AreElementsEqual(expected, actual);
  223. }
  224. /// <summary>
  225. /// Asserts the enumerable sequence has the expected elements.
  226. /// </summary>
  227. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  228. /// <param name="actual">Actual sequence to compare against the expected elements.</param>
  229. /// <param name="expected">Expected elements.</param>
  230. /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
  231. public static void AssertEqual<T>(this IEnumerable<T> actual, params T[] expected)
  232. {
  233. if (actual == null)
  234. throw new ArgumentNullException("actual");
  235. if (expected == null)
  236. throw new ArgumentNullException("expected");
  237. ReactiveAssert.AreElementsEqual(expected, actual);
  238. }
  239. /// <summary>
  240. /// Asserts that both observable sequences have equal length and equal notifications.
  241. /// </summary>
  242. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  243. /// <param name="actual">Actual sequence to compare against the expected one.</param>
  244. /// <param name="expected">Expected sequence.</param>
  245. /// <exception cref="ArgumentNullException"><paramref name="expected"/> or <paramref name="actual"/> is null.</exception>
  246. public static void AssertEqual<T>(this IObservable<T> actual, IObservable<T> expected)
  247. {
  248. if (actual == null)
  249. throw new ArgumentNullException("actual");
  250. if (expected == null)
  251. throw new ArgumentNullException("expected");
  252. ReactiveAssert.AreElementsEqual(expected, actual);
  253. }
  254. }
  255. }