Example #1
0
        public static void ParseDotSeparatorExtremaInCommaSeparatorCultures(Type type, string min, string max)
        {
            RemoteExecutorForUap.Invoke((t, m1, m2) =>
            {
                using (new ThreadCultureChange("en-US"))
                {
                    Assert.True(new RangeAttribute(Type.GetType(t), m1, m2).IsValid(null));
                }

                using (new ThreadCultureChange("fr-FR"))
                {
                    var range = new RangeAttribute(Type.GetType(t), m1, m2);
                    AssertExtensions.Throws <ArgumentException>("value", () => range.IsValid(null));
                }
            }, type.ToString(), min, max).Dispose();
        }
        /// <summary>
        /// Determines whether the specified value of the object is valid.
        /// </summary>
        /// <param name="value">The value of the object to validate.</param>
        /// <param name="validationContext">The validation context.</param>
        /// <returns>true if the specified value is valid; otherwise, false.</returns>
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            string nines        = new string('9', Precision);
            string decimalNines = nines.Insert(Precision - scale, ".");
            double minMaxValue  = double.Parse(decimalNines);

            RangeAttribute range = new RangeAttribute(-minMaxValue, minMaxValue);

            if (range.IsValid(value))
            {
                return(ValidationResult.Success);
            }
            else
            {
                return(new ValidationResult(range.ErrorMessage));
            }
        }
Example #3
0
        public static void CommaDecimalExtremaAndNonStringValuesInvariantConvert(Type type, string min, string max, object value)
        {
            using (new ThreadCultureChange("en-US"))
            {
                var range = new RangeAttribute(type, min, max)
                {
                    ConvertValueInInvariantCulture = true
                };
                AssertExtensions.Throws <ArgumentException>("value", () => range.IsValid(value));
            }

            using (new ThreadCultureChange("fr-FR"))
            {
                Assert.True(
                    new RangeAttribute(type, min, max)
                {
                    ConvertValueInInvariantCulture = true
                }.IsValid(value));
            }
        }
Example #4
0
        public static void CommaDecimalExtremaAndNonStringValuesInvariantParse(Type type, string min, string max, object value)
        {
            using (new TempCulture("en-US"))
            {
                RangeAttribute range = new RangeAttribute(type, min, max)
                {
                    ParseLimitsInInvariantCulture = true
                };
                AssertExtensions.Throws <ArgumentException>("value", () => range.IsValid(value));
            }

            using (new TempCulture("fr-FR"))
            {
                RangeAttribute range = new RangeAttribute(type, min, max)
                {
                    ParseLimitsInInvariantCulture = true
                };
                AssertExtensions.Throws <ArgumentException>("value", () => range.IsValid(value));
            }
        }
Example #5
0
        public static void CommaDecimalExtremaAndInvalidValuesInvariantConvert(Type type, string min, string max, string value)
        {
            using (new TempCulture("en-US"))
            {
                RangeAttribute range = new RangeAttribute(type, min, max)
                {
                    ConvertValueInInvariantCulture = true
                };
                Assert.Throws <ArgumentException>(() => range.IsValid(value));
            }

            using (new TempCulture("fr-FR"))
            {
                RangeAttribute range = new RangeAttribute(type, min, max)
                {
                    ConvertValueInInvariantCulture = true
                };
                Assert.Throws <ArgumentException>(() => range.IsValid(value));
            }
        }
Example #6
0
        public static void DotDecimalExtremaAndValuesInvariantParse(Type type, string min, string max, string value)
        {
            using (new TempCulture("en-US"))
            {
                Assert.True(
                    new RangeAttribute(type, min, max)
                {
                    ParseLimitsInInvariantCulture = true
                }.IsValid(value));
            }

            using (new TempCulture("fr-FR"))
            {
                RangeAttribute range = new RangeAttribute(type, min, max)
                {
                    ParseLimitsInInvariantCulture = true
                };
                Assert.Throws <ArgumentException>(() => range.IsValid(value));
            }
        }
Example #7
0
        public static void DotDecimalExtremaAndInvalidValuesInvariantParse(Type type, string min, string max, string value)
        {
            using (new ThreadCultureChange("en-US"))
            {
                Assert.False(
                    new RangeAttribute(type, min, max)
                {
                    ParseLimitsInInvariantCulture = true
                }.IsValid(value));
            }

            using (new ThreadCultureChange("fr-FR"))
            {
                var range = new RangeAttribute(type, min, max)
                {
                    ParseLimitsInInvariantCulture = true
                };
                AssertExtensions.Throws <ArgumentException>("value", () => range.IsValid(value));
            }
        }
Example #8
0
        public static void CommaDecimalExtremaAndInvalidValuesInvariantConvert(Type type, string min, string max, string value)
        {
            RemoteInvoke((t, m1, m2, v) =>
            {
                Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfoByIetfLanguageTag("en-US");

                RangeAttribute range = new RangeAttribute(Type.GetType(t), m1, m2)
                {
                    ConvertValueInInvariantCulture = true
                };
                AssertExtensions.Throws <ArgumentException>("value", () => range.IsValid(v));

                Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfoByIetfLanguageTag("fr-FR");

                range = new RangeAttribute(Type.GetType(t), m1, m2)
                {
                    ConvertValueInInvariantCulture = true
                };
                AssertExtensions.Throws <ArgumentException>("value", () => range.IsValid(v));
            }, type.ToString(), min, max, value).Dispose();
        }
Example #9
0
        public static void CommaDecimalExtremaAndInvalidValuesInvariantConvert(Type type, string min, string max, string value)
        {
            RemoteExecutorForUap.Invoke((t, m1, m2, v) =>
            {
                using (new ThreadCultureChange("en-US"))
                {
                    var range = new RangeAttribute(Type.GetType(t), m1, m2)
                    {
                        ConvertValueInInvariantCulture = true
                    };
                    AssertExtensions.Throws <ArgumentException>("value", () => range.IsValid(v));
                }

                using (new ThreadCultureChange("fr-FR"))
                {
                    var range = new RangeAttribute(Type.GetType(t), m1, m2)
                    {
                        ConvertValueInInvariantCulture = true
                    };
                    AssertExtensions.Throws <ArgumentException>("value", () => range.IsValid(v));
                }
            }, type.ToString(), min, max, value).Dispose();
        }
Example #10
0
 /// <inheritdoc />
 public override bool IsValid(object value)
 {
     return(_rangeAttribute.IsValid(value));
 }
Example #11
0
 public override bool IsValid(object value)
 {
     return(m_range.IsValid(value));
 }