Esempio n. 1
0
        /// <summary>
        /// Verify that <paramref name="value"/> is not the default mustBeTrue for that type.
        /// </summary>
        public static void IsNotDefaultValue <T>(T value, string propertyName, string errorLocation, string customMessage = null)
        {
            InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
            InternalContract.RequireNotNull(propertyName, nameof(propertyName));
            var message = customMessage ?? $"Property {propertyName} ({value}) must not have the default value ({default(T)}.";

            GenericAssert <FulcrumAssertionFailedException> .IsNotDefaultValue(value, errorLocation, message);
        }
Esempio n. 2
0
        /// <summary>
        /// Verify that <paramref name="value"/> is not null.
        /// </summary>
        public static void IsNotNull(object value, string propertyName, string errorLocation, string customMessage = null)
        {
            InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
            InternalContract.RequireNotNull(propertyName, nameof(propertyName));
            var message = customMessage ?? $"Property {propertyName} ({value}) must not be null.";

            GenericAssert <FulcrumAssertionFailedException> .IsNotNull(value, errorLocation, message);
        }
Esempio n. 3
0
        /// <summary>
        /// Verify that <paramref name="value"/> is not null, not empty and contains other characters than white space.
        /// </summary>
        public static void IsNotNullOrWhiteSpace(string value, string propertyName, string errorLocation, string customMessage = null)
        {
            InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
            InternalContract.RequireNotNull(propertyName, nameof(propertyName));
            var message = customMessage ?? $"Property {propertyName} ({value}) must not be null or empty and it must contain other characters than white space.";

            GenericAssert <FulcrumAssertionFailedException> .IsNotNullOrWhiteSpace(value, errorLocation, message);
        }
Esempio n. 4
0
        private static void ThrowException(string errorLocation, string message)
        {
            InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
            var exception = (TException)Activator.CreateInstance(typeof(TException), message);

            exception.ErrorLocation = errorLocation;
            throw exception;
        }
Esempio n. 5
0
        /// <summary>
        /// Verify that <paramref name="propertyValue"/> is equal to <paramref name="expectedValue"/>.
        /// </summary>
        public static void AreEqual(object expectedValue, object propertyValue, string propertyName, string errorLocation, string customMessage = null)
        {
            InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
            InternalContract.RequireNotNull(propertyName, nameof(propertyName));
            var message = customMessage ?? $"Expected property {propertyName} ({propertyValue}) to be equal to ({expectedValue}).";

            GenericAssert <FulcrumAssertionFailedException> .AreEqual(expectedValue, propertyValue, errorLocation, message);
        }
Esempio n. 6
0
 /// <summary>
 /// Verify that <paramref name="actualValue"/> is less than or equal to <paramref name="greaterOrEqualValue"/>.
 /// </summary>
 public static void IsLessThanOrEqualTo <T>(T greaterOrEqualValue, T actualValue, string errorLocation, string customMessage = null)
     where T : IComparable <T>
 {
     InternalContract.RequireNotNull(greaterOrEqualValue, nameof(greaterOrEqualValue));
     InternalContract.RequireNotNull(actualValue, nameof(actualValue));
     InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
     GenericAssert <FulcrumAssertionFailedException> .IsLessThanOrEqualTo(greaterOrEqualValue, actualValue, errorLocation, customMessage);
 }
Esempio n. 7
0
 /// <summary>
 /// Verify that <paramref name="value"/> is not null, not empty and has other characters than white space.
 /// </summary>
 public static void IsNotNullOrWhiteSpace(string value, string errorLocation, string customMessage = null)
 {
     InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
     if (!string.IsNullOrWhiteSpace(value))
     {
         return;
     }
     ThrowException(errorLocation, customMessage ?? $"Did not expect value ({value}) to be null, empty or only contain whitespace.");
 }
Esempio n. 8
0
 /// <summary>
 /// Verify that <paramref name="value"/> is null.
 /// </summary>
 public static void IsNull(object value, string errorLocation, string customMessage = null)
 {
     InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
     if (value == null)
     {
         return;
     }
     ThrowException(errorLocation, customMessage ?? $"Expected value ({value}) to be null.");
 }
Esempio n. 9
0
 /// <summary>
 /// Verify that <paramref name="value"/> is true.
 /// </summary>
 public static void IsTrue(bool value, string errorLocation, string customMessage = null)
 {
     InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
     if (value)
     {
         return;
     }
     ThrowException(errorLocation, customMessage ?? "Expected value to be true.");
 }
