Пример #1
0
        /// <summary>
        ///     Checks whether the given value is not smaller or equal to the specified <paramref name="minValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="minValue">The highest invalid value.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is smaller or equal to <paramref name="minValue" />, while the
        ///     specified <paramref name="validator" /> is created using the
        ///     <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is smaller or equal to <paramref name="minValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <int> IsNotLessOrEqual(this ConditionValidator <int> validator, int minValue,
                                                                string conditionDescription)
        {
            if (validator.Value <= minValue)
            {
                Throw.ValueShouldNotBeSmallerThanOrEqualTo(validator, minValue, conditionDescription);
            }

            return(validator);
        }
        /// <summary>
        ///     Checks whether the specified <paramref name="condition" /> equals <b>true</b>.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the <see cref="ConditionValidator{T}.Value">Value</see> of the specified
        ///     <paramref name="validator" />.
        /// </typeparam>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="condition"><b>true</b> to prevent an <see cref="Exception" /> from being thrown; otherwise, false.</param>
        /// <param name="conditionDescription">
        ///     Describes the condition that should hold. i.e.: 'value should
        ///     be valid'. When the description contains a {0} marker, that marker will be replaced with the actual
        ///     name of the parameter. The description will be used in the message of the thrown exception.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentException">
        ///     Thrown when the <paramref name="condition" /> equals false, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        ///     Thrown when the <paramref name="condition" /> equals false and the
        ///     <see cref="ConditionValidator{T}.Value">Value</see> of the specified <paramref name="validator" /> is a null
        ///     reference, while the specified <paramref name="validator" /> is created using the
        ///     <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="InvalidEnumArgumentException">
        ///     Thrown when the <paramref name="condition" /> equals false and the
        ///     <see cref="ConditionValidator{T}.Value">Value</see> of the specified <paramref name="validator" /> is an
        ///     <see cref="System.Enum" /> type, while the specified <paramref name="validator" /> is created using the
        ///     <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <paramref name="condition" /> equals false, while the
        ///     specified <paramref name="validator" /> is created using the
        ///     <see cref="Condition.Ensures{T}(T,string)">Ensures</see> extension method.
        /// </exception>
        public static ConditionValidator <T> Evaluate <T>(this ConditionValidator <T> validator, bool condition,
                                                          string conditionDescription)
        {
            if (!condition)
            {
                Throw.ExpressionEvaluatedFalse(validator, conditionDescription);
            }

            return(validator);
        }
Пример #3
0
        /// <summary>
        ///     Checks whether the given value is not greater than the specified <paramref name="maxValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="maxValue">The lowest valid value.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is greater than <paramref name="maxValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is greater than <paramref name="maxValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <int> IsNotGreaterThan(this ConditionValidator <int> validator, int maxValue,
                                                                string conditionDescription)
        {
            if (validator.Value > maxValue)
            {
                Throw.ValueShouldNotBeGreaterThan(validator, maxValue, conditionDescription);
            }

            return(validator);
        }
        /// <summary>
        ///     Checks whether the given value is smaller or equal to the specified <paramref name="maxValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="maxValue">The highest valid value.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is greater than <paramref name="maxValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is greater than <paramref name="maxValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <short> IsLessOrEqual(this ConditionValidator <short> validator, short maxValue,
                                                               string conditionDescription)
        {
            if (!(validator.Value <= maxValue))
            {
                Throw.ValueShouldBeSmallerThanOrEqualTo(validator, maxValue, conditionDescription);
            }

            return(validator);
        }
Пример #5
0
        /// <summary>
        ///     Checks whether the given value is greater or equal to the specified <paramref name="minValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="minValue">The lowest valid value.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is smaller than <paramref name="minValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is smaller than <paramref name="minValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <int> IsGreaterOrEqual(this ConditionValidator <int> validator, int minValue,
                                                                string conditionDescription)
        {
            if (!(validator.Value >= minValue))
            {
                Throw.ValueShouldBeGreaterThanOrEqualTo(validator, minValue, conditionDescription);
            }

            return(validator);
        }
