Ejemplo n.º 1
0
        /// <summary>Initializes an instance of the <see cref="AndArgumentConstraint{T}"/> class.</summary>
        /// <exception cref="ArgumentNullException"><paramref name="parent"/> is <see langword="null"/>.</exception>
        public AndArgumentConstraint(ArgumentConstraint <T> parent)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }

            m_parent = parent;
        }
Ejemplo n.º 2
0
        /// <summary>Verifies the value is a valid enum.</summary>
        /// <typeparam name="T">The enum type.</typeparam>
        /// <param name="source">The source value.</param>
        /// <returns>The new constraint.</returns>
        public static AndArgumentConstraint <T> IsValidEnum <T> (this ArgumentConstraint <T> source) where T : struct
        {
            if (!EnumExtensions.IsDefined(source.Argument.Value))
            {
                throw new ArgumentOutOfRangeException(source.Argument.Name, "Undefined enumeration value.");
            }

            return(new AndArgumentConstraint <T>(source));
        }
        /// <summary>Verifies the source contains only digits.</summary>
        /// <param name="source">The source.</param>
        /// <returns>The constraint.</returns>
        /// <exception cref="ArgumentException">The string contains non-digits.</exception>
        public static AndArgumentConstraint <string> IsNumeric(this ArgumentConstraint <string> source)
        {
            if (!source.Argument.Value.IsNumeric())
            {
                throw new ArgumentException("String must consist of only digits.", source.Argument.Name);
            }

            return(new AndArgumentConstraint <string>(source));
        }
        /// <summary>Verifies the source contains only digits.</summary>
        /// <param name="source">The source.</param>
        /// <param name="message">The message.</param>
        /// <param name="args">The message arguments.</param>
        /// <returns>The constraint.</returns>
        /// <exception cref="ArgumentException">The string contains non-digits.</exception>
        public static AndArgumentConstraint <string> IsNumeric(this ArgumentConstraint <string> source, string message, params object[] args)
        {
            if (!source.Argument.Value.IsNumeric())
            {
                throw new ArgumentException(String.Format(message, args), source.Argument.Name);
            }

            return(new AndArgumentConstraint <string>(source));
        }
        /// <summary>Verifies an argument is not <see langword="null"/>.</summary>
        /// <typeparam name="T">The argument type.</typeparam>
        /// <param name="source">The source.</param>
        /// <returns>The constraint.</returns>
        /// <exception cref="ArgumentNullException">The argument is <see langword="null"/>.</exception>
        public static AndArgumentConstraint <T> IsNotNull <T> (this ArgumentConstraint <T> source) where T : class
        {
            if (source.Argument.Value == null)
            {
                throw new ArgumentNullException(source.Argument.Name);
            }

            return(new AndArgumentConstraint <T>(source));
        }
        /// <summary>Verifies the value is not <see cref="Date.None"/>.</summary>
        /// <param name="source">The source value.</param>
        /// <returns>The new constraint.</returns>
        public static AndArgumentConstraint <Date> IsNotNone(this ArgumentConstraint <Date> source)
        {
            if (source.Argument.Value == Date.None)
            {
                throw new ArgumentException("Date is not set.", source.Argument.Name);
            }

            return(new AndArgumentConstraint <Date>(source));
        }
        /// <summary>Verifies an argument is <see langword="null"/>.</summary>
        /// <typeparam name="T">The argument type.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="message">The message.</param>
        /// <param name="messageArgs">The message arguments.</param>
        /// <returns>The constraint.</returns>
        /// <exception cref="ArgumentException">The argument is not <see langword="null"/>.</exception>
        public static AndArgumentConstraint <T> IsNull <T> (this ArgumentConstraint <T> source, string message, params object[] messageArgs) where T : class
        {
            if (source.Argument.Value != null)
            {
                throw new ArgumentException(String.Format(message, messageArgs), source.Argument.Name);
            }

            return(new AndArgumentConstraint <T>(source));
        }
        /// <summary>Verifies the value is valid.</summary>
        /// <param name="source">The source value.</param>
        /// <returns>The new constraint.</returns>
        /// <remarks>
        /// The value must be valid.  If the value is <see langword="null"/> then it is considered valid.
        /// </remarks>
        public static AndArgumentConstraint <T> IsValid <T> (this ArgumentConstraint <T> source) where T : IValidatableObject
        {
            if (source.Argument.Value != null)
            {
                ObjectValidator.ValidateFullObject(source.Argument.Value);
            }

            return(new AndArgumentConstraint <T>(source));
        }
