Beispiel #1
0
        protected override void OnPropertyValueChanged(PropertyValueChangedEventArgs e)
        {
            bool isValid = false;

            foreach (Attribute a in e.ChangedItem.PropertyDescriptor.Attributes)
            {
                RangeAttribute dra = a as RangeAttribute;
                if (dra != null)
                {
                    try
                    {
                        dra.Validate(e.ChangedItem.Value, e.ChangedItem.PropertyDescriptor.Name);
                        isValid = true;
                    }
                    catch (ValidationException ve)
                    {
                        var msg = $"Value '{e.ChangedItem.Value}' is not a valid value for field '{e.ChangedItem.PropertyDescriptor.Name}'";
                        MessageBox.Show(ParentForm, msg, "Caution", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        PropertyFallback(e.ChangedItem.PropertyDescriptor.Name, e.OldValue);
                    }

                    break;
                }

                /*
                 * JetStreamPatternAttribute jspa = a as JetStreamPatternAttribute;
                 * if (jspa != null)
                 * {
                 *  try
                 *  {
                 *      jspa.Validate(e.ChangedItem.Value, e.ChangedItem.PropertyDescriptor.Name);
                 *      isValid = true;
                 *  }
                 *  catch (ValidationException ve)
                 *  {
                 *      var msg = $"Value '{e.ChangedItem.Value}' is not a valid value for field '{e.ChangedItem.PropertyDescriptor.Name}'";
                 *      MessageBox.Show(ParentForm, msg, "Caution", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 *      PropertyFallback(e.ChangedItem.PropertyDescriptor.Name, e.OldValue);
                 *  }
                 *
                 *  break;
                 * }
                 */
            }

            if (isValid)
            {
                try
                {
                    base.OnPropertyValueChanged(e);
                }
                catch (ValidationException ve)
                {
                    var s = ve.Message;
                }
            }

            this.Select();
            this.Focus();
        }
Beispiel #2
0
        public static void Validate_CantConvertValueToTargetType_ThrowsException(Type type, string minimum, string maximum)
        {
            var attribute = new RangeAttribute(type, minimum, maximum);

            AssertExtensions.Throws <ArgumentException, Exception>(() => attribute.Validate("abc", new ValidationContext(new object())));
            AssertExtensions.Throws <ArgumentException, Exception>(() => attribute.IsValid("abc"));
        }
Beispiel #3
0
        public static void Validate_IConvertibleThrowsCustomException_IsNotCaught()
        {
            RangeAttribute attribute = new RangeAttribute(typeof(int), "1", "1");

            Assert.Throws <ValidationException>(() => attribute.Validate(new IConvertibleImplementor()
            {
                IntThrow = new ArithmeticException()
            }, new ValidationContext(new object())));
        }
Beispiel #4
0
        public void CornerDigitTest()
        {
            // arrange
            object value = 7;

            // act
            var          attr   = new RangeAttribute(1, 7);
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(true, result.IsValid);
        }
Beispiel #5
0
        public void MinGreaterMaxTest()
        {
            // arrange
            object value = 7;

            // act
            var          attr   = new RangeAttribute(2, 1);
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(false, result.IsValid);
        }
Beispiel #6
0
        public void OutOfRangeTest()
        {
            // arrange
            object value = 10;

            // act
            var          attr   = new RangeAttribute(1, 7);
            SingleReport result = attr.Validate(value);

            // assert
            Assert.AreEqual(false, result.IsValid);
        }
Beispiel #7
0
        public static void Validate_MinimumGreaterThanMaximum_ThrowsInvalidOperationException()
        {
            var attribute = new RangeAttribute(3, 1);

            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));

            attribute = new RangeAttribute(3.0, 1.0);
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));

            attribute = new RangeAttribute(typeof(int), "3", "1");
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));

            attribute = new RangeAttribute(typeof(double), (3.0).ToString("F1"), (1.0).ToString("F1"));
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));

            attribute = new RangeAttribute(typeof(string), "z", "a");
            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));
        }
