ExceptionValidationPluginTests.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.Generic;
  5. using System.Reactive.Linq;
  6. using Avalonia.Data;
  7. using Avalonia.Markup.Data.Plugins;
  8. using Avalonia.UnitTests;
  9. using Xunit;
  10. namespace Avalonia.Markup.UnitTests.Data.Plugins
  11. {
  12. public class ExceptionValidationPluginTests
  13. {
  14. [Fact]
  15. public void Produces_BindingNotifications()
  16. {
  17. var inpcAccessorPlugin = new InpcPropertyAccessorPlugin();
  18. var validatorPlugin = new ExceptionValidationPlugin();
  19. var data = new Data();
  20. var accessor = inpcAccessorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive));
  21. var validator = validatorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive), accessor);
  22. var result = new List<object>();
  23. validator.Subscribe(x => result.Add(x));
  24. validator.SetValue(5, BindingPriority.LocalValue);
  25. validator.SetValue(-2, BindingPriority.LocalValue);
  26. validator.SetValue(6, BindingPriority.LocalValue);
  27. Assert.Equal(new[]
  28. {
  29. new BindingNotification(0),
  30. new BindingNotification(5),
  31. new BindingNotification(new ArgumentOutOfRangeException("value"), BindingErrorType.DataValidationError),
  32. new BindingNotification(6),
  33. }, result);
  34. }
  35. public class Data : NotifyingBase
  36. {
  37. private int _mustBePositive;
  38. public int MustBePositive
  39. {
  40. get { return _mustBePositive; }
  41. set
  42. {
  43. if (value <= 0)
  44. {
  45. throw new ArgumentOutOfRangeException(nameof(value));
  46. }
  47. if (value != _mustBePositive)
  48. {
  49. _mustBePositive = value;
  50. RaisePropertyChanged();
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }