ReactiveAssert.cs 12 KB

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