Beispiel #8
0
        public static void Validate_MinimumGreaterThanMaximum_ThrowsInvalidOperationException()
        {
            var attribute = new RangeAttribute(3, 1);
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));

            attribute = new RangeAttribute(3.0, 1.0);
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));

            attribute = new RangeAttribute(typeof(int), "3", "1");
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));

            attribute = new RangeAttribute(typeof(double), (3.0).ToString("F1"), (1.0).ToString("F1"));
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));

            attribute = new RangeAttribute(typeof(string), "z", "a");
            Assert.Throws<InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));
        }
Beispiel #9
0
 public static void Validate_InvalidOperandType_ThrowsInvalidOperationException(Type type)
 {
     var attribute = new RangeAttribute(type, "someMinimum", "someMaximum");
     Assert.Throws<InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));
 }
Beispiel #10
0
        public static void Validate_InvalidOperandType_ThrowsInvalidOperationException(Type type)
        {
            var attribute = new RangeAttribute(type, "someMinimum", "someMaximum");

            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));
        }
Beispiel #11
0
 public static void Validate_DoubleConversionOverflows_ThrowsOverflowException(double minimum, double maximum, object value)
 {
     RangeAttribute attribute = new RangeAttribute(minimum, maximum);
     Assert.Throws<OverflowException>(() => attribute.Validate(value, new ValidationContext(new object())));
 }
Beispiel #12
0
        public static void Validate_MinimumOrMaximumCantBeConvertedToIntegralType_ThrowsException(Type type, string minimum, string maximum)
        {
            RangeAttribute attribute = new RangeAttribute(type, minimum, maximum);

            AssertExtensions.Throws <ArgumentException, Exception>(() => attribute.Validate("Any", new ValidationContext(new object())));
        }
Beispiel #13
0
        public static void Validate_MinimumOrMaximumNull_ThrowsInvalidOperationException(string minimum, string maximum)
        {
            RangeAttribute attribute = new RangeAttribute(typeof(int), minimum, maximum);

            Assert.Throws <InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));
        }
 public static void Validate_ConversionOverflows_ThrowsOverflowException(Type type, string minimum, string maximum, object value)
 {
     RangeAttribute attribute = new RangeAttribute(type, minimum, maximum);
     Assert.Throws<OverflowException>(() => attribute.Validate(value, new ValidationContext(new object())));
 }
Beispiel #15
0
        public static void Validate_ConversionOverflows_ThrowsOverflowException(Type type, string minimum, string maximum, object value)
        {
            RangeAttribute attribute = new RangeAttribute(type, minimum, maximum);

            Assert.Throws <OverflowException>(() => attribute.Validate(value, new ValidationContext(new object())));
        }
Beispiel #16
0
 public static void Validate_MinimumOrMaximumNull_ThrowsInvalidOperationException(string minimum, string maximum)
 {
     RangeAttribute attribute = new RangeAttribute(typeof(int), minimum, maximum);
     Assert.Throws<InvalidOperationException>(() => attribute.Validate("Any", new ValidationContext(new object())));
 }
Beispiel #17
0
 public static void Validate_MinimumOrMaximumCantBeConvertedToType_ThrowsFormatException(Type type, string minimum, string maximum)
 {
     RangeAttribute attribute = new RangeAttribute(type, minimum, maximum);
     Assert.Throws<FormatException>(() => attribute.Validate("Any", new ValidationContext(new object())));
 }
Beispiel #18
0
        public static void Validate_MinimumOrMaximumCantBeConvertedToDateTime_ThrowsFormatException(Type type, string minimum, string maximum)
        {
            RangeAttribute attribute = new RangeAttribute(type, minimum, maximum);

            Assert.Throws <FormatException>(() => attribute.Validate("Any", new ValidationContext(new object())));
        }
Beispiel #19
0
 public static void Validate_IConvertibleThrowsCustomException_IsNotCaught()
 {
     RangeAttribute attribute = new RangeAttribute(typeof(int), "1", "1");
     Assert.Throws<ArithmeticException>(() => attribute.Validate(new IConvertibleImplementor() { IntThrow = new ArithmeticException() }, new ValidationContext(new object())));
 }
Beispiel #20
0
        public static void Validate_DoubleConversionOverflows_ThrowsOverflowException(double minimum, double maximum, object value)
        {
            RangeAttribute attribute = new RangeAttribute(minimum, maximum);

            Assert.Throws <OverflowException>(() => attribute.Validate(value, new ValidationContext(new object())));
        }