Ejemplo n.º 9
0
        /// <summary>Verifies the argument is not equal to a value.</summary>
        /// <param name="source">The source value.</param>
        /// <param name="value">The value to compare against.</param>
        /// <param name="message">The message.</param>
        /// <param name="messageArgs">The message arguments.</param>
        /// <returns>The constraint.</returns>
        /// <exception cref="ArgumentOutOfRangeException">The argument is equal to the value.</exception>
        public static AndArgumentConstraint <T> IsNotEqualTo <T> (this ArgumentConstraint <T> source, T value,
                                                                  string message, params object[] messageArgs) where T : IComparable <T>
        {
            if (source.Argument.Value.CompareTo(value) != 0)
            {
                return(new AndArgumentConstraint <T>(source));
            }

            throw new ArgumentOutOfRangeException(source.Argument.Name, String.Format(message, messageArgs));
        }
        /// <summary>Verifies the source is a maximum length.</summary>
        /// <param name="source">The source.</param>
        /// <param name="value">The maximum length.</param>
        /// <param name="message">The message.</param>
        /// <param name="args">The message arguments.</param>
        /// <returns>The constraint.</returns>
        /// <exception cref="ArgumentException">String is longer than <paramref name="value"/>.</exception>
        public static AndArgumentConstraint <string> HasMaximumLength(this ArgumentConstraint <string> source, int value, string message, params object[] args)
        {
            var len = (source.Argument.Value != null) ? source.Argument.Value.Length : 0;

            if (len > value)
            {
                throw new ArgumentException(String.Format(message, args), source.Argument.Name);
            }

            return(new AndArgumentConstraint <string>(source));
        }
        /// <summary>Verifies the source is within a certain length.</summary>
        /// <param name="source">The source.</param>
        /// <param name="minLength">The minimum length.</param>
        /// <param name="maxLength">The maximum length.</param>
        /// <param name="message">The message.</param>
        /// <param name="args">The message arguments.</param>
        /// <returns>The constraint.</returns>
        /// <exception cref="ArgumentException">String is not within length.</exception>
        public static AndArgumentConstraint <string> HasLengthBetween(this ArgumentConstraint <string> source, int minLength, int maxLength, string message, params object[] args)
        {
            var len = (source.Argument.Value != null) ? source.Argument.Value.Length : 0;

            if (len < minLength || len > maxLength)
            {
                throw new ArgumentException(String.Format(message, args), source.Argument.Name);
            }

            return(new AndArgumentConstraint <string>(source));
        }
Ejemplo n.º 12
0
        /// <summary>Verifies the value is a valid enum except zero (the standard default).</summary>
        /// <typeparam name="T">The enum type.</typeparam>
        /// <param name="source">The source value.</param>
        /// <returns>The new constraint.</returns>
        public static AndArgumentConstraint <T> IsValidEnumAndNotZero <T>(this ArgumentConstraint <T> source) where T : struct, IComparable
        {
            source.IsValidEnum();

            if (source.Argument.Value.CompareTo(default(T)) == 0)
            {
                throw new ArgumentOutOfRangeException(source.Argument.Name, "Invalid enumeration value.");
            }

            return(new AndArgumentConstraint <T>(source));
        }
Ejemplo n.º 13
0
        /// <summary>Verifies the argument is between two values, exclusive.</summary>
        /// <param name="source">The source.</param>
        /// <param name="minValue">The minimum value.</param>
        /// <param name="maxValue">The maximum value.</param>
        /// <param name="message">The message.</param>
        /// <param name="messageArgs">The message arguments.</param>
        /// <returns>The constraint.</returns>
        /// <exception cref="ArgumentOutOfRangeException">The argument is not between the two values.</exception>
        public static AndArgumentConstraint <T> IsBetweenExclusive <T> (this ArgumentConstraint <T> source, T minValue, T maxValue,
                                                                        string message, params object[] messageArgs) where T : IComparable <T>
        {
            var value = source.Argument.Value;

            if (value.CompareTo(minValue) > 0 && value.CompareTo(maxValue) < 0)
            {
                return(new AndArgumentConstraint <T>(source));
            }

            throw new ArgumentOutOfRangeException(source.Argument.Name, String.Format(message, messageArgs));
        }
