Exemple #1
0
        /// <summary>
        /// Return an IEnumerable providing data for use with the
        /// supplied parameter.
        /// </summary>
        /// <param name="parameter">A ParameterInfo representing one
        /// argument to a parameterized test</param>
        /// <returns>
        /// An IEnumerable providing the required data
        /// </returns>
        public System.Collections.IEnumerable GetDataFor(IParameterInfo parameter)
        {
            var datapoints = new List <object>();

            Type parameterType = parameter.ParameterType;
            Type fixtureType   = parameter.Method.TypeInfo.Type;

            foreach (MemberInfo member in fixtureType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
            {
                if (member.IsDefined(typeof(DatapointAttribute), true))
                {
                    var field = member as FieldInfo;
                    if (GetTypeFromMemberInfo(member) == parameterType && field != null)
                    {
                        if (field.IsStatic)
                        {
                            datapoints.Add(field.GetValue(null));
                        }
                        else
                        {
                            datapoints.Add(field.GetValue(ProviderCache.GetInstanceOf(fixtureType)));
                        }
                    }
                }
                else if (member.IsDefined(typeof(DatapointSourceAttribute), true))
                {
                    if (GetElementTypeFromMemberInfo(member) == parameterType)
                    {
                        object instance;

                        FieldInfo    field    = member as FieldInfo;
                        PropertyInfo property = member as PropertyInfo;
                        MethodInfo   method   = member as MethodInfo;
                        if (field != null)
                        {
                            instance = field.IsStatic ? null : ProviderCache.GetInstanceOf(fixtureType);
                            foreach (object data in (IEnumerable)field.GetValue(instance))
                            {
                                datapoints.Add(data);
                            }
                        }
                        else if (property != null)
                        {
                            MethodInfo getMethod = property.GetGetMethod(true);
                            instance = getMethod.IsStatic ? null : ProviderCache.GetInstanceOf(fixtureType);
                            foreach (object data in (IEnumerable)property.GetValue(instance, null))
                            {
                                datapoints.Add(data);
                            }
                        }
                        else if (method != null)
                        {
                            instance = method.IsStatic ? null : ProviderCache.GetInstanceOf(fixtureType);
                            foreach (object data in (IEnumerable)method.Invoke(instance, new Type[0]))
                            {
                                datapoints.Add(data);
                            }
                        }
                    }
                }
            }

            if (datapoints.Count == 0)
            {
                if (parameterType == typeof(bool))
                {
                    datapoints.Add(true);
                    datapoints.Add(false);
                }
                else if (parameterType.GetTypeInfo().IsEnum)
                {
                    foreach (object o in TypeHelper.GetEnumValues(parameterType))
                    {
                        datapoints.Add(o);
                    }
                }
            }

            return(datapoints);
        }