IndeiValidatorTests.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Runtime.CompilerServices;
  8. using Avalonia.Data;
  9. using Avalonia.Markup.Data.Plugins;
  10. using Xunit;
  11. namespace Avalonia.Markup.UnitTests.Data
  12. {
  13. public class IndeiValidatorTests
  14. {
  15. [Fact]
  16. public void Setting_Non_Validating_Does_Not_Trigger_Validation()
  17. {
  18. var inpcAccessorPlugin = new InpcPropertyAccessorPlugin();
  19. var validatorPlugin = new IndeiValidationPlugin();
  20. var data = new Data();
  21. var accessor = inpcAccessorPlugin.Start(new WeakReference(data), nameof(data.NonValidated));
  22. var validator = validatorPlugin.Start(new WeakReference(data), nameof(data.NonValidated), accessor);
  23. var results = new List<object>();
  24. validator.Subscribe(x => results.Add(x));
  25. validator.SetValue(5, BindingPriority.LocalValue);
  26. Assert.Equal(
  27. new[]
  28. {
  29. new BindingNotification(0),
  30. new BindingNotification(5),
  31. }, results);
  32. }
  33. [Fact]
  34. public void Setting_Validating_Property_To_Valid_Value_Returns_Successful_BindingNotification()
  35. {
  36. var inpcAccessorPlugin = new InpcPropertyAccessorPlugin();
  37. var validatorPlugin = new IndeiValidationPlugin();
  38. var data = new Data { MustBePositive = 1 };
  39. var accessor = inpcAccessorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive));
  40. var validator = validatorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive), accessor);
  41. var results = new List<object>();
  42. validator.Subscribe(x => results.Add(x));
  43. validator.SetValue(5, BindingPriority.LocalValue);
  44. Assert.Equal(
  45. new[]
  46. {
  47. new BindingNotification(1),
  48. new BindingNotification(5),
  49. }, results);
  50. }
  51. [Fact]
  52. public void Setting_Validating_Property_To_Invalid_Value_Returns_DataValidationError()
  53. {
  54. var inpcAccessorPlugin = new InpcPropertyAccessorPlugin();
  55. var validatorPlugin = new IndeiValidationPlugin();
  56. var data = new Data { MustBePositive = 1 };
  57. var accessor = inpcAccessorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive));
  58. var validator = validatorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive), accessor);
  59. var results = new List<object>();
  60. validator.Subscribe(x => results.Add(x));
  61. validator.SetValue(-5, BindingPriority.LocalValue);
  62. Assert.Equal(
  63. new[]
  64. {
  65. new BindingNotification(1),
  66. new BindingNotification(new Exception("MustBePositive must be positive"), BindingErrorType.DataValidationError, -5),
  67. }, results);
  68. }
  69. public class Data : INotifyPropertyChanged, INotifyDataErrorInfo
  70. {
  71. private int nonValidated;
  72. public int NonValidated
  73. {
  74. get { return nonValidated; }
  75. set { nonValidated = value; NotifyPropertyChanged(); }
  76. }
  77. private int mustBePositive;
  78. public int MustBePositive
  79. {
  80. get { return mustBePositive; }
  81. set
  82. {
  83. mustBePositive = value;
  84. NotifyErrorsChanged();
  85. }
  86. }
  87. public bool HasErrors
  88. {
  89. get
  90. {
  91. return MustBePositive > 0;
  92. }
  93. }
  94. public event PropertyChangedEventHandler PropertyChanged;
  95. public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
  96. private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
  97. {
  98. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  99. }
  100. private void NotifyErrorsChanged([CallerMemberName] string propertyName = "")
  101. {
  102. ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
  103. }
  104. public IEnumerable GetErrors(string propertyName)
  105. {
  106. if (propertyName == nameof(MustBePositive) && MustBePositive <= 0)
  107. {
  108. yield return $"{nameof(MustBePositive)} must be positive";
  109. }
  110. }
  111. }
  112. }
  113. }