EventPatternSourceBaseTest.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. ReactiveAssert.Throws<ArgumentNullException>(() => new MyEventPatternSource(null, (a, x) => { }));
  19. ReactiveAssert.Throws<ArgumentNullException>(() => new MyEventPatternSource(xs, null));
  20. var e = new MyEventPatternSource(xs, (a, x) => { })
  21. {
  22. GetInvoke = h => (_, __) => { }
  23. };
  24. ReactiveAssert.Throws<ArgumentNullException>(() => e.OnNext += null);
  25. e.GetInvoke = h => null;
  26. ReactiveAssert.Throws<ArgumentNullException>(() => e.OnNext += (_, __) => { });
  27. e.GetInvoke = null;
  28. ReactiveAssert.Throws<ArgumentNullException>(() => e.OnNext -= null);
  29. }
  30. }
  31. internal class MyEventPatternSource : EventPatternSourceBase<object, EventArgs>
  32. {
  33. public MyEventPatternSource(IObservable<EventPattern<object, EventArgs>> source, Action<Action<object, EventArgs>, EventPattern<object, EventArgs>> invokeHandler)
  34. : base(source, invokeHandler)
  35. {
  36. }
  37. public Func<EventHandler<EventArgs>, Action<object, EventArgs>> GetInvoke;
  38. public event EventHandler<EventArgs> OnNext
  39. {
  40. add
  41. {
  42. Add(value, GetInvoke(value));
  43. }
  44. remove
  45. {
  46. Remove(value);
  47. }
  48. }
  49. }
  50. }