PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/Builders/DatapointProvider.cs

#
C# | 151 lines | 121 code | 21 blank | 9 comment | 31 complexity | b517969627d03f8d866461a57b51e022 MD5 | raw file
  1// ****************************************************************
  2// Copyright 2008, Charlie Poole
  3// This is free software licensed under the NUnit license. You may
  4// obtain a copy of the license at http://nunit.org
  5// ****************************************************************
  6
  7using System;
  8using System.Reflection;
  9using System.Collections;
 10using NUnit.Core.Extensibility;
 11
 12namespace NUnit.Core.Builders
 13{
 14    /// <summary>
 15    /// Provides data from fields marked with the DatapointAttribute or the
 16    /// DatapointsAttribute.
 17    /// </summary>
 18    public class DatapointProvider : IDataPointProvider
 19    {
 20        private static readonly string DatapointAttribute = "NUnit.Framework.DatapointAttribute";
 21        private static readonly string DatapointsAttribute = "NUnit.Framework.DatapointsAttribute";
 22
 23        #region IDataPointProvider Members
 24
 25        public bool HasDataFor(System.Reflection.ParameterInfo parameter)
 26        {
 27            Type parameterType = parameter.ParameterType;
 28            MemberInfo method = parameter.Member;
 29            Type fixtureType = method.ReflectedType;
 30
 31            if (!Reflect.HasAttribute(method, NUnitFramework.TheoryAttribute, true))
 32                return false;
 33
 34            if (parameterType == typeof(bool) || parameterType.IsEnum)
 35                return true;
 36
 37            foreach (MemberInfo member in fixtureType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
 38            {
 39                if (Reflect.HasAttribute(member, DatapointAttribute, true) &&
 40                    GetTypeFromMemberInfo(member) == parameterType)
 41                        return true;
 42                else if (Reflect.HasAttribute(member, DatapointsAttribute, true) &&
 43                    GetElementTypeFromMemberInfo(member) == parameterType)
 44                        return true;
 45            }
 46
 47            return false;
 48        }
 49
 50        public System.Collections.IEnumerable GetDataFor(System.Reflection.ParameterInfo parameter)
 51        {
 52            ArrayList datapoints = new ArrayList();
 53
 54            Type parameterType = parameter.ParameterType;
 55            Type fixtureType = parameter.Member.ReflectedType;
 56
 57            foreach (MemberInfo member in fixtureType.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
 58            {
 59                if (Reflect.HasAttribute(member, DatapointAttribute, true))
 60                {
 61                    if (GetTypeFromMemberInfo(member) == parameterType &&
 62                        member.MemberType == MemberTypes.Field)
 63                    {
 64                        FieldInfo field = member as FieldInfo;
 65                        if (field.IsStatic)
 66                            datapoints.Add(field.GetValue(null));
 67                        else
 68                            datapoints.Add(field.GetValue(ProviderCache.GetInstanceOf(fixtureType)));
 69                    }
 70                }
 71                else if (Reflect.HasAttribute(member, DatapointsAttribute, true))
 72                {
 73                    if (GetElementTypeFromMemberInfo(member) == parameterType)
 74                    {
 75                        object instance;
 76
 77                        switch(member.MemberType)
 78                        {
 79                            case MemberTypes.Field:
 80                                FieldInfo field = member as FieldInfo;
 81                                instance = field.IsStatic ? null : ProviderCache.GetInstanceOf(fixtureType);
 82                                datapoints.AddRange((ICollection)field.GetValue(instance));
 83                                break;
 84                            case MemberTypes.Property:
 85                                PropertyInfo property = member as PropertyInfo;
 86                                MethodInfo getMethod = property.GetGetMethod(true);
 87                                instance = getMethod.IsStatic ? null : ProviderCache.GetInstanceOf(fixtureType);
 88                                datapoints.AddRange((ICollection)property.GetValue(instance,null));
 89                                break;
 90                            case MemberTypes.Method:
 91                                MethodInfo method = member as MethodInfo;
 92                                instance = method.IsStatic ? null : ProviderCache.GetInstanceOf(fixtureType);
 93                                datapoints.AddRange((ICollection)method.Invoke(instance, Type.EmptyTypes));
 94                                break;
 95                        }
 96                    }
 97                }
 98            }
 99
100            if (datapoints.Count == 0)
101            {
102                if (parameterType == typeof(bool))
103                {
104                    datapoints.Add(true);
105                    datapoints.Add(false);
106                }
107                else if (parameterType.IsEnum)
108                {
109                    datapoints.AddRange(System.Enum.GetValues(parameterType));
110                }
111            }
112
113            return datapoints;
114        }
115
116        private Type GetTypeFromMemberInfo(MemberInfo member)
117        {
118            switch (member.MemberType)
119            {
120                case MemberTypes.Field:
121                    return ((FieldInfo)member).FieldType;
122                case MemberTypes.Property:
123                    return ((PropertyInfo)member).PropertyType;
124                case MemberTypes.Method:
125                    return ((MethodInfo)member).ReturnType;
126                default:
127                    return null;
128            }
129        }
130
131        private Type GetElementTypeFromMemberInfo(MemberInfo member)
132        {
133            Type type = GetTypeFromMemberInfo(member);
134
135            if (type == null)
136                return null;
137
138            if (type.IsArray)
139                return type.GetElementType();
140
141#if NET_2_0
142            if (type.IsGenericType && type.Name == "IEnumerable`1")
143                return type.GetGenericArguments()[0];
144#endif
145
146            return null;
147        }
148
149        #endregion
150    }
151}