ReactiveTest.cs 8.9 KB

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