Esempio n. 1
0
        /// <summary>
        /// Validates a property and its getter and/or setter.
        /// </summary>
        /// <param name="actualProperty">Property to validate.</param>
        /// <param name="propertyAccessorExpectations">Expected property accessors.</param>
        /// <param name="propertyTypeValidator">Validator for the type of the property.</param>
        public static void ValidatePropertyTypeAndAccessors(
            PropertyInfo actualProperty,
            PropertyAccessorExpectations propertyAccessorExpectations,
            TypeValidatorBase propertyTypeValidator)
        {
            Contract.Requires(actualProperty != null);
            Contract.Requires(propertyTypeValidator != null);

            propertyTypeValidator.Validate(actualProperty.PropertyType);

            var getMethod = actualProperty.GetGetMethod(true);

            if ((propertyAccessorExpectations & PropertyAccessorExpectations.Get) != PropertyAccessorExpectations.Get)
            {
                Assert.That(getMethod, Is.Null);
            }
            else
            {
                Assert.That(getMethod, Is.Not.Null);
                ValidateParameters(getMethod, propertyTypeValidator);
            }

            var setMethod = actualProperty.GetSetMethod(true);

            if ((propertyAccessorExpectations & PropertyAccessorExpectations.Set) != PropertyAccessorExpectations.Set)
            {
                Assert.That(setMethod, Is.Null);
            }
            else
            {
                Assert.That(setMethod, Is.Not.Null);
                ValidateParameters(setMethod, NonGenericTypeValidator.ForVoidType(), propertyTypeValidator);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Validates a type against expected generic argument types.
        /// </summary>
        /// <param name="actualType">Type to validate.</param>
        /// <param name="typeValidator">Expected types.</param>
        public static void ValidateType(
            Type actualType,
            TypeValidatorBase typeValidator)
        {
            Contract.Requires(actualType != null);
            Contract.Requires(typeValidator != null);

            typeValidator.Validate(actualType);
        }
Esempio n. 3
0
        /// <summary>
        /// Validates a method against expected return and parameter types.
        /// </summary>
        /// <param name="actualMethod">Method to validate.</param>
        /// <param name="returnTypeValidator">Expected return type.</param>
        /// <param name="parameterTypeValidators">Expected parameter types.</param>
        public static void ValidateParameters(
            MethodInfo actualMethod,
            TypeValidatorBase returnTypeValidator,
            params TypeValidatorBase[] parameterTypeValidators)
        {
            Contract.Requires(actualMethod != null);
            Contract.Requires(returnTypeValidator != null);
            Contract.Requires(parameterTypeValidators != null);

            returnTypeValidator.Validate(actualMethod.ReturnType);

            var parameters = actualMethod.GetParameters();
            Assert.That(parameterTypeValidators.Length, Is.EqualTo(parameters.Length));
            Parallel.For(0, parameterTypeValidators.Length, i => parameterTypeValidators[i].Validate(parameters[i].ParameterType));
        }
Esempio n. 4
0
        /// <summary>
        /// Validates a method against expected return and parameter types.
        /// </summary>
        /// <param name="actualMethod">Method to validate.</param>
        /// <param name="returnTypeValidator">Expected return type.</param>
        /// <param name="parameterTypeValidators">Expected parameter types.</param>
        public static void ValidateParameters(
            MethodInfo actualMethod,
            TypeValidatorBase returnTypeValidator,
            params TypeValidatorBase[] parameterTypeValidators)
        {
            Contract.Requires(actualMethod != null);
            Contract.Requires(returnTypeValidator != null);
            Contract.Requires(parameterTypeValidators != null);

            returnTypeValidator.Validate(actualMethod.ReturnType);

            var parameters = actualMethod.GetParameters();

            Assert.That(parameterTypeValidators.Length, Is.EqualTo(parameters.Length));
            Parallel.For(0, parameterTypeValidators.Length, i => parameterTypeValidators[i].Validate(parameters[i].ParameterType));
        }
Esempio n. 5
0
        /// <summary>
        /// Validates a property and its getter and/or setter.
        /// </summary>
        /// <param name="actualProperty">Property to validate.</param>
        /// <param name="propertyAccessorExpectations">Expected property accessors.</param>
        /// <param name="propertyTypeValidator">Validator for the type of the property.</param>
        public static void ValidatePropertyTypeAndAccessors(
            PropertyInfo actualProperty,
            PropertyAccessorExpectations propertyAccessorExpectations,
            TypeValidatorBase propertyTypeValidator)
        {
            Contract.Requires(actualProperty != null);
            Contract.Requires(propertyTypeValidator != null);

            propertyTypeValidator.Validate(actualProperty.PropertyType);

            var getMethod = actualProperty.GetGetMethod(true);
            if ((propertyAccessorExpectations & PropertyAccessorExpectations.Get) != PropertyAccessorExpectations.Get)
            {
                Assert.That(getMethod, Is.Null);
            }
            else
            {
                Assert.That(getMethod, Is.Not.Null);
                ValidateParameters(getMethod, propertyTypeValidator);
            }

            var setMethod = actualProperty.GetSetMethod(true);
            if ((propertyAccessorExpectations & PropertyAccessorExpectations.Set) != PropertyAccessorExpectations.Set)
            {
                Assert.That(setMethod, Is.Null);
            }
            else
            {
                Assert.That(setMethod, Is.Not.Null);
                ValidateParameters(setMethod, NonGenericTypeValidator.ForVoidType(), propertyTypeValidator);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Validates a type against expected generic argument types.
        /// </summary>
        /// <param name="actualType">Type to validate.</param>
        /// <param name="typeValidator">Expected types.</param>
        public static void ValidateType(
            Type actualType,
            TypeValidatorBase typeValidator)
        {
            Contract.Requires(actualType != null);
            Contract.Requires(typeValidator != null);

            typeValidator.Validate(actualType);
        }