1 // ***********************************************************************
2 // Copyright (c) 2015 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
30 using System.Reflection;
31 using NUnit.Compatibility;
32 using NUnit.Framework.Interfaces;
33 using System.Collections.Generic;
35 using NUnit.Framework.TUnit;
41 namespace NUnit.Framework.Internal
44 /// The MethodWrapper class wraps a MethodInfo so that it may
45 /// be used in a platform-independent manner.
47 public class MethodWrapper : IMethodInfo
50 /// Construct a MethodWrapper for a Type and a MethodInfo.
52 public MethodWrapper(Type type, MethodInfo method)
54 TypeInfo = new TypeWrapper(type);
59 /// Construct a MethodInfo for a given Type and method name.
61 public MethodWrapper(Type type, string methodName)
63 TypeInfo = new TypeWrapper(type);
64 MethodInfo = type.GetMethod(methodName);
67 #region IMethod Implementation
70 /// Gets the Type from which this method was reflected.
72 public ITypeInfo TypeInfo { get; private set; }
75 /// Gets the MethodInfo for this method.
77 public MethodInfo MethodInfo { get; private set; }
80 /// Gets the name of the method.
84 get { return MethodInfo.Name; }
88 /// Gets a value indicating whether the method is abstract.
90 public bool IsAbstract
92 get { return MethodInfo.IsAbstract; }
96 /// Gets a value indicating whether the method is public.
100 get { return MethodInfo.IsPublic; }
104 /// Gets a value indicating whether the method contains unassigned generic type parameters.
106 public bool ContainsGenericParameters
108 get { return MethodInfo.ContainsGenericParameters; }
112 /// Gets a value indicating whether the method is a generic method.
114 public bool IsGenericMethod
116 get { return MethodInfo.IsGenericMethod; }
120 /// Gets a value indicating whether the MethodInfo represents the definition of a generic method.
122 public bool IsGenericMethodDefinition
124 get { return MethodInfo.IsGenericMethodDefinition; }
128 /// Gets the return Type of the method.
130 public ITypeInfo ReturnType
132 get { return new TypeWrapper(MethodInfo.ReturnType); }
136 /// Gets the parameters of the method.
138 /// <returns></returns>
139 public IParameterInfo[] GetParameters()
141 var parameters = MethodInfo.GetParameters();
142 var result = new IParameterInfo[parameters.Length];
144 for (int i = 0; i < parameters.Length; i++)
145 result[i] = new ParameterWrapper(this, parameters[i]);
151 /// Returns the Type arguments of a generic method or the Type parameters of a generic method definition.
153 public Type[] GetGenericArguments()
155 return MethodInfo.GetGenericArguments();
159 /// Replaces the type parameters of the method with the array of types provided and returns a new IMethodInfo.
161 /// <param name="typeArguments">The type arguments to be used</param>
162 /// <returns>A new IMethodInfo with the type arguments replaced</returns>
163 public IMethodInfo MakeGenericMethod(params Type[] typeArguments)
165 return new MethodWrapper(TypeInfo.Type, MethodInfo.MakeGenericMethod(typeArguments));
169 /// Returns an array of custom attributes of the specified type applied to this method
171 public T[] GetCustomAttributes<T>(bool inherit) where T : class
174 //return MethodInfo.GetAttributes<T>(inherit).ToArray();
175 var allAttributes = MethodInfo.GetCustomAttributes();
176 List<string> attributeDic = new List<string>();
177 foreach (Attribute atb in allAttributes)
179 attributeDic.Add(atb.GetType().FullName);
181 var assembly = MethodInfo.DeclaringType.GetTypeInfo().Assembly;
182 List<T> objects = new List<T>();
184 string path = System.IO.Path.GetDirectoryName(assembly.Location);
186 if (!Directory.Exists(path))
188 TLogger.WriteError(TLogger.ExceptionTag, "" + path + " - not a directory");
189 return objects.ToArray();
191 foreach (var assemblyPath in Directory.GetFiles(path, "*.Tests.dll"))
194 IEnumerable<Type> types;
197 Assembly please = AssemblyHelper.Load(assemblyPath);
198 if (please == null) continue;
199 types = please.GetTypes().Where(p => !p.GetTypeInfo().IsAbstract && p.GetTypeInfo().IsClass && p.GetTypeInfo().ImplementedInterfaces.Contains(typeof(T)));
205 for (int i = 0; i < types.Count(); i++)
209 if (attributeDic.Contains(types.ElementAt(i).FullName))
211 objects.Add((T)Activator.CreateInstance(types.ElementAt(i)));
216 //TLogger.Write(TLogger.ExceptionTag, ex.ToString());
221 return objects.ToArray();
223 return (T[])MethodInfo.GetCustomAttributes(typeof(T), inherit);
228 /// Gets a value indicating whether one or more attributes of the spcified type are defined on the method.
230 public bool IsDefined<T>(bool inherit)
233 return MethodInfo.GetCustomAttributes(inherit).Any(a => typeof(T).IsAssignableFrom(a.GetType()));
235 return MethodInfo.IsDefined(typeof(T), inherit);
240 /// Invokes the method, converting any TargetInvocationException to an NUnitException.
242 /// <param name="fixture">The object on which to invoke the method</param>
243 /// <param name="args">The argument list for the method</param>
244 /// <returns>The return value from the invoked method</returns>
245 public object Invoke(object fixture, params object[] args)
247 return Reflect.InvokeMethod(MethodInfo, fixture, args);
251 /// Override ToString() so that error messages in NUnit's own tests make sense
253 public override string ToString()
255 return MethodInfo.Name;
261 private string GetAssemblyName(string assemblyFullPath)
264 string[] delimiter1 = { "\\" };
265 string[] delimiter2 = { "/" };
266 string[] delimiterDot = { "." };
268 string returnValue = "";
271 strAry = assemblyFullPath.Split(delimiter1, StringSplitOptions.None);
273 if (strAry.Length < 2)
274 strAry = assemblyFullPath.Split(delimiter2, StringSplitOptions.None);
276 foreach (string str in strAry)
278 if (str.Contains(".Tests.dll"))
280 string[] strSplit = str.Split(delimiterDot, StringSplitOptions.None);
281 returnValue = strSplit[0];
282 //LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "check : "+ returnValue);