1
0

ReactiveTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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, using inference to determine the type of <typeparamref name="T"/>.
  60. /// </summary>
  61. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  62. /// <param name="ticks">Recorded virtual time the OnCompleted notification occurs.</param>
  63. /// <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>
  64. /// <returns>Recorded OnCompleted notification.</returns>
  65. public static Recorded<Notification<T>> OnCompleted<T>(long ticks, T witness)
  66. {
  67. return new Recorded<Notification<T>>(ticks, Notification.CreateOnCompleted<T>());
  68. }
  69. /// <summary>
  70. /// Factory method for an OnError notification record at a given time with a given error.
  71. /// </summary>
  72. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  73. /// <param name="ticks">Recorded virtual time the OnError notification occurs.</param>
  74. /// <param name="exception">Recorded exception stored in the OnError notification.</param>
  75. /// <returns>Recorded OnError notification.</returns>
  76. /// <exception cref="ArgumentNullException"><paramref name="exception"/> is null.</exception>
  77. public static Recorded<Notification<T>> OnError<T>(long ticks, Exception exception)
  78. {
  79. if (exception == null)
  80. throw new ArgumentNullException("exception");
  81. return new Recorded<Notification<T>>(ticks, Notification.CreateOnError<T>(exception));
  82. }
  83. /// <summary>
  84. /// 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.
  85. /// </summary>
  86. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  87. /// <param name="ticks">Recorded virtual time the OnError notification occurs.</param>
  88. /// <param name="predicate">Predicate function to check the OnError notification value against an expected exception.</param>
  89. /// <returns>Recorded OnError notification with a predicate to assert a given exception.</returns>
  90. /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
  91. public static Recorded<Notification<T>> OnError<T>(long ticks, Func<Exception, bool> predicate)
  92. {
  93. if (predicate == null)
  94. throw new ArgumentNullException("predicate");
  95. return new Recorded<Notification<T>>(ticks, new OnErrorPredicate<T>(predicate));
  96. }
  97. /// <summary>
  98. /// 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"/>.
  99. /// </summary>
  100. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  101. /// <param name="ticks">Recorded virtual time the OnError notification occurs.</param>
  102. /// <param name="exception">Recorded exception stored in the OnError notification.</param>
  103. /// <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>
  104. /// <returns>Recorded OnError notification.</returns>
  105. /// <exception cref="ArgumentNullException"><paramref name="exception"/> is null.</exception>
  106. public static Recorded<Notification<T>> OnError<T>(long ticks, Exception exception, T witness)
  107. {
  108. if (exception == null)
  109. throw new ArgumentNullException("exception");
  110. return new Recorded<Notification<T>>(ticks, Notification.CreateOnError<T>(exception));
  111. }
  112. /// <summary>
  113. /// 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"/>.
  114. /// </summary>
  115. /// <typeparam name="T">The element type for the resulting notification object.</typeparam>
  116. /// <param name="ticks">Recorded virtual time the OnError notification occurs.</param>
  117. /// <param name="predicate">Predicate function to check the OnError notification value against an expected exception.</param>
  118. /// <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>
  119. /// <returns>Recorded OnError notification with a predicate to assert a given exception.</returns>
  120. /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
  121. public static Recorded<Notification<T>> OnError<T>(long ticks, Func<Exception, bool> predicate, T witness)
  122. {
  123. if (predicate == null)
  124. throw new ArgumentNullException("predicate");
  125. return new Recorded<Notification<T>>(ticks, new OnErrorPredicate<T>(predicate));
  126. }
  127. /// <summary>
  128. /// Factory method for a subscription record based on a given subscription and disposal time.
  129. /// </summary>
  130. /// <param name="start">Virtual time indicating when the subscription was created.</param>
  131. /// <param name="end">Virtual time indicating when the subscription was disposed.</param>
  132. /// <returns>Subscription object.</returns>
  133. public static Subscription Subscribe(long start, long end)
  134. {
  135. return new Subscription(start, end);
  136. }
  137. /// <summary>
  138. /// Factory method for a subscription record based on a given subscription time.
  139. /// </summary>
  140. /// <param name="start">Virtual time indicating when the subscription was created.</param>
  141. /// <returns>Subscription object.</returns>
  142. public static Subscription Subscribe(long start)
  143. {
  144. return new Subscription(start);
  145. }
  146. #region Predicate-based notification assert helper classes
  147. class OnNextPredicate<T> : PredicateNotification<T>
  148. {
  149. private readonly Func<T, bool> _predicate;
  150. public OnNextPredicate(Func<T, bool> predicate)
  151. {
  152. _predicate = predicate;
  153. }
  154. public override bool Equals(Notification<T> other)
  155. {
  156. if (Object.ReferenceEquals(this, other))
  157. return true;
  158. if (Object.ReferenceEquals(other, null))
  159. return false;
  160. if (other.Kind != NotificationKind.OnNext)
  161. return false;
  162. return _predicate(other.Value);
  163. }
  164. }
  165. class OnErrorPredicate<T> : PredicateNotification<T>
  166. {
  167. private readonly Func<Exception, bool> _predicate;
  168. public OnErrorPredicate(Func<Exception, bool> predicate)
  169. {
  170. _predicate = predicate;
  171. }
  172. public override bool Equals(Notification<T> other)
  173. {
  174. if (Object.ReferenceEquals(this, other))
  175. return true;
  176. if (Object.ReferenceEquals(other, null))
  177. return false;
  178. if (other.Kind != NotificationKind.OnError)
  179. return false;
  180. return _predicate(other.Exception);
  181. }
  182. }
  183. abstract class PredicateNotification<T> : Notification<T>
  184. {
  185. #region Non-implemented members (by design)
  186. public override T Value
  187. {
  188. get { throw new NotSupportedException(); }
  189. }
  190. public override bool HasValue
  191. {
  192. get { throw new NotSupportedException(); }
  193. }
  194. public override Exception Exception
  195. {
  196. get { throw new NotSupportedException(); }
  197. }
  198. public override NotificationKind Kind
  199. {
  200. get { throw new NotSupportedException(); }
  201. }
  202. public override void Accept(IObserver<T> observer)
  203. {
  204. throw new NotSupportedException();
  205. }
  206. public override TResult Accept<TResult>(IObserver<T, TResult> observer)
  207. {
  208. throw new NotSupportedException();
  209. }
  210. public override void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted)
  211. {
  212. throw new NotSupportedException();
  213. }
  214. public override TResult Accept<TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted)
  215. {
  216. throw new NotSupportedException();
  217. }
  218. #endregion
  219. }
  220. #endregion
  221. }
  222. }