public static string GetParameterTypeString([NotNull] TestParameter testParameter) { Assert.ArgumentNotNull(testParameter, nameof(testParameter)); string typeString = testParameter.Type.Name; if (testParameter.ArrayDimension == 0) { return(typeString); } var sb = new StringBuilder(); sb.Append(typeString); for (var i = 0; i < testParameter.ArrayDimension; i++) { sb.Append("[]"); } return(sb.ToString()); }
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)); }