PerspexObjectTests_Validation.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.Reactive.Subjects;
  5. using Xunit;
  6. namespace Perspex.Base.UnitTests
  7. {
  8. public class PerspexObjectTests_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(PerspexProperty.UnsetValue);
  58. }
  59. private class Class1 : PerspexObject
  60. {
  61. public static readonly StyledProperty<int> QuxProperty =
  62. PerspexProperty.Register<Class1, int>("Qux", validate: Validate);
  63. public Class1()
  64. {
  65. MaxQux = 10;
  66. ErrorQux = 20;
  67. }
  68. public int MaxQux { get; set; }
  69. public int ErrorQux { get; }
  70. private static int Validate(Class1 instance, int value)
  71. {
  72. if (value > instance.ErrorQux)
  73. {
  74. throw new ArgumentOutOfRangeException();
  75. }
  76. return Math.Min(Math.Max(value, 0), ((Class1)instance).MaxQux);
  77. }
  78. }
  79. private class Class2 : PerspexObject
  80. {
  81. public static readonly StyledProperty<int> QuxProperty =
  82. Class1.QuxProperty.AddOwner<Class2>();
  83. static Class2()
  84. {
  85. QuxProperty.OverrideValidation<Class2>(Validate);
  86. }
  87. private static int Validate(Class2 instance, int value)
  88. {
  89. if (value < 100)
  90. {
  91. throw new ArgumentOutOfRangeException();
  92. }
  93. return value;
  94. }
  95. }
  96. private class Class3 : Class2
  97. {
  98. static Class3()
  99. {
  100. QuxProperty.OverrideValidation<Class3>(null);
  101. }
  102. }
  103. }
  104. }