EventPatternSourceBaseTest.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.Reactive;
  6. using System.Reactive.Linq;
  7. using Microsoft.Reactive.Testing;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. namespace ReactiveTests.Tests
  10. {
  11. [TestClass]
  12. public class EventPatternSourceBaseTest
  13. {
  14. [TestMethod]
  15. public void ArgumentChecking()
  16. {
  17. var xs = Observable.Empty<EventPattern<object, EventArgs>>();
  18. #pragma warning disable CA1806 // (Unused new instance.) We expect the constructor to throw.
  19. ReactiveAssert.Throws<ArgumentNullException>(() => new MyEventPatternSource(null, (a, x) => { }));
  20. ReactiveAssert.Throws<ArgumentNullException>(() => new MyEventPatternSource(xs, null));
  21. #pragma warning restore CA1806
  22. var e = new MyEventPatternSource(xs, (a, x) => { })
  23. {
  24. GetInvoke = h => (_, __) => { }
  25. };
  26. ReactiveAssert.Throws<ArgumentNullException>(() => e.OnNext += null);
  27. e.GetInvoke = h => null;
  28. ReactiveAssert.Throws<ArgumentNullException>(() => e.OnNext += (_, __) => { });
  29. e.GetInvoke = null;
  30. ReactiveAssert.Throws<ArgumentNullException>(() => e.OnNext -= null);
  31. }
  32. }
  33. internal class MyEventPatternSource : EventPatternSourceBase<object, EventArgs>
  34. {
  35. public MyEventPatternSource(IObservable<EventPattern<object, EventArgs>> source, Action<Action<object, EventArgs>, EventPattern<object, EventArgs>> invokeHandler)
  36. : base(source, invokeHandler)
  37. {
  38. }
  39. public Func<EventHandler<EventArgs>, Action<object, EventArgs>> GetInvoke;
  40. public event EventHandler<EventArgs> OnNext
  41. {
  42. add
  43. {
  44. Add(value, GetInvoke(value));
  45. }
  46. remove
  47. {
  48. Remove(value);
  49. }
  50. }
  51. }
  52. }