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.Collections.Generic;
31 using System.Reflection;
32 using NUnit.Compatibility;
33 using NUnit.Framework.Interfaces;
35 using NUnit.Framework.TUnit;
41 namespace NUnit.Framework.Internal
44 /// The ParameterWrapper class wraps a ParameterInfo so that it may
45 /// be used in a platform-independent manner.
47 public class ParameterWrapper : IParameterInfo
50 /// Construct a ParameterWrapper for a given method and parameter
52 /// <param name="method"></param>
53 /// <param name="parameterInfo"></param>
54 public ParameterWrapper(IMethodInfo method, ParameterInfo parameterInfo)
57 ParameterInfo = parameterInfo;
64 /// Gets a value indicating whether the parameter is optional
66 public bool IsOptional
68 get { return ParameterInfo.IsOptional; }
73 /// Gets an IMethodInfo representing the method for which this is a parameter.
75 public IMethodInfo Method { get; private set; }
78 /// Gets the underlying ParameterInfo
80 public ParameterInfo ParameterInfo { get; private set; }
83 /// Gets the Type of the parameter
85 public Type ParameterType
87 get { return ParameterInfo.ParameterType; }
95 /// Returns an array of custom attributes of the specified type applied to this method
97 public T[] GetCustomAttributes<T>(bool inherit) where T : class
100 var allAttributes = ParameterInfo.GetCustomAttributes();
101 List<string> attributeDic = new List<string>();
102 foreach (Attribute atb in allAttributes)
104 attributeDic.Add(atb.GetType().FullName);
106 var assembly = ParameterInfo.ParameterType.GetTypeInfo().Assembly;
107 List<T> objects = new List<T>();
109 string path = System.IO.Path.GetDirectoryName(assembly.Location);
110 if (!Directory.Exists(path))
112 TLogger.WriteError(TLogger.ExceptionTag, "" + path + " - not a directory");
113 return objects.ToArray();
115 foreach (var assemblyPath in Directory.GetFiles(path, "*.Tests.dll"))
117 IEnumerable<Type> types;
120 Assembly please = AssemblyHelper.Load(assemblyPath);
121 if (please == null) continue;
122 types = please.GetTypes().Where(p => !p.GetTypeInfo().IsAbstract && p.GetTypeInfo().IsClass && p.GetTypeInfo().ImplementedInterfaces.Contains(typeof(T)));
126 //TLogger.Write(TLogger.ExceptionTag, ex.ToString());
129 for (int i = 0; i < types.Count(); i++)
133 if (attributeDic.Contains(types.ElementAt(i).FullName))
135 objects.Add((T)Activator.CreateInstance(types.ElementAt(i)));
140 //TLogger.Write(TLogger.ExceptionTag, ex.ToString());
145 return objects.ToArray();
147 return (T[])ParameterInfo.GetCustomAttributes(typeof(T), inherit);
152 /// Gets a value indicating whether one or more attributes of the specified type are defined on the parameter.
154 public bool IsDefined<T>(bool inherit)
157 return ParameterInfo.GetCustomAttributes(inherit).Any(a => typeof(T).IsAssignableFrom(a.GetType()));
159 return ParameterInfo.IsDefined(typeof(T), inherit);
166 private string GetAssemblyName(string assemblyFullPath)
169 string[] delimiter1 = { "\\" };
170 string[] delimiter2 = { "/" };
171 string[] delimiterDot = { "." };
173 string returnValue = "";
176 strAry = assemblyFullPath.Split(delimiter1, StringSplitOptions.None);
178 if (strAry.Length < 2)
179 strAry = assemblyFullPath.Split(delimiter2, StringSplitOptions.None);
181 foreach (string str in strAry)
183 if (str.Contains("Tests.dll"))
185 string[] strSplit = str.Split(delimiterDot, StringSplitOptions.None);
186 returnValue = strSplit[0];
187 // LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, "check : "+ returnValue);
194 LogUtils.Write(LogUtils.ERROR, LogUtils.TAG, e.ToString());