Ejemplo n.º 14
0
        /// <summary>Verifies the value is not in the given list.</summary>
        /// <typeparam name="T">The enum type.</typeparam>
        /// <param name="source">The source value.</param>
        /// <param name="invalidValues">The list of invalid values.</param>
        /// <returns>The new constraint.</returns>
        public static AndArgumentConstraint <T> IsNotIn <T>(this ArgumentConstraint <T> source, params T[] invalidValues) where T : struct, IComparable
        {
            foreach (var invalidValue in invalidValues)
            {
                if (source.Argument.Value.CompareTo(invalidValue) == 0)
                {
                    throw new ArgumentOutOfRangeException(source.Argument.Name, "Invalid enumeration value.");
                }
            }
            ;

            return(new AndArgumentConstraint <T>(source));
        }
        /// <summary>Verifies the source is not <see langword="null"/> or only contains whitespace.</summary>
        /// <param name="source">The source.</param>
        /// <returns>The constraint.</returns>
        /// <exception cref="ArgumentNullException">The string is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">The string is empty.</exception>
        public static AndArgumentConstraint <string> IsNotNullOrWhiteSpace(this ArgumentConstraint <string> source)
        {
            if (source.Argument.Value == null)
            {
                throw new ArgumentNullException(source.Argument.Name);
            }

            if (String.IsNullOrWhiteSpace(source.Argument.Value))
            {
                throw new ArgumentException("String cannot be empty.", source.Argument.Name);
            }

            return(new AndArgumentConstraint <string>(source));
        }
        /// <summary>Verifies the source is not <see langword="null"/> or empty.</summary>
        /// <param name="source">The source.</param>
        /// <param name="message">The message.</param>
        /// <param name="args">The message arguments.</param>
        /// <returns>The constraint.</returns>
        /// <exception cref="ArgumentNullException">The string is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">The string is empty.</exception>
        public static AndArgumentConstraint <string> IsNotNullOrEmpty(this ArgumentConstraint <string> source, string message, params object[] args)
        {
            if (source.Argument.Value == null)
            {
                throw new ArgumentNullException(source.Argument.Name, String.Format(message, args));
            }

            if (source.Argument.Value.Length == 0)
            {
                throw new ArgumentException(source.Argument.Name, String.Format(message, args));
            }

            return(new AndArgumentConstraint <string>(source));
        }
        /// <summary>Verifies the source is not <see langword="null"/> or only contains whitespace.</summary>
        /// <param name="source">The source.</param>
        /// <param name="message">The message.</param>
        /// <param name="args">The message arguments.</param>
        /// <returns>The constraint.</returns>
        /// <exception cref="ArgumentNullException">The string is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">The string is empty.</exception>
        public static AndArgumentConstraint <string> IsNotNullOrWhiteSpace(this ArgumentConstraint <string> source, string message, params object[] args)
        {
            if (source.Argument.Value == null)
            {
                throw new ArgumentNullException(source.Argument.Name, String.Format(message, args));
            }

            if (String.IsNullOrWhiteSpace(source.Argument.Value))
            {
                throw new ArgumentException(String.Format(message, args), source.Argument.Name);
            }

            return(new AndArgumentConstraint <string>(source));
        }
Ejemplo n.º 18
0
 /// <summary>Verifies the argument is between two values, exclusive.</summary>
 /// <param name="source">The source.</param>
 /// <param name="minValue">The minimum value.</param>
 /// <param name="maxValue">The maximum value.</param>
 /// <returns>The constraint.</returns>
 /// <exception cref="ArgumentOutOfRangeException">The argument is not between the two values.</exception>
 public static AndArgumentConstraint <T> IsBetweenExclusive <T> (this ArgumentConstraint <T> source, T minValue, T maxValue) where T : IComparable <T>
 {
     return(IsBetweenExclusive(source, minValue, maxValue, "Value must be greater than {0} and less than {1}.", minValue, maxValue));
 }
