ArgumentValidationTest.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reactive;
  8. using System.Reactive.Concurrency;
  9. using System.Reactive.Disposables;
  10. using System.Reactive.Joins;
  11. using System.Reactive.Linq;
  12. using System.Reactive.Subjects;
  13. using System.Reflection;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. using Microsoft.VisualStudio.TestTools.UnitTesting;
  17. namespace ReactiveTests.Tests
  18. {
  19. /// <summary>
  20. /// Check if the Observable operator methods perform the proper
  21. /// argument validations en-masse with reflective checks.
  22. /// </summary>
  23. [TestClass]
  24. public class ArgumentValidationTest
  25. {
  26. #region + Default values for the generic types +
  27. /// <summary>
  28. /// Contains a map of various types, represented
  29. /// as strings generated via <see cref="TypeNameOf(Type)"/>,
  30. /// mapped to a value.
  31. /// </summary>
  32. private static Dictionary<string, object> _defaultValues;
  33. /// <summary>
  34. /// Prepare the default instances for various types used
  35. /// throughout Rx.NET.
  36. /// </summary>
  37. static ArgumentValidationTest()
  38. {
  39. _defaultValues = new Dictionary<string, object>
  40. {
  41. { "IObservable`1[Object]", Observable.Return(new object()) },
  42. { "IObservable`1[Int32]", Observable.Return(1) },
  43. { "IObservable`1[Task`1[Int32]]", Observable.Return(Task.FromResult(1)) },
  44. { "IObservable`1[Notification`1[Int32]]", Observable.Return(Notification.CreateOnNext(1)) },
  45. { "IObservable`1[Int64]", Observable.Return(1L) },
  46. { "IObservable`1[Double]", Observable.Return(1.0) },
  47. { "IObservable`1[Single]", Observable.Return(1.0f) },
  48. { "IObservable`1[Decimal]", Observable.Return(1.0m) },
  49. { "IObservable`1[Nullable`1[Int32]]", Observable.Return<int?>(1) },
  50. { "IObservable`1[Nullable`1[Int64]]", Observable.Return<long?>(1L) },
  51. { "IObservable`1[Nullable`1[Double]]", Observable.Return<double?>(1.0) },
  52. { "IObservable`1[Nullable`1[Single]]", Observable.Return<float?>(1.0f) },
  53. { "IObservable`1[Nullable`1[Decimal]]", Observable.Return<decimal?>(1.0m) },
  54. { "IObservable`1[IObservable`1[Int32]]", Observable.Return(Observable.Return(1)) },
  55. { "IObservable`1[][Int32]", new[] { Observable.Return(1) } },
  56. { "IConnectableObservable`1[Int32]", Observable.Return(1).Publish() },
  57. { "Int32", 1 },
  58. { "Int64", 1L },
  59. { "IScheduler", Scheduler.Immediate },
  60. { "TimeSpan", TimeSpan.FromMilliseconds(1) },
  61. { "DateTimeOffset", DateTimeOffset.Now },
  62. { "Object", new object() },
  63. { "Exception", new Exception() },
  64. { "String", "String" },
  65. { "Boolean", false },
  66. { "IDictionary`2[Int32, IObservable`1[Int32]]", new Dictionary<int, IObservable<int>>() },
  67. { "Type", typeof(object) },
  68. { "Int32[]", new[] { 1 } },
  69. { "ISubject`1[Int32]", new Subject<int>() },
  70. { "ISubject`2[Int32, Int32]", new Subject<int>() },
  71. { "IEnumerable`1[Int32]", new[] { 1 } },
  72. { "IEnumerable`1[IObservable`1[Int32]]", new[] { Observable.Return(1) } },
  73. { "SynchronizationContext", SynchronizationContext.Current ?? new SynchronizationContext() },
  74. { "IEqualityComparer`1[Int32]", EqualityComparer<int>.Default },
  75. { "IComparer`1[Int32]", Comparer<int>.Default },
  76. { "IObserver`1[Int32]", Observer.Create<int>(v => { }) },
  77. { "CancellationToken", new CancellationToken() },
  78. { "Action", new Action(() => { }) },
  79. { "Action`1[Int32]", new Action<int>(v => { }) },
  80. { "Action`1[Exception]", new Action<Exception>(v => { }) },
  81. { "Action`1[IDisposable]", new Action<IDisposable>(v => { }) },
  82. { "Action`1[EventHandler]", new Action<EventHandler>(v => { }) },
  83. { "Action`1[EventHandler`1[Int32]]", new Action<EventHandler<int>>(v => { }) },
  84. { "Action`1[Action`1[Int32]]", new Action<Action<int>>(v => { }) },
  85. { "Action`1[Action]", new Action<Action>(v => { }) },
  86. { "Action`1[IAsyncResult]", new Action<IAsyncResult>(v => { }) },
  87. { "Action`2[Int32, Int32]", new Action<int, int>((v, u) => { }) },
  88. { "Func`1[Boolean]", new Func<bool>(() => true) },
  89. { "Func`1[Int32]", new Func<int>(() => 1) },
  90. { "Func`1[IObservable`1[Int32]]", new Func<IObservable<int>>(() => Observable.Return(1)) },
  91. { "Func`1[ISubject`2[Int32, Int32]]", new Func<ISubject<int, int>>(() => new Subject<int>()) },
  92. { "Func`1[Task`1[IObservable`1[Int32]]]", new Func<Task<IObservable<int>>>(() => Task.FromResult(Observable.Return(1))) },
  93. { "Func`1[IDisposable]", new Func<IDisposable>(() => Disposable.Empty) },
  94. { "Func`1[Task]", new Func<Task>(() => Task.FromResult(1)) },
  95. { "Func`1[Task`1[Int32]]", new Func<Task<int>>(() => Task.FromResult(1)) },
  96. { "Func`1[IEnumerable`1[IObservable`1[Object]]]", new Func<IEnumerable<IObservable<object>>>(() => new[] { Observable.Return((object)1) }) },
  97. { "Func`2[Int32, IObservable`1[Int32]]", new Func<int, IObservable<int>>(v => Observable.Return(v)) },
  98. { "Func`2[Exception, IObservable`1[Int32]]", new Func<Exception, IObservable<int>>(v => Observable.Return(1)) },
  99. { "Func`2[Int32, Task`1[Int32]]", new Func<int, Task<int>>(v => Task.FromResult(v)) },
  100. { "Func`2[Int32, Int32]", new Func<int, int>(v => v) },
  101. { "Func`2[Int32, IEnumerable`1[Int32]]", new Func<int, IEnumerable<int>>(v => new[] { v }) },
  102. { "Func`2[Int32, Boolean]", new Func<int, bool>(v => true) },
  103. { "Func`2[Int32, TimeSpan]", new Func<int, TimeSpan>(v => TimeSpan.FromMilliseconds(1)) },
  104. { "Func`2[Int32, DateTimeOffset]", new Func<int, DateTimeOffset>(v => DateTimeOffset.Now) },
  105. { "Func`2[IList`1[Int32], Int32]", new Func<IList<int>, int>(v => v.Count) },
  106. { "Func`2[Int32, Nullable`1[Double]]", new Func<int, double?>(v => v) },
  107. { "Func`2[Int32, Nullable`1[Single]]", new Func<int, float?>(v => v) },
  108. { "Func`2[Int32, Nullable`1[Int32]]", new Func<int, int?>(v => v) },
  109. { "Func`2[Int32, Nullable`1[Decimal]]", new Func<int, decimal?>(v => v) },
  110. { "Func`2[Int32, Nullable`1[Int64]]", new Func<int, long?>(v => v) },
  111. { "Func`2[Int32, Double]", new Func<int, double>(v => v) },
  112. { "Func`2[Int32, Single]", new Func<int, float>(v => v) },
  113. { "Func`2[Int32, Decimal]", new Func<int, decimal>(v => v) },
  114. { "Func`2[Int32, Int64]", new Func<int, long>(v => v) },
  115. { "Func`2[IObservable`1[Object], IObservable`1[Int32]]", new Func<IObservable<object>, IObservable<int>>(v => v.Select(w => 1)) },
  116. { "Func`2[IObservable`1[Exception], IObservable`1[Int32]]", new Func<IObservable<Exception>, IObservable<int>>(v => v.Select(w => 1)) },
  117. { "Func`2[IGroupedObservable`2[Int32, Int32], IObservable`1[Int32]]", new Func<IGroupedObservable<int, int>, IObservable<int>>(v => v) },
  118. { "Func`2[IObservable`1[Int32], IObservable`1[Int32]]", new Func<IObservable<int>, IObservable<int>>(v => v.Select(w => 1)) },
  119. { "Func`2[CancellationToken, Task`1[IObservable`1[Int32]]]", new Func<CancellationToken, Task<IObservable<int>>>(v => Task.FromResult(Observable.Return(1))) },
  120. { "Func`2[IDisposable, Task`1[IObservable`1[Int32]]]", new Func<IDisposable, Task<IObservable<int>>>(v => Task.FromResult(Observable.Return(1))) },
  121. { "Func`2[IDisposable, IObservable`1[Int32]]", new Func<IDisposable, IObservable<int>>(v => Observable.Return(1)) },
  122. { "Func`2[CancellationToken, Task`1[IDisposable]]", new Func<CancellationToken, Task<IDisposable>>(v => Task.FromResult(Disposable.Empty)) },
  123. { "Func`2[EventHandler`1[Int32], Int32]", new Func<EventHandler<int>, int>(v => 1) },
  124. { "Func`2[Action`1[Int32], Int32]", new Func<Action<int>, int>(v => 1) },
  125. { "Func`2[IObserver`1[Int32], IDisposable]", new Func<IObserver<int>, IDisposable>(v => Disposable.Empty) },
  126. { "Func`2[IObserver`1[Int32], Action]", new Func<IObserver<int>, Action>(v => () => { }) },
  127. { "Func`2[IObserver`1[Int32], Task]", new Func<IObserver<int>, Task>(v => Task.FromResult(1)) },
  128. { "Func`2[IObserver`1[Int32], Task`1[IDisposable]]", new Func<IObserver<int>, Task<IDisposable>>(v => Task.FromResult(Disposable.Empty)) },
  129. { "Func`2[IObserver`1[Int32], Task`1[Action]]", new Func<IObserver<int>, Task<Action>>(v => Task.FromResult<Action>(() => { })) },
  130. { "Func`2[CancellationToken, Task]", new Func<CancellationToken, Task>(v => Task.FromResult(1)) },
  131. { "Func`2[CancellationToken, Task`1[Int32]]", new Func<CancellationToken, Task<int>>(v => Task.FromResult(1)) },
  132. { "Func`2[IAsyncResult, Int32]", new Func<IAsyncResult, int>(v => 1) },
  133. { "Func`2[IObserver`1[Int32], IEnumerable`1[IObservable`1[Object]]]", new Func<IObserver<int>, IEnumerable<IObservable<object>>>(v => new[] { Observable.Return((object)1) }) },
  134. { "Func`2[IObservable`1[Int32], Int32]", new Func<IObservable<int>, int>(v => 1) },
  135. { "Func`3[Int32, Int32, IObservable`1[Int32]]", new Func<int, int, IObservable<int>>((v, u) => Observable.Return(v + u)) },
  136. { "Func`3[Int32, Int32, Task`1[Int32]]", new Func<int, int, Task<int>>((v, u) => Task.FromResult(v + u)) },
  137. { "Func`3[Int32, CancellationToken, Task`1[Int32]]", new Func<int, CancellationToken, Task<int>>((v, u) => Task.FromResult(v)) },
  138. { "Func`3[Int32, Int32, Int32]", new Func<int, int, int>((v, u) => v + u) },
  139. { "Func`3[Int32, Int32, IEnumerable`1[Int32]]", new Func<int, int, IEnumerable<int>>((v, u) => new[] { v, u }) },
  140. { "Func`3[Int32, Int32, Boolean]", new Func<int, int, bool>((v, u) => true) },
  141. { "Func`3[Int32, IObservable`1[Int32], Int32]", new Func<int, IObservable<int>, int>((v, u) => v) },
  142. { "Func`3[IDisposable, CancellationToken, Task`1[IObservable`1[Int32]]]", new Func<IDisposable, CancellationToken, Task<IObservable<int>>>((v, u) => Task.FromResult(Observable.Return(1))) },
  143. { "Func`3[IObserver`1[Int32], CancellationToken, Task]", new Func<IObserver<int>, CancellationToken, Task>((v, w) => Task.FromResult(1)) },
  144. { "Func`3[IObserver`1[Int32], CancellationToken, Task`1[IDisposable]]", new Func<IObserver<int>, CancellationToken, Task<IDisposable>>((v, w) => Task.FromResult(Disposable.Empty)) },
  145. { "Func`3[IObserver`1[Int32], CancellationToken, Task`1[Action]]", new Func<IObserver<int>, CancellationToken, Task<Action>>((v, w) => Task.FromResult<Action>(() => { })) },
  146. { "Func`3[AsyncCallback, Object, IAsyncResult]", new Func<AsyncCallback, object, IAsyncResult>((v, w) => null) },
  147. { "Func`4[Int32, Int32, CancellationToken, Task`1[Int32]]", new Func<int, int, CancellationToken, Task<int>>((v, u, w) => Task.FromResult(v)) },
  148. { "Func`4[Int32, Int32, Int32, Int32]", new Func<int, int, int, int>((v1, v2, v3) => v1 + v2 + v3) },
  149. { "Func`4[Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, AsyncCallback, object, IAsyncResult>((v, w, x) => null) },
  150. { "Func`5[Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int>((v1, v2, v3, v4) => v1 + v2 + v3 + v4) },
  151. { "Func`6[Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int>((v1, v2, v3, v4, v5) => v1 + v2 + v3 + v4 + v5) },
  152. { "Func`7[Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6) => v1 + v2 + v3 + v4 + v5 + v6) },
  153. { "Func`8[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7) => v1 + v2 + v3 + v4 + v5 + v6 + v7) },
  154. { "Func`9[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8) => v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8) },
  155. { "Func`10[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9) => v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9) },
  156. { "Func`11[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) => v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10) },
  157. { "Func`12[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) => v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10 + v11) },
  158. { "Func`13[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) => v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10 + v11 + v12) },
  159. { "Func`14[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) => v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10 + v11 + v12 + v13) },
  160. { "Func`15[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) => v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10 + v11 + v12 + v13 + v14) },
  161. { "Func`16[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) => v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10 + v11 + v12 + v13 + v14 + v15) },
  162. { "Func`17[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Func<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) => v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9 + v10 + v11 + v12 + v13 + v14 + v15 + v16) },
  163. {
  164. "Plan`1[][Int32]",
  165. new Plan<int>[] {
  166. Observable.Return(1).Then(v => v)
  167. }
  168. },
  169. {
  170. "IEnumerable`1[Plan`1[Int32]]",
  171. new Plan<int>[] {
  172. Observable.Return(1).Then(v => v)
  173. }
  174. },
  175. { "Action`3[Int32, Int32, Int32]", new Action<int, int, int>((v1, v2, v3) => { }) },
  176. { "Action`4[Int32, Int32, Int32, Int32]", new Action<int, int, int, int>((v1, v2, v3, v4) => { }) },
  177. { "Action`5[Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int>((v1, v2, v3, v4, v5) => { }) },
  178. { "Action`6[Int32, Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6) => { }) },
  179. { "Action`7[Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7) => { }) },
  180. { "Action`8[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8) => { }) },
  181. { "Action`9[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9) => { }) },
  182. { "Action`10[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) => { }) },
  183. { "Action`11[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) => { }) },
  184. { "Action`12[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) => { }) },
  185. { "Action`13[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) => { }) },
  186. { "Action`14[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) => { }) },
  187. { "Action`15[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) => { }) },
  188. { "Action`16[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32]", new Action<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) => { }) },
  189. { "Func`5[Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4) => null) },
  190. { "Func`6[Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5) => null) },
  191. { "Func`7[Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6) => null) },
  192. { "Func`8[Int32, Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6, v7) => null) },
  193. { "Func`9[Int32, Int32, Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6, v7, v8) => null) },
  194. { "Func`10[Int32, Int32, Int32, Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6, v7, v8, v9) => null) },
  195. { "Func`11[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) => null) },
  196. { "Func`12[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, int, int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) => null) },
  197. { "Func`13[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, int, int, int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) => null) },
  198. { "Func`14[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, int, int, int, int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) => null) },
  199. { "Func`15[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, int, int, int, int, int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) => null) },
  200. { "Func`16[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, int, int, int, int, int, int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) => null) },
  201. { "Func`17[Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, AsyncCallback, Object, IAsyncResult]", new Func<int, int, int, int, int, int, int, int, int, int, int, int, int, int, AsyncCallback, object, IAsyncResult>((v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) => null) }
  202. };
  203. }
  204. #endregion
  205. [TestMethod]
  206. public void Verify_Observable()
  207. {
  208. VerifyClass(typeof(Observable));
  209. }
  210. [TestMethod]
  211. public void Verify_ObservableEx()
  212. {
  213. VerifyClass(typeof(ObservableEx));
  214. }
  215. #region + Verification method +
  216. /// <summary>
  217. /// Verify that public static members of the class
  218. /// check for nulls in their arguments as
  219. /// well as when invoking Subscribe with null.
  220. /// </summary>
  221. /// <param name="type">The type to verify.</param>
  222. private static void VerifyClass(Type type)
  223. {
  224. foreach (var method in type.GetMethods())
  225. {
  226. // public static only (skip methods like Equals)
  227. if (!method.IsPublic || !method.IsStatic)
  228. {
  229. continue;
  230. }
  231. var m = default(MethodInfo);
  232. // Is this a generic method?
  233. if (method.IsGenericMethodDefinition)
  234. {
  235. // we need to specialize it to concrete types
  236. // for the reflective call to work
  237. // get the type arguments
  238. var ga = method.GetGenericArguments();
  239. var targs = new Type[ga.Length];
  240. // fill in the type arguments
  241. for (var k = 0; k < targs.Length; k++)
  242. {
  243. // watch out for type constrains
  244. // the default typeof(int) will not work when
  245. // exception or IDisposable is required at minimum
  246. var gac = ga[k].GetGenericParameterConstraints();
  247. // no type constraints
  248. if (gac.Length == 0)
  249. {
  250. targs[k] = typeof(int);
  251. }
  252. else if (gac[0] == typeof(Exception))
  253. {
  254. targs[k] = typeof(Exception);
  255. }
  256. else if (gac[0] == typeof(IDisposable))
  257. {
  258. targs[k] = typeof(IDisposable);
  259. }
  260. else
  261. {
  262. // If we get here, a new rule should be added above
  263. throw new Exception("Unknown constraint: " + gac + "\r\n" + method);
  264. }
  265. }
  266. // generate a specialized method with the concrete generic arguments
  267. try
  268. {
  269. m = method.MakeGenericMethod(targs);
  270. }
  271. catch (Exception ex)
  272. {
  273. throw new Exception("MakeGenericMethod threw: " + method, ex);
  274. }
  275. }
  276. else
  277. {
  278. // non generic method, we can invoke this directly
  279. m = method;
  280. }
  281. var args = m.GetParameters();
  282. // for each parameter of the (generic) method
  283. for (var i = 0; i < args.Length; i++)
  284. {
  285. // prepare a pattern for the method invocation
  286. var margs = new object[args.Length];
  287. // some arguments can be null, often indicated with a default == null marker
  288. // this tracks this case and forgives for not throwing an ArgumentNullException
  289. var argumentCanBeNull = true;
  290. // for each argument index
  291. // with the loop i, this creates an N x N matrix where in each row, one argument is null
  292. for (var j = 0; j < args.Length; j++)
  293. {
  294. // figure out the type of the argument
  295. var pt = args[j].ParameterType;
  296. // by using some type naming convention as string
  297. var paramTypeName = TypeNameOf(pt);
  298. // classes, interfaces, arrays and abstract markers can be null
  299. // for the diagonal entries of the test matrix
  300. if (j == i && (pt.IsClass || pt.IsInterface || pt.IsArray || pt.IsAbstract))
  301. {
  302. margs[j] = null;
  303. // check if the argument can be actually
  304. argumentCanBeNull = args[j].HasDefaultValue && args[j].DefaultValue == null;
  305. }
  306. else
  307. {
  308. // this argument is not tested for null or is not a null type
  309. // find the default instance for it
  310. if (_defaultValues.ContainsKey(paramTypeName))
  311. {
  312. margs[j] = _defaultValues[paramTypeName];
  313. }
  314. else
  315. {
  316. // default values have to be instantiated in _defaultValues for
  317. // each possible generic type arguments.
  318. // this will indicate what concrete instance value is missing.
  319. throw new Exception("Default instance not found for: " + paramTypeName + "\r\n\r\n" + m);
  320. }
  321. }
  322. }
  323. // assume it threw
  324. var thrown = true;
  325. var obj = default(object);
  326. try
  327. {
  328. obj = m.Invoke(null, margs);
  329. thrown = false;
  330. }
  331. catch (ArgumentNullException)
  332. {
  333. // expected exception, just in case
  334. }
  335. catch (Exception ex)
  336. {
  337. // reflection wraps the actual exception, let's unwrap it
  338. if (!(ex.InnerException is ArgumentNullException))
  339. {
  340. throw new Exception("Method threw: " + method + " @ " + i, ex);
  341. }
  342. }
  343. // if the call didn't throw and the argument being tested isn't defaulted to null, throw
  344. if (!thrown && !argumentCanBeNull)
  345. {
  346. throw new Exception("Should have thrown: " + method + " @ " + i);
  347. }
  348. // if the call didn't throw and returned a null object, throw
  349. // no operators should return null
  350. if (obj == null && !thrown)
  351. {
  352. throw new NullReferenceException("null return: " + method + " @ " + i);
  353. }
  354. }
  355. // Now check the same method with valid arguments but
  356. // Subscribe(null) if it returns an IObservable subclass
  357. if (m.ReturnType.Name.Equals("IObservable`1")
  358. || m.ReturnType.Name.Equals("IConnectableObservable`1"))
  359. {
  360. // these will fail other argument validation with the defaults, skip them
  361. if (m.Name.Equals("FromEventPattern"))
  362. {
  363. continue;
  364. }
  365. // prepare method arguments
  366. var margs = new object[args.Length];
  367. for (var j = 0; j < args.Length; j++)
  368. {
  369. var pt = args[j].ParameterType;
  370. var paramTypeName = TypeNameOf(pt);
  371. if (_defaultValues.ContainsKey(paramTypeName))
  372. {
  373. margs[j] = _defaultValues[paramTypeName];
  374. }
  375. else
  376. {
  377. // default values have to be instantiated in _defaultValues for
  378. // each possible generic type arguments.
  379. // this will indicate what concrete instance value is missing.
  380. //
  381. // it may fail independently of the null test above because
  382. // the particular type is non-nullable thus skipped above
  383. // or was the solo argument and it got never tested with a non-null
  384. // value
  385. throw new Exception("Default instance not found (Subscribe(null) check): " + paramTypeName + "\r\n\r\n" + m);
  386. }
  387. }
  388. // Assume it throws
  389. var thrown = true;
  390. try
  391. {
  392. // Should not return null, but would be mistaken for
  393. // throwing because of Subscribe(null)
  394. if (m.Invoke(null, margs) is IObservable<int> o)
  395. {
  396. o.Subscribe(null);
  397. thrown = false;
  398. }
  399. }
  400. catch (ArgumentNullException)
  401. {
  402. // expected
  403. }
  404. catch (Exception ex)
  405. {
  406. // Unexpected exception
  407. // Maybe some other validation failed inside the method call
  408. // Consider skipping this method (set)
  409. //
  410. // Otherwise, the operator may run with the null IObserver
  411. // for a while and crash later.
  412. throw new Exception("Method threw (Subscribe(null) check): " + m, ex);
  413. }
  414. // If it didn't throw, report it
  415. if (!thrown)
  416. {
  417. throw new Exception("Should have thrown (Subscribe(null) check): " + m);
  418. }
  419. }
  420. }
  421. }
  422. /// <summary>
  423. /// Generate a string representation of a possibly generic type
  424. /// that is not verbose (i.e, no "System." everywhere).
  425. /// </summary>
  426. /// <param name="type">The type to get a string representation</param>
  427. /// <returns>The string representation of a possibly generic type</returns>
  428. private static string TypeNameOf(Type type)
  429. {
  430. var ga = type.GetGenericArguments();
  431. if (ga.Length == 0)
  432. {
  433. return type.Name;
  434. }
  435. return type.Name + "[" + string.Join(", ", ga.Select(t => TypeNameOf(t)).ToArray()) + "]";
  436. }
  437. #endregion
  438. }
  439. }