static Type GetParameterHostType(IMemberInfo member, TestParameterAttribute attr, out object sourceInstance) { var hostType = typeof(ITestParameterSource <>).GetTypeInfo(); var genericInstance = hostType.MakeGenericType(member.Type).GetTypeInfo(); var attrType = attr.GetType(); if (genericInstance.IsAssignableFrom(attrType.GetTypeInfo())) { sourceInstance = attr; return(attrType); } if (member.Type.Equals(typeof(bool))) { sourceInstance = null; return(typeof(BooleanTestSource)); } else if (member.TypeInfo.IsEnum) { sourceInstance = null; return(typeof(EnumTestSource <>).MakeGenericType(member.Type)); } throw new InternalErrorException(); }
static TestHost CreateParameterAttributeHost( TypeInfo fixtureType, IMemberInfo member, TestParameterAttribute attr) { object sourceInstance; var sourceType = GetParameterHostType(member, attr, out sourceInstance); IParameterSerializer serializer; if (!GetParameterSerializer(member.TypeInfo, out serializer)) { throw new InternalErrorException(); } if (sourceInstance == null) { sourceInstance = Activator.CreateInstance(sourceType); } var type = typeof(ParameterSourceHost <>).MakeGenericType(member.Type); return((TestHost)Activator.CreateInstance( type, attr.Identifier ?? member.Name, sourceInstance, serializer, attr.Filter, false, attr.Flags)); }
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 constructorIndex) { ConstructorInfo constr = type.GetConstructors()[constructorIndex]; 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, constructorIndex, 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, GetDescription(type, constr, parameter)); 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; const bool isConstructorParameter = false; var testParameter = new TestParameter(property.Name, property.PropertyType, GetDescription(property), isConstructorParameter) { DefaultValue = attribute.DefaultValue }; testParameters.Add(testParameter); } return(new ReadOnlyList <TestParameter>(testParameters)); }