TestObserver.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. namespace Avalonia.Base.UnitTests.Styling
  3. {
  4. internal class TestObserver<T> : IObserver<T>
  5. {
  6. private bool _hasValue;
  7. private T _value;
  8. public bool Completed { get; private set; }
  9. public Exception Error { get; private set; }
  10. public T GetValue()
  11. {
  12. if (!_hasValue)
  13. {
  14. throw new Exception("Observable provided no value.");
  15. }
  16. if (Completed)
  17. {
  18. throw new Exception("Observable completed unexpectedly.");
  19. }
  20. if (Error != null)
  21. {
  22. throw new Exception("Observable errored unexpectedly.");
  23. }
  24. _hasValue = false;
  25. return _value;
  26. }
  27. public void OnCompleted()
  28. {
  29. Completed = true;
  30. }
  31. public void OnError(Exception error)
  32. {
  33. Error = error;
  34. }
  35. public void OnNext(T value)
  36. {
  37. if (!_hasValue)
  38. {
  39. _value = value;
  40. _hasValue = true;
  41. }
  42. else
  43. {
  44. throw new Exception("Observable pushed more than one value.");
  45. }
  46. }
  47. }
  48. }