ExpressionObserverTests_SetValue.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) The Perspex 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. using System.Collections.Generic;
  5. using System.Reactive.Linq;
  6. using Perspex.Markup.Data;
  7. using Xunit;
  8. namespace Perspex.Markup.UnitTests.Binding
  9. {
  10. public class ExpressionObserverTests_SetValue
  11. {
  12. [Fact]
  13. public void Should_Set_Simple_Property_Value()
  14. {
  15. var data = new { Foo = "foo" };
  16. var target = new ExpressionObserver(data, "Foo");
  17. target.SetValue("bar");
  18. Assert.Equal("foo", data.Foo);
  19. }
  20. [Fact]
  21. public void Should_Set_Value_On_Simple_Property_Chain()
  22. {
  23. var data = new Class1 { Foo = new Class2 { Bar = "bar" } };
  24. var target = new ExpressionObserver(data, "Foo.Bar");
  25. target.SetValue("foo");
  26. Assert.Equal("foo", data.Foo.Bar);
  27. }
  28. [Fact]
  29. public void Should_Not_Try_To_Set_Value_On_Broken_Chain()
  30. {
  31. var data = new Class1 { Foo = new Class2 { Bar = "bar" } };
  32. var target = new ExpressionObserver(data, "Foo.Bar");
  33. // Ensure the ExpressionObserver's subscriptions are kept active.
  34. target.OfType<string>().Subscribe(x => { });
  35. data.Foo = null;
  36. Assert.False(target.SetValue("foo"));
  37. }
  38. private class Class1 : NotifyingBase
  39. {
  40. private Class2 _foo;
  41. public Class2 Foo
  42. {
  43. get { return _foo; }
  44. set
  45. {
  46. _foo = value;
  47. RaisePropertyChanged(nameof(Foo));
  48. }
  49. }
  50. }
  51. private class Class2 : NotifyingBase
  52. {
  53. private string _bar;
  54. public string Bar
  55. {
  56. get { return _bar; }
  57. set
  58. {
  59. _bar = value;
  60. RaisePropertyChanged(nameof(Bar));
  61. }
  62. }
  63. }
  64. }
  65. }