Esempio n. 10
0
 /// <summary>
 /// Verify that <paramref name="actualValue"/> is equal to <paramref name="expectedValue"/>.
 /// </summary>
 public static void AreEqual(object expectedValue, object actualValue, string errorLocation, string customMessage = null)
 {
     InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
     if (Equals(expectedValue, actualValue))
     {
         return;
     }
     ThrowException(errorLocation, customMessage ?? $"Expected ({actualValue}) to be equal to ({expectedValue}).");
 }
Esempio n. 11
0
        /// <summary>
        /// Verify that <paramref name="propertyValue"/> is less than <paramref name="greaterValue"/>.
        /// </summary>
        public static void IsLessThan <T>(T greaterValue, T propertyValue, string propertyName, string errorLocation, string customMessage = null)
            where T : IComparable <T>
        {
            InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
            InternalContract.RequireNotNull(propertyName, nameof(propertyName));
            var message = customMessage ?? $"Expected property {propertyName} ({propertyValue}) to be less than ({greaterValue}).";

            GenericAssert <FulcrumAssertionFailedException> .IsLessThan(greaterValue, propertyValue, errorLocation, message);
        }
Esempio n. 12
0
 /// <summary>
 /// Verify that <paramref name="value"/> is not the default value for that type.
 /// </summary>
 public static void IsNotDefaultValue <T>(T value, string errorLocation, string customMessage = null)
 {
     InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
     if (!value.Equals(default(T)))
     {
         return;
     }
     ThrowException(errorLocation, customMessage ?? $"Did not expect value to be default value ({default(T)}).");
 }
Esempio n. 13
0
        /// <summary>
        /// Verify that <paramref name="actualValue"/> is greater than or equal to <paramref name="lesserOrEqualValue"/>.
        /// </summary>
        public static void IsGreaterThanOrEqualTo <T>(T lesserOrEqualValue, T actualValue, string errorLocation, string customMessage = null)
            where T : IComparable <T>
        {
            InternalContract.RequireNotNull(lesserOrEqualValue, nameof(lesserOrEqualValue));
            InternalContract.RequireNotNull(actualValue, nameof(actualValue));
            InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
            var message = customMessage ?? $"Expected ({actualValue}) to be greater than or equal to ({lesserOrEqualValue}).";

            IsTrue(actualValue.CompareTo(lesserOrEqualValue) >= 0, errorLocation, message);
        }
Esempio n. 14
0
        /// <summary>
        /// Verify that <paramref name="actualValue"/> is less than to <paramref name="greaterValue"/>.
        /// </summary>
        public static void IsLessThan <T>(T greaterValue, T actualValue, string errorLocation, string customMessage = null)
            where T : IComparable <T>
        {
            InternalContract.RequireNotNull(greaterValue, nameof(greaterValue));
            InternalContract.RequireNotNull(actualValue, nameof(actualValue));
            InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
            var message = customMessage ?? $"Expected ({actualValue}) to be less than ({greaterValue}).";

            IsTrue(actualValue.CompareTo(greaterValue) < 0, errorLocation, message);
        }
        /// <summary>
        /// Verify that <paramref name="parameterValue"/> is less than to <paramref name="greaterValue"/>.
        /// </summary>
        public static void RequireLessThan <T>(T greaterValue, T parameterValue, string parameterName, string customMessage = null)
            where T : IComparable <T>
        {
            InternalContract.RequireNotNull(greaterValue, nameof(greaterValue));
            InternalContract.RequireNotNull(parameterValue, nameof(parameterValue));
            InternalContract.RequireNotNull(parameterName, nameof(parameterName));
            var message = customMessage ?? $"ContractViolation: {parameterName} ({parameterValue}) must be less than ({greaterValue}).";

            Require(parameterValue.CompareTo(greaterValue) < 0, message);
        }
        /// <summary>
        /// Verify that <paramref name="parameterValue"/> is greater than or equal to <paramref name="lesserOrEqualValue"/>.
        /// </summary>
        public static void RequireGreaterThanOrEqualTo <T>(T lesserOrEqualValue, T parameterValue, string parameterName, string customMessage = null)
            where T : IComparable <T>
        {
            InternalContract.RequireNotNull(lesserOrEqualValue, nameof(lesserOrEqualValue));
            InternalContract.RequireNotNull(parameterValue, nameof(parameterValue));
            InternalContract.RequireNotNull(parameterName, nameof(parameterName));
            var message = customMessage ?? $"ContractViolation: {parameterName} ({parameterValue}) must be greater than or equal to ({lesserOrEqualValue}).";

            Require(parameterValue.CompareTo(lesserOrEqualValue) <= 0, message);
        }
