1
0

ReactiveTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 OnCompleted notification record at a given time.
  60. /// </summary>
  61. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  62. /// <param name="dummy">An unused instance of type T, to force the compiler to infer that T as part of the method's return value.</param>
  63. /// <param name="ticks">Recorded virtual time the OnCompleted notification occurs.</param>
  64. /// <returns>Recorded OnCompleted notification.</returns>
  65. /// <remarks>This overload is used for anonymous types - by passing in an instance of the type, the compiler can infer the
  66. /// anonymous type without you having to try naming the type.</remarks>
  67. public static Recorded<Notification<T>> OnCompleted<T>(T dummy, long ticks)
  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.
  101. /// </summary>
  102. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  103. /// <param name="dummy">An unused instance of type T, to force the compiler to infer that T as part of the method's return value.</param>
  104. /// <param name="ticks">Recorded virtual time the OnError notification occurs.</param>
  105. /// <param name="exception">Recorded exception stored in the OnError notification.</param>
  106. /// <returns>Recorded OnError notification.</returns>
  107. /// <exception cref="ArgumentNullException"><paramref name="exception"/> is null.</exception>
  108. /// <remarks>This overload is used for anonymous types - by passing in an instance of the type, the compiler can infer the
  109. /// anonymous type without you having to try naming the type.</remarks>
  110. public static Recorded<Notification<T>> OnError<T>(T dummy, long ticks, Exception exception)
  111. {
  112. if (exception == null)
  113. throw new ArgumentNullException("exception");
  114. return new Recorded<Notification<T>>(ticks, Notification.CreateOnError<T>(exception));
  115. }
  116. /// <summary>
  117. /// 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.
  118. /// </summary>
  119. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  120. /// <param name="dummy">An unused instance of type T, to force the compiler to infer that T as part of the method's return value.</param>
  121. /// <param name="ticks">Recorded virtual time the OnError notification occurs.</param>
  122. /// <param name="predicate">Predicate function to check the OnError notification value against an expected exception.</param>
  123. /// <returns>Recorded OnError notification with a predicate to assert a given exception.</returns>
  124. /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
  125. /// <remarks>This overload is used for anonymous types - by passing in an instance of the type, the compiler can infer the
  126. /// anonymous type without you having to try naming the type.</remarks>
  127. public static Recorded<Notification<T>> OnError<T>(T dummy, long ticks, Func<Exception, bool> predicate)
  128. {
  129. if (predicate == null)
  130. throw new ArgumentNullException("predicate");
  131. return new Recorded<Notification<T>>(ticks, new OnErrorPredicate<T>(predicate));
  132. }
  133. /// <summary>
  134. /// Factory method for a subscription record based on a given subscription and disposal time.
  135. /// </summary>
  136. /// <param name="start">Virtual time indicating when the subscription was created.</param>
  137. /// <param name="end">Virtual time indicating when the subscription was disposed.</param>
  138. /// <returns>Subscription object.</returns>
  139. public static Subscription Subscribe(long start, long end)
  140. {
  141. return new Subscription(start, end);
  142. }
  143. /// <summary>
  144. /// Factory method for a subscription record based on a given subscription time.
  145. /// </summary>
  146. /// <param name="start">Virtual time indicating when the subscription was created.</param>
  147. /// <returns>Subscription object.</returns>
  148. public static Subscription Subscribe(long start)
  149. {
  150. return new Subscription(start);
  151. }
  152. #region Predicate-based notification assert helper classes
  153. class OnNextPredicate<T> : PredicateNotification<T>
  154. {
  155. private readonly Func<T, bool> _predicate;
  156. public OnNextPredicate(Func<T, bool> predicate)
  157. {
  158. _predicate = predicate;
  159. }
  160. public override bool Equals(Notification<T> other)
  161. {
  162. if (Object.ReferenceEquals(this, other))
  163. return true;
  164. if (Object.ReferenceEquals(other, null))
  165. return false;
  166. if (other.Kind != NotificationKind.OnNext)
  167. return false;
  168. return _predicate(other.Value);
  169. }
  170. }
  171. class OnErrorPredicate<T> : PredicateNotification<T>
  172. {
  173. private readonly Func<Exception, bool> _predicate;
  174. public OnErrorPredicate(Func<Exception, bool> predicate)
  175. {
  176. _predicate = predicate;
  177. }
  178. public override bool Equals(Notification<T> other)
  179. {
  180. if (Object.ReferenceEquals(this, other))
  181. return true;
  182. if (Object.ReferenceEquals(other, null))
  183. return false;
  184. if (other.Kind != NotificationKind.OnError)
  185. return false;
  186. return _predicate(other.Exception);
  187. }
  188. }
  189. abstract class PredicateNotification<T> : Notification<T>
  190. {
  191. #region Non-implemented members (by design)
  192. public override T Value
  193. {
  194. get { throw new NotSupportedException(); }
  195. }
  196. public override bool HasValue
  197. {
  198. get { throw new NotSupportedException(); }
  199. }
  200. public override Exception Exception
  201. {
  202. get { throw new NotSupportedException(); }
  203. }
  204. public override NotificationKind Kind
  205. {
  206. get { throw new NotSupportedException(); }
  207. }
  208. public override void Accept(IObserver<T> observer)
  209. {
  210. throw new NotSupportedException();
  211. }
  212. public override TResult Accept<TResult>(IObserver<T, TResult> observer)
  213. {
  214. throw new NotSupportedException();
  215. }
  216. public override void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted)
  217. {
  218. throw new NotSupportedException();
  219. }
  220. public override TResult Accept<TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted)
  221. {
  222. throw new NotSupportedException();
  223. }
  224. #endregion
  225. }
  226. #endregion
  227. }
  228. }