public void Validation_Plugins_Send_Correct_Notifications() { var data = new IndeiTest(); var observer = new ExpressionObserver(data, nameof(data.MustBePositive), true); var result = new List<object>(); observer.Subscribe(x => result.Add(x)); observer.SetValue(5); observer.SetValue(-5); observer.SetValue("foo"); observer.SetValue(5); Assert.Equal(new[] { new BindingNotification(0), // Value is notified twice as ErrorsChanged is always called by IndeiTest. new BindingNotification(5), new BindingNotification(5), // Value is first signalled without an error as validation hasn't been updated. new BindingNotification(-5), new BindingNotification(new Exception("Must be positive"), BindingErrorType.DataValidationError, -5), // Exception is thrown by trying to set value to "foo". new BindingNotification( new ArgumentException("Object of type 'System.String' cannot be converted to type 'System.Int32'."), BindingErrorType.DataValidationError), // Value is set then validation is updated. new BindingNotification(new Exception("Must be positive"), BindingErrorType.DataValidationError, 5), new BindingNotification(5), }, result); }
public void Validation_Plugins_Send_Correct_Notifications() { var data = new IndeiTest(); var observer = new ExpressionObserver(data, nameof(data.MustBePositive), true); var result = new List <object>(); observer.Subscribe(x => result.Add(x)); observer.SetValue(5); observer.SetValue(-5); observer.SetValue("foo"); observer.SetValue(5); Assert.Equal(new[] { new BindingNotification(0), // Value is notified twice as ErrorsChanged is always called by IndeiTest. new BindingNotification(5), new BindingNotification(5), // Value is first signalled without an error as validation hasn't been updated. new BindingNotification(-5), new BindingNotification(new Exception("Must be positive"), BindingErrorType.DataValidationError, -5), // Exception is thrown by trying to set value to "foo". new BindingNotification( new ArgumentException("Object of type 'System.String' cannot be converted to type 'System.Int32'."), BindingErrorType.DataValidationError), // Value is set then validation is updated. new BindingNotification(new Exception("Must be positive"), BindingErrorType.DataValidationError, 5), new BindingNotification(5), }, result); }
public void Indei_Validation_Does_Not_Subscribe_When_DataValidatation_Not_Enabled() { var data = new IndeiTest { MustBePositive = 5 }; var observer = new ExpressionObserver(data, nameof(data.MustBePositive), false); observer.Subscribe(_ => { }); Assert.Equal(0, data.ErrorsChangedSubscriptionCount); }
public void Disabled_Indei_Validation_Does_Not_Subscribe() { var data = new IndeiTest { MustBePositive = 5 }; var observer = new ExpressionObserver(data, nameof(data.MustBePositive), false); observer.Subscribe(_ => { }); Assert.Equal(0, data.SubscriptionCount); }
public void Enabled_Indei_Validation_Subscribes() { var data = new IndeiTest { MustBePositive = 5 }; var observer = new ExpressionObserver(data, nameof(data.MustBePositive), true); var sub = observer.Subscribe(_ => { }); Assert.Equal(1, data.ErrorsChangedSubscriptionCount); sub.Dispose(); Assert.Equal(0, data.ErrorsChangedSubscriptionCount); }
public void Indei_Validation_Does_Not_Subscribe_When_DataValidatation_Not_Enabled() { var data = new IndeiTest { MustBePositive = 5 }; var observer = ExpressionObserver.Create(data, o => o.MustBePositive, false); observer.Subscribe(_ => { }); Assert.Equal(0, data.ErrorsChangedSubscriptionCount); }
public void Enabled_Indei_Validation_Subscribes() { var data = new IndeiTest { MustBePositive = 5 }; var observer = ExpressionObserver.Create(data, o => o.MustBePositive, true); var sub = observer.Subscribe(_ => { }); Assert.Equal(1, data.ErrorsChangedSubscriptionCount); sub.Dispose(); // Forces WeakEvent compact Dispatcher.UIThread.RunJobs(); Assert.Equal(0, data.ErrorsChangedSubscriptionCount); }
public void Validation_Plugins_Send_Correct_Notifications() { var data = new IndeiTest(); var observer = ExpressionObserver.Create(data, o => o.MustBePositive, true); var result = new List <object>(); var errmsg = string.Empty; try { typeof(IndeiTest).GetProperty(nameof(IndeiTest.MustBePositive)).SetValue(data, "foo"); } catch (Exception e) { errmsg = e.Message; } observer.Subscribe(x => result.Add(x)); observer.SetValue(5); observer.SetValue(-5); observer.SetValue("foo"); observer.SetValue(5); Assert.Equal(new[] { new BindingNotification(0), // Value is notified twice as ErrorsChanged is always called by IndeiTest. new BindingNotification(5), new BindingNotification(5), // Value is first signalled without an error as validation hasn't been updated. new BindingNotification(-5), new BindingNotification(new DataValidationException("Must be positive"), BindingErrorType.DataValidationError, -5), // Exception is thrown by trying to set value to "foo". new BindingNotification( new ArgumentException(errmsg), BindingErrorType.DataValidationError), // Value is set then validation is updated. new BindingNotification(new DataValidationException("Must be positive"), BindingErrorType.DataValidationError, 5), new BindingNotification(5), }, result); GC.KeepAlive(data); }
public void Sends_Correct_Notifications_With_Property_Chain() { var container = new Container(); var inner = new IndeiTest(); var observer = new ExpressionObserver( container, $"{nameof(Container.Inner)}.{nameof(IndeiTest.MustBePositive)}", true); var result = new List <object>(); observer.Subscribe(x => result.Add(x)); Assert.Equal(new[] { new BindingNotification( new MarkupBindingChainException("Null value", "Inner.MustBePositive", "Inner"), BindingErrorType.Error, AvaloniaProperty.UnsetValue), }, result); }
public void Sends_Correct_Notifications_With_Property_Chain() { var container = new Container(); var inner = new IndeiTest(); var observer = new ExpressionObserver( container, $"{nameof(Container.Inner)}.{nameof(IndeiTest.MustBePositive)}", true); var result = new List<object>(); observer.Subscribe(x => result.Add(x)); Assert.Equal(new[] { new BindingNotification( new MarkupBindingChainNullException("Inner.MustBePositive", "Inner"), BindingErrorType.Error, AvaloniaProperty.UnsetValue), }, result); }