internal static void ValueShouldBeNull <T>(ContractValidator <T> validator, string conditionDescription) { string condition = GetFormattedConditionMessage(validator, SR.ValueShouldBeNull, conditionDescription, validator.ArgumentName); validator.ThrowException(condition); }
internal static void CollectionShouldBeEmpty <T>(ContractValidator <T> validator, string conditionDescription) { string condition = GetFormattedConditionMessage(validator, SR.CollectionShouldBeEmpty, conditionDescription, validator.ArgumentName); validator.ThrowException(condition); }
internal static void StringShouldStartWith(ContractValidator <string> validator, string value, string conditionDescription) { string condition = GetFormattedConditionMessage(validator, SR.StringShouldStartWithX, conditionDescription, validator.ArgumentName, value.Stringify()); validator.ThrowException(condition); }
internal static void ValueShouldNotBeAnEmptyString(ContractValidator <string> validator, string conditionDescription) { string condition = GetFormattedConditionMessage(validator, SR.StringShouldNotBeEmpty, conditionDescription, validator.ArgumentName); validator.ThrowException(condition); }
internal static void CollectionShouldNotContainAllOf <T>(ContractValidator <T> validator, IEnumerable values, string conditionDescription) { string condition = GetFormattedConditionMessage(validator, SR.CollectionShouldNotContainAllOfX, conditionDescription, validator.ArgumentName, values.Stringify()); validator.ThrowException(condition); }
internal static void ValueShouldNotBeOfType <T>(ContractValidator <T> validator, Type type, string conditionDescription) { string condition = GetFormattedConditionMessage(validator, SR.ValueShouldNotBeOfTypeX, conditionDescription, validator.ArgumentName, type.Name); validator.ThrowException(condition); }
internal static void StringShouldNotBeNullOrWhiteSpace(ContractValidator <string> validator, string conditionDescription) { string condition = GetFormattedConditionMessage(validator, SR.StringShouldNotBeNullOrWhiteSpace, conditionDescription, validator.ArgumentName); validator.ThrowException(condition); }
internal static void CollectionShouldContain <T>(ContractValidator <T> validator, object value, string conditionDescription) where T : IEnumerable { string condition = GetFormattedConditionMessage(validator, SR.CollectionShouldContainX, conditionDescription, validator.ArgumentName, value.Stringify()); validator.ThrowException(condition); }
/// <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="ContractValidator{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="ContractValidator{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="ContractValidator{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 ContractValidator <DateTime> IsNotGreaterThan(this ContractValidator <DateTime> validator, DateTime maxValue) { if (validator.Value > maxValue) { Throw.ValueShouldNotBeGreaterThan(validator, maxValue, null); } 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="ContractValidator{T}"/> that holds the value that has to be checked.</param> /// <param name="value">The invalid value to compare with.</param> /// <returns>The specified <paramref name="validator"/> instance.</returns> /// <exception cref="ArgumentException">Thrown when the <see cref="ContractValidator{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="ContractValidator{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 ContractValidator <DateTime> IsNotEqualTo(this ContractValidator <DateTime> validator, DateTime value) { if (validator.Value == value) { Throw.ValueShouldBeUnequalTo(validator, value, null); } 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="ContractValidator{T}"/> that holds the value that has to be checked.</param> /// <param name="value">The invalid value to compare with.</param> /// <returns>The specified <paramref name="validator"/> instance.</returns> /// <exception cref="ArgumentException">Thrown when the <see cref="ContractValidator{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="ContractValidator{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 ContractValidator <short> IsNotEqualTo(this ContractValidator <short> validator, short value) { if (validator.Value == value) { Throw.ValueShouldBeUnequalTo(validator, value, 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="ContractValidator{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="ContractValidator{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="ContractValidator{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 ContractValidator <short> IsNotLessOrEqual(this ContractValidator <short> validator, short minValue) { if (validator.Value <= minValue) { Throw.ValueShouldNotBeSmallerThanOrEqualTo(validator, minValue, null); } return(validator); }
/// <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="ContractValidator{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="ContractValidator{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="ContractValidator{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 ContractValidator <decimal> IsNotGreaterThan(this ContractValidator <decimal> validator, decimal maxValue) { if (validator.Value > maxValue) { Throw.ValueShouldNotBeGreaterThan(validator, maxValue, null); } return(validator); }
/// <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="ContractValidator{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="ContractValidator{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="ContractValidator{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 ContractValidator <short> IsGreaterOrEqual(this ContractValidator <short> validator, short minValue) { if (!(validator.Value >= minValue)) { Throw.ValueShouldBeGreaterThanOrEqualTo(validator, minValue, null); } return(validator); }
/// <summary> /// Checks whether the given value is not null. An exception is thrown otherwise. /// </summary> /// <typeparam name="T">The type of the <see cref="ContractValidator{T}.Value">Value</see> of the specified <paramref name="validator"/>.</typeparam> /// <param name="validator">The <see cref="ContractValidator{T}"/> that holds the value that has to be checked.</param> /// <returns>The specified <paramref name="validator"/> instance.</returns> /// <exception cref="ArgumentNullException">Thrown when the <see cref="ContractValidator{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="ContractValidator{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 ContractValidator <T> IsNotNull <T>(this ContractValidator <T> validator) where T : class { if (validator.Value == null) { Throw.ValueShouldNotBeNull(validator, null); } return(validator); }
/// <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="ContractValidator{T}"/> that holds the value that has to be checked.</param> /// <param name="value">The valid value to compare with.</param> /// <returns>The specified <paramref name="validator"/> instance.</returns> /// <exception cref="ArgumentException">Thrown when the <see cref="ContractValidator{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="ContractValidator{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 ContractValidator <decimal> IsEqualTo(this ContractValidator <decimal> validator, decimal value) { if (!(validator.Value == value)) { Throw.ValueShouldBeEqualTo(validator, value, 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="ContractValidator{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="ContractValidator{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="ContractValidator{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 ContractValidator <decimal> IsNotLessThan(this ContractValidator <decimal> validator, decimal minValue) { if (validator.Value < minValue) { Throw.ValueShouldNotBeSmallerThan(validator, minValue, null); } return(validator); }
/// <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="ContractValidator{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="ContractValidator{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="ContractValidator{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 ContractValidator <decimal> IsLessThan(this ContractValidator <decimal> validator, decimal maxValue) { if (!(validator.Value < maxValue)) { Throw.ValueShouldBeSmallerThan(validator, maxValue, null); } 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="ContractValidator{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="ContractValidator{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="ContractValidator{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 ContractValidator <short> IsNotGreaterOrEqual(this ContractValidator <short> validator, short maxValue) { if (validator.Value >= maxValue) { Throw.ValueShouldNotBeGreaterThanOrEqualTo(validator, maxValue, 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="ContractValidator{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="ContractValidator{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="ContractValidator{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 ContractValidator <decimal> IsGreaterThan(this ContractValidator <decimal> validator, decimal minValue) { if (!(validator.Value > minValue)) { Throw.ValueShouldBeGreaterThan(validator, minValue, null); } 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="ContractValidator{T}"/> that holds the value that has to be checked.</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="ContractValidator{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="ContractValidator{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 ContractValidator <short> IsLessOrEqual(this ContractValidator <short> validator, short maxValue) { if (!(validator.Value <= maxValue)) { Throw.ValueShouldBeSmallerThanOrEqualTo(validator, maxValue, null); } return(validator); }
/// <summary> /// Checks whether the given value is <b>true</b>. An exception is thrown otherwise. /// </summary> /// <param name="validator">The <see cref="ContractValidator{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="ContractValidator{T}.ArgumentName">ArgumentName</see>. /// </param> /// <returns>The specified <paramref name="validator"/> instance.</returns> /// <exception cref="ArgumentException">Thrown when the <see cref="ContractValidator{T}.Value">Value</see> of the specified <paramref name="validator"/> is <b>false</b> or 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="ContractValidator{T}.Value">Value</see> of the specified <paramref name="validator"/> is <b>false</b> or 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 ContractValidator <bool?> IsTrue(this ContractValidator <bool?> validator, string conditionDescription) { if (!(validator.Value == true)) { Throw.ValueShouldBeTrue(validator, conditionDescription); } 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="ContractValidator{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="ContractValidator{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="ContractValidator{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 ContractValidator <DateTime> IsGreaterThan(this ContractValidator <DateTime> validator, DateTime minValue) { if (!(validator.Value > minValue)) { Throw.ValueShouldBeGreaterThan(validator, minValue, null); } return(validator); }
/// <summary> /// Checks whether the given value is <b>true</b>. An exception is thrown otherwise. /// </summary> /// <param name="validator">The <see cref="ContractValidator{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="ContractValidator{T}.Value">Value</see> of the specified <paramref name="validator"/> is <b>false</b> or 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="ContractValidator{T}.Value">Value</see> of the specified <paramref name="validator"/> is <b>false</b> or 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 ContractValidator <bool?> IsTrue(this ContractValidator <bool?> validator) { if (!(validator.Value == true)) { Throw.ValueShouldBeTrue(validator, null); } return(validator); }
/// <summary> /// Checks whether the given value is <b>false</b>. An exception is thrown otherwise. /// </summary> /// <param name="validator">The <see cref="ContractValidator{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="ContractValidator{T}.ArgumentName">ArgumentName</see>. /// </param> /// <returns>The specified <paramref name="validator"/> instance.</returns> /// <exception cref="ArgumentException">Thrown when the <see cref="ContractValidator{T}.Value">Value</see> of the specified <paramref name="validator"/> is <b>true</b>, 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="ContractValidator{T}.Value">Value</see> of the specified <paramref name="validator"/> is <b>true</b>, while the specified <paramref name="validator"/> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see> extension method.</exception> public static ContractValidator <bool> IsFalse(this ContractValidator <bool> validator, string conditionDescription) { if (validator.Value) { Throw.ValueShouldBeFalse(validator, conditionDescription); } return(validator); }
/// <summary> /// Checks whether the given value is <b>false</b>. An exception is thrown otherwise. /// </summary> /// <param name="validator">The <see cref="ContractValidator{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="ContractValidator{T}.Value">Value</see> of the specified <paramref name="validator"/> is <b>true</b>, 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="ContractValidator{T}.Value">Value</see> of the specified <paramref name="validator"/> is <b>true</b>, while the specified <paramref name="validator"/> is created using the <see cref="Condition.Ensures{T}(T,string)">Ensures</see> extension method.</exception> public static ContractValidator <bool> IsFalse(this ContractValidator <bool> validator) { if (validator.Value) { Throw.ValueShouldBeFalse(validator, null); } 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="ContractValidator{T}.Value">Value</see> of the specified <paramref name="validator"/>.</typeparam> /// <param name="validator">The <see cref="ContractValidator{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> /// <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="ContractValidator{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="ContractValidator{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 ContractValidator <T> Evaluate <T>(this ContractValidator <T> validator, bool condition) { if (!condition) { Throw.ExpressionEvaluatedFalse(validator, null); } return(validator); }
internal static void ExpressionEvaluatedFalse <T>(ContractValidator <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); }
/// <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="ContractValidator{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="ContractValidator{T}.ArgumentName">ArgumentName</see>. /// </param> /// <returns>The specified <paramref name="validator"/> instance.</returns> /// <exception cref="ArgumentException">Thrown when the <see cref="ContractValidator{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="ContractValidator{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 ContractValidator <short> IsNotEqualTo(this ContractValidator <short> validator, short value, string conditionDescription) { if (validator.Value == value) { Throw.ValueShouldBeUnequalTo(validator, value, conditionDescription); } return(validator); }
internal static void ValueShouldBeUnequalTo <T>(ContractValidator <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); }