internal override Exception BuildExceptionBasedOnViolationType(ConstraintViolationType type,
                                                                       string message)
        {
            var constructor = AlternativeExceptionHelper <TException> .Constructor;

            return((Exception)constructor.Invoke(new object[] { message }));
        }
        public void EnsuresTest03()
        {
            ConditionValidator <int> ensuresValidator = Condition.Ensures(3);

            const string ValidCondition         = "valid condition";
            const string ValidAdditionalMessage = "valid additional message";
            const ConstraintViolationType InvalidConstraintViolationType = (ConstraintViolationType)666;

            const string AssertMessage = "EnsuresValidator.ThrowException should throw an " +
                                         "ArgumentException when an invalid ConstraintViolationType is supplied.";

            try
            {
                ensuresValidator.ThrowException(ValidCondition, ValidAdditionalMessage,
                                                InvalidConstraintViolationType);

                Assert.Fail(AssertMessage);
            }
            catch (Exception ex)
            {
                Assert.AreEqual(typeof(PostconditionException), ex.GetType(), AssertMessage);

                Assert.IsTrue(ex.Message.Contains(ValidCondition),
                              "The exception message does not contain the condition.");
            }
        }
Пример #3
0
 // Returns the 'InvalidEnumViolation' when the T is an Enum and otherwise the specified defaultValue.
 private static ConstraintViolationType GetEnumViolationOrDefault <T>(
     ConstraintViolationType defaultValue)
 {
     if (typeof(T).GetTypeInfo().IsEnum)
     {
         return(ConstraintViolationType.InvalidEnumViolation);
     }
     return(defaultValue);
 }
        /// <summary>Throws an exception.</summary>
        /// <param name="condition">
        ///     Describes the condition that doesn't hold, e.g., "Value should not be
        ///     null".
        /// </param>
        /// <param name="additionalMessage">
        ///     An additional message that will be appended to the exception
        ///     message, e.g. "The actual value is 3.". This value may be null or empty.
        /// </param>
        /// <param name="type">
        ///     Gives extra information on the exception type that must be build. The actual
        ///     implementation of the validator may ignore some or all values.
        /// </param>
        protected override void ThrowExceptionCore(string condition, string additionalMessage,
                                                   ConstraintViolationType type)
        {
            var message = BuildExceptionMessage(condition, additionalMessage);

            var exceptionToThrow = BuildExceptionBasedOnViolationType(type, message);

            throw exceptionToThrow;
        }
Пример #5
0
 protected override void ThrowExceptionCore(string condition,
                                            string additionalMessage, ConstraintViolationType type)
 {
     if (string.IsNullOrWhiteSpace(Codigo))
     {
         throw new Exception(condition);
     }
     throw new ExcepcionReglaNegocio(condition, Codigo);
 }
Пример #6
0
        internal static void ExpressionEvaluatedFalse <T>(ConditionValidator <T> validator, string conditionDescription)
        {
            string condition = GetFormattedConditionMessage(validator, SR.ValueShouldBeValid,
                                                            conditionDescription, validator.ArgumentName);

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

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

            ConstraintViolationType violationType = GetEnumViolationOrDefault <T>();

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

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

            validator.ThrowException(condition, additionalMessage, violationType);
        }
Пример #9
0
        internal static void ValueShouldBeGreaterThan <T>(ConditionValidator <T> validator, T minValue,
                                                          string conditionDescription)
        {
            string condition = GetFormattedConditionMessage(validator, SR.ValueShouldBeGreaterThanX,
                                                            conditionDescription, validator.ArgumentName, minValue.Stringify());

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

            validator.ThrowException(condition, additionalMessage, violationType);
        }
Пример #10
0
        internal static void LambdaXShouldHoldForValue <T>(ConditionValidator <T> validator, LambdaExpression lambda,
                                                           string conditionDescription)
        {
            string lambdaDefinition = GetLambdaDefinition(lambda);

            string condition = GetFormattedConditionMessage(validator, SR.LambdaXShouldHoldForValue,
                                                            conditionDescription, validator.ArgumentName, lambdaDefinition);

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

            validator.ThrowException(condition, additionalMessage, violationType);
        }
Пример #11
0
        /// <summary>Throws an exception.</summary>
        /// <param name="condition">Describes the condition that doesn't hold, e.g., "Value should not be
        /// null".</param>
        /// <param name="additionalMessage">An additional message that will be appended to the exception
        /// message, e.g. "The actual value is 3.". This value may be null or empty.</param>
        /// <param name="type">Gives extra information on the exception type that must be build. The actual
        /// implementation of the validator may ignore some or all values.</param>
        protected override void ThrowExceptionCore(string condition, string additionalMessage,
                                                   ConstraintViolationType type)
        {
            // Build up an exception message: "Postcondition '[condition]' failed.".
            string exceptionMessage = SR.GetString(SR.PostconditionXFailed, condition);

            if (!String.IsNullOrEmpty(additionalMessage))
            {
                // The library can supply some additional information about the value of the validated
                // argument. This message will be appended to the exception message.
                exceptionMessage += " " + additionalMessage;
            }

            throw new PostconditionException(exceptionMessage);
        }
