public void Bind_Logs_Binding_Error()
        {
            var target = new Class1();
            var source = new Subject <BindingValue <double> >();
            var called = false;
            var expectedMessageTemplate = "Error in binding to {Target}.{Property}: {Message}";

            LogCallback checkLogMessage = (level, area, src, mt, pv) =>
            {
                if (level == LogEventLevel.Warning &&
                    area == LogArea.Binding &&
                    mt == expectedMessageTemplate)
                {
                    called = true;
                }
            };

            using (TestLogSink.Start(checkLogMessage))
            {
                target.Bind(Class1.QuxProperty, source);
                source.OnNext(6.7);
                source.OnNext(BindingValue <double> .BindingError(new InvalidOperationException("Foo")));

                Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
                Assert.True(called);
            }
        }
Пример #2
0
        public void Binding_To_Direct_Property_Logs_BindingError()
        {
            var target = new Class1();
            var source = new Subject <BindingValue <string> >();
            var called = false;

            LogCallback checkLogMessage = (level, area, src, mt, pv) =>
            {
                if (level == LogEventLevel.Warning &&
                    area == LogArea.Binding &&
                    mt == "Error in binding to {Target}.{Property}: {Message}" &&
                    pv.Length == 3 &&
                    pv[0] is Class1 &&
                    object.ReferenceEquals(pv[1], Class1.FooProperty) &&
                    (string)pv[2] == "Binding Error Message")
                {
                    called = true;
                }
            };

            using (TestLogSink.Start(checkLogMessage))
            {
                target.Bind(Class1.FooProperty, source);
                source.OnNext("baz");
                source.OnNext(BindingValue <string> .BindingError(new InvalidOperationException("Binding Error Message")));
            }

            Assert.True(called);
        }
        public void Binding_Error_With_FallbackValue_Causes_Target_Update()
        {
            var target = new Class1();
            var source = new Subject <BindingValue <string> >();

            target.Bind(Class1.FooProperty, source);
            source.OnNext("initial");
            source.OnNext(BindingValue <string> .BindingError(new InvalidOperationException("Foo"), "bar"));

            Assert.Equal("bar", target.GetValue(Class1.FooProperty));
        }
        public void Binding_Error_Reverts_To_Default_Value()
        {
            var target = new Class1();
            var source = new Subject <BindingValue <string> >();

            target.Bind(Class1.FooProperty, source);
            source.OnNext("initial");
            source.OnNext(BindingValue <string> .BindingError(new InvalidOperationException("Foo")));

            Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
        }
        public void Binding_Non_Validated_Direct_Property_Does_Not_Call_UpdateDataValidation()
        {
            var target = new Class1();
            var source = new Subject <BindingValue <int> >();

            target.Bind(Class1.NonValidatedDirectProperty, source);
            source.OnNext(6);
            source.OnNext(BindingValue <int> .BindingError(new Exception()));
            source.OnNext(BindingValue <int> .DataValidationError(new Exception()));
            source.OnNext(6);

            Assert.Empty(target.Notifications);
        }
 public void OnNext(BindingValue <object?> value)
 {
     try
     {
         PublishNext(value.Convert <T>());
     }
     catch (InvalidCastException e)
     {
         Logger.TryGet(LogEventLevel.Error, LogArea.Binding)?.Log(
             _target,
             "Binding produced invalid value for {$Property} ({$PropertyType}): {$Value} ({$ValueType})",
             _property.Name,
             _property.PropertyType,
             value.Value,
             value.Value?.GetType());
         PublishNext(BindingValue <T> .BindingError(e));
     }
 }
        public void Binding_Validated_Direct_Property_Calls_UpdateDataValidation()
        {
            var target = new Class1();
            var source = new Subject <BindingValue <int> >();

            target.Bind(Class1.ValidatedDirectIntProperty, source);
            source.OnNext(6);
            source.OnNext(BindingValue <int> .BindingError(new Exception()));
            source.OnNext(BindingValue <int> .DataValidationError(new Exception()));
            source.OnNext(7);

            var result = target.Notifications;

            Assert.Equal(4, result.Count);
            Assert.Equal(BindingValueType.Value, result[0].type);
            Assert.Equal(6, result[0].value);
            Assert.Equal(BindingValueType.BindingError, result[1].type);
            Assert.Equal(BindingValueType.DataValidationError, result[2].type);
            Assert.Equal(BindingValueType.Value, result[3].type);
            Assert.Equal(7, result[3].value);
        }
Пример #8
0
        protected BindingValue <object> TryConvert(object value)
        {
            if (value == UnsetValue)
            {
                return(BindingValue <object> .Unset);
            }
            else if (value == BindingOperations.DoNothing)
            {
                return(BindingValue <object> .DoNothing);
            }

            if (!TypeUtilities.TryConvertImplicit(PropertyType, value, out var converted))
            {
                var error = new ArgumentException(string.Format(
                                                      "Invalid value for Property '{0}': '{1}' ({2})",
                                                      Name,
                                                      value,
                                                      value?.GetType().FullName ?? "(null)"));
                return(BindingValue <object> .BindingError(error));
            }

            return(converted);
        }