AvaloniaObjectTests_Validation.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 System.Reactive.Subjects;
  5. using Xunit;
  6. namespace Avalonia.Base.UnitTests
  7. {
  8. public class AvaloniaObjectTests_Validation
  9. {
  10. [Fact]
  11. public void SetValue_Causes_Validation()
  12. {
  13. var target = new Class1();
  14. target.SetValue(Class1.QuxProperty, 5);
  15. Assert.Throws<ArgumentOutOfRangeException>(() => target.SetValue(Class1.QuxProperty, 25));
  16. Assert.Equal(5, target.GetValue(Class1.QuxProperty));
  17. }
  18. [Fact]
  19. public void SetValue_Causes_Coercion()
  20. {
  21. var target = new Class1();
  22. target.SetValue(Class1.QuxProperty, 5);
  23. Assert.Equal(5, target.GetValue(Class1.QuxProperty));
  24. target.SetValue(Class1.QuxProperty, -5);
  25. Assert.Equal(0, target.GetValue(Class1.QuxProperty));
  26. target.SetValue(Class1.QuxProperty, 15);
  27. Assert.Equal(10, target.GetValue(Class1.QuxProperty));
  28. }
  29. [Fact]
  30. public void Revalidate_Causes_Recoercion()
  31. {
  32. var target = new Class1();
  33. target.SetValue(Class1.QuxProperty, 7);
  34. Assert.Equal(7, target.GetValue(Class1.QuxProperty));
  35. target.MaxQux = 5;
  36. target.Revalidate(Class1.QuxProperty);
  37. }
  38. [Fact]
  39. public void Validation_Can_Be_Overridden()
  40. {
  41. var target = new Class2();
  42. Assert.Throws<ArgumentOutOfRangeException>(() => target.SetValue(Class1.QuxProperty, 5));
  43. }
  44. [Fact]
  45. public void Validation_Can_Be_Overridden_With_Null()
  46. {
  47. var target = new Class3();
  48. target.SetValue(Class1.QuxProperty, 50);
  49. Assert.Equal(50, target.GetValue(Class1.QuxProperty));
  50. }
  51. [Fact]
  52. public void Binding_To_UnsetValue_Doesnt_Throw()
  53. {
  54. var target = new Class1();
  55. var source = new Subject<object>();
  56. target.Bind(Class1.QuxProperty, source);
  57. source.OnNext(AvaloniaProperty.UnsetValue);
  58. }
  59. [Fact]
  60. public void Attached_Property_Should_Be_Validated()
  61. {
  62. var target = new Class2();
  63. target.SetValue(Class1.AttachedProperty, 15);
  64. Assert.Equal(10, target.GetValue(Class1.AttachedProperty));
  65. }
  66. private class Class1 : AvaloniaObject
  67. {
  68. public static readonly StyledProperty<int> QuxProperty =
  69. AvaloniaProperty.Register<Class1, int>("Qux", validate: Validate);
  70. public static readonly AttachedProperty<int> AttachedProperty =
  71. AvaloniaProperty.RegisterAttached<Class1, Class2, int>("Attached", validate: Validate);
  72. public Class1()
  73. {
  74. MaxQux = 10;
  75. ErrorQux = 20;
  76. }
  77. public int MaxQux { get; set; }
  78. public int ErrorQux { get; }
  79. private static int Validate(Class1 instance, int value)
  80. {
  81. if (value > instance.ErrorQux)
  82. {
  83. throw new ArgumentOutOfRangeException();
  84. }
  85. return Math.Min(Math.Max(value, 0), ((Class1)instance).MaxQux);
  86. }
  87. private static int Validate(Class2 instance, int value)
  88. {
  89. return Math.Min(value, 10);
  90. }
  91. }
  92. private class Class2 : AvaloniaObject
  93. {
  94. public static readonly StyledProperty<int> QuxProperty =
  95. Class1.QuxProperty.AddOwner<Class2>();
  96. static Class2()
  97. {
  98. QuxProperty.OverrideValidation<Class2>(Validate);
  99. }
  100. private static int Validate(Class2 instance, int value)
  101. {
  102. if (value < 100)
  103. {
  104. throw new ArgumentOutOfRangeException();
  105. }
  106. return value;
  107. }
  108. }
  109. private class Class3 : Class2
  110. {
  111. static Class3()
  112. {
  113. QuxProperty.OverrideValidation<Class3>(null);
  114. }
  115. }
  116. }
  117. }