Ejemplo n.º 19
0
 /// <summary>Verifies the argument is less than a value.</summary>
 /// <param name="source">The source value.</param>
 /// <param name="value">The value to compare against.</param>
 /// <returns>The constraint.</returns>
 /// <exception cref="ArgumentOutOfRangeException">The argument is not less than the value.</exception>
 public static AndArgumentConstraint <T> IsLessThan <T> (this ArgumentConstraint <T> source, T value) where T : IComparable <T>
 {
     return(IsLessThan <T>(source, value, "Value must be less than {0}", value));
 }
 public static AndArgumentConstraint <sbyte> IsLessThanOrEqualToZero(this ArgumentConstraint <sbyte> source, string message, params object[] messageArgs)
 {
     return(source.IsLessThanOrEqualTo(default(sbyte), message, messageArgs));
 }
 public static AndArgumentConstraint <ulong> IsZero(this ArgumentConstraint <ulong> source, string message, params object[] messageArgs)
 {
     return(source.IsEqualTo(default(ulong), message, messageArgs));
 }
 public static AndArgumentConstraint <ulong> IsZero(this ArgumentConstraint <ulong> source)
 {
     return(IsZero(source, "Value must be zero."));
 }
 public static AndArgumentConstraint <sbyte> IsLessThanOrEqualToZero(this ArgumentConstraint <sbyte> source)
 {
     return(IsLessThanOrEqualToZero(source, "Value must be less than or equal to zero."));
 }
 public static AndArgumentConstraint <uint> IsNotZero(this ArgumentConstraint <uint> source)
 {
     return(IsNotZero(source, "Value cannot be zero."));
 }
 public static AndArgumentConstraint <ushort> IsNotZero(this ArgumentConstraint <ushort> source, string message, params object[] messageArgs)
 {
     return(source.IsNotEqualTo(default(ushort), message, messageArgs));
 }
 public static AndArgumentConstraint <sbyte> IsGreaterThanZero(this ArgumentConstraint <sbyte> source, string message, params object[] messageArgs)
 {
     return(source.IsGreaterThan(default(sbyte), message, messageArgs));
 }
 /// <summary>Verifies the argument is less than zero.</summary>
 /// <param name="source">The source.</param>
 /// <param name="message">The message.</param>
 /// <param name="messageArgs">The message arguments.</param>
 /// <returns>The constralong.</returns>
 /// <exception cref="ArgumentOutOfRangeException">The argument is not less than zero.</exception>
 public static AndArgumentConstraint <long> IsLessThanZero(this ArgumentConstraint <long> source, string message, params object[] messageArgs)
 {
     return(source.IsLessThan(default(long), message, messageArgs));
 }
 /// <summary>Verifies the argument is less than zero.</summary>
 /// <param name="source">The source.</param>
 /// <returns>The constralong.</returns>
 /// <exception cref="ArgumentOutOfRangeException">The argument is not less than zero.</exception>
 public static AndArgumentConstraint <long> IsLessThanZero(this ArgumentConstraint <long> source)
 {
     return(IsLessThanZero(source, "Value must be less than zero."));
 }
Ejemplo n.º 29
0
 /// <summary>Verifies the argument is equal to a value.</summary>
 /// <param name="source">The source value.</param>
 /// <param name="value">The value to compare against.</param>
 /// <returns>The constraint.</returns>
 /// <exception cref="ArgumentOutOfRangeException">The argument is not equal to the value.</exception>
 public static AndArgumentConstraint <T> IsEqualTo <T> (this ArgumentConstraint <T> source, T value) where T : IComparable <T>
 {
     return(IsEqualTo <T>(source, value, "Value must be equal to {0}", value));
 }
 /// <summary>Verifies the argument is greater than zero.</summary>
 /// <param name="source">The source.</param>
 /// <returns>The constralong.</returns>
 /// <exception cref="ArgumentOutOfRangeException">The argument is not greater than zero.</exception>
 public static AndArgumentConstraint <long> IsGreaterThanZero(this ArgumentConstraint <long> source)
 {
     return(IsGreaterThanZero(source, "Value must be greater than zero."));
 }