AvaloniaObjectTests_GetSubject.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Linq;
  4. using Xunit;
  5. namespace Avalonia.Base.UnitTests
  6. {
  7. public class AvaloniaObjectTests_GetSubject
  8. {
  9. [Fact]
  10. public void GetSubject_Returns_Values()
  11. {
  12. var source = new Class1 { Foo = "foo" };
  13. var target = source.GetSubject(Class1.FooProperty);
  14. var result = new List<string>();
  15. target.Subscribe(x => result.Add(x));
  16. source.Foo = "bar";
  17. source.Foo = "baz";
  18. Assert.Equal(new[] { "foo", "bar", "baz" }, result);
  19. }
  20. [Fact]
  21. public void GetSubject_Sets_Values()
  22. {
  23. var source = new Class1 { Foo = "foo" };
  24. var target = source.GetSubject(Class1.FooProperty);
  25. target.OnNext("bar");
  26. Assert.Equal("bar", source.Foo);
  27. }
  28. private class Class1 : AvaloniaObject
  29. {
  30. public static readonly StyledProperty<string> FooProperty =
  31. AvaloniaProperty.Register<Class1, string>(nameof(Foo), "foodefault");
  32. public string Foo
  33. {
  34. get { return GetValue(FooProperty); }
  35. set { SetValue(FooProperty, value); }
  36. }
  37. }
  38. }
  39. }