Exemple #1
0
        protected static void AddConstructorParameters(
            [NotNull] List <TestParameter> parameters,
            [NotNull] Type qaTestType,
            int constructorIndex,
            [NotNull] IList <int> ignoreParameters)
        {
            ConstructorInfo constr = qaTestType.GetConstructors()[constructorIndex];

            IList <ParameterInfo> constrParams = constr.GetParameters();

            for (var iParam = 0; iParam < constrParams.Count; iParam++)
            {
                if (ignoreParameters.Contains(iParam))
                {
                    continue;
                }

                ParameterInfo constrParam = constrParams[iParam];

                var testParameter = new TestParameter(
                    constrParam.Name, constrParam.ParameterType,
                    TestImplementationUtils.GetDescription(constrParam),
                    isConstructorParameter: true);

                parameters.Add(testParameter);
            }
        }
        public override string GetParameterDescription(string parameterName)
        {
            ConstructorInfo ctor = TestType.GetConstructors()[_constructorId];

            // TODO: revise, case-insensitive match is ok? (parameter name search is insensitive elsewhere)
            const StringComparison stringComparison = StringComparison.OrdinalIgnoreCase;

            foreach (ParameterInfo parameterInfo in ctor.GetParameters())
            {
                if (string.Equals(parameterInfo.Name, parameterName, stringComparison))
                {
                    return(TestImplementationUtils.GetDescription(parameterInfo));
                }
            }

            foreach (PropertyInfo propertyInfo in TestType.GetProperties())
            {
                if (string.Equals(propertyInfo.Name, parameterName, stringComparison))
                {
                    return(TestImplementationUtils.GetDescription(propertyInfo));
                }
            }

            return(null);
        }
Exemple #3
0
        protected static void AddOptionalTestParameters(
            [NotNull] List <TestParameter> parameters,
            [NotNull] Type qaTestType,
            [CanBeNull] IEnumerable <string> ignoredTestParameters = null,
            [CanBeNull] IEnumerable <string> additionalProperties  = null)
        {
            Dictionary <string, TestParameter> attributesByName =
                parameters.ToDictionary(parameter => parameter.Name);

            if (ignoredTestParameters != null)
            {
                foreach (string ignoreAttribute in ignoredTestParameters)
                {
                    attributesByName.Add(ignoreAttribute, null);
                }
            }

            HashSet <string> additionalPropertiesSet =
                additionalProperties != null
                                        ? new HashSet <string>(additionalProperties)
                                        : null;

            foreach (PropertyInfo property in qaTestType.GetProperties())
            {
                MethodInfo setMethod = property.GetSetMethod();

                if (setMethod == null || !setMethod.IsPublic)
                {
                    continue;
                }

                TestParameterAttribute testParameterAttribute = null;
                if (additionalPropertiesSet == null ||
                    !additionalPropertiesSet.Contains(property.Name))
                {
                    testParameterAttribute =
                        ReflectionUtils.GetAttribute <TestParameterAttribute>(property);

                    if (testParameterAttribute == null)
                    {
                        continue;
                    }
                }

                if (attributesByName.ContainsKey(property.Name))
                {
                    continue;
                }

                var testParameter = new TestParameter(
                    property.Name, property.PropertyType,
                    TestImplementationUtils.GetDescription(property),
                    isConstructorParameter: false);

                if (testParameterAttribute != null)
                {
                    testParameter.DefaultValue = testParameterAttribute.DefaultValue;
                }
                else
                {
                    object defaultValue;
                    if (ReflectionUtils.TryGetDefaultValue(property, out defaultValue))
                    {
                        testParameter.DefaultValue = defaultValue;
                    }
                }

                parameters.Add(testParameter);
                attributesByName.Add(property.Name, testParameter);
            }
        }
        public static IList <TestParameter> CreateParameters([NotNull] Type type,
                                                             int constructorId)
        {
            ConstructorInfo constr = type.GetConstructors()[constructorId];

            ParameterInfo[] constructorParameters = constr.GetParameters();
            PropertyInfo[]  properties            = type.GetProperties();

            var testParameterProperties =
                new Dictionary <PropertyInfo, TestParameterAttribute>();

            foreach (PropertyInfo propertyInfo in properties)
            {
                if (!propertyInfo.CanRead || !propertyInfo.CanWrite)
                {
                    continue;
                }

                var testParameterAttribute =
                    ReflectionUtils.GetAttribute <TestParameterAttribute>(
                        propertyInfo);

                if (testParameterAttribute == null)
                {
                    continue;
                }

                var isValid = true;
                foreach (ParameterInfo constructorParameter in constructorParameters)
                {
                    if (string.Equals(constructorParameter.Name, propertyInfo.Name,
                                      StringComparison.InvariantCultureIgnoreCase))
                    {
                        isValid = false;

                        _msg.Warn(GetMessageConstructorParameterExistsAlsoAsProperty(
                                      type, constructorId, constructorParameter));
                    }
                }

                if (isValid)
                {
                    testParameterProperties.Add(propertyInfo, testParameterAttribute);
                }
            }

            var testParameters =
                new List <TestParameter>(constructorParameters.Length +
                                         testParameterProperties.Count);

            foreach (ParameterInfo parameter in constructorParameters)
            {
                var testParameter = new TestParameter(
                    parameter.Name, parameter.ParameterType,
                    TestImplementationUtils.GetDescription(parameter),
                    isConstructorParameter: true);

                object defaultValue;
                if (ReflectionUtils.TryGetDefaultValue(parameter, out defaultValue))
                {
                    testParameter.DefaultValue = defaultValue;
                }

                testParameters.Add(testParameter);
            }

            foreach (KeyValuePair <PropertyInfo, TestParameterAttribute> pair in
                     testParameterProperties)
            {
                PropertyInfo           property  = pair.Key;
                TestParameterAttribute attribute = pair.Value;

                var testParameter = new TestParameter(
                    property.Name, property.PropertyType,
                    TestImplementationUtils.GetDescription(property),
                    isConstructorParameter: false);

                testParameter.DefaultValue = attribute.DefaultValue;

                testParameters.Add(testParameter);
            }

            return(new ReadOnlyList <TestParameter>(testParameters));
        }
        public override string GetTestDescription()
        {
            ConstructorInfo ctor = TestType.GetConstructors()[_constructorId];

            return(TestImplementationUtils.GetDescription(ctor));
        }