Esempio n. 17
0
        /// <summary>
        /// Verify that the result of <paramref name="expression"/> is equal to <paramref name="expectedValue"/>.
        /// </summary>
        public static void AreEqual(object expectedValue, Expression <Func <string> > expression, string errorLocation, string customMessage = null)
        {
            InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
            var actualValue = expression.Compile()();

            if (Equals(expectedValue, actualValue))
            {
                return;
            }
            ThrowException(errorLocation, customMessage ?? $"Expected '{expression.Body}' ({actualValue}) to be equal to ({expectedValue}).");
        }
Esempio n. 18
0
        /// <summary>
        /// Verify that the result of <paramref name="expression"/> is not null, not empty and contains other characters than white space.
        /// </summary>
        public static void IsNotNullOrWhiteSpace(Expression <Func <string> > expression, string errorLocation, string customMessage = null)
        {
            InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
            var value = expression.Compile()();

            if (!string.IsNullOrWhiteSpace(value))
            {
                return;
            }
            ThrowException(errorLocation, customMessage ?? $"Did not expect '{expression.Body}' ({value}) to be null, empty or only contain whitespace.");
        }
Esempio n. 19
0
        /// <summary>
        /// Verify that the result of <paramref name="expression"/> is true.
        /// </summary>
        public static void IsTrue(Expression <Func <bool> > expression, string errorLocation, string customMessage = null)
        {
            InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
            var value = expression.Compile()();

            if (value)
            {
                return;
            }
            ThrowException(errorLocation, customMessage ?? $"Expected '{expression.Body} to be true.");
        }
Esempio n. 20
0
        /// <summary>
        /// Verify that the result of <paramref name="expression"/> is null.
        /// </summary>
        public static void IsNotNull(Expression <Func <object> > expression, string errorLocation, string customMessage = null)
        {
            InternalContract.RequireNotNull((object)expression, nameof(expression));
            var value = expression.Compile()();

            if (value != null)
            {
                return;
            }
            ThrowException(errorLocation, customMessage ?? $"Did not expect '{expression.Body}' to be null.");
        }
Esempio n. 21
0
 /// <summary>
 /// Verify that <paramref name="actualValue"/> is equal to <paramref name="expectedValue"/>.
 /// </summary>
 public static void AreEqual(object expectedValue, object actualValue, string errorLocation, string customMessage = null)
 {
     InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
     GenericAssert <FulcrumAssertionFailedException> .AreEqual(expectedValue, actualValue, errorLocation, customMessage);
 }
 /// <summary>
 /// Always fail, with the given <paramref name="message"/>.
 /// </summary>
 public static void Fail(string message)
 {
     InternalContract.RequireNotNullOrWhitespace(message, nameof(message));
     ThrowException(message);
 }
 private static string GetErrorMessageIfFalse(bool mustBeTrue, string message)
 {
     InternalContract.RequireNotNullOrWhitespace(message, nameof(message));
     return(mustBeTrue ? null : message);
 }
Esempio n. 24
0
 /// <summary>
 /// Verify that <paramref name="value"/> is not null, not empty and contains other characters than white space.
 /// </summary>
 public static void IsNotNullOrWhiteSpace(string value, string errorLocation, string customMessage = null)
 {
     InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
     GenericAssert <FulcrumAssertionFailedException> .IsNotNullOrWhiteSpace(value, errorLocation, customMessage);
 }
Esempio n. 25
0
 /// <summary>
 /// Verify that <paramref name="mustBeTrue"/> is true.
 /// </summary>
 /// <param name="mustBeTrue">The value that must be true.</param>
 /// <param name="message">A message that documents/explains this failure. This message should normally start with "Expected ...".</param>
 /// <param name="errorLocation">A unique errorLocation for the part of errorLocation where the validation didn't hold.</param>
 public static void IsTrue(bool mustBeTrue, string errorLocation, string message)
 {
     InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
     InternalContract.RequireNotNullOrWhitespace(message, nameof(message));
     GenericAssert <FulcrumAssertionFailedException> .IsTrue(mustBeTrue, errorLocation, message);
 }
Esempio n. 26
0
 /// <summary>
 /// Will always fail. Used in parts of the errorLocation where we should never end up. E.g. a default case in a switch statement where all cases should be covered, so we should never end up in the default case.
 /// </summary>
 /// <param name="errorLocation">A unique errorLocation for the part of errorLocation where the validation didn't hold.</param>
 /// <param name="message">A message that documents/explains this failure. This message should normally start with "Expected ...".</param>
 public static void Fail(string errorLocation, string message)
 {
     InternalContract.RequireNotNull(message, nameof(message));
     InternalContract.RequireNotNull(errorLocation, nameof(errorLocation));
     GenericAssert <FulcrumAssertionFailedException> .Fail(errorLocation, message);
 }