Пример #6
0
        private static string GetActualStringLengthMessage(ConditionValidator <string> validator)
        {
            var length = validator.Value != null ? validator.Value.Length : 0;

            if (length == 1)
            {
                return(SR.GetString(SR.TheActualValueIs1CharacterLong, validator.ArgumentName));
            }
            return(SR.GetString(SR.TheActualValueIsXCharactersLong, validator.ArgumentName, length));
        }
Пример #7
0
        internal static void ExpressionEvaluatedFalse <T>(ConditionValidator <T> validator, string conditionDescription)
        {
            var condition = GetFormattedConditionMessage(validator, SR.ValueShouldBeValid,
                                                         conditionDescription, validator.ArgumentName);

            var additionalMessage = GetActualValueMessage(validator);
            var violationType     = GetEnumViolationOrDefault <T>();

            validator.ThrowException(condition, additionalMessage, violationType);
        }
Пример #8
0
        internal static void ValueShouldBeUnequalTo <T>(ConditionValidator <T> validator, T value,
                                                        string conditionDescription)
        {
            var condition = GetFormattedConditionMessage(validator, SR.ValueShouldBeUnequalToX,
                                                         conditionDescription, validator.ArgumentName, value.Stringify());

            var violationType = GetEnumViolationOrDefault <T>();

            validator.ThrowException(condition, violationType);
        }
        /// <summary>
        ///     Checks whether the given value is null. An exception is thrown otherwise.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the <see cref="ConditionValidator{T}.Value">Value</see> of the specified
        ///     <paramref name="validator" />.
        /// </typeparam>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is not null, while the specified <paramref name="validator" /> is created
        ///     using the <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is not null, while the specified <paramref name="validator" /> is created
        ///     using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see> extension method.
        /// </exception>
        public static ConditionValidator <T?> IsNull <T>(this ConditionValidator <T?> validator)
            where T : struct
        {
            if (validator.Value.HasValue)
            {
                Throw.ValueShouldBeNull(validator, null);
            }

            return(validator);
        }
        /// <summary>
        ///     Checks whether the given value is greater than the specified <paramref name="minValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="minValue">The highest invalid value.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is smaller or equal to <paramref name="minValue" />, while the
        ///     specified <paramref name="validator" /> is created using the
        ///     <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is smaller or equal to <paramref name="minValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <short> IsGreaterThan(this ConditionValidator <short> validator, short minValue,
                                                               string conditionDescription)
        {
            if (!(validator.Value > minValue))
            {
                Throw.ValueShouldBeGreaterThan(validator, minValue, conditionDescription);
            }

            return(validator);
        }
Пример #11
0
        /// <summary>
        ///     Checks whether the given value is equal to the specified <paramref name="value" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="value">The valid value to compare with.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is not equal to <paramref name="value" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is not equal to <paramref name="value" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <decimal> IsEqualTo(this ConditionValidator <decimal> validator, decimal value,
                                                             string conditionDescription)
        {
            if (!(validator.Value == value))
            {
                Throw.ValueShouldBeEqualTo(validator, value, conditionDescription);
            }

            return(validator);
        }
        /// <summary>
        ///     Checks whether the given value is not greater or equal to the specified <paramref name="maxValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="maxValue">The lowest invalid value.</param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is greater or equal to <paramref name="maxValue" />, while the
        ///     specified <paramref name="validator" /> is created using the
        ///     <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is greater or equal to <paramref name="maxValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <short> IsNotGreaterOrEqual(this ConditionValidator <short> validator,
                                                                     short maxValue)
        {
            if (validator.Value >= maxValue)
            {
                Throw.ValueShouldNotBeGreaterThanOrEqualTo(validator, maxValue, null);
            }

            return(validator);
        }
