ReactiveTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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.Reactive;
  6. namespace Microsoft.Reactive.Testing
  7. {
  8. /// <summary>
  9. /// Base class to write unit tests for applications and libraries built using Reactive Extensions.
  10. /// </summary>
  11. public class ReactiveTest
  12. {
  13. /// <summary>
  14. /// Default virtual time used for creation of observable sequences in <see cref="ReactiveTest"/>-based unit tests.
  15. /// </summary>
  16. public const long Created = 100;
  17. /// <summary>
  18. /// Default virtual time used to subscribe to observable sequences in <see cref="ReactiveTest"/>-based unit tests.
  19. /// </summary>
  20. public const long Subscribed = 200;
  21. /// <summary>
  22. /// Default virtual time used to dispose subscriptions in <see cref="ReactiveTest"/>-based unit tests.
  23. /// </summary>
  24. public const long Disposed = 1000;
  25. /// <summary>
  26. /// Factory method for an OnNext notification record at a given time with a given value.
  27. /// </summary>
  28. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  29. /// <param name="ticks">Recorded virtual time the OnNext notification occurs.</param>
  30. /// <param name="value">Recorded value stored in the OnNext notification.</param>
  31. /// <returns>Recorded OnNext notification.</returns>
  32. public static Recorded<Notification<T>> OnNext<T>(long ticks, T value)
  33. {
  34. return new Recorded<Notification<T>>(ticks, Notification.CreateOnNext<T>(value));
  35. }
  36. /// <summary>
  37. /// Factory method for writing an assert that checks for an OnNext notification record at a given time, using the specified predicate to check the value.
  38. /// </summary>
  39. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  40. /// <param name="ticks">Recorded virtual time the OnNext notification occurs.</param>
  41. /// <param name="predicate">Predicate function to check the OnNext notification value against an expected value.</param>
  42. /// <returns>Recorded OnNext notification with a predicate to assert a given value.</returns>
  43. /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
  44. public static Recorded<Notification<T>> OnNext<T>(long ticks, Func<T, bool> predicate)
  45. {
  46. if (predicate == null)
  47. throw new ArgumentNullException("predicate");
  48. return new Recorded<Notification<T>>(ticks, new OnNextPredicate<T>(predicate));
  49. }
  50. /// <summary>
  51. /// Factory method for an OnCompleted notification record at a given time.
  52. /// </summary>
  53. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  54. /// <param name="ticks">Recorded virtual time the OnCompleted notification occurs.</param>
  55. /// <returns>Recorded OnCompleted notification.</returns>
  56. public static Recorded<Notification<T>> OnCompleted<T>(long ticks)
  57. {
  58. return new Recorded<Notification<T>>(ticks, Notification.CreateOnCompleted<T>());
  59. }
  60. /// <summary>
  61. /// Factory method for an OnCompleted notification record at a given time, using inference to determine the type of <typeparamref name="T"/>.
  62. /// </summary>
  63. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  64. /// <param name="ticks">Recorded virtual time the OnCompleted notification occurs.</param>
  65. /// <param name="witness">Object solely used to infer the type of the <typeparamref name="T"/> type parameter. This parameter is typically used when creating a sequence of anonymously typed elements.</param>
  66. /// <returns>Recorded OnCompleted notification.</returns>
  67. public static Recorded<Notification<T>> OnCompleted<T>(long ticks, T witness)
  68. {
  69. return new Recorded<Notification<T>>(ticks, Notification.CreateOnCompleted<T>());
  70. }
  71. /// <summary>
  72. /// Factory method for an OnError notification record at a given time with a given error.
  73. /// </summary>
  74. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  75. /// <param name="ticks">Recorded virtual time the OnError notification occurs.</param>
  76. /// <param name="exception">Recorded exception stored in the OnError notification.</param>
  77. /// <returns>Recorded OnError notification.</returns>
  78. /// <exception cref="ArgumentNullException"><paramref name="exception"/> is null.</exception>
  79. public static Recorded<Notification<T>> OnError<T>(long ticks, Exception exception)
  80. {
  81. if (exception == null)
  82. throw new ArgumentNullException("exception");
  83. return new Recorded<Notification<T>>(ticks, Notification.CreateOnError<T>(exception));
  84. }
  85. /// <summary>
  86. /// Factory method for writing an assert that checks for an OnError notification record at a given time, using the specified predicate to check the exception.
  87. /// </summary>
  88. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  89. /// <param name="ticks">Recorded virtual time the OnError notification occurs.</param>
  90. /// <param name="predicate">Predicate function to check the OnError notification value against an expected exception.</param>
  91. /// <returns>Recorded OnError notification with a predicate to assert a given exception.</returns>
  92. /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
  93. public static Recorded<Notification<T>> OnError<T>(long ticks, Func<Exception, bool> predicate)
  94. {
  95. if (predicate == null)
  96. throw new ArgumentNullException("predicate");
  97. return new Recorded<Notification<T>>(ticks, new OnErrorPredicate<T>(predicate));
  98. }
  99. /// <summary>
  100. /// Factory method for an OnError notification record at a given time with a given error, using inference to determine the type of <typeparamref name="T"/>.
  101. /// </summary>
  102. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  103. /// <param name="ticks">Recorded virtual time the OnError notification occurs.</param>
  104. /// <param name="exception">Recorded exception stored in the OnError notification.</param>
  105. /// <param name="witness">Object solely used to infer the type of the <typeparamref name="T"/> type parameter. This parameter is typically used when creating a sequence of anonymously typed elements.</param>
  106. /// <returns>Recorded OnError notification.</returns>
  107. /// <exception cref="ArgumentNullException"><paramref name="exception"/> is null.</exception>
  108. public static Recorded<Notification<T>> OnError<T>(long ticks, Exception exception, T witness)
  109. {
  110. if (exception == null)
  111. throw new ArgumentNullException("exception");
  112. return new Recorded<Notification<T>>(ticks, Notification.CreateOnError<T>(exception));
  113. }
  114. /// <summary>
  115. /// Factory method for writing an assert that checks for an OnError notification record at a given time, using the specified predicate to check the exception and inference to determine the type of <typeparamref name="T"/>.
  116. /// </summary>
  117. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  118. /// <param name="ticks">Recorded virtual time the OnError notification occurs.</param>
  119. /// <param name="predicate">Predicate function to check the OnError notification value against an expected exception.</param>
  120. /// <param name="witness">Object solely used to infer the type of the <typeparamref name="T"/> type parameter. This parameter is typically used when creating a sequence of anonymously typed elements.</param>
  121. /// <returns>Recorded OnError notification with a predicate to assert a given exception.</returns>
  122. /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
  123. public static Recorded<Notification<T>> OnError<T>(long ticks, Func<Exception, bool> predicate, T witness)
  124. {
  125. if (predicate == null)
  126. throw new ArgumentNullException("predicate");
  127. return new Recorded<Notification<T>>(ticks, new OnErrorPredicate<T>(predicate));
  128. }
  129. /// <summary>
  130. /// Factory method for a subscription record based on a given subscription and disposal time.
  131. /// </summary>
  132. /// <param name="start">Virtual time indicating when the subscription was created.</param>
  133. /// <param name="end">Virtual time indicating when the subscription was disposed.</param>
  134. /// <returns>Subscription object.</returns>
  135. public static Subscription Subscribe(long start, long end)
  136. {
  137. return new Subscription(start, end);
  138. }
  139. /// <summary>
  140. /// Factory method for a subscription record based on a given subscription time.
  141. /// </summary>
  142. /// <param name="start">Virtual time indicating when the subscription was created.</param>
  143. /// <returns>Subscription object.</returns>
  144. public static Subscription Subscribe(long start)
  145. {
  146. return new Subscription(start);
  147. }
  148. #region Predicate-based notification assert helper classes
  149. class OnNextPredicate<T> : PredicateNotification<T>
  150. {
  151. private readonly Func<T, bool> _predicate;
  152. public OnNextPredicate(Func<T, bool> predicate)
  153. {
  154. _predicate = predicate;
  155. }
  156. public override bool Equals(Notification<T> other)
  157. {
  158. if (Object.ReferenceEquals(this, other))
  159. return true;
  160. if (Object.ReferenceEquals(other, null))
  161. return false;
  162. if (other.Kind != NotificationKind.OnNext)
  163. return false;
  164. return _predicate(other.Value);
  165. }
  166. }
  167. class OnErrorPredicate<T> : PredicateNotification<T>
  168. {
  169. private readonly Func<Exception, bool> _predicate;
  170. public OnErrorPredicate(Func<Exception, bool> predicate)
  171. {
  172. _predicate = predicate;
  173. }
  174. public override bool Equals(Notification<T> other)
  175. {
  176. if (Object.ReferenceEquals(this, other))
  177. return true;
  178. if (Object.ReferenceEquals(other, null))
  179. return false;
  180. if (other.Kind != NotificationKind.OnError)
  181. return false;
  182. return _predicate(other.Exception);
  183. }
  184. }
  185. abstract class PredicateNotification<T> : Notification<T>
  186. {
  187. #region Non-implemented members (by design)
  188. public override T Value
  189. {
  190. get { throw new NotSupportedException(); }
  191. }
  192. public override bool HasValue
  193. {
  194. get { throw new NotSupportedException(); }
  195. }
  196. public override Exception Exception
  197. {
  198. get { throw new NotSupportedException(); }
  199. }
  200. public override NotificationKind Kind
  201. {
  202. get { throw new NotSupportedException(); }
  203. }
  204. public override void Accept(IObserver<T> observer)
  205. {
  206. throw new NotSupportedException();
  207. }
  208. public override TResult Accept<TResult>(IObserver<T, TResult> observer)
  209. {
  210. throw new NotSupportedException();
  211. }
  212. public override void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted)
  213. {
  214. throw new NotSupportedException();
  215. }
  216. public override TResult Accept<TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted)
  217. {
  218. throw new NotSupportedException();
  219. }
  220. #endregion
  221. }
  222. #endregion
  223. }
  224. }