EventPatternSourceBaseTest.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 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 Xunit;
  9. namespace ReactiveTests.Tests
  10. {
  11. public class EventPatternSourceBaseTest
  12. {
  13. [Fact]
  14. public void ArgumentChecking()
  15. {
  16. var xs = Observable.Empty<EventPattern<object, EventArgs>>();
  17. ReactiveAssert.Throws<ArgumentNullException>(() => new MyEventPatternSource(null, (a, x) => { }));
  18. ReactiveAssert.Throws<ArgumentNullException>(() => new MyEventPatternSource(xs, null));
  19. var e = new MyEventPatternSource(xs, (a, x) => { });
  20. e.GetInvoke = h => (_, __) => { };
  21. ReactiveAssert.Throws<ArgumentNullException>(() => e.OnNext += null);
  22. e.GetInvoke = h => null;
  23. ReactiveAssert.Throws<ArgumentNullException>(() => e.OnNext += (_, __) => { });
  24. e.GetInvoke = null;
  25. ReactiveAssert.Throws<ArgumentNullException>(() => e.OnNext -= null);
  26. }
  27. }
  28. class MyEventPatternSource : EventPatternSourceBase<object, EventArgs>
  29. {
  30. public MyEventPatternSource(IObservable<EventPattern<object, EventArgs>> source, Action<Action<object, EventArgs>, EventPattern<object, EventArgs>> invokeHandler)
  31. : base(source, invokeHandler)
  32. {
  33. }
  34. public Func<EventHandler<EventArgs>, Action<object, EventArgs>> GetInvoke;
  35. public event EventHandler<EventArgs> OnNext
  36. {
  37. add
  38. {
  39. base.Add(value, GetInvoke(value));
  40. }
  41. remove
  42. {
  43. Remove(value);
  44. }
  45. }
  46. }
  47. }