AvaloniaObjectTests_AddOwner.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. using Xunit;
  5. namespace Avalonia.Base.UnitTests
  6. {
  7. public class AvaloniaObjectTests_AddOwner
  8. {
  9. [Fact]
  10. public void AddOwnered_Property_Retains_Default_Value()
  11. {
  12. var target = new Class2();
  13. Assert.Equal("foodefault", target.GetValue(Class2.FooProperty));
  14. }
  15. [Fact]
  16. public void AddOwnered_Property_Does_Not_Retain_Validation()
  17. {
  18. var target = new Class2();
  19. target.SetValue(Class2.FooProperty, "throw");
  20. }
  21. private class Class1 : AvaloniaObject
  22. {
  23. public static readonly StyledProperty<string> FooProperty =
  24. AvaloniaProperty.Register<Class1, string>(
  25. "Foo",
  26. "foodefault",
  27. validate: ValidateFoo);
  28. private static string ValidateFoo(AvaloniaObject arg1, string arg2)
  29. {
  30. if (arg2 == "throw")
  31. {
  32. throw new IndexOutOfRangeException();
  33. }
  34. return arg2;
  35. }
  36. }
  37. private class Class2 : AvaloniaObject
  38. {
  39. public static readonly StyledProperty<string> FooProperty =
  40. Class1.FooProperty.AddOwner<Class2>();
  41. }
  42. }
  43. }