AvaloniaObjectTests_GetSubject.cs 1.2 KB

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