Пример #13
0
        /// <summary>
        ///     Checks whether the given value is less than the specified <paramref name="maxValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="maxValue">The lowest invalid value.</param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is greater or equal to <paramref name="maxValue" />, while the
        ///     specified <paramref name="validator" /> is created using the
        ///     <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is greater or equal to <paramref name="maxValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <decimal> IsLessThan(this ConditionValidator <decimal> validator,
                                                              decimal maxValue)
        {
            if (!(validator.Value < maxValue))
            {
                Throw.ValueShouldBeSmallerThan(validator, maxValue, null);
            }

            return(validator);
        }
Пример #14
0
        /// <summary>
        ///     Checks whether the given value is not greater than the specified <paramref name="maxValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="maxValue">The lowest valid value.</param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is greater than <paramref name="maxValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is greater than <paramref name="maxValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <decimal> IsNotGreaterThan(this ConditionValidator <decimal> validator,
                                                                    decimal maxValue)
        {
            if (validator.Value > maxValue)
            {
                Throw.ValueShouldNotBeGreaterThan(validator, maxValue, null);
            }

            return(validator);
        }
        /// <summary>
        ///     Checks whether the given value is not smaller or equal to the specified <paramref name="minValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="minValue">The highest invalid value.</param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is smaller or equal to <paramref name="minValue" />, while the
        ///     specified <paramref name="validator" /> is created using the
        ///     <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is smaller or equal to <paramref name="minValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <short> IsNotLessOrEqual(this ConditionValidator <short> validator,
                                                                  short minValue)
        {
            if (validator.Value <= minValue)
            {
                Throw.ValueShouldNotBeSmallerThanOrEqualTo(validator, minValue, null);
            }

            return(validator);
        }
        /// <summary>
        ///     Checks whether the given value is not less than the specified <paramref name="minValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="minValue">The lowest valid value.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is smaller than <paramref name="minValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is smaller than <paramref name="minValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <short> IsNotLessThan(this ConditionValidator <short> validator, short minValue,
                                                               string conditionDescription)
        {
            if (validator.Value < minValue)
            {
                Throw.ValueShouldNotBeSmallerThan(validator, minValue, conditionDescription);
            }

            return(validator);
        }
        /// <summary>
        ///     Checks whether the given value is unequal to the specified <paramref name="value" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="value">The invalid value to compare with.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is equal to <paramref name="value" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is equal to <paramref name="value" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <short> IsNotEqualTo(this ConditionValidator <short> validator, short value,
                                                              string conditionDescription)
        {
            if (validator.Value == value)
            {
                Throw.ValueShouldBeUnequalTo(validator, value, conditionDescription);
            }

            return(validator);
        }
        /// <summary>
        ///     Checks whether the given value is null. An exception is thrown otherwise.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the <see cref="ConditionValidator{T}.Value">Value</see> of the specified
        ///     <paramref name="validator" />.
        /// </typeparam>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is not null, while the specified <paramref name="validator" /> is created
        ///     using the <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is not null, while the specified <paramref name="validator" /> is created
        ///     using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see> extension method.
        /// </exception>
        public static ConditionValidator <T> IsNull <T>(this ConditionValidator <T> validator, string conditionDescription)
            where T : class
        {
            if (validator.Value != null)
            {
                Throw.ValueShouldBeNull(validator, conditionDescription);
            }

            return(validator);
        }
Пример #19
0
        /// <summary>
        ///     Checks whether the given value is less than the specified <paramref name="maxValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="maxValue">The lowest invalid value.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is greater or equal to <paramref name="maxValue" />, while the
        ///     specified <paramref name="validator" /> is created using the
        ///     <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is greater or equal to <paramref name="maxValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <int> IsLessThan(this ConditionValidator <int> validator, int maxValue,
                                                          string conditionDescription)
        {
            if (!(validator.Value < maxValue))
            {
                Throw.ValueShouldBeSmallerThan(validator, maxValue, conditionDescription);
            }

            return(validator);
        }