Пример #12
0
        public void EnsuresTest03()
        {
            ConditionValidator <int> ensuresValidator = Condition.Ensures(3);

            const string ValidCondition         = "valid condition";
            const string ValidAdditionalMessage = "valid additional message";
            const ConstraintViolationType InvalidConstraintViolationType = (ConstraintViolationType)666;

            const string AssertMessage = "EnsuresValidator.ThrowException should throw an " +
                                         "ArgumentException when an invalid ConstraintViolationType is supplied.";

            Action action = () => ensuresValidator.ThrowException(ValidCondition, ValidAdditionalMessage,
                                                                  InvalidConstraintViolationType);

            action.Should().Throw <PostconditionException>(AssertMessage).Which.Message.Should().Contain(ValidCondition, "The exception message does not contain the condition.");
        }
Пример #13
0
        internal virtual Exception BuildExceptionBasedOnViolationType(ConstraintViolationType type, string message)
        {
            switch (type)
            {
            case ConstraintViolationType.OutOfRangeViolation:
                return(new ArgumentOutOfRangeException(ArgumentName, message));

            case ConstraintViolationType.InvalidEnumViolation:
                var enumMessage = BuildInvalidEnumArgumentExceptionMessage(message);
                return(new InvalidEnumArgumentException(enumMessage));

            default:
                return(Value != null
                        ? new ArgumentException(message, ArgumentName)
                        : new ArgumentNullException(ArgumentName, message));
            }
        }
Пример #14
0
        internal virtual Exception BuildExceptionBasedOnViolationType(ConstraintViolationType type,
                                                                      string message)
        {
            switch (type)
            {
            case ConstraintViolationType.OutOfRangeViolation:
                return(new ArgumentOutOfRangeException(this.ArgumentName, message));

            case ConstraintViolationType.InvalidEnumViolation:
                string enumMessage = this.BuildInvalidEnumArgumentExceptionMessage(message);
                return(new InvalidEnumArgumentException(enumMessage));

            default:
                if (this.Value != null)
                {
                    return(new ArgumentException(message, this.ArgumentName));
                }
                else
                {
                    return(new ArgumentNullException(this.ArgumentName, message));
                }
            }
        }
        public void RequiresTest03()
        {
            ConditionValidator <int> requiresValidator = Condition.Requires(3);

            const string ValidCondition         = "valid condition";
            const string ValidAdditionalMessage = "valid additional message";
            const ConstraintViolationType InvalidConstraintViolationType = (ConstraintViolationType)666;

            const string AssertMessage = "RequiresValidator.ThrowException should throw an " +
                                         "ArgumentException when an invalid ConstraintViolationType is supplied.";

            try
            {
                requiresValidator.ThrowException(ValidCondition, ValidAdditionalMessage, InvalidConstraintViolationType);

                Assert.True(false, AssertMessage);
            }
            catch (Exception ex)
            {
                ex.GetType().ShouldBe(typeof(ArgumentException));
                ex.Message.ShouldContain(ValidCondition, "The exception message does not contain the condition.");
            }
        }
 internal void ThrowException(string condition, ConstraintViolationType type)
 {
     ThrowExceptionCore(condition, null, type);
 }
Пример #17
0
 protected override void ThrowExceptionCore(string condition,
                                            string additionalMessage, ConstraintViolationType type)
 {
     throw new ExcepcionValidacion(condition);
 }
Пример #18
0
 protected override void ThrowExceptionCore(string condition, string additionalMessage,
                                            ConstraintViolationType type)
 {
     // Don't throw.
 }
Пример #19
0
 protected override void ThrowExceptionCore(string condition, string additionalMessage, ConstraintViolationType type)
 {
     throw new ExcepcionReglaNegocio(Codigo, condition);
 }
 [EditorBrowsable(EditorBrowsableState.Never)] // see top of page for note on this attribute.
 public void ThrowException(string condition, string additionalMessage, ConstraintViolationType type)
 {
     ThrowExceptionCore(condition, additionalMessage, type);
 }
 /// <summary>Throws an exception.</summary>
 /// <param name="condition">
 ///     Describes the condition that doesn't hold, e.g., "Value should not be
 ///     null".
 /// </param>
 /// <param name="additionalMessage">
 ///     An additional message that will be appended to the exception
 ///     message, e.g. "The actual value is 3.". This value may be null or empty.
 /// </param>
 /// <param name="type">
 ///     Gives extra information on the exception type that must be build. The actual
 ///     implementation of the validator may ignore some or all values.
 /// </param>
 /// <remarks>
 ///     Implement this method when deriving from <see cref="ConditionValidator{T}" />.
 ///     The implementation should at least build the exception message from the
 ///     <paramref name="condition" /> and optional <paramref name="additionalMessage" />. Usage of the
 ///     <paramref name="type" /> is completely optional, but the implementation should at least be flexible
 ///     and be able to handle unknown <see cref="ConstraintViolationType" /> values. Values may be added
 ///     in future releases.
 /// </remarks>
 /// <example>
 ///     For an example see the documentation for <see cref="ConditionValidator{T}" />.
 /// </example>
 protected abstract void ThrowExceptionCore(string condition, string additionalMessage,
                                            ConstraintViolationType type);