TestObserver.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. namespace Avalonia.Styling.UnitTests
  5. {
  6. internal class TestObserver<T> : IObserver<T>
  7. {
  8. private bool _hasValue;
  9. private T _value;
  10. public bool Completed { get; private set; }
  11. public Exception Error { get; private set; }
  12. public T GetValue()
  13. {
  14. if (!_hasValue)
  15. {
  16. throw new Exception("Observable provided no value.");
  17. }
  18. if (Completed)
  19. {
  20. throw new Exception("Observable completed unexpectedly.");
  21. }
  22. if (Error != null)
  23. {
  24. throw new Exception("Observable errored unexpectedly.");
  25. }
  26. _hasValue = false;
  27. return _value;
  28. }
  29. public void OnCompleted()
  30. {
  31. Completed = true;
  32. }
  33. public void OnError(Exception error)
  34. {
  35. Error = error;
  36. }
  37. public void OnNext(T value)
  38. {
  39. if (!_hasValue)
  40. {
  41. _value = value;
  42. _hasValue = true;
  43. }
  44. else
  45. {
  46. throw new Exception("Observable pushed more than one value.");
  47. }
  48. }
  49. }
  50. }