ReactiveAssert.cs 12 KB

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