Пример #20
0
        /// <summary>
        ///     Checks whether the given value is not less than the specified <paramref name="minValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="minValue">The lowest valid value.</param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is smaller than <paramref name="minValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is smaller than <paramref name="minValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <decimal> IsNotLessThan(this ConditionValidator <decimal> validator,
                                                                 decimal minValue)
        {
            if (validator.Value < minValue)
            {
                Throw.ValueShouldNotBeSmallerThan(validator, minValue, null);
            }

            return(validator);
        }
Пример #21
0
        /// <summary>
        ///     Checks whether the given value is greater or equal to the specified <paramref name="minValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="minValue">The lowest valid value.</param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is smaller than <paramref name="minValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is smaller than <paramref name="minValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <decimal> IsGreaterOrEqual(this ConditionValidator <decimal> validator,
                                                                    decimal minValue)
        {
            if (!(validator.Value >= minValue))
            {
                Throw.ValueShouldBeGreaterThanOrEqualTo(validator, minValue, null);
            }

            return(validator);
        }
Пример #22
0
        internal static void CollectionShouldContainMoreOrEqual <T>(ConditionValidator <T> validator,
                                                                    int numberOfElements, string conditionDescription) where T : IEnumerable
        {
            var condition =
                GetFormattedConditionMessage(validator, SR.CollectionShouldContainXOrMoreElements,
                                             conditionDescription, validator.ArgumentName, numberOfElements);

            var additionalMessage = GetCollectionContainsElementsMessage(validator);

            validator.ThrowException(condition, additionalMessage);
        }
Пример #23
0
        internal static void ValueShouldNotBeBetween <T>(ConditionValidator <T> validator, T minValue, T maxValue,
                                                         string conditionDescription)
        {
            var condition = GetFormattedConditionMessage(validator, SR.ValueShouldNotBeBetweenXAndY,
                                                         conditionDescription, validator.ArgumentName, minValue.Stringify(), maxValue.Stringify());

            var additionalMessage = GetActualValueMessage(validator);
            var violationType     = GetEnumViolationOrDefault <T>();

            validator.ThrowException(condition, additionalMessage, violationType);
        }
        /// <summary>
        ///     Checks whether the given value is not null. An exception is thrown otherwise.
        /// </summary>
        /// <typeparam name="T">
        ///     The type of the <see cref="ConditionValidator{T}.Value">Value</see> of the specified
        ///     <paramref name="validator" />.
        /// </typeparam>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentNullException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is null, while the specified <paramref name="validator" /> is created using
        ///     the <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is null, while the specified <paramref name="validator" /> is created using
        ///     the <see cref="Condition.Ensures{T}(T,string)">Ensures</see> extension method.
        /// </exception>
        public static ConditionValidator <T?> IsNotNull <T>(this ConditionValidator <T?> validator,
                                                            string conditionDescription)
            where T : struct
        {
            if (!validator.Value.HasValue)
            {
                Throw.ValueShouldNotBeNull(validator, conditionDescription);
            }

            return(validator);
        }
        /// <summary>
        ///     Checks whether the given value is not greater or equal to the specified <paramref name="maxValue" />.
        ///     An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="maxValue">The lowest invalid value.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is greater or equal to <paramref name="maxValue" />, while the
        ///     specified <paramref name="validator" /> is created using the
        ///     <see cref="Condition.Requires{T}(T,string)">Requires</see> extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is greater or equal to <paramref name="maxValue" />, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <byte> IsNotGreaterOrEqual(this ConditionValidator <byte> validator,
                                                                    byte maxValue,
                                                                    string conditionDescription)
        {
            if (validator.Value >= maxValue)
            {
                Throw.ValueShouldNotBeGreaterThanOrEqualTo(validator, maxValue, conditionDescription);
            }

            return(validator);
        }
        /// <summary>
        ///     Checks whether the given value is not between <paramref name="minValue" /> and
        ///     <paramref name="maxValue" /> (including those values). An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="minValue">The lowest invalid value.</param>
        /// <param name="maxValue">The highest invalid value.</param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is in the specified range, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is in the specified range, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <byte> IsNotInRange(this ConditionValidator <byte> validator, byte minValue,
                                                             byte maxValue)
        {
            var value = validator.Value;

            if (value >= minValue && value <= maxValue)
            {
                Throw.ValueShouldNotBeBetween(validator, minValue, maxValue, null);
            }

            return(validator);
        }
Пример #27
0
        /// <summary>
        ///     Checks whether the given value is between <paramref name="minValue" /> and
        ///     <paramref name="maxValue" /> (including those values). An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="minValue">The lowest valid value.</param>
        /// <param name="maxValue">The highest valid value.</param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is not in the specified range, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is not in the specified range, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <long> IsInRange(this ConditionValidator <long> validator, long minValue,
                                                          long maxValue)
        {
            var value = validator.Value;

            if (!(value >= minValue && value <= maxValue))
            {
                Throw.ValueShouldBeBetween(validator, minValue, maxValue, null);
            }

            return(validator);
        }
Пример #28
0
        internal static void ValueShouldNotBeSmallerThanOrEqualTo <T>(ConditionValidator <T> validator, T minValue,
                                                                      string conditionDescription)
        {
            var condition = GetFormattedConditionMessage(validator, SR.ValueShouldNotBeSmallerThanOrEqualToX,
                                                         conditionDescription, validator.ArgumentName, minValue.Stringify());

            var additionalMessage = GetActualValueMessage(validator);
            var violationType     =
                GetEnumViolationOrDefault <T>(ConstraintViolationType.OutOfRangeViolation);

            validator.ThrowException(condition, additionalMessage, violationType);
        }
        /// <summary>
        ///     Checks whether the given value is between <paramref name="minValue" /> and
        ///     <paramref name="maxValue" /> (including those values). An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="minValue">The lowest valid value.</param>
        /// <param name="maxValue">The highest valid value.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of
        ///     the specified <paramref name="validator" /> is not in the specified range, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is not in the specified range, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <byte> IsInRange(this ConditionValidator <byte> validator, byte minValue,
                                                          byte maxValue,
                                                          string conditionDescription)
        {
            var value = validator.Value;

            if (!(value >= minValue && value <= maxValue))
            {
                Throw.ValueShouldBeBetween(validator, minValue, maxValue, conditionDescription);
            }

            return(validator);
        }
Пример #30
0
        /// <summary>
        ///     Checks whether the given value is not between <paramref name="minValue" /> and
        ///     <paramref name="maxValue" /> (including those values). An exception is thrown otherwise.
        /// </summary>
        /// <param name="validator">The <see cref="ConditionValidator{T}" /> that holds the value that has to be checked.</param>
        /// <param name="minValue">The lowest invalid value.</param>
        /// <param name="maxValue">The highest invalid value.</param>
        /// <param name="conditionDescription">
        ///     The description of the condition that should hold. The string may hold the placeholder '{0}' for
        ///     the <see cref="ConditionValidator{T}.ArgumentName">ArgumentName</see>.
        /// </param>
        /// <returns>The specified <paramref name="validator" /> instance.</returns>
        /// <exception cref="ArgumentException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is in the specified range, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Requires{T}(T,string)">Requires</see>
        ///     extension method.
        /// </exception>
        /// <exception cref="PostconditionException">
        ///     Thrown when the <see cref="ConditionValidator{T}.Value">Value</see> of the
        ///     specified <paramref name="validator" /> is in the specified range, while the specified
        ///     <paramref name="validator" /> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see>
        ///     extension method.
        /// </exception>
        public static ConditionValidator <long> IsNotInRange(this ConditionValidator <long> validator, long minValue,
                                                             long maxValue,
                                                             string conditionDescription)
        {
            var value = validator.Value;

            if (value >= minValue && value <= maxValue)
            {
                Throw.ValueShouldNotBeBetween(validator, minValue, maxValue, conditionDescription);
            }

            return(validator);
        }